Commit ca40bc98 authored by bashi's avatar bashi Committed by Commit bot

Extract client handing from ChildMemoryCoordinatorImpl

Add ClientRegistry class which becomes a base class of both
MemoryCoordinator and ChildMemoryCoordinatorImpl since client
registration will be the same in both coordinators.

BUG=617492

Review-Url: https://codereview.chromium.org/2144163002
Cr-Commit-Position: refs/heads/master@{#406725}
parent 823d1b17
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
'memory_coordinator_mojo_bindings', 'memory_coordinator_mojo_bindings',
], ],
'sources': [ 'sources': [
'memory_coordinator/common/client_registry.cc',
'memory_coordinator/common/client_registry.h',
'memory_coordinator/common/memory_coordinator_client.h', 'memory_coordinator/common/memory_coordinator_client.h',
'memory_coordinator/common/memory_coordinator_features.cc', 'memory_coordinator/common/memory_coordinator_features.cc',
'memory_coordinator/common/memory_coordinator_features.h', 'memory_coordinator/common/memory_coordinator_features.h',
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef COMPONENTS_MEMORY_COORDINATOR_BROWSER_MEMORY_COORDINATOR_H_ #ifndef COMPONENTS_MEMORY_COORDINATOR_BROWSER_MEMORY_COORDINATOR_H_
#define COMPONENTS_MEMORY_COORDINATOR_BROWSER_MEMORY_COORDINATOR_H_ #define COMPONENTS_MEMORY_COORDINATOR_BROWSER_MEMORY_COORDINATOR_H_
#include "components/memory_coordinator/common/client_registry.h"
#include "components/memory_coordinator/public/interfaces/memory_coordinator.mojom.h" #include "components/memory_coordinator/public/interfaces/memory_coordinator.mojom.h"
#include "mojo/public/cpp/bindings/binding.h" #include "mojo/public/cpp/bindings/binding.h"
...@@ -15,10 +16,10 @@ class MemoryCoordinatorHandleImpl; ...@@ -15,10 +16,10 @@ class MemoryCoordinatorHandleImpl;
// MemoryCoordinator is responsible for the whole memory management accross the // MemoryCoordinator is responsible for the whole memory management accross the
// browser and child proceeses. It will dispatch memory events to its clients // browser and child proceeses. It will dispatch memory events to its clients
// and child processes based on its best knowledge of the memory usage. // and child processes based on its best knowledge of the memory usage.
class MemoryCoordinator { class MemoryCoordinator : public ClientRegistry {
public: public:
MemoryCoordinator(); MemoryCoordinator();
~MemoryCoordinator(); ~MemoryCoordinator() override;
void CreateHandle(int render_process_id, void CreateHandle(int render_process_id,
mojom::MemoryCoordinatorHandleRequest request); mojom::MemoryCoordinatorHandleRequest request);
......
...@@ -8,26 +8,16 @@ namespace memory_coordinator { ...@@ -8,26 +8,16 @@ namespace memory_coordinator {
ChildMemoryCoordinatorImpl::ChildMemoryCoordinatorImpl( ChildMemoryCoordinatorImpl::ChildMemoryCoordinatorImpl(
mojom::MemoryCoordinatorHandlePtr parent) mojom::MemoryCoordinatorHandlePtr parent)
: binding_(this), clients_(new ClientList), parent_(std::move(parent)) { : binding_(this), parent_(std::move(parent)) {
parent_->AddChild(binding_.CreateInterfacePtrAndBind()); parent_->AddChild(binding_.CreateInterfacePtrAndBind());
} }
ChildMemoryCoordinatorImpl::~ChildMemoryCoordinatorImpl() { ChildMemoryCoordinatorImpl::~ChildMemoryCoordinatorImpl() {
} }
void ChildMemoryCoordinatorImpl::RegisterClient(
MemoryCoordinatorClient* client) {
clients_->AddObserver(client);
}
void ChildMemoryCoordinatorImpl::UnregisterClient(
MemoryCoordinatorClient* client) {
clients_->RemoveObserver(client);
}
void ChildMemoryCoordinatorImpl::OnStateChange(mojom::MemoryState state) { void ChildMemoryCoordinatorImpl::OnStateChange(mojom::MemoryState state) {
clients_->Notify(FROM_HERE, &MemoryCoordinatorClient::OnMemoryStateChange, clients()->Notify(FROM_HERE, &MemoryCoordinatorClient::OnMemoryStateChange,
state); state);
} }
} // namespace memory_coordinator } // namespace memory_coordinator
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#define COMPONENTS_MEMORY_COORDINATOR_CHILD_CHILD_MEMORY_COORDINATOR_IMPL_H_ #define COMPONENTS_MEMORY_COORDINATOR_CHILD_CHILD_MEMORY_COORDINATOR_IMPL_H_
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/observer_list_threadsafe.h" #include "components/memory_coordinator/common/client_registry.h"
#include "components/memory_coordinator/common/memory_coordinator_client.h" #include "components/memory_coordinator/common/memory_coordinator_client.h"
#include "components/memory_coordinator/common/memory_coordinator_features.h" #include "components/memory_coordinator/common/memory_coordinator_features.h"
#include "components/memory_coordinator/public/interfaces/child_memory_coordinator.mojom.h" #include "components/memory_coordinator/public/interfaces/child_memory_coordinator.mojom.h"
...@@ -19,15 +19,12 @@ namespace memory_coordinator { ...@@ -19,15 +19,12 @@ namespace memory_coordinator {
// It lives in child processes and is responsible for dispatching memory events // It lives in child processes and is responsible for dispatching memory events
// to its clients. // to its clients.
class ChildMemoryCoordinatorImpl class ChildMemoryCoordinatorImpl
: NON_EXPORTED_BASE(public mojom::ChildMemoryCoordinator) { : public ClientRegistry,
NON_EXPORTED_BASE(public mojom::ChildMemoryCoordinator) {
public: public:
explicit ChildMemoryCoordinatorImpl(mojom::MemoryCoordinatorHandlePtr parent); explicit ChildMemoryCoordinatorImpl(mojom::MemoryCoordinatorHandlePtr parent);
~ChildMemoryCoordinatorImpl() override; ~ChildMemoryCoordinatorImpl() override;
// Registers/unregisters a client. Does not take ownership of client.
void RegisterClient(MemoryCoordinatorClient* client);
void UnregisterClient(MemoryCoordinatorClient* client);
// mojom::ChildMemoryCoordinator implementations: // mojom::ChildMemoryCoordinator implementations:
void OnStateChange(mojom::MemoryState state) override; void OnStateChange(mojom::MemoryState state) override;
...@@ -35,8 +32,6 @@ class ChildMemoryCoordinatorImpl ...@@ -35,8 +32,6 @@ class ChildMemoryCoordinatorImpl
friend class ChildMemoryCoordinatorImplTest; friend class ChildMemoryCoordinatorImplTest;
mojo::Binding<mojom::ChildMemoryCoordinator> binding_; mojo::Binding<mojom::ChildMemoryCoordinator> binding_;
using ClientList = base::ObserverListThreadSafe<MemoryCoordinatorClient>;
scoped_refptr<ClientList> clients_;
mojom::MemoryCoordinatorHandlePtr parent_; mojom::MemoryCoordinatorHandlePtr parent_;
DISALLOW_COPY_AND_ASSIGN(ChildMemoryCoordinatorImpl); DISALLOW_COPY_AND_ASSIGN(ChildMemoryCoordinatorImpl);
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
source_set("common") { source_set("common") {
sources = [ sources = [
"client_registry.cc",
"client_registry.h",
"memory_coordinator_client.h", "memory_coordinator_client.h",
"memory_coordinator_features.cc", "memory_coordinator_features.cc",
"memory_coordinator_features.h", "memory_coordinator_features.h",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/memory_coordinator/common/client_registry.h"
namespace memory_coordinator {
ClientRegistry::ClientRegistry() : clients_(new ClientList) {}
ClientRegistry::~ClientRegistry() {}
void ClientRegistry::RegisterClient(MemoryCoordinatorClient* client) {
clients_->AddObserver(client);
}
void ClientRegistry::UnregisterClient(MemoryCoordinatorClient* client) {
clients_->RemoveObserver(client);
}
} // namespace memory_coordinator
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_MEMORY_COORDINATOR_COMMON_CLIENT_REGISTRY_H_
#define COMPONENTS_MEMORY_COORDINATOR_COMMON_CLIENT_REGISTRY_H_
#include "base/observer_list_threadsafe.h"
#include "components/memory_coordinator/common/memory_coordinator_client.h"
namespace memory_coordinator {
// ClientRegistry is a base class of process-specific memory coordinator
// and provides ways to register/unregister MemoryCoordinatorClients.
class ClientRegistry {
public:
ClientRegistry();
virtual ~ClientRegistry();
// Registers/unregisters a client. Does not take ownership of client.
void RegisterClient(MemoryCoordinatorClient* client);
void UnregisterClient(MemoryCoordinatorClient* client);
protected:
using ClientList = base::ObserverListThreadSafe<MemoryCoordinatorClient>;
ClientList* clients() { return clients_.get(); }
private:
scoped_refptr<ClientList> clients_;
};
} // namespace memory_coordinator
#endif // COMPONENTS_MEMORY_COORDINATOR_COMMON_CLIENT_REGISTRY_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment