| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MemoryPurgeController_h | 5 #ifndef MemoryPurgeController_h |
| 6 #define MemoryPurgeController_h | 6 #define MemoryPurgeController_h |
| 7 | 7 |
| 8 #include "platform/PlatformExport.h" | 8 #include "platform/PlatformExport.h" |
| 9 #include "platform/Timer.h" | 9 #include "platform/Timer.h" |
| 10 #include "platform/heap/Handle.h" | 10 #include "platform/heap/Handle.h" |
| 11 #include "public/platform/WebMemoryPressureLevel.h" | 11 #include "public/platform/WebMemoryPressureLevel.h" |
| 12 #include "wtf/MainThread.h" | 12 #include "wtf/MainThread.h" |
| 13 | 13 |
| 14 namespace blink { | 14 namespace blink { |
| 15 | 15 |
| 16 enum class DeviceKind { | 16 enum class DeviceKind { |
| 17 NotSpecified, | 17 NotSpecified, |
| 18 LowEnd, | 18 LowEnd, |
| 19 }; | 19 }; |
| 20 | 20 |
| 21 // Classes which have discardable/reducible memory can implement this | 21 // Classes which have discardable/reducible memory can implement this |
| 22 // interface to be informed when they should reduce memory consumption. | 22 // interface to be informed when they should reduce memory consumption. |
| 23 // MemoryPurgeController assumes that subclasses of MemoryPurgeClient are | 23 // MemoryPurgeController assumes that subclasses of MemoryPurgeClient are |
| 24 // WillBes. | 24 // WillBes. |
| 25 class PLATFORM_EXPORT MemoryPurgeClient : public WillBeGarbageCollectedMixin { | 25 class PLATFORM_EXPORT MemoryPurgeClient : public GarbageCollectedMixin { |
| 26 public: | 26 public: |
| 27 virtual ~MemoryPurgeClient() { } | 27 virtual ~MemoryPurgeClient() { } |
| 28 | 28 |
| 29 // MemoryPurgeController invokes this callback when a memory purge event | 29 // MemoryPurgeController invokes this callback when a memory purge event |
| 30 // has occurred. | 30 // has occurred. |
| 31 virtual void purgeMemory(DeviceKind) = 0; | 31 virtual void purgeMemory(DeviceKind) = 0; |
| 32 | 32 |
| 33 DECLARE_VIRTUAL_TRACE(); | 33 DECLARE_VIRTUAL_TRACE(); |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 // MemoryPurgeController listens to some events which could be opportunities | 36 // MemoryPurgeController listens to some events which could be opportunities |
| 37 // for reducing memory consumption and notifies its clients. | 37 // for reducing memory consumption and notifies its clients. |
| 38 // Since we want to control memory per tab, MemoryPurgeController is owned by | 38 // Since we want to control memory per tab, MemoryPurgeController is owned by |
| 39 // Page. | 39 // Page. |
| 40 class PLATFORM_EXPORT MemoryPurgeController final : public NoBaseWillBeGarbageCo
llectedFinalized<MemoryPurgeController> { | 40 class PLATFORM_EXPORT MemoryPurgeController final : public GarbageCollectedFinal
ized<MemoryPurgeController> { |
| 41 USING_FAST_MALLOC_WILL_BE_REMOVED(MemoryPurgeController); | |
| 42 WTF_MAKE_NONCOPYABLE(MemoryPurgeController); | 41 WTF_MAKE_NONCOPYABLE(MemoryPurgeController); |
| 43 public: | 42 public: |
| 44 static void onMemoryPressure(WebMemoryPressureLevel); | 43 static void onMemoryPressure(WebMemoryPressureLevel); |
| 45 | 44 |
| 46 static PassOwnPtrWillBeRawPtr<MemoryPurgeController> create() | 45 static RawPtr<MemoryPurgeController> create() |
| 47 { | 46 { |
| 48 return adoptPtrWillBeNoop(new MemoryPurgeController); | 47 return (new MemoryPurgeController); |
| 49 } | 48 } |
| 50 ~MemoryPurgeController(); | 49 ~MemoryPurgeController(); |
| 51 | 50 |
| 52 void registerClient(MemoryPurgeClient* client) | 51 void registerClient(MemoryPurgeClient* client) |
| 53 { | 52 { |
| 54 ASSERT(isMainThread()); | 53 ASSERT(isMainThread()); |
| 55 ASSERT(client); | 54 ASSERT(client); |
| 56 ASSERT(!m_clients.contains(client)); | 55 ASSERT(!m_clients.contains(client)); |
| 57 m_clients.add(client); | 56 m_clients.add(client); |
| 58 } | 57 } |
| 59 | 58 |
| 60 void unregisterClient(MemoryPurgeClient* client) | 59 void unregisterClient(MemoryPurgeClient* client) |
| 61 { | 60 { |
| 62 // Don't assert m_clients.contains() so that clients can unregister | 61 // Don't assert m_clients.contains() so that clients can unregister |
| 63 // unconditionally. | 62 // unconditionally. |
| 64 ASSERT(isMainThread()); | 63 ASSERT(isMainThread()); |
| 65 m_clients.remove(client); | 64 m_clients.remove(client); |
| 66 } | 65 } |
| 67 | 66 |
| 68 void purgeMemory(); | 67 void purgeMemory(); |
| 69 | 68 |
| 70 DECLARE_TRACE(); | 69 DECLARE_TRACE(); |
| 71 | 70 |
| 72 private: | 71 private: |
| 73 MemoryPurgeController(); | 72 MemoryPurgeController(); |
| 74 | 73 |
| 75 WillBeHeapHashSet<RawPtrWillBeWeakMember<MemoryPurgeClient>> m_clients; | 74 HeapHashSet<WeakMember<MemoryPurgeClient>> m_clients; |
| 76 DeviceKind m_deviceKind; | 75 DeviceKind m_deviceKind; |
| 77 }; | 76 }; |
| 78 | 77 |
| 79 } // namespace blink | 78 } // namespace blink |
| 80 | 79 |
| 81 #endif // MemoryPurgeController_h | 80 #endif // MemoryPurgeController_h |
| OLD | NEW |