Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // MemoryPressureHandler provides static APIs for handling memory pressure on | |
| 6 // platforms that have such signals, such as Android. | |
| 7 // Chrome will try to discard buffers that aren't deemed essential (individual | |
| 8 // modules will implement their own policy). | |
| 9 // | |
| 10 // Note that the platform would potentially kill chrome in order to free memory | |
| 11 // that other apps may be requesting. | |
| 12 // The trade-off here is in then terms of how long does it take to free such | |
| 13 // memory, and later on potentially re-create it, versus being killed and | |
| 14 // restarted from cold. | |
| 15 | |
| 16 #ifndef BASE_MEMORY_PRESSURE_HANDLER_H_ | |
| 17 #define BASE_MEMORY_PRESSURE_HANDLER_H_ | |
|
joth
2013/06/05 16:51:23
nit: don't like the Handler name as it doesn't han
bulach
2013/06/05 19:02:11
Done.
| |
| 18 | |
| 19 #include "base/base_export.h" | |
| 20 #include "base/basictypes.h" | |
| 21 #include "base/memory/ref_counted.h" | |
| 22 #include "base/memory/singleton.h" | |
| 23 #include "base/observer_list_threadsafe.h" | |
| 24 | |
| 25 namespace base { | |
| 26 | |
| 27 // To start listening, create a new instance, passing a callback to a | |
| 28 // function that takes an MemoryPressureType parameter. To stop listening, | |
| 29 // simply delete the listener object. The implementation guarantees | |
| 30 // that the callback will always be called on the thread that created | |
| 31 // the listener. | |
| 32 // | |
| 33 // Example: | |
| 34 // | |
| 35 // void OnMemoryPressure(MemoryPressureType memory_pressure_type) { | |
| 36 // ... | |
| 37 // } | |
| 38 // | |
| 39 // // Start listening. | |
| 40 // MemoryPressureHandler::Listener* my_listener = | |
| 41 // new MemoryPressureHandler::Listener(base::Bind(&OnMemoryPressure)); | |
| 42 // | |
| 43 // ... | |
| 44 // | |
| 45 // // Stop listening. | |
| 46 // delete my_listener | |
| 47 // | |
| 48 class BASE_EXPORT MemoryPressureHandler { | |
| 49 public: | |
| 50 enum MemoryPressureType { | |
| 51 #define DEFINE_MEMORY_PRESSURE_TYPE(name, value) name = value, | |
| 52 #include "base/memory/memory_pressure_handler_list.h" | |
| 53 }; | |
| 54 typedef base::Callback<void(MemoryPressureType)> MemoryPressureCallback; | |
| 55 | |
| 56 class Listener { | |
| 57 public: | |
| 58 explicit Listener(const MemoryPressureCallback& memory_pressure_callback); | |
| 59 ~Listener(); | |
| 60 | |
| 61 private: | |
| 62 friend class MemoryPressureHandler; | |
| 63 | |
| 64 void Notify(MemoryPressureType memory_pressure_type); | |
| 65 | |
| 66 MemoryPressureCallback callback_; | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(Listener); | |
| 69 }; | |
| 70 | |
| 71 static MemoryPressureHandler* GetInstance(); | |
| 72 | |
| 73 // Internal use only: called when the platform receives a memory pressure | |
| 74 // signal. | |
| 75 void HandleMemoryPressure(MemoryPressureType memory_pressure_type); | |
| 76 | |
| 77 private: | |
| 78 friend struct DefaultSingletonTraits<MemoryPressureHandler>; | |
| 79 | |
| 80 MemoryPressureHandler(); | |
| 81 ~MemoryPressureHandler(); | |
| 82 | |
| 83 void RegisterListener(Listener* listener); | |
| 84 void UnregisterListener(Listener* listener); | |
| 85 | |
| 86 scoped_refptr<ObserverListThreadSafe<Listener> > observers_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(MemoryPressureHandler); | |
| 89 }; | |
| 90 | |
| 91 } // namespace base | |
| 92 | |
| 93 #endif // BASE_MEMORY_PRESSURE_HANDLER_H_ | |
| OLD | NEW |