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 // On Windows the publicly available methods (::Observe() and ::Notify()) are
34 // called on the main thread while the ::LowMemoryCallback() method is called
35 // by an external thread. All functions called from those must acquire a lock
36 // on this mutex before accessing the object's fields to prevent races.
38 // On Linux we might tell polling to start/stop from our polling thread
39 // or from the main thread during ::Observe().
42 uint32_t mNumOfTabUnloading
MOZ_GUARDED_BY(mMutex
);
43 uint32_t mNumOfMemoryPressure
MOZ_GUARDED_BY(mMutex
);
45 nsCOMPtr
<nsITabUnloader
> mTabUnloader
;
46 nsCOMPtr
<nsIObserverService
> mObserverSvc
;
47 // Do not change this value off the main thread.
50 virtual ~nsAvailableMemoryWatcherBase() = default;
51 virtual nsresult
Init();
53 void UpdateLowMemoryTimeStamp();
54 void RecordTelemetryEventOnHighMemory(const MutexAutoLock
&)
58 static already_AddRefed
<nsAvailableMemoryWatcherBase
> GetSingleton();
60 nsAvailableMemoryWatcherBase();
62 #if defined(XP_DARWIN)
63 virtual void OnMemoryPressureChanged(MacMemoryPressureLevel aNewLevel
){};
64 virtual void AddChildAnnotations(
65 const UniquePtr
<ipc::CrashReporterHost
>& aCrashReporter
){};
68 NS_DECL_THREADSAFE_ISUPPORTS
69 NS_DECL_NSIAVAILABLEMEMORYWATCHERBASE
73 // Method to create a platform-specific object
74 already_AddRefed
<nsAvailableMemoryWatcherBase
> CreateAvailableMemoryWatcher();
76 } // namespace mozilla
78 #endif // ifndef mozilla_AvailableMemoryWatcher_h