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

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

Issue 1439333002: Service Worker: Add Clients.get(id) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move the origin check lines and update layout tests 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_client_utils.h" 5 #include "content/browser/service_worker/service_worker_client_utils.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "content/browser/frame_host/frame_tree_node.h" 10 #include "content/browser/frame_host/frame_tree_node.h"
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 void AddNonWindowClient(ServiceWorkerProviderHost* host, 218 void AddNonWindowClient(ServiceWorkerProviderHost* host,
219 const ServiceWorkerClientQueryOptions& options, 219 const ServiceWorkerClientQueryOptions& options,
220 ServiceWorkerClients* clients) { 220 ServiceWorkerClients* clients) {
221 blink::WebServiceWorkerClientType host_client_type = host->client_type(); 221 blink::WebServiceWorkerClientType host_client_type = host->client_type();
222 if (host_client_type == blink::WebServiceWorkerClientTypeWindow) 222 if (host_client_type == blink::WebServiceWorkerClientTypeWindow)
223 return; 223 return;
224 if (options.client_type != blink::WebServiceWorkerClientTypeAll && 224 if (options.client_type != blink::WebServiceWorkerClientTypeAll &&
225 options.client_type != host_client_type) 225 options.client_type != host_client_type)
226 return; 226 return;
227 227
228 ServiceWorkerClientInfo client_info(blink::WebPageVisibilityStateHidden, 228 ServiceWorkerClientInfo client_info(
229 false, // is_focused 229 host->client_uuid(), blink::WebPageVisibilityStateHidden,
230 host->document_url(), 230 false, // is_focused
231 REQUEST_CONTEXT_FRAME_TYPE_NONE, 231 host->document_url(), REQUEST_CONTEXT_FRAME_TYPE_NONE, base::TimeTicks(),
232 base::TimeTicks(), host_client_type); 232 host_client_type);
233 client_info.client_uuid = host->client_uuid();
234 clients->push_back(client_info); 233 clients->push_back(client_info);
235 } 234 }
236 235
236 void OnGetWindowClientOnUI(
237 int process_id,
238 int frame_id,
239 const std::string& client_uuid,
240 const ServiceWorkerProviderHost::GetClientInfoCallback& callback) {
241 DCHECK_CURRENTLY_ON(BrowserThread::UI);
242
243 ServiceWorkerClientInfo info =
244 ServiceWorkerProviderHost::GetWindowClientInfoOnUI(process_id, frame_id,
245 client_uuid);
246
247 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
248 base::Bind(callback, info));
249 }
250
237 void OnGetWindowClientsOnUI( 251 void OnGetWindowClientsOnUI(
238 // The tuple contains process_id, frame_id, client_uuid. 252 // The tuple contains process_id, frame_id, client_uuid.
239 const std::vector<base::Tuple<int, int, std::string>>& clients_info, 253 const std::vector<base::Tuple<int, int, std::string>>& clients_info,
240 const GURL& script_url, 254 const GURL& script_url,
241 const GetWindowClientsCallback& callback) { 255 const GetWindowClientsCallback& callback) {
242 scoped_ptr<ServiceWorkerClients> clients(new ServiceWorkerClients); 256 scoped_ptr<ServiceWorkerClients> clients(new ServiceWorkerClients);
243 257
244 for (const auto& it : clients_info) { 258 for (const auto& it : clients_info) {
245 ServiceWorkerClientInfo info = 259 ServiceWorkerClientInfo info =
246 ServiceWorkerProviderHost::GetWindowClientInfoOnUI(base::get<0>(it), 260 ServiceWorkerProviderHost::GetWindowClientInfoOnUI(
247 base::get<1>(it)); 261 base::get<0>(it), base::get<1>(it), base::get<2>(it));
248 262
249 // If the request to the provider_host returned an empty 263 // If the request to the provider_host returned an empty
250 // ServiceWorkerClientInfo, that means that it wasn't possible to associate 264 // ServiceWorkerClientInfo, that means that it wasn't possible to associate
251 // it with a valid RenderFrameHost. It might be because the frame was killed 265 // it with a valid RenderFrameHost. It might be because the frame was killed
252 // or navigated in between. 266 // or navigated in between.
253 if (info.IsEmpty()) 267 if (info.IsEmpty())
254 continue; 268 continue;
255 269
256 // We can get info for a frame that was navigating end ended up with a 270 // We can get info for a frame that was navigating end ended up with a
257 // different URL than expected. In such case, we should make sure to not 271 // different URL than expected. In such case, we should make sure to not
258 // expose cross-origin WindowClient. 272 // expose cross-origin WindowClient.
259 if (info.url.GetOrigin() != script_url.GetOrigin()) 273 if (info.url.GetOrigin() != script_url.GetOrigin())
260 continue; 274 continue;
261 275
262 info.client_uuid = base::get<2>(it);
263 clients->push_back(info); 276 clients->push_back(info);
264 } 277 }
265 278
266 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 279 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
267 base::Bind(callback, base::Passed(&clients))); 280 base::Bind(callback, base::Passed(&clients)));
268 } 281 }
269 282
270 struct ServiceWorkerClientInfoSortMRU { 283 struct ServiceWorkerClientInfoSortMRU {
271 bool operator()(const ServiceWorkerClientInfo& a, 284 bool operator()(const ServiceWorkerClientInfo& a,
272 const ServiceWorkerClientInfo& b) const { 285 const ServiceWorkerClientInfo& b) const {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 const base::WeakPtr<ServiceWorkerContextCore>& context, 377 const base::WeakPtr<ServiceWorkerContextCore>& context,
365 const NavigationCallback& callback) { 378 const NavigationCallback& callback) {
366 DCHECK_CURRENTLY_ON(BrowserThread::IO); 379 DCHECK_CURRENTLY_ON(BrowserThread::IO);
367 BrowserThread::PostTask( 380 BrowserThread::PostTask(
368 BrowserThread::UI, FROM_HERE, 381 BrowserThread::UI, FROM_HERE,
369 base::Bind( 382 base::Bind(
370 &NavigateClientOnUI, url, script_url, process_id, frame_id, 383 &NavigateClientOnUI, url, script_url, process_id, frame_id,
371 base::Bind(&DidNavigate, context, script_url.GetOrigin(), callback))); 384 base::Bind(&DidNavigate, context, script_url.GetOrigin(), callback)));
372 } 385 }
373 386
387 void GetClient(
388 const base::WeakPtr<ServiceWorkerVersion>& controller,
389 const std::string& client_uuid,
390 const base::WeakPtr<ServiceWorkerContextCore>& context,
391 const ServiceWorkerProviderHost::GetClientInfoCallback& callback) {
392 DCHECK_CURRENTLY_ON(BrowserThread::IO);
393
394 ServiceWorkerProviderHost* provider_host =
395 context->GetProviderHostByClientID(client_uuid);
396
397 if (!provider_host) {
398 // The client may already have been closed, just ignore.
399 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
400 base::Bind(callback, ServiceWorkerClientInfo()));
401 return;
402 }
403
404 if (provider_host->document_url().GetOrigin() !=
405 controller->script_url().GetOrigin()) {
406 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
407 base::Bind(callback, ServiceWorkerClientInfo()));
408 return;
409 }
410
411 if (provider_host->client_type() == blink::WebServiceWorkerClientTypeWindow) {
412 BrowserThread::PostTask(
zino 2016/02/17 18:38:13 Couldn't we use "provider_host->GetWindowClientInf
jungkees 2016/02/18 15:02:20 Yes, that's effectively the same call flow between
413 BrowserThread::UI, FROM_HERE,
414 base::Bind(&OnGetWindowClientOnUI, provider_host->process_id(),
415 provider_host->frame_id(), provider_host->client_uuid(),
416 callback));
417 return;
418 }
419
420 DCHECK(provider_host->client_type() ==
421 blink::WebServiceWorkerClientTypeWorker ||
422 provider_host->client_type() ==
423 blink::WebServiceWorkerClientTypeSharedWorker);
424
425 ServiceWorkerClientInfo client_info(
426 provider_host->client_uuid(), blink::WebPageVisibilityStateHidden,
427 false, // is_focused
428 provider_host->document_url(), REQUEST_CONTEXT_FRAME_TYPE_NONE,
429 base::TimeTicks(), provider_host->client_type());
430 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
431 base::Bind(callback, client_info));
432 }
433
374 void GetClients(const base::WeakPtr<ServiceWorkerVersion>& controller, 434 void GetClients(const base::WeakPtr<ServiceWorkerVersion>& controller,
375 const ServiceWorkerClientQueryOptions& options, 435 const ServiceWorkerClientQueryOptions& options,
376 const ClientsCallback& callback) { 436 const ClientsCallback& callback) {
377 DCHECK_CURRENTLY_ON(BrowserThread::IO); 437 DCHECK_CURRENTLY_ON(BrowserThread::IO);
378 438
379 ServiceWorkerClients clients; 439 ServiceWorkerClients clients;
380 if (!controller->HasControllee() && !options.include_uncontrolled) { 440 if (!controller->HasControllee() && !options.include_uncontrolled) {
381 DidGetClients(callback, &clients); 441 DidGetClients(callback, &clients);
382 return; 442 return;
383 } 443 }
384 444
385 // For Window clients we want to query the info on the UI thread first. 445 // For Window clients we want to query the info on the UI thread first.
386 if (options.client_type == blink::WebServiceWorkerClientTypeWindow || 446 if (options.client_type == blink::WebServiceWorkerClientTypeWindow ||
387 options.client_type == blink::WebServiceWorkerClientTypeAll) { 447 options.client_type == blink::WebServiceWorkerClientTypeAll) {
388 GetWindowClients(controller, options, callback); 448 GetWindowClients(controller, options, callback);
389 return; 449 return;
390 } 450 }
391 451
392 GetNonWindowClients(controller, options, &clients); 452 GetNonWindowClients(controller, options, &clients);
393 DidGetClients(callback, &clients); 453 DidGetClients(callback, &clients);
394 } 454 }
395 455
396 } // namespace service_worker_client_utils 456 } // namespace service_worker_client_utils
397 } // namespace content 457 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698