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
3 // found in the LICENSE file.
5 #ifndef MemoryPurgeController_h
6 #define MemoryPurgeController_h
8 #include "platform/PlatformExport.h"
9 #include "platform/heap/Handle.h"
10 #include "wtf/MainThread.h"
14 enum class MemoryPurgeMode
{
15 // The tab contains the webview went to background
17 // TODO(bashi): Add more modes as needed.
20 enum class DeviceKind
{
25 // Classes which have discardable/reducible memory can implement this
26 // interface to be informed when they should reduce memory consumption.
27 // MemoryPurgeController assumes that subclasses of MemoryPurgeClient are
29 class MemoryPurgeClient
: public WillBeGarbageCollectedMixin
{
31 virtual ~MemoryPurgeClient() { }
33 // MemoryPurgeController invokes this callback when a memory purge event
35 virtual void purgeMemory(MemoryPurgeMode
, DeviceKind
) = 0;
40 // MemoryPurgeController listens to some events which could be opportunities
41 // for reducing memory consumption and notifies its clients.
42 // Since we want to control memory per tab, MemoryPurgeController is owned by
44 class PLATFORM_EXPORT MemoryPurgeController final
: public NoBaseWillBeGarbageCollected
<MemoryPurgeController
> {
45 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(MemoryPurgeController
);
47 static PassOwnPtrWillBeRawPtr
<MemoryPurgeController
> create()
49 return adoptPtrWillBeNoop(new MemoryPurgeController
);
52 void registerClient(MemoryPurgeClient
* client
)
54 ASSERT(isMainThread());
56 ASSERT(!m_clients
.contains(client
));
57 m_clients
.add(client
);
60 void unregisterClient(MemoryPurgeClient
* client
)
62 ASSERT(isMainThread());
63 ASSERT(m_clients
.contains(client
));
64 m_clients
.remove(client
);
70 MemoryPurgeController();
72 void purgeMemory(MemoryPurgeMode
);
74 WillBeHeapHashSet
<RawPtrWillBeWeakMember
<MemoryPurgeClient
>> m_clients
;
75 DeviceKind m_deviceKind
;
80 #endif // MemoryPurgeController_h