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

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

Issue 2052613003: Speculatively launch Service Workers on mouse/touch events. [3/5] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove unnecessary includes Created 4 years, 5 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_context_wrapper.h" 5 #include "content/browser/service_worker/service_worker_context_wrapper.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/barrier_closure.h" 13 #include "base/barrier_closure.h"
14 #include "base/bind.h" 14 #include "base/bind.h"
15 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
16 #include "base/lazy_instance.h" 16 #include "base/lazy_instance.h"
17 #include "base/location.h" 17 #include "base/location.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/profiler/scoped_tracker.h" 19 #include "base/profiler/scoped_tracker.h"
20 #include "base/single_thread_task_runner.h" 20 #include "base/single_thread_task_runner.h"
21 #include "base/threading/sequenced_worker_pool.h" 21 #include "base/threading/sequenced_worker_pool.h"
22 #include "base/threading/thread_task_runner_handle.h" 22 #include "base/threading/thread_task_runner_handle.h"
23 #include "content/browser/renderer_host/render_process_host_impl.h"
23 #include "content/browser/service_worker/service_worker_context_core.h" 24 #include "content/browser/service_worker/service_worker_context_core.h"
24 #include "content/browser/service_worker/service_worker_context_observer.h" 25 #include "content/browser/service_worker/service_worker_context_observer.h"
25 #include "content/browser/service_worker/service_worker_process_manager.h" 26 #include "content/browser/service_worker/service_worker_process_manager.h"
26 #include "content/browser/service_worker/service_worker_quota_client.h" 27 #include "content/browser/service_worker/service_worker_quota_client.h"
27 #include "content/browser/service_worker/service_worker_version.h" 28 #include "content/browser/service_worker/service_worker_version.h"
28 #include "content/browser/storage_partition_impl.h" 29 #include "content/browser/storage_partition_impl.h"
29 #include "content/common/service_worker/service_worker_utils.h" 30 #include "content/common/service_worker/service_worker_utils.h"
30 #include "content/public/browser/browser_context.h" 31 #include "content/public/browser/browser_context.h"
31 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/render_process_host.h"
32 #include "net/base/url_util.h" 34 #include "net/base/url_util.h"
33 #include "storage/browser/quota/quota_manager_proxy.h" 35 #include "storage/browser/quota/quota_manager_proxy.h"
34 #include "storage/browser/quota/special_storage_policy.h" 36 #include "storage/browser/quota/special_storage_policy.h"
37 #include "third_party/WebKit/public/platform/WebNavigationHintType.h"
35 38
36 namespace content { 39 namespace content {
37 40
38 namespace { 41 namespace {
39 42
40 typedef std::set<std::string> HeaderNameSet; 43 typedef std::set<std::string> HeaderNameSet;
41 base::LazyInstance<HeaderNameSet> g_excluded_header_name_set = 44 base::LazyInstance<HeaderNameSet> g_excluded_header_name_set =
42 LAZY_INSTANCE_INITIALIZER; 45 LAZY_INSTANCE_INITIALIZER;
43 46
44 void RunSoon(const base::Closure& closure) { 47 void RunSoon(const base::Closure& closure) {
(...skipping 29 matching lines...) Expand all
74 ServiceWorkerStatusCode status, 77 ServiceWorkerStatusCode status,
75 scoped_refptr<ServiceWorkerRegistration> registration) { 78 scoped_refptr<ServiceWorkerRegistration> registration) {
76 DCHECK_CURRENTLY_ON(BrowserThread::IO); 79 DCHECK_CURRENTLY_ON(BrowserThread::IO);
77 if (status != SERVICE_WORKER_OK || !registration->waiting_version()) 80 if (status != SERVICE_WORKER_OK || !registration->waiting_version())
78 return; 81 return;
79 82
80 registration->waiting_version()->set_skip_waiting(true); 83 registration->waiting_version()->set_skip_waiting(true);
81 registration->ActivateWaitingVersionWhenReady(); 84 registration->ActivateWaitingVersionWhenReady();
82 } 85 }
83 86
87 ServiceWorkerMetrics::EventType GetNavigationHintEventType(
88 blink::WebNavigationHintType type) {
89 switch (type) {
90 case blink::WebNavigationHintType::LinkMouseDown:
91 return ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN;
92 case blink::WebNavigationHintType::LinkTapUnconfirmed:
93 return ServiceWorkerMetrics::EventType::
94 NAVIGATION_HINT_LINK_TAP_UNCONFIRMED;
95 case blink::WebNavigationHintType::LinkTapDown:
96 return ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_DOWN;
97 }
98 NOTREACHED() << "Unexpected navigation hint" << static_cast<int>(type);
99 return ServiceWorkerMetrics::EventType::UNKNOWN;
100 }
101
84 } // namespace 102 } // namespace
85 103
86 void ServiceWorkerContext::AddExcludedHeadersForFetchEvent( 104 void ServiceWorkerContext::AddExcludedHeadersForFetchEvent(
87 const std::set<std::string>& header_names) { 105 const std::set<std::string>& header_names) {
88 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. 106 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed.
89 tracked_objects::ScopedTracker tracking_profile( 107 tracked_objects::ScopedTracker tracking_profile(
90 FROM_HERE_WITH_EXPLICIT_FUNCTION( 108 FROM_HERE_WITH_EXPLICIT_FUNCTION(
91 "477117 ServiceWorkerContext::AddExcludedHeadersForFetchEvent")); 109 "477117 ServiceWorkerContext::AddExcludedHeadersForFetchEvent"));
92 DCHECK_CURRENTLY_ON(BrowserThread::IO); 110 DCHECK_CURRENTLY_ON(BrowserThread::IO);
93 g_excluded_header_name_set.Get().insert(header_names.begin(), 111 g_excluded_header_name_set.Get().insert(header_names.begin(),
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 return; 293 return;
276 } 294 }
277 if (!context_core_) 295 if (!context_core_)
278 return; 296 return;
279 context_core_->storage()->FindRegistrationForPattern( 297 context_core_->storage()->FindRegistrationForPattern(
280 net::SimplifyUrlForRequest(pattern), 298 net::SimplifyUrlForRequest(pattern),
281 base::Bind(&ServiceWorkerContextWrapper::DidFindRegistrationForUpdate, 299 base::Bind(&ServiceWorkerContextWrapper::DidFindRegistrationForUpdate,
282 this)); 300 this));
283 } 301 }
284 302
303 void ServiceWorkerContextWrapper::StartServiceWorkerForNavigationHint(
304 const GURL& document_url,
305 blink::WebNavigationHintType type,
306 int render_process_id,
307 const ResultCallback& callback) {
308 DCHECK_CURRENTLY_ON(BrowserThread::UI);
309 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
310 if (!host ||
311 !RenderProcessHostImpl::IsSuitableHost(host, host->GetBrowserContext(),
312 document_url)) {
313 callback.Run(false);
314 return;
315 }
316
317 BrowserThread::PostTask(
318 BrowserThread::IO, FROM_HERE,
319 base::Bind(
320 &ServiceWorkerContextWrapper::DidCheckRenderProcessForNavigationHint,
321 this, document_url, type, render_process_id, callback));
322 }
323
324 void ServiceWorkerContextWrapper::DidCheckRenderProcessForNavigationHint(
325 const GURL& document_url,
326 blink::WebNavigationHintType type,
327 int render_process_id,
328 const ResultCallback& callback) {
329 DCHECK_CURRENTLY_ON(BrowserThread::IO);
330 if (!context_core_) {
331 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
332 base::Bind(callback, false));
333 return;
334 }
335 FindReadyRegistrationForDocument(
336 document_url,
337 base::Bind(
338 &ServiceWorkerContextWrapper::DidFindRegistrationForNavigationHint,
339 this, type, render_process_id, callback));
340 }
341
342 void ServiceWorkerContextWrapper::DidFindRegistrationForNavigationHint(
343 blink::WebNavigationHintType type,
344 int render_process_id,
345 const ResultCallback& callback,
346 ServiceWorkerStatusCode status,
347 scoped_refptr<ServiceWorkerRegistration> registration) {
348 DCHECK_CURRENTLY_ON(BrowserThread::IO);
349 if (status != SERVICE_WORKER_OK || !registration->active_version()) {
350 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
351 base::Bind(callback, false));
352 return;
353 }
354
355 // Add the process reference of |render_process_id| not to launch a new
356 // renderer process for the service worker.
357 context_core_->process_manager()->AddProcessReferenceToPattern(
358 registration->pattern(), render_process_id);
359
360 registration->active_version()->StartWorker(
361 GetNavigationHintEventType(type),
362 base::Bind(
363 &ServiceWorkerContextWrapper::DidStartServiceWorkerForNavigationHint,
364 this, registration->pattern(), render_process_id, callback));
365 }
366
367 void ServiceWorkerContextWrapper::DidStartServiceWorkerForNavigationHint(
368 const GURL& pattern,
369 int render_process_id,
370 const ResultCallback& callback,
371 ServiceWorkerStatusCode code) {
372 DCHECK_CURRENTLY_ON(BrowserThread::IO);
373
374 // Remove the process reference added in DidFindRegistrationForNavigationHint.
375 context_core_->process_manager()->RemoveProcessReferenceFromPattern(
376 pattern, render_process_id);
377 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
378 base::Bind(callback, code == SERVICE_WORKER_OK));
379 }
380
285 void ServiceWorkerContextWrapper::StartServiceWorker( 381 void ServiceWorkerContextWrapper::StartServiceWorker(
286 const GURL& pattern, 382 const GURL& pattern,
287 const StatusCallback& callback) { 383 const StatusCallback& callback) {
288 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { 384 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
289 BrowserThread::PostTask( 385 BrowserThread::PostTask(
290 BrowserThread::IO, FROM_HERE, 386 BrowserThread::IO, FROM_HERE,
291 base::Bind(&ServiceWorkerContextWrapper::StartServiceWorker, this, 387 base::Bind(&ServiceWorkerContextWrapper::StartServiceWorker, this,
292 pattern, callback)); 388 pattern, callback));
293 return; 389 return;
294 } 390 }
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 observer_list_->Notify(FROM_HERE, 836 observer_list_->Notify(FROM_HERE,
741 &ServiceWorkerContextObserver::OnStorageWiped); 837 &ServiceWorkerContextObserver::OnStorageWiped);
742 } 838 }
743 839
744 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() { 840 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() {
745 DCHECK_CURRENTLY_ON(BrowserThread::IO); 841 DCHECK_CURRENTLY_ON(BrowserThread::IO);
746 return context_core_.get(); 842 return context_core_.get();
747 } 843 }
748 844
749 } // namespace content 845 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698