1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_AvailableMemoryWatcher_h
8 #define mozilla_AvailableMemoryWatcher_h
10 #include "mozilla/ipc/CrashReporterHost.h"
11 #include "mozilla/UniquePtr.h"
12 #include "MemoryPressureLevelMac.h"
14 #include "nsIAvailableMemoryWatcherBase.h"
15 #include "nsIObserver.h"
16 #include "nsIObserverService.h"
20 // This class implements a platform-independent part to watch the system's
21 // memory situation and invoke the registered callbacks when we detect
22 // a low-memory situation or a high-memory situation.
23 // The actual logic to monitor the memory status is implemented in a subclass
24 // of nsAvailableMemoryWatcherBase per platform.
25 class nsAvailableMemoryWatcherBase
: public nsIAvailableMemoryWatcherBase
,
27 static StaticRefPtr
<nsAvailableMemoryWatcherBase
> sSingleton
;
28 static const char* const kObserverTopics
[];
30 TimeStamp mLowMemoryStart
;
33 uint32_t mNumOfTabUnloading
;
34 uint32_t mNumOfMemoryPressure
;
36 nsCOMPtr
<nsITabUnloader
> mTabUnloader
;
37 nsCOMPtr
<nsIObserverService
> mObserverSvc
;
38 // Do not change this value off the main thread.
41 virtual ~nsAvailableMemoryWatcherBase() = default;
42 virtual nsresult
Init();
44 void UpdateLowMemoryTimeStamp();
45 void RecordTelemetryEventOnHighMemory();
48 static already_AddRefed
<nsAvailableMemoryWatcherBase
> GetSingleton();
50 nsAvailableMemoryWatcherBase();
52 #if defined(XP_MACOSX)
53 virtual void OnMemoryPressureChanged(MacMemoryPressureLevel aNewLevel
){};
54 virtual void AddChildAnnotations(
55 const UniquePtr
<ipc::CrashReporterHost
>& aCrashReporter
){};
58 NS_DECL_THREADSAFE_ISUPPORTS
59 NS_DECL_NSIAVAILABLEMEMORYWATCHERBASE
63 // Method to create a platform-specific object
64 already_AddRefed
<nsAvailableMemoryWatcherBase
> CreateAvailableMemoryWatcher();
66 } // namespace mozilla
68 #endif // ifndef mozilla_AvailableMemoryWatcher_h