MemoryPressureObserver.h (1798B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: sw=2 ts=8 et : 3 */ 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #ifndef MOZILLA_LAYERS_MEMORYPRESSUREOBSERVER_H 9 #define MOZILLA_LAYERS_MEMORYPRESSUREOBSERVER_H 10 11 #include "nsIObserver.h" 12 13 namespace mozilla { 14 namespace layers { 15 16 // A simple memory pressure observer implementation born out of the realization 17 // that almost all of our memory pressure observers do exactly the same thing. 18 // 19 // The intended way to use it is to have the class that nees to react on memory 20 // pressure inherit the MemoryPressureListener interface and own a strong 21 // reference to a MemoryPressureListener object. 22 // Call Unregister on the listener in the destructor of your class or whenever 23 // you do not which to receive the notification anymore, otherwise the listener 24 // will be held alive by the observer service (leak) and keep a dangling pointer 25 // to your class. 26 27 /// See nsIMemory.idl 28 enum class MemoryPressureReason { 29 LOW_MEMORY, 30 LOW_MEMORY_ONGOING, 31 HEAP_MINIMIZE, 32 }; 33 34 class MemoryPressureListener { 35 public: 36 virtual void OnMemoryPressure(MemoryPressureReason aWhy) = 0; 37 }; 38 39 class MemoryPressureObserver final : public nsIObserver { 40 public: 41 NS_DECL_ISUPPORTS 42 NS_DECL_NSIOBSERVER 43 44 // Returns null if anything goes wrong. 45 static already_AddRefed<MemoryPressureObserver> Create( 46 MemoryPressureListener* aListener); 47 48 void Unregister(); 49 50 private: 51 explicit MemoryPressureObserver(MemoryPressureListener* aListener); 52 ~MemoryPressureObserver(); 53 MemoryPressureListener* mListener; 54 }; 55 56 } // namespace layers 57 } // namespace mozilla 58 59 #endif