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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/service_worker_context_wrapper.cc
diff --git a/content/browser/service_worker/service_worker_context_wrapper.cc b/content/browser/service_worker/service_worker_context_wrapper.cc
index bb5d2dedf23143e92054e6d3e1dc720bac54169c..230f14398f2283917786be4616f7b05f9e014dcc 100644
--- a/content/browser/service_worker/service_worker_context_wrapper.cc
+++ b/content/browser/service_worker/service_worker_context_wrapper.cc
@@ -20,6 +20,7 @@
#include "base/single_thread_task_runner.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread_task_runner_handle.h"
+#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_context_observer.h"
#include "content/browser/service_worker/service_worker_process_manager.h"
@@ -29,9 +30,11 @@
#include "content/common/service_worker/service_worker_utils.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/render_process_host.h"
#include "net/base/url_util.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "storage/browser/quota/special_storage_policy.h"
+#include "third_party/WebKit/public/platform/WebNavigationHintType.h"
namespace content {
@@ -81,6 +84,21 @@ void SkipWaitingWorkerOnIO(
registration->ActivateWaitingVersionWhenReady();
}
+ServiceWorkerMetrics::EventType GetNavigationHintEventType(
+ blink::WebNavigationHintType type) {
+ switch (type) {
+ case blink::WebNavigationHintType::LinkMouseDown:
+ return ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN;
+ case blink::WebNavigationHintType::LinkTapUnconfirmed:
+ return ServiceWorkerMetrics::EventType::
+ NAVIGATION_HINT_LINK_TAP_UNCONFIRMED;
+ case blink::WebNavigationHintType::LinkTapDown:
+ return ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_DOWN;
+ }
+ NOTREACHED() << "Unexpected navigation hint" << static_cast<int>(type);
+ return ServiceWorkerMetrics::EventType::UNKNOWN;
+}
+
} // namespace
void ServiceWorkerContext::AddExcludedHeadersForFetchEvent(
@@ -282,6 +300,84 @@ void ServiceWorkerContextWrapper::UpdateRegistration(const GURL& pattern) {
this));
}
+void ServiceWorkerContextWrapper::StartServiceWorkerForNavigationHint(
+ const GURL& document_url,
+ blink::WebNavigationHintType type,
+ int render_process_id,
+ const ResultCallback& callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
+ if (!host ||
+ !RenderProcessHostImpl::IsSuitableHost(host, host->GetBrowserContext(),
+ document_url)) {
+ callback.Run(false);
+ return;
+ }
+
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(
+ &ServiceWorkerContextWrapper::DidCheckRenderProcessForNavigationHint,
+ this, document_url, type, render_process_id, callback));
+}
+
+void ServiceWorkerContextWrapper::DidCheckRenderProcessForNavigationHint(
+ const GURL& document_url,
+ blink::WebNavigationHintType type,
+ int render_process_id,
+ const ResultCallback& callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (!context_core_) {
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(callback, false));
+ return;
+ }
+ FindReadyRegistrationForDocument(
+ document_url,
+ base::Bind(
+ &ServiceWorkerContextWrapper::DidFindRegistrationForNavigationHint,
+ this, type, render_process_id, callback));
+}
+
+void ServiceWorkerContextWrapper::DidFindRegistrationForNavigationHint(
+ blink::WebNavigationHintType type,
+ int render_process_id,
+ const ResultCallback& callback,
+ ServiceWorkerStatusCode status,
+ scoped_refptr<ServiceWorkerRegistration> registration) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (status != SERVICE_WORKER_OK || !registration->active_version()) {
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(callback, false));
+ return;
+ }
+
+ // Add the process reference of |render_process_id| not to launch a new
+ // renderer process for the service worker.
+ context_core_->process_manager()->AddProcessReferenceToPattern(
+ registration->pattern(), render_process_id);
+
+ registration->active_version()->StartWorker(
+ GetNavigationHintEventType(type),
+ base::Bind(
+ &ServiceWorkerContextWrapper::DidStartServiceWorkerForNavigationHint,
+ this, registration->pattern(), render_process_id, callback));
+}
+
+void ServiceWorkerContextWrapper::DidStartServiceWorkerForNavigationHint(
+ const GURL& pattern,
+ int render_process_id,
+ const ResultCallback& callback,
+ ServiceWorkerStatusCode code) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ // Remove the process reference added in DidFindRegistrationForNavigationHint.
+ context_core_->process_manager()->RemoveProcessReferenceFromPattern(
+ pattern, render_process_id);
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(callback, code == SERVICE_WORKER_OK));
+}
+
void ServiceWorkerContextWrapper::StartServiceWorker(
const GURL& pattern,
const StatusCallback& callback) {

Powered by Google App Engine
This is Rietveld 408576698