| OLD | NEW |
| 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 Loading... |
| 74 ServiceWorkerStatusCode status, | 77 ServiceWorkerStatusCode status, |
| 75 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 78 const 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 default: |
| 98 NOTREACHED() << "Unexpected navigation hint" << static_cast<int>(type); |
| 99 return ServiceWorkerMetrics::EventType::UNKNOWN; |
| 100 } |
| 101 } |
| 102 |
| 103 void CheckRenderProcessForNavigationHintOnUI( |
| 104 const GURL& document_url, |
| 105 int render_process_id, |
| 106 const ServiceWorkerContext::ResultCallback& callback) { |
| 107 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 108 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); |
| 109 if (!host) { |
| 110 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 111 base::Bind(callback, false)); |
| 112 return; |
| 113 } |
| 114 BrowserThread::PostTask( |
| 115 BrowserThread::IO, FROM_HERE, |
| 116 base::Bind(callback, RenderProcessHostImpl::IsSuitableHost( |
| 117 host, host->GetBrowserContext(), document_url))); |
| 118 } |
| 119 |
| 84 } // namespace | 120 } // namespace |
| 85 | 121 |
| 86 void ServiceWorkerContext::AddExcludedHeadersForFetchEvent( | 122 void ServiceWorkerContext::AddExcludedHeadersForFetchEvent( |
| 87 const std::set<std::string>& header_names) { | 123 const std::set<std::string>& header_names) { |
| 88 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. | 124 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. |
| 89 tracked_objects::ScopedTracker tracking_profile( | 125 tracked_objects::ScopedTracker tracking_profile( |
| 90 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 126 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 91 "477117 ServiceWorkerContext::AddExcludedHeadersForFetchEvent")); | 127 "477117 ServiceWorkerContext::AddExcludedHeadersForFetchEvent")); |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 128 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 93 g_excluded_header_name_set.Get().insert(header_names.begin(), | 129 g_excluded_header_name_set.Get().insert(header_names.begin(), |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 return; | 311 return; |
| 276 } | 312 } |
| 277 if (!context_core_) | 313 if (!context_core_) |
| 278 return; | 314 return; |
| 279 context_core_->storage()->FindRegistrationForPattern( | 315 context_core_->storage()->FindRegistrationForPattern( |
| 280 net::SimplifyUrlForRequest(pattern), | 316 net::SimplifyUrlForRequest(pattern), |
| 281 base::Bind(&ServiceWorkerContextWrapper::DidFindRegistrationForUpdate, | 317 base::Bind(&ServiceWorkerContextWrapper::DidFindRegistrationForUpdate, |
| 282 this)); | 318 this)); |
| 283 } | 319 } |
| 284 | 320 |
| 321 void ServiceWorkerContextWrapper::StartServiceWorkerForNavigationHint( |
| 322 const GURL& document_url, |
| 323 blink::WebNavigationHintType type, |
| 324 int render_process_id, |
| 325 const ResultCallback& callback) { |
| 326 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 327 BrowserThread::PostTask( |
| 328 BrowserThread::UI, FROM_HERE, |
| 329 base::Bind( |
| 330 &CheckRenderProcessForNavigationHintOnUI, document_url, |
| 331 render_process_id, |
| 332 base::Bind(&ServiceWorkerContextWrapper:: |
| 333 DidCheckRenderProcessForNavigationHint, |
| 334 this, document_url, type, render_process_id, callback))); |
| 335 } |
| 336 |
| 337 void ServiceWorkerContextWrapper::DidCheckRenderProcessForNavigationHint( |
| 338 const GURL& document_url, |
| 339 blink::WebNavigationHintType type, |
| 340 int render_process_id, |
| 341 const ResultCallback& callback, |
| 342 bool result) { |
| 343 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 344 if (!result || !context_core_) { |
| 345 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 346 base::Bind(callback, false)); |
| 347 return; |
| 348 } |
| 349 context_core_->storage()->FindRegistrationForDocument( |
| 350 document_url, |
| 351 base::Bind( |
| 352 &ServiceWorkerContextWrapper::DidFindRegistrationForNavigationHint, |
| 353 this, type, render_process_id, callback)); |
| 354 } |
| 355 |
| 356 void ServiceWorkerContextWrapper::DidFindRegistrationForNavigationHint( |
| 357 blink::WebNavigationHintType type, |
| 358 int render_process_id, |
| 359 const ResultCallback& callback, |
| 360 ServiceWorkerStatusCode status, |
| 361 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 362 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 363 if (status != SERVICE_WORKER_OK || !registration->active_version()) { |
| 364 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 365 base::Bind(callback, false)); |
| 366 return; |
| 367 } |
| 368 |
| 369 context_core_->process_manager()->AddProcessReferenceToPattern( |
| 370 registration->pattern(), render_process_id); |
| 371 |
| 372 registration->active_version()->StartWorker( |
| 373 GetNavigationHintEventType(type), |
| 374 base::Bind( |
| 375 &ServiceWorkerContextWrapper::DidStartServiceWorkerForNavigationHint, |
| 376 this, registration->pattern(), render_process_id, callback)); |
| 377 } |
| 378 |
| 379 void ServiceWorkerContextWrapper::DidStartServiceWorkerForNavigationHint( |
| 380 const GURL& pattern, |
| 381 int render_process_id, |
| 382 const ResultCallback& callback, |
| 383 ServiceWorkerStatusCode code) { |
| 384 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 385 |
| 386 context_core_->process_manager()->RemoveProcessReferenceFromPattern( |
| 387 pattern, render_process_id); |
| 388 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 389 base::Bind(callback, code == SERVICE_WORKER_OK)); |
| 390 } |
| 391 |
| 285 void ServiceWorkerContextWrapper::StartServiceWorker( | 392 void ServiceWorkerContextWrapper::StartServiceWorker( |
| 286 const GURL& pattern, | 393 const GURL& pattern, |
| 287 const StatusCallback& callback) { | 394 const StatusCallback& callback) { |
| 288 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 395 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 289 BrowserThread::PostTask( | 396 BrowserThread::PostTask( |
| 290 BrowserThread::IO, FROM_HERE, | 397 BrowserThread::IO, FROM_HERE, |
| 291 base::Bind(&ServiceWorkerContextWrapper::StartServiceWorker, this, | 398 base::Bind(&ServiceWorkerContextWrapper::StartServiceWorker, this, |
| 292 pattern, callback)); | 399 pattern, callback)); |
| 293 return; | 400 return; |
| 294 } | 401 } |
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 observer_list_->Notify(FROM_HERE, | 847 observer_list_->Notify(FROM_HERE, |
| 741 &ServiceWorkerContextObserver::OnStorageWiped); | 848 &ServiceWorkerContextObserver::OnStorageWiped); |
| 742 } | 849 } |
| 743 | 850 |
| 744 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() { | 851 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() { |
| 745 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 852 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 746 return context_core_.get(); | 853 return context_core_.get(); |
| 747 } | 854 } |
| 748 | 855 |
| 749 } // namespace content | 856 } // namespace content |
| OLD | NEW |