Bug 1825336 - Make toolkit/components/url-classifier/ buildable outside of a unified...
[gecko.git] / xpcom / base / AvailableMemoryWatcher.h
blob68e075388878d637edef153aa99c47212e7c51d6
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"
13 #include "nsCOMPtr.h"
14 #include "nsIAvailableMemoryWatcherBase.h"
15 #include "nsIObserver.h"
16 #include "nsIObserverService.h"
18 namespace mozilla {
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,
26 public nsIObserver {
27 static StaticRefPtr<nsAvailableMemoryWatcherBase> sSingleton;
28 static const char* const kObserverTopics[];
30 TimeStamp mLowMemoryStart;
32 protected:
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.
39 bool mInteracting;
41 virtual ~nsAvailableMemoryWatcherBase() = default;
42 virtual nsresult Init();
43 void Shutdown();
44 void UpdateLowMemoryTimeStamp();
45 void RecordTelemetryEventOnHighMemory();
47 public:
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){};
56 #endif
58 NS_DECL_THREADSAFE_ISUPPORTS
59 NS_DECL_NSIAVAILABLEMEMORYWATCHERBASE
60 NS_DECL_NSIOBSERVER
63 // Method to create a platform-specific object
64 already_AddRefed<nsAvailableMemoryWatcherBase> CreateAvailableMemoryWatcher();
66 } // namespace mozilla
68 #endif // ifndef mozilla_AvailableMemoryWatcher_h