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

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

Issue 2778173002: Revert of "Speculatively launch Service Workers on mouse/touch events." (Closed)
Patch Set: rebase Created 3 years, 8 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"
24 #include "content/browser/service_worker/embedded_worker_status.h" 23 #include "content/browser/service_worker/embedded_worker_status.h"
25 #include "content/browser/service_worker/service_worker_context_core.h" 24 #include "content/browser/service_worker/service_worker_context_core.h"
26 #include "content/browser/service_worker/service_worker_context_observer.h" 25 #include "content/browser/service_worker/service_worker_context_observer.h"
27 #include "content/browser/service_worker/service_worker_process_manager.h" 26 #include "content/browser/service_worker/service_worker_process_manager.h"
28 #include "content/browser/service_worker/service_worker_quota_client.h" 27 #include "content/browser/service_worker/service_worker_quota_client.h"
29 #include "content/browser/service_worker/service_worker_version.h" 28 #include "content/browser/service_worker/service_worker_version.h"
30 #include "content/browser/storage_partition_impl.h" 29 #include "content/browser/storage_partition_impl.h"
31 #include "content/common/service_worker/service_worker_utils.h" 30 #include "content/common/service_worker/service_worker_utils.h"
32 #include "content/public/browser/browser_context.h" 31 #include "content/public/browser/browser_context.h"
33 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/render_process_host.h"
35 #include "net/base/url_util.h" 33 #include "net/base/url_util.h"
36 #include "storage/browser/quota/quota_manager_proxy.h" 34 #include "storage/browser/quota/quota_manager_proxy.h"
37 #include "storage/browser/quota/special_storage_policy.h" 35 #include "storage/browser/quota/special_storage_policy.h"
38 #include "third_party/WebKit/public/platform/WebNavigationHintType.h"
39 36
40 namespace content { 37 namespace content {
41 38
42 namespace { 39 namespace {
43 40
44 typedef std::set<std::string> HeaderNameSet; 41 typedef std::set<std::string> HeaderNameSet;
45 base::LazyInstance<HeaderNameSet>::DestructorAtExit g_excluded_header_name_set = 42 base::LazyInstance<HeaderNameSet>::DestructorAtExit g_excluded_header_name_set =
46 LAZY_INSTANCE_INITIALIZER; 43 LAZY_INSTANCE_INITIALIZER;
47 44
48 void RunSoon(const base::Closure& closure) { 45 void RunSoon(const base::Closure& closure) {
(...skipping 29 matching lines...) Expand all
78 ServiceWorkerStatusCode status, 75 ServiceWorkerStatusCode status,
79 scoped_refptr<ServiceWorkerRegistration> registration) { 76 scoped_refptr<ServiceWorkerRegistration> registration) {
80 DCHECK_CURRENTLY_ON(BrowserThread::IO); 77 DCHECK_CURRENTLY_ON(BrowserThread::IO);
81 if (status != SERVICE_WORKER_OK || !registration->waiting_version()) 78 if (status != SERVICE_WORKER_OK || !registration->waiting_version())
82 return; 79 return;
83 80
84 registration->waiting_version()->set_skip_waiting(true); 81 registration->waiting_version()->set_skip_waiting(true);
85 registration->ActivateWaitingVersionWhenReady(); 82 registration->ActivateWaitingVersionWhenReady();
86 } 83 }
87 84
88 ServiceWorkerMetrics::EventType GetNavigationHintEventType(
89 blink::WebNavigationHintType type) {
90 switch (type) {
91 case blink::WebNavigationHintType::LinkMouseDown:
92 return ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN;
93 case blink::WebNavigationHintType::LinkTapUnconfirmed:
94 return ServiceWorkerMetrics::EventType::
95 NAVIGATION_HINT_LINK_TAP_UNCONFIRMED;
96 case blink::WebNavigationHintType::LinkTapDown:
97 return ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_DOWN;
98 }
99 NOTREACHED() << "Unexpected navigation hint" << static_cast<int>(type);
100 return ServiceWorkerMetrics::EventType::UNKNOWN;
101 }
102
103 } // namespace 85 } // namespace
104 86
105 void ServiceWorkerContext::AddExcludedHeadersForFetchEvent( 87 void ServiceWorkerContext::AddExcludedHeadersForFetchEvent(
106 const std::set<std::string>& header_names) { 88 const std::set<std::string>& header_names) {
107 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. 89 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed.
108 tracked_objects::ScopedTracker tracking_profile( 90 tracked_objects::ScopedTracker tracking_profile(
109 FROM_HERE_WITH_EXPLICIT_FUNCTION( 91 FROM_HERE_WITH_EXPLICIT_FUNCTION(
110 "477117 ServiceWorkerContext::AddExcludedHeadersForFetchEvent")); 92 "477117 ServiceWorkerContext::AddExcludedHeadersForFetchEvent"));
111 DCHECK_CURRENTLY_ON(BrowserThread::IO); 93 DCHECK_CURRENTLY_ON(BrowserThread::IO);
112 g_excluded_header_name_set.Get().insert(header_names.begin(), 94 g_excluded_header_name_set.Get().insert(header_names.begin(),
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 return; 259 return;
278 } 260 }
279 if (!context_core_) 261 if (!context_core_)
280 return; 262 return;
281 context_core_->storage()->FindRegistrationForPattern( 263 context_core_->storage()->FindRegistrationForPattern(
282 net::SimplifyUrlForRequest(pattern), 264 net::SimplifyUrlForRequest(pattern),
283 base::Bind(&ServiceWorkerContextWrapper::DidFindRegistrationForUpdate, 265 base::Bind(&ServiceWorkerContextWrapper::DidFindRegistrationForUpdate,
284 this)); 266 this));
285 } 267 }
286 268
287 void ServiceWorkerContextWrapper::StartServiceWorkerForNavigationHint(
288 const GURL& document_url,
289 blink::WebNavigationHintType type,
290 int render_process_id,
291 const ResultCallback& callback) {
292 DCHECK_CURRENTLY_ON(BrowserThread::UI);
293 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
294 if (!host ||
295 !RenderProcessHostImpl::IsSuitableHost(host, host->GetBrowserContext(),
296 document_url)) {
297 callback.Run(false);
298 return;
299 }
300
301 BrowserThread::PostTask(
302 BrowserThread::IO, FROM_HERE,
303 base::Bind(
304 &ServiceWorkerContextWrapper::DidCheckRenderProcessForNavigationHint,
305 this, document_url, type, render_process_id, callback));
306 }
307
308 void ServiceWorkerContextWrapper::DidCheckRenderProcessForNavigationHint(
309 const GURL& document_url,
310 blink::WebNavigationHintType type,
311 int render_process_id,
312 const ResultCallback& callback) {
313 DCHECK_CURRENTLY_ON(BrowserThread::IO);
314 if (!context_core_) {
315 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
316 base::Bind(callback, false));
317 return;
318 }
319 FindReadyRegistrationForDocument(
320 document_url,
321 base::Bind(
322 &ServiceWorkerContextWrapper::DidFindRegistrationForNavigationHint,
323 this, type, render_process_id, callback));
324 }
325
326 void ServiceWorkerContextWrapper::DidFindRegistrationForNavigationHint(
327 blink::WebNavigationHintType type,
328 int render_process_id,
329 const ResultCallback& callback,
330 ServiceWorkerStatusCode status,
331 scoped_refptr<ServiceWorkerRegistration> registration) {
332 DCHECK_CURRENTLY_ON(BrowserThread::IO);
333 if (status != SERVICE_WORKER_OK || !registration->active_version()) {
334 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
335 base::Bind(callback, false));
336 return;
337 }
338
339 if (registration->active_version()->fetch_handler_existence() ==
340 ServiceWorkerVersion::FetchHandlerExistence::DOES_NOT_EXIST) {
341 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
342 base::Bind(callback, false));
343 return;
344 }
345
346 // Add the process reference of |render_process_id| not to launch a new
347 // renderer process for the service worker.
348 context_core_->process_manager()->AddProcessReferenceToPattern(
349 registration->pattern(), render_process_id);
350
351 registration->active_version()->StartWorker(
352 GetNavigationHintEventType(type),
353 base::Bind(
354 &ServiceWorkerContextWrapper::DidStartServiceWorkerForNavigationHint,
355 this, registration->pattern(), render_process_id, callback));
356 }
357
358 void ServiceWorkerContextWrapper::DidStartServiceWorkerForNavigationHint(
359 const GURL& pattern,
360 int render_process_id,
361 const ResultCallback& callback,
362 ServiceWorkerStatusCode code) {
363 DCHECK_CURRENTLY_ON(BrowserThread::IO);
364
365 // Remove the process reference added in DidFindRegistrationForNavigationHint.
366 context_core_->process_manager()->RemoveProcessReferenceFromPattern(
367 pattern, render_process_id);
368 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
369 base::Bind(callback, code == SERVICE_WORKER_OK));
370 }
371
372 void ServiceWorkerContextWrapper::StartServiceWorker( 269 void ServiceWorkerContextWrapper::StartServiceWorker(
373 const GURL& pattern, 270 const GURL& pattern,
374 const StatusCallback& callback) { 271 const StatusCallback& callback) {
375 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { 272 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
376 BrowserThread::PostTask( 273 BrowserThread::PostTask(
377 BrowserThread::IO, FROM_HERE, 274 BrowserThread::IO, FROM_HERE,
378 base::Bind(&ServiceWorkerContextWrapper::StartServiceWorker, this, 275 base::Bind(&ServiceWorkerContextWrapper::StartServiceWorker, this,
379 pattern, callback)); 276 pattern, callback));
380 return; 277 return;
381 } 278 }
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
901 observer_list_->Notify(FROM_HERE, 798 observer_list_->Notify(FROM_HERE,
902 &ServiceWorkerContextObserver::OnStorageWiped); 799 &ServiceWorkerContextObserver::OnStorageWiped);
903 } 800 }
904 801
905 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() { 802 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() {
906 DCHECK_CURRENTLY_ON(BrowserThread::IO); 803 DCHECK_CURRENTLY_ON(BrowserThread::IO);
907 return context_core_.get(); 804 return context_core_.get();
908 } 805 }
909 806
910 } // namespace content 807 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698