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

Unified Diff: content/browser/service_worker/service_worker_dispatcher_host.cc

Issue 2521793004: service worker: Persist NavigationPreloadState (Closed)
Patch Set: expect_ name Created 4 years, 1 month 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_dispatcher_host.cc
diff --git a/content/browser/service_worker/service_worker_dispatcher_host.cc b/content/browser/service_worker/service_worker_dispatcher_host.cc
index bd34fabf1b4cd971d3d2c844db7ea84a57845de2..5a5dc4c9f082867514628bdb954e39954c31ab5c 100644
--- a/content/browser/service_worker/service_worker_dispatcher_host.cc
+++ b/content/browser/service_worker/service_worker_dispatcher_host.cc
@@ -58,6 +58,9 @@ const char kGetNavigationPreloadStateErrorPrefix[] =
"Failed to get navigation preload state: ";
const char kSetNavigationPreloadHeaderErrorPrefix[] =
"Failed to set navigation preload header: ";
+const char kNoActiveWorkerErrorMessage[] =
+ "The registration does not have an active worker.";
+const char kDatabaseErrorMessage[] = "Failed to access storage.";
const uint32_t kFilteredMessageClasses[] = {
ServiceWorkerMsgStart, EmbeddedWorkerMsgStart,
@@ -707,6 +710,16 @@ void ServiceWorkerDispatcherHost::OnEnableNavigationPreload(
this, bad_message::SWDH_ENABLE_NAVIGATION_PRELOAD_BAD_REGISTRATION_ID);
return;
}
+ // The spec discussion consensus is to reject if there is no active worker:
+ // https://github.com/w3c/ServiceWorker/issues/920#issuecomment-262212670
+ // TODO(falken): Remove this comment when the spec is updated.
+ if (!registration->active_version()) {
+ Send(new ServiceWorkerMsg_EnableNavigationPreloadError(
+ thread_id, request_id, WebServiceWorkerError::ErrorTypeState,
+ std::string(kEnableNavigationPreloadErrorPrefix) +
+ std::string(kNoActiveWorkerErrorMessage)));
+ return;
+ }
std::vector<GURL> urls = {provider_host->document_url(),
registration->pattern()};
@@ -726,9 +739,11 @@ void ServiceWorkerDispatcherHost::OnEnableNavigationPreload(
return;
}
- // TODO(falken): Write to disk before resolving the promise.
- registration->EnableNavigationPreload(enable);
- Send(new ServiceWorkerMsg_DidEnableNavigationPreload(thread_id, request_id));
+ GetContext()->storage()->UpdateNavigationPreloadEnabled(
+ registration->id(), registration->pattern().GetOrigin(), enable,
+ base::Bind(
+ &ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadEnabled, this,
+ thread_id, request_id, registration->id(), enable));
}
void ServiceWorkerDispatcherHost::OnGetNavigationPreloadState(
@@ -791,9 +806,7 @@ void ServiceWorkerDispatcherHost::OnGetNavigationPreloadState(
}
Send(new ServiceWorkerMsg_DidGetNavigationPreloadState(
- thread_id, request_id,
- NavigationPreloadState(registration->is_navigation_preload_enabled(),
- registration->navigation_preload_header())));
+ thread_id, request_id, registration->navigation_preload_state()));
}
void ServiceWorkerDispatcherHost::OnSetNavigationPreloadHeader(
@@ -837,6 +850,16 @@ void ServiceWorkerDispatcherHost::OnSetNavigationPreloadHeader(
bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_BAD_REGISTRATION_ID);
return;
}
+ // The spec discussion consensus is to reject if there is no active worker:
+ // https://github.com/w3c/ServiceWorker/issues/920#issuecomment-262212670
+ // TODO(falken): Remove this comment when the spec is updated.
+ if (!registration->active_version()) {
+ Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError(
+ thread_id, request_id, WebServiceWorkerError::ErrorTypeState,
+ std::string(kSetNavigationPreloadHeaderErrorPrefix) +
+ std::string(kNoActiveWorkerErrorMessage)));
+ return;
+ }
std::vector<GURL> urls = {provider_host->document_url(),
registration->pattern()};
@@ -864,10 +887,10 @@ void ServiceWorkerDispatcherHost::OnSetNavigationPreloadHeader(
return;
}
- // TODO(falken): Write to disk before resolving the promise.
- registration->SetNavigationPreloadHeader(value);
- Send(new ServiceWorkerMsg_DidSetNavigationPreloadHeader(thread_id,
- request_id));
+ GetContext()->storage()->UpdateNavigationPreloadHeader(
+ registration->id(), registration->pattern().GetOrigin(), value,
+ base::Bind(&ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader,
+ this, thread_id, request_id, registration->id(), value));
}
void ServiceWorkerDispatcherHost::OnPostMessageToWorker(
@@ -1651,6 +1674,47 @@ ServiceWorkerDispatcherHost::GetProviderHostForRequest(ProviderStatus* status,
return provider_host;
}
+void ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadEnabled(
+ int thread_id,
+ int request_id,
+ int registration_id,
+ bool enable,
+ ServiceWorkerStatusCode status) {
+ if (status != SERVICE_WORKER_OK) {
+ Send(new ServiceWorkerMsg_EnableNavigationPreloadError(
+ thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown,
+ std::string(kEnableNavigationPreloadErrorPrefix) +
+ std::string(kDatabaseErrorMessage)));
+ return;
+ }
+ ServiceWorkerRegistration* registration =
+ GetContext()->GetLiveRegistration(registration_id);
+ if (registration)
+ registration->EnableNavigationPreload(enable);
+ Send(new ServiceWorkerMsg_DidEnableNavigationPreload(thread_id, request_id));
+}
+
+void ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader(
+ int thread_id,
+ int request_id,
+ int registration_id,
+ const std::string& value,
+ ServiceWorkerStatusCode status) {
+ if (status != SERVICE_WORKER_OK) {
+ Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError(
+ thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown,
+ std::string(kSetNavigationPreloadHeaderErrorPrefix) +
+ std::string(kDatabaseErrorMessage)));
+ return;
+ }
+ ServiceWorkerRegistration* registration =
+ GetContext()->GetLiveRegistration(registration_id);
+ if (registration)
+ registration->SetNavigationPreloadHeader(value);
+ Send(new ServiceWorkerMsg_DidSetNavigationPreloadHeader(thread_id,
+ request_id));
+}
+
void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) {
ServiceWorkerHandle* handle = handles_.Lookup(handle_id);
if (!handle) {
« no previous file with comments | « content/browser/service_worker/service_worker_dispatcher_host.h ('k') | content/browser/service_worker/service_worker_info.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698