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

Side by Side Diff: content/browser/service_worker/service_worker_process_manager.cc

Issue 443593002: ServiceWorker: Move worker candidate process knowledge to ServiceWorkerProcessManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/service_worker/service_worker_process_manager.h" 5 #include "content/browser/service_worker/service_worker_process_manager.h"
6 6
7 #include "content/browser/renderer_host/render_process_host_impl.h" 7 #include "content/browser/renderer_host/render_process_host_impl.h"
8 #include "content/browser/service_worker/service_worker_context_wrapper.h" 8 #include "content/browser/service_worker/service_worker_context_wrapper.h"
9 #include "content/public/browser/browser_thread.h" 9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/site_instance.h" 10 #include "content/public/browser/site_instance.h"
11 #include "url/gurl.h" 11 #include "url/gurl.h"
12 12
13 namespace content { 13 namespace content {
14 14
15 namespace {
16
17 // Functor to sort by the .second element of a struct.
18 struct SecondGreater {
19 template <typename Value>
20 bool operator()(const Value& lhs, const Value& rhs) {
21 return lhs.second > rhs.second;
22 }
23 };
24
25 } // namespace
26
15 static bool IncrementWorkerRefCountByPid(int process_id) { 27 static bool IncrementWorkerRefCountByPid(int process_id) {
16 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); 28 RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
17 if (!rph || rph->FastShutdownStarted()) 29 if (!rph || rph->FastShutdownStarted())
18 return false; 30 return false;
19 31
20 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); 32 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount();
21 return true; 33 return true;
22 } 34 }
23 35
24 ServiceWorkerProcessManager::ProcessInfo::ProcessInfo( 36 ServiceWorkerProcessManager::ProcessInfo::ProcessInfo(
(...skipping 29 matching lines...) Expand all
54 for (std::map<int, ProcessInfo>::const_iterator it = instance_info_.begin(); 66 for (std::map<int, ProcessInfo>::const_iterator it = instance_info_.begin();
55 it != instance_info_.end(); 67 it != instance_info_.end();
56 ++it) { 68 ++it) {
57 RenderProcessHost* rph = RenderProcessHost::FromID(it->second.process_id); 69 RenderProcessHost* rph = RenderProcessHost::FromID(it->second.process_id);
58 DCHECK(rph); 70 DCHECK(rph);
59 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); 71 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount();
60 } 72 }
61 instance_info_.clear(); 73 instance_info_.clear();
62 } 74 }
63 75
76 void ServiceWorkerProcessManager::AddScopePendingProcesses(
77 const GURL& scope, const std::vector<int>& pending_processes) {
78 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
79 BrowserThread::PostTask(
80 BrowserThread::UI,
81 FROM_HERE,
82 base::Bind(&ServiceWorkerProcessManager::AddScopePendingProcesses,
83 weak_this_,
84 scope,
85 pending_processes));
86 return;
87 }
88
89 ProcessRefMap& process_refs = scope_processes_[scope];
90 for (std::vector<int>::const_iterator it = pending_processes.begin();
91 it != pending_processes.end();
92 ++it) {
93 if (process_refs.find(*it) != process_refs.end())
94 continue;
95 if (RenderProcessHost* host = RenderProcessHost::FromID(*it))
96 host->AddObserver(this);
97 else if (*it != process_id_for_test_)
98 continue;
99 process_refs.insert(std::make_pair(*it, 0));
100 }
101 }
102
103 void ServiceWorkerProcessManager::AddScopeProcessReference(
104 const GURL& scope, int process_id) {
105 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
106 BrowserThread::PostTask(
107 BrowserThread::UI,
108 FROM_HERE,
109 base::Bind(&ServiceWorkerProcessManager::AddScopeProcessReference,
110 weak_this_,
111 scope,
112 process_id));
113 return;
114 }
115
116 ProcessRefMap& process_refs = scope_processes_[scope];
117 ProcessRefMap::iterator found = process_refs.find(process_id);
118 if (found == process_refs.end())
119 found = process_refs.insert(std::make_pair(process_id, 0)).first;
120 ++found->second;
121 }
122
123 void ServiceWorkerProcessManager::RemoveScopeProcessReference(
124 const GURL& scope, int process_id) {
125 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
126 BrowserThread::PostTask(
127 BrowserThread::UI,
128 FROM_HERE,
129 base::Bind(&ServiceWorkerProcessManager::RemoveScopeProcessReference,
130 weak_this_,
131 scope,
132 process_id));
133 return;
134 }
135
136 ScopeProcessRefMap::iterator it = scope_processes_.find(scope);
137 if (it == scope_processes_.end()) {
138 NOTREACHED() << "Scope process refrences not found: " << scope;
139 return;
140 }
141 ProcessRefMap& process_refs = it->second;
142 ProcessRefMap::iterator found = process_refs.find(process_id);
143 if (found == process_refs.end()) {
144 NOTREACHED() << "Releasing unknown process ref " << process_id;
145 return;
146 }
147 if (--found->second == 0) {
148 process_refs.erase(found);
149 if (process_refs.empty())
150 scope_processes_.erase(it);
151 }
152 }
153
154 bool ServiceWorkerProcessManager::ScopeHasProcessToRun(
155 const GURL& scope) const {
156 ScopeProcessRefMap::const_iterator it = scope_processes_.find(scope);
157 if (it == scope_processes_.end())
158 return false;
159 return !it->second.empty();
160 }
161
162 void ServiceWorkerProcessManager::RenderProcessHostDestroyed(
163 RenderProcessHost* host) {
164 for (ScopeProcessRefMap::iterator it = scope_processes_.begin();
165 it != scope_processes_.end(); ) {
166 ProcessRefMap& process_refs = it->second;
167 ProcessRefMap::iterator found = process_refs.find(host->GetID());
168 if (found != process_refs.end()) {
169 process_refs.erase(found);
170 if (process_refs.empty()) {
171 it = scope_processes_.erase(it);
172 continue;
173 }
174 }
175 ++it;
176 }
177 }
178
64 void ServiceWorkerProcessManager::AllocateWorkerProcess( 179 void ServiceWorkerProcessManager::AllocateWorkerProcess(
65 int embedded_worker_id, 180 int embedded_worker_id,
66 const std::vector<int>& process_ids, 181 const GURL& scope,
67 const GURL& script_url, 182 const GURL& script_url,
68 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& 183 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>&
69 callback) { 184 callback) {
70 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 185 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
71 BrowserThread::PostTask( 186 BrowserThread::PostTask(
72 BrowserThread::UI, 187 BrowserThread::UI,
73 FROM_HERE, 188 FROM_HERE,
74 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, 189 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess,
75 weak_this_, 190 weak_this_,
76 embedded_worker_id, 191 embedded_worker_id,
77 process_ids, 192 scope,
78 script_url, 193 script_url,
79 callback)); 194 callback));
80 return; 195 return;
81 } 196 }
82 197
83 if (process_id_for_test_ != -1) { 198 if (process_id_for_test_ != -1) {
84 // Let tests specify the returned process ID. Note: We may need to be able 199 // Let tests specify the returned process ID. Note: We may need to be able
85 // to specify the error code too. 200 // to specify the error code too.
86 BrowserThread::PostTask( 201 BrowserThread::PostTask(
87 BrowserThread::IO, 202 BrowserThread::IO,
88 FROM_HERE, 203 FROM_HERE,
89 base::Bind(callback, SERVICE_WORKER_OK, process_id_for_test_)); 204 base::Bind(callback, SERVICE_WORKER_OK, process_id_for_test_));
90 return; 205 return;
91 } 206 }
92 207
93 DCHECK(!ContainsKey(instance_info_, embedded_worker_id)) 208 DCHECK(!ContainsKey(instance_info_, embedded_worker_id))
94 << embedded_worker_id << " already has a process allocated"; 209 << embedded_worker_id << " already has a process allocated";
95 210
96 for (std::vector<int>::const_iterator it = process_ids.begin(); 211 std::vector<int> sorted_candidates = SortProcessesForScope(scope);
97 it != process_ids.end(); 212 if (!sorted_candidates.empty()) {
98 ++it) { 213 for (std::vector<int>::const_iterator it = sorted_candidates.begin();
99 if (IncrementWorkerRefCountByPid(*it)) { 214 it != sorted_candidates.end();
215 ++it) {
216 if (!IncrementWorkerRefCountByPid(*it))
217 continue;
100 instance_info_.insert( 218 instance_info_.insert(
101 std::make_pair(embedded_worker_id, ProcessInfo(*it))); 219 std::make_pair(embedded_worker_id, ProcessInfo(*it)));
102 BrowserThread::PostTask(BrowserThread::IO, 220 BrowserThread::PostTask(BrowserThread::IO,
103 FROM_HERE, 221 FROM_HERE,
104 base::Bind(callback, SERVICE_WORKER_OK, *it)); 222 base::Bind(callback, SERVICE_WORKER_OK, *it));
105 return; 223 return;
106 } 224 }
107 } 225 }
108 226
109 if (!browser_context_) { 227 if (!browser_context_) {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 } else { 290 } else {
173 rph = RenderProcessHost::FromID(info->second.process_id); 291 rph = RenderProcessHost::FromID(info->second.process_id);
174 DCHECK(rph) 292 DCHECK(rph)
175 << "Process " << info->second.process_id 293 << "Process " << info->second.process_id
176 << " was destroyed unexpectedly. Did we actually hold a reference?"; 294 << " was destroyed unexpectedly. Did we actually hold a reference?";
177 } 295 }
178 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); 296 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount();
179 instance_info_.erase(info); 297 instance_info_.erase(info);
180 } 298 }
181 299
300 std::vector<int> ServiceWorkerProcessManager::SortProcessesForScope(
301 const GURL& scope) const {
302 ScopeProcessRefMap::const_iterator it = scope_processes_.find(scope);
303 if (it == scope_processes_.end())
304 return std::vector<int>();
305
306 std::vector<std::pair<int, int> > counted(
307 it->second.begin(), it->second.end());
308 std::sort(counted.begin(), counted.end(), SecondGreater());
309
310 std::vector<int> result(counted.size());
311 for (size_t i = 0; i < counted.size(); ++i)
312 result[i] = counted[i].first;
313 return result;
314 }
315
182 } // namespace content 316 } // namespace content
183 317
184 namespace base { 318 namespace base {
185 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the 319 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the
186 // member WeakPtr to safely guard the object's lifetime when used on that 320 // member WeakPtr to safely guard the object's lifetime when used on that
187 // thread. 321 // thread.
188 void DefaultDeleter<content::ServiceWorkerProcessManager>::operator()( 322 void DefaultDeleter<content::ServiceWorkerProcessManager>::operator()(
189 content::ServiceWorkerProcessManager* ptr) const { 323 content::ServiceWorkerProcessManager* ptr) const {
190 content::BrowserThread::DeleteSoon( 324 content::BrowserThread::DeleteSoon(
191 content::BrowserThread::UI, FROM_HERE, ptr); 325 content::BrowserThread::UI, FROM_HERE, ptr);
192 } 326 }
193 } // namespace base 327 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698