| 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 int64 version_id, | 158 int64 version_id, |
| 152 base::WeakPtr<ServiceWorkerContextCore> context) | 159 base::WeakPtr<ServiceWorkerContextCore> context) |
| 153 : version_id_(version_id), | 160 : version_id_(version_id), |
| 154 registration_id_(kInvalidServiceWorkerVersionId), | 161 registration_id_(kInvalidServiceWorkerVersionId), |
| 155 script_url_(script_url), | 162 script_url_(script_url), |
| 156 status_(NEW), | 163 status_(NEW), |
| 157 context_(context), | 164 context_(context), |
| 158 script_cache_map_(this, context), | 165 script_cache_map_(this, context), |
| 159 is_doomed_(false), | 166 is_doomed_(false), |
| 160 skip_waiting_(false), | 167 skip_waiting_(false), |
| 168 claiming_clients_(false), |
| 161 weak_factory_(this) { | 169 weak_factory_(this) { |
| 162 DCHECK(context_); | 170 DCHECK(context_); |
| 163 DCHECK(registration); | 171 DCHECK(registration); |
| 164 if (registration) { | 172 if (registration) { |
| 165 registration_id_ = registration->id(); | 173 registration_id_ = registration->id(); |
| 166 scope_ = registration->pattern(); | 174 scope_ = registration->pattern(); |
| 167 } | 175 } |
| 168 context_->AddLiveVersion(this); | 176 context_->AddLiveVersion(this); |
| 169 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); | 177 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); |
| 170 embedded_worker_->AddListener(this); | 178 embedded_worker_->AddListener(this); |
| (...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 793 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, | 801 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, |
| 794 OnPostMessageToDocument) | 802 OnPostMessageToDocument) |
| 795 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, | 803 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, |
| 796 OnFocusClient) | 804 OnFocusClient) |
| 797 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoSuccess, | 805 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoSuccess, |
| 798 OnGetClientInfoSuccess) | 806 OnGetClientInfoSuccess) |
| 799 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoError, | 807 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoError, |
| 800 OnGetClientInfoError) | 808 OnGetClientInfoError) |
| 801 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, | 809 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, |
| 802 OnSkipWaiting) | 810 OnSkipWaiting) |
| 811 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, |
| 812 OnClaimClients) |
| 803 IPC_MESSAGE_UNHANDLED(handled = false) | 813 IPC_MESSAGE_UNHANDLED(handled = false) |
| 804 IPC_END_MESSAGE_MAP() | 814 IPC_END_MESSAGE_MAP() |
| 805 return handled; | 815 return handled; |
| 806 } | 816 } |
| 807 | 817 |
| 808 void ServiceWorkerVersion::OnStartMessageSent( | 818 void ServiceWorkerVersion::OnStartMessageSent( |
| 809 ServiceWorkerStatusCode status) { | 819 ServiceWorkerStatusCode status) { |
| 810 if (status != SERVICE_WORKER_OK) | 820 if (status != SERVICE_WORKER_OK) |
| 811 RunCallbacks(this, &start_callbacks_, status); | 821 RunCallbacks(this, &start_callbacks_, status); |
| 812 } | 822 } |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1098 pending_skip_waiting_requests_.push_back(request_id); | 1108 pending_skip_waiting_requests_.push_back(request_id); |
| 1099 if (pending_skip_waiting_requests_.size() == 1) | 1109 if (pending_skip_waiting_requests_.size() == 1) |
| 1100 registration->ActivateWaitingVersionWhenReady(); | 1110 registration->ActivateWaitingVersionWhenReady(); |
| 1101 } | 1111 } |
| 1102 | 1112 |
| 1103 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { | 1113 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { |
| 1104 if (running_status() == STARTING || running_status() == RUNNING) | 1114 if (running_status() == STARTING || running_status() == RUNNING) |
| 1105 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); | 1115 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); |
| 1106 } | 1116 } |
| 1107 | 1117 |
| 1118 void ServiceWorkerVersion::OnClaimClients(int request_id) { |
| 1119 if (status_ != ACTIVATING && status_ != ACTIVATED) { |
| 1120 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
| 1121 request_id, blink::WebServiceWorkerError::ErrorTypeState, |
| 1122 base::ASCIIToUTF16(kClaimClientsStateErrorMesage))); |
| 1123 return; |
| 1124 } |
| 1125 |
| 1126 ServiceWorkerRegistration* registration = NULL; |
| 1127 if (!context_ || |
| 1128 !(registration = context_->GetLiveRegistration(registration_id_))) { |
| 1129 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
| 1130 request_id, blink::WebServiceWorkerError::ErrorTypeAbort, |
| 1131 base::ASCIIToUTF16(kClaimClientsShutdownErrorMesage))); |
| 1132 return; |
| 1133 } |
| 1134 |
| 1135 DCHECK_EQ(this, registration->active_version()); |
| 1136 claiming_clients_ = true; |
| 1137 registration->ClaimClients(); |
| 1138 embedded_worker_->SendMessage(ServiceWorkerMsg_DidClaimClients(request_id)); |
| 1139 claiming_clients_ = false; |
| 1140 } |
| 1141 |
| 1108 void ServiceWorkerVersion::DidGetClientInfo( | 1142 void ServiceWorkerVersion::DidGetClientInfo( |
| 1109 int client_id, | 1143 int client_id, |
| 1110 scoped_refptr<GetClientDocumentsCallback> callback, | 1144 scoped_refptr<GetClientDocumentsCallback> callback, |
| 1111 ServiceWorkerStatusCode status, | 1145 ServiceWorkerStatusCode status, |
| 1112 const ServiceWorkerClientInfo& info) { | 1146 const ServiceWorkerClientInfo& info) { |
| 1113 if (status == SERVICE_WORKER_OK) | 1147 if (status == SERVICE_WORKER_OK) |
| 1114 callback->AddClientInfo(client_id, info); | 1148 callback->AddClientInfo(client_id, info); |
| 1115 } | 1149 } |
| 1116 | 1150 |
| 1117 void ServiceWorkerVersion::ScheduleStopWorker() { | 1151 void ServiceWorkerVersion::ScheduleStopWorker() { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 int request_id) { | 1206 int request_id) { |
| 1173 callbacks->Remove(request_id); | 1207 callbacks->Remove(request_id); |
| 1174 if (is_doomed_) { | 1208 if (is_doomed_) { |
| 1175 // The stop should be already scheduled, but try to stop immediately, in | 1209 // The stop should be already scheduled, but try to stop immediately, in |
| 1176 // order to release worker resources soon. | 1210 // order to release worker resources soon. |
| 1177 StopWorkerIfIdle(); | 1211 StopWorkerIfIdle(); |
| 1178 } | 1212 } |
| 1179 } | 1213 } |
| 1180 | 1214 |
| 1181 } // namespace content | 1215 } // namespace content |
| OLD | NEW |