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_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 Loading... |
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 Loading... |
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 Loading... |
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(), request_id); |
| 1120 if (status_ != ACTIVATING && status_ != ACTIVATED) { |
| 1121 callback.Run(SERVICE_WORKER_ERROR_STATE); |
| 1122 return; |
| 1123 } |
| 1124 if (!context_) { |
| 1125 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 1126 return; |
| 1127 } |
| 1128 |
| 1129 ServiceWorkerRegistration* registration = |
| 1130 context_->GetLiveRegistration(registration_id_); |
| 1131 if (!registration) { |
| 1132 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 1133 return; |
| 1134 } |
| 1135 registration->ClaimClients(callback); |
| 1136 } |
| 1137 |
| 1138 void ServiceWorkerVersion::DidClaimClients( |
| 1139 int request_id, ServiceWorkerStatusCode status) { |
| 1140 if (status == SERVICE_WORKER_ERROR_STATE) { |
| 1141 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
| 1142 request_id, blink::WebServiceWorkerError::ErrorTypeState, |
| 1143 base::ASCIIToUTF16(kClaimClientsStateErrorMesage))); |
| 1144 return; |
| 1145 } |
| 1146 if (status == SERVICE_WORKER_ERROR_ABORT) { |
| 1147 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
| 1148 request_id, blink::WebServiceWorkerError::ErrorTypeAbort, |
| 1149 base::ASCIIToUTF16(kClaimClientsShutdownErrorMesage))); |
| 1150 return; |
| 1151 } |
| 1152 DCHECK(status == SERVICE_WORKER_OK); |
| 1153 embedded_worker_->SendMessage(ServiceWorkerMsg_DidClaimClients(request_id)); |
| 1154 } |
| 1155 |
1108 void ServiceWorkerVersion::DidGetClientInfo( | 1156 void ServiceWorkerVersion::DidGetClientInfo( |
1109 int client_id, | 1157 int client_id, |
1110 scoped_refptr<GetClientDocumentsCallback> callback, | 1158 scoped_refptr<GetClientDocumentsCallback> callback, |
1111 ServiceWorkerStatusCode status, | 1159 ServiceWorkerStatusCode status, |
1112 const ServiceWorkerClientInfo& info) { | 1160 const ServiceWorkerClientInfo& info) { |
1113 if (status == SERVICE_WORKER_OK) | 1161 if (status == SERVICE_WORKER_OK) |
1114 callback->AddClientInfo(client_id, info); | 1162 callback->AddClientInfo(client_id, info); |
1115 } | 1163 } |
1116 | 1164 |
1117 void ServiceWorkerVersion::ScheduleStopWorker() { | 1165 void ServiceWorkerVersion::ScheduleStopWorker() { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1172 int request_id) { | 1220 int request_id) { |
1173 callbacks->Remove(request_id); | 1221 callbacks->Remove(request_id); |
1174 if (is_doomed_) { | 1222 if (is_doomed_) { |
1175 // The stop should be already scheduled, but try to stop immediately, in | 1223 // The stop should be already scheduled, but try to stop immediately, in |
1176 // order to release worker resources soon. | 1224 // order to release worker resources soon. |
1177 StopWorkerIfIdle(); | 1225 StopWorkerIfIdle(); |
1178 } | 1226 } |
1179 } | 1227 } |
1180 | 1228 |
1181 } // namespace content | 1229 } // namespace content |
OLD | NEW |