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 // MemoryPressure provides static APIs for handling memory pressure on |
| 6 // platforms that have such signals, such as Android. |
| 7 // The app will try to discard buffers that aren't deemed essential (individual |
| 8 // modules will implement their own policy). |
| 9 // |
| 10 // Refer to memory_pressure_level_list.h for information about what sorts of |
| 11 // signals can be sent under what conditions. |
| 12 |
| 13 #ifndef BASE_MEMORY_PRESSURE_LISTENER_H_ |
| 14 #define BASE_MEMORY_PRESSURE_H_ |
| 15 |
| 16 #include "base/base_export.h" |
| 17 #include "base/basictypes.h" |
| 18 #include "base/callback.h" |
| 19 |
| 20 namespace base { |
| 21 |
| 22 // To start listening, create a new instance, passing a callback to a |
| 23 // function that takes a MemoryPressureLevel parameter. To stop listening, |
| 24 // simply delete the listener object. The implementation guarantees |
| 25 // that the callback will always be called on the thread that created |
| 26 // the listener. |
| 27 // If this is the same thread as the system is broadcasting the memory pressure |
| 28 // event on, then it is guaranteed you're called synchronously within that |
| 29 // broadcast and hence you should not do long-running garbage collection work. |
| 30 // But conversely, if there's something that needs to be released before |
| 31 // control is returned to system code, this is the place to do it. |
| 32 // Please see notes on memory_pressure_level_list.h: some levels are absolutely |
| 33 // critical, and if not enough memory is returned to the system, it'll |
| 34 // potentially kill the app, and then later the app will have to be |
| 35 // cold-started. |
| 36 // |
| 37 // |
| 38 // Example: |
| 39 // |
| 40 // void OnMemoryPressure(MemoryPressureLevel memory_pressure_level) { |
| 41 // ... |
| 42 // } |
| 43 // |
| 44 // // Start listening. |
| 45 // MemoryPressureListener* my_listener = |
| 46 // new MemoryPressureListener(base::Bind(&OnMemoryPressure)); |
| 47 // |
| 48 // ... |
| 49 // |
| 50 // // Stop listening. |
| 51 // delete my_listener; |
| 52 // |
| 53 class BASE_EXPORT MemoryPressureListener { |
| 54 public: |
| 55 enum MemoryPressureLevel { |
| 56 #define DEFINE_MEMORY_PRESSURE_LEVEL(name, value) name = value, |
| 57 #include "base/memory/memory_pressure_level_list.h" |
| 58 #undef DEFINE_MEMORY_PRESSURE_LEVEL |
| 59 }; |
| 60 |
| 61 typedef base::Callback<void(MemoryPressureLevel)> MemoryPressureCallback; |
| 62 |
| 63 explicit MemoryPressureListener( |
| 64 const MemoryPressureCallback& memory_pressure_callback); |
| 65 ~MemoryPressureListener(); |
| 66 |
| 67 // Intended for use by the platform specific implementation. |
| 68 static void NotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); |
| 69 |
| 70 private: |
| 71 void Notify(MemoryPressureLevel memory_pressure_level); |
| 72 |
| 73 MemoryPressureCallback callback_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(MemoryPressureListener); |
| 76 }; |
| 77 |
| 78 } // namespace base |
| 79 |
| 80 #endif // BASE_MEMORY_PRESSURE_LISTENER_H_ |
OLD | NEW |