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

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

Issue 1708583003: Pass data saver pref value into embedded worker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <utility> 10 #include <utility>
11 11
12 #include "content/browser/renderer_host/render_process_host_impl.h" 12 #include "content/browser/renderer_host/render_process_host_impl.h"
13 #include "content/browser/service_worker/service_worker_context_wrapper.h" 13 #include "content/browser/service_worker/service_worker_context_wrapper.h"
14 #include "content/common/service_worker/embedded_worker_settings.h"
14 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/content_browser_client.h"
15 #include "content/public/browser/site_instance.h" 17 #include "content/public/browser/site_instance.h"
16 #include "content/public/common/child_process_host.h" 18 #include "content/public/common/child_process_host.h"
19 #include "content/public/common/content_client.h"
17 #include "url/gurl.h" 20 #include "url/gurl.h"
18 21
19 namespace content { 22 namespace content {
20 23
21 namespace { 24 namespace {
22 25
23 // Functor to sort by the .second element of a struct. 26 // Functor to sort by the .second element of a struct.
24 struct SecondGreater { 27 struct SecondGreater {
25 template <typename Value> 28 template <typename Value>
26 bool operator()(const Value& lhs, const Value& rhs) { 29 bool operator()(const Value& lhs, const Value& rhs) {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 return !it->second.empty(); 132 return !it->second.empty();
130 } 133 }
131 134
132 void ServiceWorkerProcessManager::AllocateWorkerProcess( 135 void ServiceWorkerProcessManager::AllocateWorkerProcess(
133 int embedded_worker_id, 136 int embedded_worker_id,
134 const GURL& pattern, 137 const GURL& pattern,
135 const GURL& script_url, 138 const GURL& script_url,
136 bool can_use_existing_process, 139 bool can_use_existing_process,
137 const base::Callback<void(ServiceWorkerStatusCode, 140 const base::Callback<void(ServiceWorkerStatusCode,
138 int process_id, 141 int process_id,
139 bool is_new_process)>& callback) { 142 bool is_new_process,
143 const EmbeddedWorkerSettings&)>& callback) {
140 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 144 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
141 BrowserThread::PostTask( 145 BrowserThread::PostTask(
142 BrowserThread::UI, FROM_HERE, 146 BrowserThread::UI, FROM_HERE,
143 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, 147 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess,
144 weak_this_, embedded_worker_id, pattern, script_url, 148 weak_this_, embedded_worker_id, pattern, script_url,
145 can_use_existing_process, callback)); 149 can_use_existing_process, callback));
146 return; 150 return;
147 } 151 }
148 152
153 EmbeddedWorkerSettings settings;
154 settings.data_saver_enabled =
155 GetContentClient()->browser()->IsDataSaverEnabled(browser_context_);
falken 2016/02/19 06:58:35 We should add a comment that this the EWSettings f
bengr 2016/03/07 23:35:29 Done.
156
149 if (process_id_for_test_ != ChildProcessHost::kInvalidUniqueID) { 157 if (process_id_for_test_ != ChildProcessHost::kInvalidUniqueID) {
150 // Let tests specify the returned process ID. Note: We may need to be able 158 // Let tests specify the returned process ID. Note: We may need to be able
151 // to specify the error code too. 159 // to specify the error code too.
152 int result = can_use_existing_process ? process_id_for_test_ 160 int result = can_use_existing_process ? process_id_for_test_
153 : new_process_id_for_test_; 161 : new_process_id_for_test_;
154 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 162 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
155 base::Bind(callback, SERVICE_WORKER_OK, result, 163 base::Bind(callback, SERVICE_WORKER_OK, result,
156 false /* is_new_process */)); 164 false /* is_new_process */, settings));
157 return; 165 return;
158 } 166 }
159 167
160 if (IsShutdown()) { 168 if (IsShutdown()) {
161 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 169 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
162 base::Bind(callback, SERVICE_WORKER_ERROR_ABORT, 170 base::Bind(callback, SERVICE_WORKER_ERROR_ABORT,
163 ChildProcessHost::kInvalidUniqueID, 171 ChildProcessHost::kInvalidUniqueID,
164 false /* is_new_process */)); 172 false /* is_new_process */, settings));
165 return; 173 return;
166 } 174 }
167 175
168 // TODO(nhiroki): Make sure the instance info is not mixed up. 176 // TODO(nhiroki): Make sure the instance info is not mixed up.
169 // (http://crbug.com/568915) 177 // (http://crbug.com/568915)
170 CHECK(!ContainsKey(instance_info_, embedded_worker_id)) 178 CHECK(!ContainsKey(instance_info_, embedded_worker_id))
171 << embedded_worker_id << " already has a process allocated"; 179 << embedded_worker_id << " already has a process allocated";
172 180
173 if (can_use_existing_process) { 181 if (can_use_existing_process) {
174 int process_id = FindAvailableProcess(pattern); 182 int process_id = FindAvailableProcess(pattern);
175 if (process_id != ChildProcessHost::kInvalidUniqueID) { 183 if (process_id != ChildProcessHost::kInvalidUniqueID) {
176 RenderProcessHost::FromID(process_id)->IncrementWorkerRefCount(); 184 RenderProcessHost::FromID(process_id)->IncrementWorkerRefCount();
177 instance_info_.insert( 185 instance_info_.insert(
178 std::make_pair(embedded_worker_id, ProcessInfo(process_id))); 186 std::make_pair(embedded_worker_id, ProcessInfo(process_id)));
179 BrowserThread::PostTask( 187 BrowserThread::PostTask(
180 BrowserThread::IO, FROM_HERE, 188 BrowserThread::IO, FROM_HERE,
181 base::Bind(callback, SERVICE_WORKER_OK, process_id, 189 base::Bind(callback, SERVICE_WORKER_OK, process_id,
182 false /* is_new_process */)); 190 false /* is_new_process */, settings));
183 return; 191 return;
184 } 192 }
185 } 193 }
186 194
187 // No existing processes available; start a new one. 195 // No existing processes available; start a new one.
188 scoped_refptr<SiteInstance> site_instance = 196 scoped_refptr<SiteInstance> site_instance =
189 SiteInstance::CreateForURL(browser_context_, script_url); 197 SiteInstance::CreateForURL(browser_context_, script_url);
190 RenderProcessHost* rph = site_instance->GetProcess(); 198 RenderProcessHost* rph = site_instance->GetProcess();
191 199
192 // This Init() call posts a task to the IO thread that adds the RPH's 200 // This Init() call posts a task to the IO thread that adds the RPH's
193 // ServiceWorkerDispatcherHost to the 201 // ServiceWorkerDispatcherHost to the
194 // EmbeddedWorkerRegistry::process_sender_map_. 202 // EmbeddedWorkerRegistry::process_sender_map_.
195 if (!rph->Init()) { 203 if (!rph->Init()) {
196 LOG(ERROR) << "Couldn't start a new process!"; 204 LOG(ERROR) << "Couldn't start a new process!";
197 BrowserThread::PostTask( 205 BrowserThread::PostTask(
198 BrowserThread::IO, FROM_HERE, 206 BrowserThread::IO, FROM_HERE,
199 base::Bind(callback, SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND, 207 base::Bind(callback, SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND,
200 ChildProcessHost::kInvalidUniqueID, 208 ChildProcessHost::kInvalidUniqueID,
201 false /* is_new_process */)); 209 false /* is_new_process */, settings));
202 return; 210 return;
203 } 211 }
204 212
205 instance_info_.insert( 213 instance_info_.insert(
206 std::make_pair(embedded_worker_id, ProcessInfo(site_instance))); 214 std::make_pair(embedded_worker_id, ProcessInfo(site_instance)));
207 215
208 rph->IncrementWorkerRefCount(); 216 rph->IncrementWorkerRefCount();
209 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 217 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
210 base::Bind(callback, SERVICE_WORKER_OK, rph->GetID(), 218 base::Bind(callback, SERVICE_WORKER_OK, rph->GetID(),
211 true /* is_new_process */)); 219 true /* is_new_process */, settings));
212 } 220 }
213 221
214 void ServiceWorkerProcessManager::ReleaseWorkerProcess(int embedded_worker_id) { 222 void ServiceWorkerProcessManager::ReleaseWorkerProcess(int embedded_worker_id) {
215 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 223 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
216 BrowserThread::PostTask( 224 BrowserThread::PostTask(
217 BrowserThread::UI, 225 BrowserThread::UI,
218 FROM_HERE, 226 FROM_HERE,
219 base::Bind(&ServiceWorkerProcessManager::ReleaseWorkerProcess, 227 base::Bind(&ServiceWorkerProcessManager::ReleaseWorkerProcess,
220 weak_this_, 228 weak_this_,
221 embedded_worker_id)); 229 embedded_worker_id));
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 namespace std { 316 namespace std {
309 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the 317 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the
310 // member WeakPtr to safely guard the object's lifetime when used on that 318 // member WeakPtr to safely guard the object's lifetime when used on that
311 // thread. 319 // thread.
312 void default_delete<content::ServiceWorkerProcessManager>::operator()( 320 void default_delete<content::ServiceWorkerProcessManager>::operator()(
313 content::ServiceWorkerProcessManager* ptr) const { 321 content::ServiceWorkerProcessManager* ptr) const {
314 content::BrowserThread::DeleteSoon( 322 content::BrowserThread::DeleteSoon(
315 content::BrowserThread::UI, FROM_HERE, ptr); 323 content::BrowserThread::UI, FROM_HERE, ptr);
316 } 324 }
317 } // namespace std 325 } // namespace std
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698