| 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" | |
| 24 #include "content/browser/service_worker/service_worker_context_core.h" | 23 #include "content/browser/service_worker/service_worker_context_core.h" |
| 25 #include "content/browser/service_worker/service_worker_context_observer.h" | 24 #include "content/browser/service_worker/service_worker_context_observer.h" |
| 26 #include "content/browser/service_worker/service_worker_process_manager.h" | 25 #include "content/browser/service_worker/service_worker_process_manager.h" |
| 27 #include "content/browser/service_worker/service_worker_quota_client.h" | 26 #include "content/browser/service_worker/service_worker_quota_client.h" |
| 28 #include "content/browser/service_worker/service_worker_version.h" | 27 #include "content/browser/service_worker/service_worker_version.h" |
| 29 #include "content/browser/storage_partition_impl.h" | 28 #include "content/browser/storage_partition_impl.h" |
| 30 #include "content/common/service_worker/service_worker_utils.h" | 29 #include "content/common/service_worker/service_worker_utils.h" |
| 31 #include "content/public/browser/browser_context.h" | 30 #include "content/public/browser/browser_context.h" |
| 32 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/render_process_host.h" | |
| 34 #include "net/base/url_util.h" | 32 #include "net/base/url_util.h" |
| 35 #include "storage/browser/quota/quota_manager_proxy.h" | 33 #include "storage/browser/quota/quota_manager_proxy.h" |
| 36 #include "storage/browser/quota/special_storage_policy.h" | 34 #include "storage/browser/quota/special_storage_policy.h" |
| 37 #include "third_party/WebKit/public/platform/WebNavigationHintType.h" | |
| 38 | 35 |
| 39 namespace content { | 36 namespace content { |
| 40 | 37 |
| 41 namespace { | 38 namespace { |
| 42 | 39 |
| 43 typedef std::set<std::string> HeaderNameSet; | 40 typedef std::set<std::string> HeaderNameSet; |
| 44 base::LazyInstance<HeaderNameSet> g_excluded_header_name_set = | 41 base::LazyInstance<HeaderNameSet> g_excluded_header_name_set = |
| 45 LAZY_INSTANCE_INITIALIZER; | 42 LAZY_INSTANCE_INITIALIZER; |
| 46 | 43 |
| 47 void RunSoon(const base::Closure& closure) { | 44 void RunSoon(const base::Closure& closure) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 77 ServiceWorkerStatusCode status, | 74 ServiceWorkerStatusCode status, |
| 78 scoped_refptr<ServiceWorkerRegistration> registration) { | 75 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 76 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 80 if (status != SERVICE_WORKER_OK || !registration->waiting_version()) | 77 if (status != SERVICE_WORKER_OK || !registration->waiting_version()) |
| 81 return; | 78 return; |
| 82 | 79 |
| 83 registration->waiting_version()->set_skip_waiting(true); | 80 registration->waiting_version()->set_skip_waiting(true); |
| 84 registration->ActivateWaitingVersionWhenReady(); | 81 registration->ActivateWaitingVersionWhenReady(); |
| 85 } | 82 } |
| 86 | 83 |
| 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 | |
| 102 } // namespace | 84 } // namespace |
| 103 | 85 |
| 104 void ServiceWorkerContext::AddExcludedHeadersForFetchEvent( | 86 void ServiceWorkerContext::AddExcludedHeadersForFetchEvent( |
| 105 const std::set<std::string>& header_names) { | 87 const std::set<std::string>& header_names) { |
| 106 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. | 88 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. |
| 107 tracked_objects::ScopedTracker tracking_profile( | 89 tracked_objects::ScopedTracker tracking_profile( |
| 108 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 90 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 109 "477117 ServiceWorkerContext::AddExcludedHeadersForFetchEvent")); | 91 "477117 ServiceWorkerContext::AddExcludedHeadersForFetchEvent")); |
| 110 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 111 g_excluded_header_name_set.Get().insert(header_names.begin(), | 93 g_excluded_header_name_set.Get().insert(header_names.begin(), |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 return; | 275 return; |
| 294 } | 276 } |
| 295 if (!context_core_) | 277 if (!context_core_) |
| 296 return; | 278 return; |
| 297 context_core_->storage()->FindRegistrationForPattern( | 279 context_core_->storage()->FindRegistrationForPattern( |
| 298 net::SimplifyUrlForRequest(pattern), | 280 net::SimplifyUrlForRequest(pattern), |
| 299 base::Bind(&ServiceWorkerContextWrapper::DidFindRegistrationForUpdate, | 281 base::Bind(&ServiceWorkerContextWrapper::DidFindRegistrationForUpdate, |
| 300 this)); | 282 this)); |
| 301 } | 283 } |
| 302 | 284 |
| 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 | |
| 381 void ServiceWorkerContextWrapper::StartServiceWorker( | 285 void ServiceWorkerContextWrapper::StartServiceWorker( |
| 382 const GURL& pattern, | 286 const GURL& pattern, |
| 383 const StatusCallback& callback) { | 287 const StatusCallback& callback) { |
| 384 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 288 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 385 BrowserThread::PostTask( | 289 BrowserThread::PostTask( |
| 386 BrowserThread::IO, FROM_HERE, | 290 BrowserThread::IO, FROM_HERE, |
| 387 base::Bind(&ServiceWorkerContextWrapper::StartServiceWorker, this, | 291 base::Bind(&ServiceWorkerContextWrapper::StartServiceWorker, this, |
| 388 pattern, callback)); | 292 pattern, callback)); |
| 389 return; | 293 return; |
| 390 } | 294 } |
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 observer_list_->Notify(FROM_HERE, | 740 observer_list_->Notify(FROM_HERE, |
| 837 &ServiceWorkerContextObserver::OnStorageWiped); | 741 &ServiceWorkerContextObserver::OnStorageWiped); |
| 838 } | 742 } |
| 839 | 743 |
| 840 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() { | 744 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() { |
| 841 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 745 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 842 return context_core_.get(); | 746 return context_core_.get(); |
| 843 } | 747 } |
| 844 | 748 |
| 845 } // namespace content | 749 } // namespace content |
| OLD | NEW |