| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 BASE_MEMORY_MEMORY_CLIENT_REGISTRY_H_ | 5 #ifndef BASE_MEMORY_MEMORY_CLIENT_REGISTRY_H_ |
| 6 #define BASE_MEMORY_MEMORY_CLIENT_REGISTRY_H_ | 6 #define BASE_MEMORY_MEMORY_CLIENT_REGISTRY_H_ |
| 7 | 7 |
| 8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/memory/memory_coordinator_client.h" | 9 #include "base/memory/memory_coordinator_client.h" |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "base/observer_list_threadsafe.h" | 11 #include "base/observer_list_threadsafe.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 | 14 |
| 15 // MemoryCoordinatorClientRegistry is the registry for | 15 // MemoryCoordinatorClientRegistry is the registry of MemoryCoordinatorClients. |
| 16 // MemoryCoordinatorClients. Callbacks of MemoryCoordinatorClient are called | 16 // This class manages clients and provides a way to notify memory state changes |
| 17 // via MemoryCoordinator. | 17 // to clients, but this isn't responsible to determine how/when to change |
| 18 // memory states. |
| 19 // |
| 20 // Threading guarantees: |
| 21 // This class uses ObserverListThreadsafe internally, which means that |
| 22 // * Registering/unregistering callbacks are thread-safe. |
| 23 // * Callbacks are invoked on the same thread on which they are registered. |
| 24 // See base/observer_list_threadsafe.h for reference. |
| 25 // |
| 26 // Ownership management: |
| 27 // This class doesn't take the ownership of clients. Clients must be |
| 28 // unregistered before they are destroyed. |
| 18 class BASE_EXPORT MemoryCoordinatorClientRegistry { | 29 class BASE_EXPORT MemoryCoordinatorClientRegistry { |
| 19 public: | 30 public: |
| 20 static MemoryCoordinatorClientRegistry* GetInstance(); | 31 static MemoryCoordinatorClientRegistry* GetInstance(); |
| 21 | 32 |
| 22 ~MemoryCoordinatorClientRegistry(); | 33 ~MemoryCoordinatorClientRegistry(); |
| 23 | 34 |
| 24 // Registers/unregisters a client. Does not take ownership of client. | 35 // Registers/unregisters a client. Does not take ownership of client. |
| 25 void Register(MemoryCoordinatorClient* client); | 36 void Register(MemoryCoordinatorClient* client); |
| 26 void Unregister(MemoryCoordinatorClient* client); | 37 void Unregister(MemoryCoordinatorClient* client); |
| 27 | 38 |
| 28 using ClientList = ObserverListThreadSafe<MemoryCoordinatorClient>; | 39 // Notify clients of a memory state change. |
| 29 ClientList* clients() { return clients_.get(); } | 40 void Notify(MemoryState state); |
| 30 | 41 |
| 31 private: | 42 private: |
| 32 friend struct DefaultSingletonTraits<MemoryCoordinatorClientRegistry>; | 43 friend struct DefaultSingletonTraits<MemoryCoordinatorClientRegistry>; |
| 33 | 44 |
| 34 MemoryCoordinatorClientRegistry(); | 45 MemoryCoordinatorClientRegistry(); |
| 35 | 46 |
| 47 using ClientList = ObserverListThreadSafe<MemoryCoordinatorClient>; |
| 36 scoped_refptr<ClientList> clients_; | 48 scoped_refptr<ClientList> clients_; |
| 37 }; | 49 }; |
| 38 | 50 |
| 39 } // namespace base | 51 } // namespace base |
| 40 | 52 |
| 41 #endif // BASE_MEMORY_MEMORY_CLIENT_REGISTRY_H_ | 53 #endif // BASE_MEMORY_MEMORY_CLIENT_REGISTRY_H_ |
| OLD | NEW |