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

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

Issue 224733014: Introduce ServiceWorkerHandle for tracking WebServiceWorkerImpl reference (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sigh, rebased Created 6 years, 8 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 | Annotate | Revision Log
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_dispatcher_host.h" 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "content/browser/message_port_message_filter.h" 9 #include "content/browser/message_port_message_filter.h"
10 #include "content/browser/message_port_service.h" 10 #include "content/browser/message_port_service.h"
11 #include "content/browser/service_worker/embedded_worker_registry.h" 11 #include "content/browser/service_worker/embedded_worker_registry.h"
12 #include "content/browser/service_worker/service_worker_context_core.h" 12 #include "content/browser/service_worker/service_worker_context_core.h"
13 #include "content/browser/service_worker/service_worker_context_wrapper.h" 13 #include "content/browser/service_worker/service_worker_context_wrapper.h"
14 #include "content/browser/service_worker/service_worker_handle.h"
14 #include "content/browser/service_worker/service_worker_registration.h" 15 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/browser/service_worker/service_worker_utils.h" 16 #include "content/browser/service_worker/service_worker_utils.h"
16 #include "content/common/service_worker/embedded_worker_messages.h" 17 #include "content/common/service_worker/embedded_worker_messages.h"
17 #include "content/common/service_worker/service_worker_messages.h" 18 #include "content/common/service_worker/service_worker_messages.h"
18 #include "ipc/ipc_message_macros.h" 19 #include "ipc/ipc_message_macros.h"
19 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" 20 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h"
20 #include "url/gurl.h" 21 #include "url/gurl.h"
21 22
22 using blink::WebServiceWorkerError; 23 using blink::WebServiceWorkerError;
23 24
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessage, 97 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessage,
97 OnPostMessage) 98 OnPostMessage)
98 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStarted, 99 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStarted,
99 OnWorkerStarted) 100 OnWorkerStarted)
100 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStopped, 101 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStopped,
101 OnWorkerStopped) 102 OnWorkerStopped)
102 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_SendMessageToBrowser, 103 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_SendMessageToBrowser,
103 OnSendMessageToBrowser) 104 OnSendMessageToBrowser)
104 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_ReportException, 105 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_ReportException,
105 OnReportException) 106 OnReportException)
107 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ServiceWorkerObjectDestroyed,
108 OnServiceWorkerObjectDestroyed)
106 IPC_MESSAGE_UNHANDLED(handled = false) 109 IPC_MESSAGE_UNHANDLED(handled = false)
107 IPC_END_MESSAGE_MAP() 110 IPC_END_MESSAGE_MAP()
108 111
109 return handled; 112 return handled;
110 } 113 }
111 114
115 int ServiceWorkerDispatcherHost::RegisterServiceWorkerHandle(
116 scoped_ptr<ServiceWorkerHandle> handle) {
117 return handles_.Add(handle.release());
118 }
119
112 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( 120 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker(
113 int32 thread_id, 121 int32 thread_id,
114 int32 request_id, 122 int32 request_id,
115 const GURL& pattern, 123 const GURL& pattern,
116 const GURL& script_url) { 124 const GURL& script_url) {
117 if (!context_ || !ServiceWorkerUtils::IsFeatureEnabled()) { 125 if (!context_ || !ServiceWorkerUtils::IsFeatureEnabled()) {
118 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( 126 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError(
119 thread_id, 127 thread_id,
120 request_id, 128 request_id,
121 WebServiceWorkerError::DisabledError, 129 WebServiceWorkerError::DisabledError,
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 context_->UnregisterServiceWorker( 172 context_->UnregisterServiceWorker(
165 pattern, 173 pattern,
166 render_process_id_, 174 render_process_id_,
167 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, 175 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete,
168 this, 176 this,
169 thread_id, 177 thread_id,
170 request_id)); 178 request_id));
171 } 179 }
172 180
173 void ServiceWorkerDispatcherHost::OnPostMessage( 181 void ServiceWorkerDispatcherHost::OnPostMessage(
174 int64 version_id, 182 int handle_id,
175 const base::string16& message, 183 const base::string16& message,
176 const std::vector<int>& sent_message_port_ids) { 184 const std::vector<int>& sent_message_port_ids) {
177 if (!context_ || !ServiceWorkerUtils::IsFeatureEnabled()) 185 if (!context_ || !ServiceWorkerUtils::IsFeatureEnabled())
178 return; 186 return;
179 187
180 std::vector<int> new_routing_ids(sent_message_port_ids.size()); 188 std::vector<int> new_routing_ids(sent_message_port_ids.size());
181 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { 189 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) {
182 new_routing_ids[i] = message_port_message_filter_->GetNextRoutingID(); 190 new_routing_ids[i] = message_port_message_filter_->GetNextRoutingID();
183 MessagePortService::GetInstance()->UpdateMessagePort( 191 MessagePortService::GetInstance()->UpdateMessagePort(
184 sent_message_port_ids[i], 192 sent_message_port_ids[i],
185 message_port_message_filter_, 193 message_port_message_filter_,
186 new_routing_ids[i]); 194 new_routing_ids[i]);
187 } 195 }
188 196
189 // TODO(kinuko,michaeln): Make sure we keep the version that has 197 ServiceWorkerHandle* handle = handles_.Lookup(handle_id);
190 // corresponding WebServiceWorkerImpl alive. 198 if (!handle) {
191 ServiceWorkerVersion* version = context_->GetLiveVersion(version_id); 199 BadMessageReceived();
192 if (!version)
193 return; 200 return;
201 }
194 202
195 version->SendMessage( 203 handle->version()->SendMessage(
196 ServiceWorkerMsg_Message(message, sent_message_port_ids, new_routing_ids), 204 ServiceWorkerMsg_Message(message, sent_message_port_ids, new_routing_ids),
197 base::Bind(&NoOpStatusCallback)); 205 base::Bind(&NoOpStatusCallback));
198 } 206 }
199 207
200 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) { 208 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) {
201 if (!context_) 209 if (!context_)
202 return; 210 return;
203 if (context_->GetProviderHost(render_process_id_, provider_id)) { 211 if (context_->GetProviderHost(render_process_id_, provider_id)) {
204 BadMessageReceived(); 212 BadMessageReceived();
205 return; 213 return;
206 } 214 }
207 scoped_ptr<ServiceWorkerProviderHost> provider_host( 215 scoped_ptr<ServiceWorkerProviderHost> provider_host(
208 new ServiceWorkerProviderHost( 216 new ServiceWorkerProviderHost(
209 render_process_id_, provider_id, context_)); 217 render_process_id_, provider_id, context_, this));
210 context_->AddProviderHost(provider_host.Pass()); 218 context_->AddProviderHost(provider_host.Pass());
211 } 219 }
212 220
213 void ServiceWorkerDispatcherHost::OnProviderDestroyed(int provider_id) { 221 void ServiceWorkerDispatcherHost::OnProviderDestroyed(int provider_id) {
214 if (!context_) 222 if (!context_)
215 return; 223 return;
216 if (!context_->GetProviderHost(render_process_id_, provider_id)) { 224 if (!context_->GetProviderHost(render_process_id_, provider_id)) {
217 BadMessageReceived(); 225 BadMessageReceived();
218 return; 226 return;
219 } 227 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 return; 261 return;
254 } 262 }
255 } 263 }
256 264
257 void ServiceWorkerDispatcherHost::RegistrationComplete( 265 void ServiceWorkerDispatcherHost::RegistrationComplete(
258 int32 thread_id, 266 int32 thread_id,
259 int32 request_id, 267 int32 request_id,
260 ServiceWorkerStatusCode status, 268 ServiceWorkerStatusCode status,
261 int64 registration_id, 269 int64 registration_id,
262 int64 version_id) { 270 int64 version_id) {
271 if (!context_)
272 return;
273
263 if (status != SERVICE_WORKER_OK) { 274 if (status != SERVICE_WORKER_OK) {
264 SendRegistrationError(thread_id, request_id, status); 275 SendRegistrationError(thread_id, request_id, status);
265 return; 276 return;
266 } 277 }
267 278
279 ServiceWorkerVersion* version = context_->GetLiveVersion(version_id);
280 DCHECK(version);
281 DCHECK_EQ(registration_id, version->registration_id());
282 int handle_id = RegisterServiceWorkerHandle(
283 ServiceWorkerHandle::Create(context_, this, thread_id, version));
268 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( 284 Send(new ServiceWorkerMsg_ServiceWorkerRegistered(
269 thread_id, request_id, version_id)); 285 thread_id, request_id, handle_id));
270 } 286 }
271 287
272 void ServiceWorkerDispatcherHost::OnWorkerStarted( 288 void ServiceWorkerDispatcherHost::OnWorkerStarted(
273 int thread_id, int embedded_worker_id) { 289 int thread_id, int embedded_worker_id) {
274 if (!context_) 290 if (!context_)
275 return; 291 return;
276 context_->embedded_worker_registry()->OnWorkerStarted( 292 context_->embedded_worker_registry()->OnWorkerStarted(
277 render_process_id_, thread_id, embedded_worker_id); 293 render_process_id_, thread_id, embedded_worker_id);
278 } 294 }
279 295
(...skipping 19 matching lines...) Expand all
299 const base::string16& error_message, 315 const base::string16& error_message,
300 int line_number, 316 int line_number,
301 int column_number, 317 int column_number,
302 const GURL& source_url) { 318 const GURL& source_url) {
303 // TODO(horo, nhiroki): Show the error on serviceworker-internals 319 // TODO(horo, nhiroki): Show the error on serviceworker-internals
304 // (http://crbug.com/359517). 320 // (http://crbug.com/359517).
305 DVLOG(2) << "[Error] " << error_message << " (" << source_url 321 DVLOG(2) << "[Error] " << error_message << " (" << source_url
306 << ":" << line_number << "," << column_number << ")"; 322 << ":" << line_number << "," << column_number << ")";
307 } 323 }
308 324
325 void ServiceWorkerDispatcherHost::OnServiceWorkerObjectDestroyed(
326 int handle_id) {
327 handles_.Remove(handle_id);
328 }
329
309 void ServiceWorkerDispatcherHost::UnregistrationComplete( 330 void ServiceWorkerDispatcherHost::UnregistrationComplete(
310 int32 thread_id, 331 int32 thread_id,
311 int32 request_id, 332 int32 request_id,
312 ServiceWorkerStatusCode status) { 333 ServiceWorkerStatusCode status) {
313 if (status != SERVICE_WORKER_OK) { 334 if (status != SERVICE_WORKER_OK) {
314 SendRegistrationError(thread_id, request_id, status); 335 SendRegistrationError(thread_id, request_id, status);
315 return; 336 return;
316 } 337 }
317 338
318 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id)); 339 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id));
319 } 340 }
320 341
321 void ServiceWorkerDispatcherHost::SendRegistrationError( 342 void ServiceWorkerDispatcherHost::SendRegistrationError(
322 int32 thread_id, 343 int32 thread_id,
323 int32 request_id, 344 int32 request_id,
324 ServiceWorkerStatusCode status) { 345 ServiceWorkerStatusCode status) {
325 base::string16 error_message; 346 base::string16 error_message;
326 blink::WebServiceWorkerError::ErrorType error_type; 347 blink::WebServiceWorkerError::ErrorType error_type;
327 GetServiceWorkerRegistrationStatusResponse( 348 GetServiceWorkerRegistrationStatusResponse(
328 status, &error_type, &error_message); 349 status, &error_type, &error_message);
329 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( 350 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError(
330 thread_id, request_id, error_type, error_message)); 351 thread_id, request_id, error_type, error_message));
331 } 352 }
332 353
333 } // namespace content 354 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698