Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: content/browser/memory/memory_coordinator.cc

Issue 2399293002: Add MemoryCoordinatorDelegate (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "content/browser/memory/memory_coordinator.h" 5 #include "content/browser/memory/memory_coordinator.h"
6 6
7 #include "base/memory/memory_coordinator_client_registry.h" 7 #include "base/memory/memory_coordinator_client_registry.h"
8 #include "content/public/common/content_features.h" 8 #include "content/public/common/content_features.h"
9 9
10 namespace content { 10 namespace content {
11 11
12 // static
13 void MemoryCoordinatorDelegate::Set(
14 const base::WeakPtr<MemoryCoordinatorDelegate>& delegate) {
15 auto* coordinator = MemoryCoordinator::GetInstance();
16 if (coordinator)
17 coordinator->SetDelegate(delegate);
18 }
19
12 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom 20 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom
13 // for the role of this class. 21 // for the role of this class.
14 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { 22 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle {
15 public: 23 public:
16 MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request) 24 MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request)
17 : binding_(this, std::move(request)) { 25 : binding_(this, std::move(request)) {
18 } 26 }
19 27
20 // mojom::MemoryCoordinatorHandle: 28 // mojom::MemoryCoordinatorHandle:
21 void AddChild(mojom::ChildMemoryCoordinatorPtr child) override { 29 void AddChild(mojom::ChildMemoryCoordinatorPtr child) override {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 76
69 // Can't send a message to a child that doesn't exist. 77 // Can't send a message to a child that doesn't exist.
70 auto iter = children_.find(render_process_id); 78 auto iter = children_.find(render_process_id);
71 if (iter == children_.end()) 79 if (iter == children_.end())
72 return false; 80 return false;
73 81
74 // A nop doesn't need to be sent, but is considered successful. 82 // A nop doesn't need to be sent, but is considered successful.
75 if (iter->second.memory_state == memory_state) 83 if (iter->second.memory_state == memory_state)
76 return true; 84 return true;
77 85
86 // Can't suspend the given renderer.
87 if (iter->second.memory_state == mojom::MemoryState::SUSPENDED &&
haraken 2016/10/07 06:38:19 Shouldn't this be: if (memory_state == mojom::M
bashi 2016/10/07 07:13:37 Yes. Thanks. Done.
88 !CanSuspendRenderer(render_process_id))
89 return false;
90
78 // Update the internal state and send the message. 91 // Update the internal state and send the message.
79 iter->second.memory_state = memory_state; 92 iter->second.memory_state = memory_state;
80 iter->second.handle->child()->OnStateChange(memory_state); 93 iter->second.handle->child()->OnStateChange(memory_state);
81 return true; 94 return true;
82 } 95 }
83 96
84 mojom::MemoryState MemoryCoordinator::GetMemoryState( 97 mojom::MemoryState MemoryCoordinator::GetMemoryState(
85 int render_process_id) const { 98 int render_process_id) const {
86 auto iter = children_.find(render_process_id); 99 auto iter = children_.find(render_process_id);
87 if (iter == children_.end()) 100 if (iter == children_.end())
88 return mojom::MemoryState::UNKNOWN; 101 return mojom::MemoryState::UNKNOWN;
89 return iter->second.memory_state; 102 return iter->second.memory_state;
90 } 103 }
91 104
92 void MemoryCoordinator::AddChildForTesting( 105 void MemoryCoordinator::AddChildForTesting(
93 int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { 106 int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) {
94 mojom::MemoryCoordinatorHandlePtr mch; 107 mojom::MemoryCoordinatorHandlePtr mch;
95 auto request = mojo::GetProxy(&mch); 108 auto request = mojo::GetProxy(&mch);
96 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( 109 std::unique_ptr<MemoryCoordinatorHandleImpl> handle(
97 new MemoryCoordinatorHandleImpl(std::move(request))); 110 new MemoryCoordinatorHandleImpl(std::move(request)));
98 handle->AddChild(std::move(child)); 111 handle->AddChild(std::move(child));
99 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); 112 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle));
100 } 113 }
101 114
102 void MemoryCoordinator::OnConnectionError(int render_process_id) { 115 void MemoryCoordinator::OnConnectionError(int render_process_id) {
103 children_.erase(render_process_id); 116 children_.erase(render_process_id);
104 } 117 }
105 118
119 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) {
120 return delegate_ && delegate_->CanSuspendRenderer(render_process_id);
121 }
122
106 void MemoryCoordinator::CreateChildInfoMapEntry( 123 void MemoryCoordinator::CreateChildInfoMapEntry(
107 int render_process_id, 124 int render_process_id,
108 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { 125 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) {
109 auto& child_info = children_[render_process_id]; 126 auto& child_info = children_[render_process_id];
110 // Process always start with normal memory state. 127 // Process always start with normal memory state.
111 // TODO(chrisha): Consider having memory state be a startup parameter of 128 // TODO(chrisha): Consider having memory state be a startup parameter of
112 // child processes, allowing them to be launched in a restricted state. 129 // child processes, allowing them to be launched in a restricted state.
113 child_info.memory_state = mojom::MemoryState::NORMAL; 130 child_info.memory_state = mojom::MemoryState::NORMAL;
114 child_info.handle = std::move(handle); 131 child_info.handle = std::move(handle);
115 } 132 }
116 133
117 MemoryCoordinator::ChildInfo::ChildInfo() {} 134 MemoryCoordinator::ChildInfo::ChildInfo() {}
118 135
119 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { 136 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) {
120 // This is a nop, but exists for compatibility with STL containers. 137 // This is a nop, but exists for compatibility with STL containers.
121 } 138 }
122 139
123 MemoryCoordinator::ChildInfo::~ChildInfo() {} 140 MemoryCoordinator::ChildInfo::~ChildInfo() {}
124 141
125 } // namespace content 142 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698