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

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

Issue 871853002: ServiceWorker: add ServiceWorkerClients.claim() support (2/3). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fetch registrations from DB Created 5 years, 10 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_version.h" 5 #include "content/browser/service_worker/service_worker_version.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
11 #include "content/browser/message_port_message_filter.h" 12 #include "content/browser/message_port_message_filter.h"
12 #include "content/browser/message_port_service.h" 13 #include "content/browser/message_port_service.h"
13 #include "content/browser/service_worker/embedded_worker_instance.h" 14 #include "content/browser/service_worker/embedded_worker_instance.h"
14 #include "content/browser/service_worker/embedded_worker_registry.h" 15 #include "content/browser/service_worker/embedded_worker_registry.h"
15 #include "content/browser/service_worker/service_worker_context_core.h" 16 #include "content/browser/service_worker/service_worker_context_core.h"
16 #include "content/browser/service_worker/service_worker_registration.h" 17 #include "content/browser/service_worker/service_worker_registration.h"
17 #include "content/browser/service_worker/service_worker_utils.h" 18 #include "content/browser/service_worker/service_worker_utils.h"
18 #include "content/common/service_worker/service_worker_messages.h" 19 #include "content/common/service_worker/service_worker_messages.h"
19 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
20 #include "content/public/common/content_switches.h" 21 #include "content/public/common/content_switches.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 // (Note that if all references to the version is dropped the worker 65 // (Note that if all references to the version is dropped the worker
65 // is also stopped without delay) 66 // is also stopped without delay)
66 const int64 kStopWorkerDelay = 30; // 30 secs. 67 const int64 kStopWorkerDelay = 30; // 30 secs.
67 68
68 // Delay for attempting to stop a doomed worker with in-flight requests. 69 // Delay for attempting to stop a doomed worker with in-flight requests.
69 const int64 kStopDoomedWorkerDelay = 5; // 5 secs. 70 const int64 kStopDoomedWorkerDelay = 5; // 5 secs.
70 71
71 // Default delay for scheduled update. 72 // Default delay for scheduled update.
72 const int kUpdateDelaySeconds = 1; 73 const int kUpdateDelaySeconds = 1;
73 74
75 const char kClaimClientsStateErrorMesage[] =
76 "Only the active worker can claim clients.";
77
78 const char kClaimClientsShutdownErrorMesage[] =
79 "Failed to claim clients due to Service Worker system shutdown.";
80
74 void RunSoon(const base::Closure& callback) { 81 void RunSoon(const base::Closure& callback) {
75 if (!callback.is_null()) 82 if (!callback.is_null())
76 base::MessageLoop::current()->PostTask(FROM_HERE, callback); 83 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
77 } 84 }
78 85
79 template <typename CallbackArray, typename Arg> 86 template <typename CallbackArray, typename Arg>
80 void RunCallbacks(ServiceWorkerVersion* version, 87 void RunCallbacks(ServiceWorkerVersion* version,
81 CallbackArray* callbacks_ptr, 88 CallbackArray* callbacks_ptr,
82 const Arg& arg) { 89 const Arg& arg) {
83 CallbackArray callbacks; 90 CallbackArray callbacks;
(...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after
793 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, 800 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument,
794 OnPostMessageToDocument) 801 OnPostMessageToDocument)
795 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, 802 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient,
796 OnFocusClient) 803 OnFocusClient)
797 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoSuccess, 804 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoSuccess,
798 OnGetClientInfoSuccess) 805 OnGetClientInfoSuccess)
799 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoError, 806 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoError,
800 OnGetClientInfoError) 807 OnGetClientInfoError)
801 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, 808 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting,
802 OnSkipWaiting) 809 OnSkipWaiting)
810 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients,
811 OnClaimClients)
803 IPC_MESSAGE_UNHANDLED(handled = false) 812 IPC_MESSAGE_UNHANDLED(handled = false)
804 IPC_END_MESSAGE_MAP() 813 IPC_END_MESSAGE_MAP()
805 return handled; 814 return handled;
806 } 815 }
807 816
808 void ServiceWorkerVersion::OnStartMessageSent( 817 void ServiceWorkerVersion::OnStartMessageSent(
809 ServiceWorkerStatusCode status) { 818 ServiceWorkerStatusCode status) {
810 if (status != SERVICE_WORKER_OK) 819 if (status != SERVICE_WORKER_OK)
811 RunCallbacks(this, &start_callbacks_, status); 820 RunCallbacks(this, &start_callbacks_, status);
812 } 821 }
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 pending_skip_waiting_requests_.push_back(request_id); 1107 pending_skip_waiting_requests_.push_back(request_id);
1099 if (pending_skip_waiting_requests_.size() == 1) 1108 if (pending_skip_waiting_requests_.size() == 1)
1100 registration->ActivateWaitingVersionWhenReady(); 1109 registration->ActivateWaitingVersionWhenReady();
1101 } 1110 }
1102 1111
1103 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { 1112 void ServiceWorkerVersion::DidSkipWaiting(int request_id) {
1104 if (running_status() == STARTING || running_status() == RUNNING) 1113 if (running_status() == STARTING || running_status() == RUNNING)
1105 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); 1114 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id));
1106 } 1115 }
1107 1116
1117 void ServiceWorkerVersion::OnClaimClients(int request_id) {
1118 StatusCallback callback = base::Bind(&ServiceWorkerVersion::DidClaimClients,
1119 weak_factory_.GetWeakPtr(),
1120 request_id);
1121 if (status_ != ACTIVATING && status_ != ACTIVATED) {
1122 callback.Run(SERVICE_WORKER_ERROR_STATE);
1123 return;
1124 }
1125
1126 ServiceWorkerRegistration* registration = nullptr;
1127 if (!context_ ||
1128 !(registration = context_->GetLiveRegistration(registration_id_))) {
1129 callback.Run(SERVICE_WORKER_ERROR_ABORT);
1130 return;
1131 }
1132
1133 registration->ClaimClients(callback);
1134 }
1135
1136 void ServiceWorkerVersion::DidClaimClients(
1137 int request_id, ServiceWorkerStatusCode status) {
1138 if (status == SERVICE_WORKER_ERROR_STATE) {
1139 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError(
1140 request_id, blink::WebServiceWorkerError::ErrorTypeState,
1141 base::ASCIIToUTF16(kClaimClientsStateErrorMesage)));
1142 return;
1143 }
1144 if (status == SERVICE_WORKER_ERROR_ABORT) {
1145 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError(
1146 request_id, blink::WebServiceWorkerError::ErrorTypeAbort,
1147 base::ASCIIToUTF16(kClaimClientsShutdownErrorMesage)));
1148 return;
1149 }
1150 DCHECK(status == SERVICE_WORKER_OK);
1151 embedded_worker_->SendMessage(ServiceWorkerMsg_DidClaimClients(request_id));
1152 }
1153
1108 void ServiceWorkerVersion::DidGetClientInfo( 1154 void ServiceWorkerVersion::DidGetClientInfo(
1109 int client_id, 1155 int client_id,
1110 scoped_refptr<GetClientDocumentsCallback> callback, 1156 scoped_refptr<GetClientDocumentsCallback> callback,
1111 ServiceWorkerStatusCode status, 1157 ServiceWorkerStatusCode status,
1112 const ServiceWorkerClientInfo& info) { 1158 const ServiceWorkerClientInfo& info) {
1113 if (status == SERVICE_WORKER_OK) 1159 if (status == SERVICE_WORKER_OK)
1114 callback->AddClientInfo(client_id, info); 1160 callback->AddClientInfo(client_id, info);
1115 } 1161 }
1116 1162
1117 void ServiceWorkerVersion::ScheduleStopWorker() { 1163 void ServiceWorkerVersion::ScheduleStopWorker() {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1172 int request_id) { 1218 int request_id) {
1173 callbacks->Remove(request_id); 1219 callbacks->Remove(request_id);
1174 if (is_doomed_) { 1220 if (is_doomed_) {
1175 // The stop should be already scheduled, but try to stop immediately, in 1221 // The stop should be already scheduled, but try to stop immediately, in
1176 // order to release worker resources soon. 1222 // order to release worker resources soon.
1177 StopWorkerIfIdle(); 1223 StopWorkerIfIdle();
1178 } 1224 }
1179 } 1225 }
1180 1226
1181 } // namespace content 1227 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698