Revert of Cleanup: Remove extra EmbeddedWorker layering in the renderer SW code ...
[chromium-blink-merge.git] / content / renderer / service_worker / service_worker_script_context.cc
blob053f9de6e0b945e2b228dc6c3e69561c16b7dfbb
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/service_worker/service_worker_script_context.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/trace_event/trace_event.h"
11 #include "content/child/notifications/notification_data_conversions.h"
12 #include "content/child/service_worker/web_service_worker_registration_impl.h"
13 #include "content/child/thread_safe_sender.h"
14 #include "content/child/webmessageportchannel_impl.h"
15 #include "content/common/message_port_messages.h"
16 #include "content/common/service_worker/service_worker_messages.h"
17 #include "content/public/common/referrer.h"
18 #include "content/renderer/service_worker/embedded_worker_context_client.h"
19 #include "ipc/ipc_message.h"
20 #include "third_party/WebKit/public/platform/WebCrossOriginServiceWorkerClient.h"
21 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h"
22 #include "third_party/WebKit/public/platform/WebServiceWorkerClientQueryOptions.h"
23 #include "third_party/WebKit/public/platform/WebServiceWorkerRequest.h"
24 #include "third_party/WebKit/public/platform/WebString.h"
25 #include "third_party/WebKit/public/platform/WebURL.h"
26 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificationData.h"
27 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h"
28 #include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h"
30 namespace content {
32 namespace {
34 void SendPostMessageToClientOnMainThread(
35 ThreadSafeSender* sender,
36 int routing_id,
37 const std::string& uuid,
38 const base::string16& message,
39 scoped_ptr<blink::WebMessagePortChannelArray> channels) {
40 sender->Send(new ServiceWorkerHostMsg_PostMessageToClient(
41 routing_id, uuid, message,
42 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels.release())));
45 void SendCrossOriginMessageToClientOnMainThread(
46 ThreadSafeSender* sender,
47 int message_port_id,
48 const base::string16& message,
49 scoped_ptr<blink::WebMessagePortChannelArray> channels) {
50 sender->Send(new MessagePortHostMsg_PostMessage(
51 message_port_id,
52 MessagePortMessage(message),
53 WebMessagePortChannelImpl::ExtractMessagePortIDs(
54 channels.release())));
57 void StashMessagePortOnMainThread(ThreadSafeSender* sender,
58 int routing_id,
59 WebMessagePortChannelImpl* channel,
60 const base::string16& name) {
61 DCHECK_GE(channel->message_port_id(), 0);
62 channel->set_is_stashed();
63 sender->Send(new ServiceWorkerHostMsg_StashMessagePort(
64 routing_id, channel->message_port_id(), name));
67 blink::WebURLRequest::FetchRequestMode GetBlinkFetchRequestMode(
68 FetchRequestMode mode) {
69 return static_cast<blink::WebURLRequest::FetchRequestMode>(mode);
72 blink::WebURLRequest::FetchCredentialsMode GetBlinkFetchCredentialsMode(
73 FetchCredentialsMode credentials_mode) {
74 return static_cast<blink::WebURLRequest::FetchCredentialsMode>(
75 credentials_mode);
78 blink::WebURLRequest::RequestContext GetBlinkRequestContext(
79 RequestContextType request_context_type) {
80 return static_cast<blink::WebURLRequest::RequestContext>(
81 request_context_type);
84 blink::WebURLRequest::FrameType GetBlinkFrameType(
85 RequestContextFrameType frame_type) {
86 return static_cast<blink::WebURLRequest::FrameType>(frame_type);
89 blink::WebServiceWorkerClientInfo
90 ToWebServiceWorkerClientInfo(const ServiceWorkerClientInfo& client_info) {
91 DCHECK(client_info.IsValid());
93 blink::WebServiceWorkerClientInfo web_client_info;
95 web_client_info.uuid = base::UTF8ToUTF16(client_info.client_uuid);
96 web_client_info.pageVisibilityState = client_info.page_visibility_state;
97 web_client_info.isFocused = client_info.is_focused;
98 web_client_info.url = client_info.url;
99 web_client_info.frameType = GetBlinkFrameType(client_info.frame_type);
100 web_client_info.clientType = client_info.client_type;
102 return web_client_info;
105 } // namespace
107 ServiceWorkerScriptContext::ServiceWorkerScriptContext(
108 EmbeddedWorkerContextClient* embedded_context,
109 blink::WebServiceWorkerContextProxy* proxy)
110 : embedded_context_(embedded_context),
111 proxy_(proxy) {
114 ServiceWorkerScriptContext::~ServiceWorkerScriptContext() {}
116 void ServiceWorkerScriptContext::OnMessageReceived(
117 const IPC::Message& message) {
118 bool handled = true;
119 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerScriptContext, message)
120 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent)
121 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent)
122 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent)
123 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent)
124 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent,
125 OnNotificationClickEvent)
126 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent)
127 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent)
128 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginConnectEvent,
129 OnCrossOriginConnectEvent)
130 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage)
131 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginMessageToWorker,
132 OnCrossOriginMessageToWorker)
133 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SendStashedMessagePorts,
134 OnSendStashedMessagePorts)
135 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients,
136 OnDidGetClients)
137 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse,
138 OnOpenWindowResponse)
139 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError,
140 OnOpenWindowError)
141 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse,
142 OnFocusClientResponse)
143 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidSkipWaiting, OnDidSkipWaiting)
144 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidClaimClients, OnDidClaimClients)
145 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ClaimClientsError, OnClaimClientsError)
146 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_Ping, OnPing);
147 IPC_MESSAGE_UNHANDLED(handled = false)
148 IPC_END_MESSAGE_MAP()
150 DCHECK(handled);
153 void ServiceWorkerScriptContext::SetRegistrationInServiceWorkerGlobalScope(
154 scoped_ptr<WebServiceWorkerRegistrationImpl> registration) {
155 proxy_->setRegistration(registration.release());
158 void ServiceWorkerScriptContext::DidHandleActivateEvent(
159 int request_id,
160 blink::WebServiceWorkerEventResult result) {
161 UMA_HISTOGRAM_TIMES(
162 "ServiceWorker.ActivateEventExecutionTime",
163 base::TimeTicks::Now() - activate_start_timings_[request_id]);
164 activate_start_timings_.erase(request_id);
166 Send(new ServiceWorkerHostMsg_ActivateEventFinished(
167 GetRoutingID(), request_id, result));
170 void ServiceWorkerScriptContext::DidHandleInstallEvent(
171 int request_id,
172 blink::WebServiceWorkerEventResult result) {
173 UMA_HISTOGRAM_TIMES(
174 "ServiceWorker.InstallEventExecutionTime",
175 base::TimeTicks::Now() - install_start_timings_[request_id]);
176 install_start_timings_.erase(request_id);
178 Send(new ServiceWorkerHostMsg_InstallEventFinished(
179 GetRoutingID(), request_id, result));
182 void ServiceWorkerScriptContext::DidHandleFetchEvent(
183 int request_id,
184 ServiceWorkerFetchEventResult result,
185 const ServiceWorkerResponse& response) {
186 UMA_HISTOGRAM_TIMES(
187 "ServiceWorker.FetchEventExecutionTime",
188 base::TimeTicks::Now() - fetch_start_timings_[request_id]);
189 fetch_start_timings_.erase(request_id);
191 Send(new ServiceWorkerHostMsg_FetchEventFinished(
192 GetRoutingID(), request_id, result, response));
195 void ServiceWorkerScriptContext::DidHandleNotificationClickEvent(
196 int request_id,
197 blink::WebServiceWorkerEventResult result) {
198 UMA_HISTOGRAM_TIMES(
199 "ServiceWorker.NotificationClickEventExecutionTime",
200 base::TimeTicks::Now() - notification_click_start_timings_[request_id]);
201 notification_click_start_timings_.erase(request_id);
203 Send(new ServiceWorkerHostMsg_NotificationClickEventFinished(
204 GetRoutingID(), request_id));
207 void ServiceWorkerScriptContext::DidHandlePushEvent(
208 int request_id,
209 blink::WebServiceWorkerEventResult result) {
210 if (result == blink::WebServiceWorkerEventResultCompleted) {
211 UMA_HISTOGRAM_TIMES(
212 "ServiceWorker.PushEventExecutionTime",
213 base::TimeTicks::Now() - push_start_timings_[request_id]);
215 push_start_timings_.erase(request_id);
217 Send(new ServiceWorkerHostMsg_PushEventFinished(
218 GetRoutingID(), request_id, result));
221 void ServiceWorkerScriptContext::DidHandleSyncEvent(int request_id) {
222 Send(new ServiceWorkerHostMsg_SyncEventFinished(
223 GetRoutingID(), request_id));
226 void ServiceWorkerScriptContext::DidHandleCrossOriginConnectEvent(
227 int request_id,
228 bool accept_connection) {
229 Send(new ServiceWorkerHostMsg_CrossOriginConnectEventFinished(
230 GetRoutingID(), request_id, accept_connection));
233 void ServiceWorkerScriptContext::GetClients(
234 const blink::WebServiceWorkerClientQueryOptions& weboptions,
235 blink::WebServiceWorkerClientsCallbacks* callbacks) {
236 DCHECK(callbacks);
237 int request_id = pending_clients_callbacks_.Add(callbacks);
238 ServiceWorkerClientQueryOptions options;
239 options.client_type = weboptions.clientType;
240 options.include_uncontrolled = weboptions.includeUncontrolled;
241 Send(new ServiceWorkerHostMsg_GetClients(
242 GetRoutingID(), request_id, options));
245 void ServiceWorkerScriptContext::OpenWindow(
246 const GURL& url, blink::WebServiceWorkerClientCallbacks* callbacks) {
247 DCHECK(callbacks);
248 int request_id = pending_client_callbacks_.Add(callbacks);
249 Send(new ServiceWorkerHostMsg_OpenWindow(GetRoutingID(), request_id, url));
252 void ServiceWorkerScriptContext::SetCachedMetadata(const GURL& url,
253 const char* data,
254 size_t size) {
255 std::vector<char> copy(data, data + size);
256 Send(new ServiceWorkerHostMsg_SetCachedMetadata(GetRoutingID(), url, copy));
259 void ServiceWorkerScriptContext::ClearCachedMetadata(const GURL& url) {
260 Send(new ServiceWorkerHostMsg_ClearCachedMetadata(GetRoutingID(), url));
263 void ServiceWorkerScriptContext::PostMessageToClient(
264 const base::string16& uuid,
265 const base::string16& message,
266 scoped_ptr<blink::WebMessagePortChannelArray> channels) {
267 // This may send channels for MessagePorts, and all internal book-keeping
268 // messages for MessagePort (e.g. QueueMessages) are sent from main thread
269 // (with thread hopping), so we need to do the same thread hopping here not
270 // to overtake those messages.
271 embedded_context_->main_thread_task_runner()->PostTask(
272 FROM_HERE,
273 base::Bind(&SendPostMessageToClientOnMainThread,
274 make_scoped_refptr(embedded_context_->thread_safe_sender()),
275 GetRoutingID(),
276 base::UTF16ToUTF8(uuid),
277 message, base::Passed(&channels)));
280 void ServiceWorkerScriptContext::PostCrossOriginMessageToClient(
281 const blink::WebCrossOriginServiceWorkerClient& client,
282 const base::string16& message,
283 scoped_ptr<blink::WebMessagePortChannelArray> channels) {
284 // This may send channels for MessagePorts, and all internal book-keeping
285 // messages for MessagePort (e.g. QueueMessages) are sent from main thread
286 // (with thread hopping), so we need to do the same thread hopping here not
287 // to overtake those messages.
288 embedded_context_->main_thread_task_runner()->PostTask(
289 FROM_HERE,
290 base::Bind(&SendCrossOriginMessageToClientOnMainThread,
291 make_scoped_refptr(embedded_context_->thread_safe_sender()),
292 client.clientID, message, base::Passed(&channels)));
295 void ServiceWorkerScriptContext::FocusClient(
296 const base::string16& uuid,
297 blink::WebServiceWorkerClientCallbacks* callback) {
298 DCHECK(callback);
299 int request_id = pending_client_callbacks_.Add(callback);
300 Send(new ServiceWorkerHostMsg_FocusClient(GetRoutingID(), request_id,
301 base::UTF16ToUTF8(uuid)));
304 void ServiceWorkerScriptContext::ClaimClients(
305 blink::WebServiceWorkerClientsClaimCallbacks* callbacks) {
306 DCHECK(callbacks);
307 int request_id = pending_claim_clients_callbacks_.Add(callbacks);
308 Send(new ServiceWorkerHostMsg_ClaimClients(GetRoutingID(), request_id));
311 void ServiceWorkerScriptContext::SkipWaiting(
312 blink::WebServiceWorkerSkipWaitingCallbacks* callbacks) {
313 DCHECK(callbacks);
314 int request_id = pending_skip_waiting_callbacks_.Add(callbacks);
315 Send(new ServiceWorkerHostMsg_SkipWaiting(GetRoutingID(), request_id));
318 void ServiceWorkerScriptContext::StashMessagePort(
319 blink::WebMessagePortChannel* channel,
320 const base::string16& name) {
321 // All internal book-keeping messages for MessagePort are sent from main
322 // thread (with thread hopping), so we need to do the same thread hopping here
323 // not to overtake those messages.
324 WebMessagePortChannelImpl* channel_impl =
325 static_cast<WebMessagePortChannelImpl*>(channel);
326 embedded_context_->main_thread_task_runner()->PostTask(
327 FROM_HERE,
328 base::Bind(&StashMessagePortOnMainThread,
329 make_scoped_refptr(embedded_context_->thread_safe_sender()),
330 GetRoutingID(), base::Unretained(channel_impl), name));
333 void ServiceWorkerScriptContext::Send(IPC::Message* message) {
334 embedded_context_->Send(message);
337 int ServiceWorkerScriptContext::GetRoutingID() const {
338 return embedded_context_->embedded_worker_id();
341 void ServiceWorkerScriptContext::OnActivateEvent(int request_id) {
342 TRACE_EVENT0("ServiceWorker",
343 "ServiceWorkerScriptContext::OnActivateEvent");
344 activate_start_timings_[request_id] = base::TimeTicks::Now();
345 proxy_->dispatchActivateEvent(request_id);
348 void ServiceWorkerScriptContext::OnInstallEvent(int request_id) {
349 TRACE_EVENT0("ServiceWorker",
350 "ServiceWorkerScriptContext::OnInstallEvent");
351 install_start_timings_[request_id] = base::TimeTicks::Now();
352 proxy_->dispatchInstallEvent(request_id);
355 void ServiceWorkerScriptContext::OnFetchEvent(
356 int request_id,
357 const ServiceWorkerFetchRequest& request) {
358 blink::WebServiceWorkerRequest webRequest;
359 TRACE_EVENT0("ServiceWorker",
360 "ServiceWorkerScriptContext::OnFetchEvent");
361 webRequest.setURL(blink::WebURL(request.url));
362 webRequest.setMethod(blink::WebString::fromUTF8(request.method));
363 for (ServiceWorkerHeaderMap::const_iterator it = request.headers.begin();
364 it != request.headers.end();
365 ++it) {
366 webRequest.setHeader(blink::WebString::fromUTF8(it->first),
367 blink::WebString::fromUTF8(it->second));
369 if (!request.blob_uuid.empty()) {
370 webRequest.setBlob(blink::WebString::fromUTF8(request.blob_uuid),
371 request.blob_size);
373 webRequest.setReferrer(
374 blink::WebString::fromUTF8(request.referrer.url.spec()),
375 request.referrer.policy);
376 webRequest.setMode(GetBlinkFetchRequestMode(request.mode));
377 webRequest.setCredentialsMode(
378 GetBlinkFetchCredentialsMode(request.credentials_mode));
379 webRequest.setRequestContext(
380 GetBlinkRequestContext(request.request_context_type));
381 webRequest.setFrameType(GetBlinkFrameType(request.frame_type));
382 webRequest.setIsReload(request.is_reload);
383 fetch_start_timings_[request_id] = base::TimeTicks::Now();
384 proxy_->dispatchFetchEvent(request_id, webRequest);
387 void ServiceWorkerScriptContext::OnSyncEvent(int request_id) {
388 TRACE_EVENT0("ServiceWorker",
389 "ServiceWorkerScriptContext::OnSyncEvent");
390 proxy_->dispatchSyncEvent(request_id);
393 void ServiceWorkerScriptContext::OnNotificationClickEvent(
394 int request_id,
395 int64_t persistent_notification_id,
396 const PlatformNotificationData& notification_data) {
397 TRACE_EVENT0("ServiceWorker",
398 "ServiceWorkerScriptContext::OnNotificationClickEvent");
399 notification_click_start_timings_[request_id] = base::TimeTicks::Now();
400 proxy_->dispatchNotificationClickEvent(
401 request_id,
402 persistent_notification_id,
403 ToWebNotificationData(notification_data));
406 void ServiceWorkerScriptContext::OnPushEvent(int request_id,
407 const std::string& data) {
408 TRACE_EVENT0("ServiceWorker",
409 "ServiceWorkerScriptContext::OnPushEvent");
410 push_start_timings_[request_id] = base::TimeTicks::Now();
411 proxy_->dispatchPushEvent(request_id, blink::WebString::fromUTF8(data));
414 void ServiceWorkerScriptContext::OnGeofencingEvent(
415 int request_id,
416 blink::WebGeofencingEventType event_type,
417 const std::string& region_id,
418 const blink::WebCircularGeofencingRegion& region) {
419 TRACE_EVENT0("ServiceWorker",
420 "ServiceWorkerScriptContext::OnGeofencingEvent");
421 proxy_->dispatchGeofencingEvent(
422 request_id, event_type, blink::WebString::fromUTF8(region_id), region);
423 Send(new ServiceWorkerHostMsg_GeofencingEventFinished(GetRoutingID(),
424 request_id));
427 void ServiceWorkerScriptContext::OnCrossOriginConnectEvent(
428 int request_id,
429 const NavigatorConnectClient& client) {
430 TRACE_EVENT0("ServiceWorker",
431 "ServiceWorkerScriptContext::OnCrossOriginConnectEvent");
432 blink::WebCrossOriginServiceWorkerClient web_client;
433 web_client.origin = client.origin;
434 web_client.targetURL = client.target_url;
435 web_client.clientID = client.message_port_id;
436 proxy_->dispatchCrossOriginConnectEvent(request_id, web_client);
439 void ServiceWorkerScriptContext::OnPostMessage(
440 const base::string16& message,
441 const std::vector<TransferredMessagePort>& sent_message_ports,
442 const std::vector<int>& new_routing_ids) {
443 TRACE_EVENT0("ServiceWorker",
444 "ServiceWorkerScriptContext::OnPostEvent");
445 blink::WebMessagePortChannelArray ports =
446 WebMessagePortChannelImpl::CreatePorts(
447 sent_message_ports, new_routing_ids,
448 embedded_context_->main_thread_task_runner());
450 // dispatchMessageEvent is expected to execute onmessage function
451 // synchronously.
452 base::TimeTicks before = base::TimeTicks::Now();
453 proxy_->dispatchMessageEvent(message, ports);
454 UMA_HISTOGRAM_TIMES(
455 "ServiceWorker.MessageEventExecutionTime",
456 base::TimeTicks::Now() - before);
459 void ServiceWorkerScriptContext::OnCrossOriginMessageToWorker(
460 const NavigatorConnectClient& client,
461 const base::string16& message,
462 const std::vector<TransferredMessagePort>& sent_message_ports,
463 const std::vector<int>& new_routing_ids) {
464 TRACE_EVENT0("ServiceWorker",
465 "ServiceWorkerScriptContext::OnCrossOriginMessageToWorker");
466 blink::WebMessagePortChannelArray ports =
467 WebMessagePortChannelImpl::CreatePorts(
468 sent_message_ports, new_routing_ids,
469 embedded_context_->main_thread_task_runner());
471 blink::WebCrossOriginServiceWorkerClient web_client;
472 web_client.origin = client.origin;
473 web_client.targetURL = client.target_url;
474 web_client.clientID = client.message_port_id;
475 proxy_->dispatchCrossOriginMessageEvent(web_client, message, ports);
478 void ServiceWorkerScriptContext::OnSendStashedMessagePorts(
479 const std::vector<TransferredMessagePort>& stashed_message_ports,
480 const std::vector<int>& new_routing_ids,
481 const std::vector<base::string16>& port_names) {
482 TRACE_EVENT0("ServiceWorker",
483 "ServiceWorkerScriptContext::OnSendStashedMessagePorts");
484 blink::WebMessagePortChannelArray ports =
485 WebMessagePortChannelImpl::CreatePorts(
486 stashed_message_ports, new_routing_ids,
487 embedded_context_->main_thread_task_runner());
488 for (blink::WebMessagePortChannel* port : ports)
489 static_cast<WebMessagePortChannelImpl*>(port)->set_is_stashed();
490 proxy_->addStashedMessagePorts(ports, port_names);
493 void ServiceWorkerScriptContext::OnDidGetClients(
494 int request_id, const std::vector<ServiceWorkerClientInfo>& clients) {
495 TRACE_EVENT0("ServiceWorker",
496 "ServiceWorkerScriptContext::OnDidGetClients");
497 blink::WebServiceWorkerClientsCallbacks* callbacks =
498 pending_clients_callbacks_.Lookup(request_id);
499 if (!callbacks) {
500 NOTREACHED() << "Got stray response: " << request_id;
501 return;
503 scoped_ptr<blink::WebServiceWorkerClientsInfo> info(
504 new blink::WebServiceWorkerClientsInfo);
505 blink::WebVector<blink::WebServiceWorkerClientInfo> convertedClients(
506 clients.size());
507 for (size_t i = 0; i < clients.size(); ++i)
508 convertedClients[i] = ToWebServiceWorkerClientInfo(clients[i]);
509 info->clients.swap(convertedClients);
510 callbacks->onSuccess(info.release());
511 pending_clients_callbacks_.Remove(request_id);
514 void ServiceWorkerScriptContext::OnOpenWindowResponse(
515 int request_id,
516 const ServiceWorkerClientInfo& client) {
517 TRACE_EVENT0("ServiceWorker",
518 "ServiceWorkerScriptContext::OnOpenWindowResponse");
519 blink::WebServiceWorkerClientCallbacks* callbacks =
520 pending_client_callbacks_.Lookup(request_id);
521 if (!callbacks) {
522 NOTREACHED() << "Got stray response: " << request_id;
523 return;
525 scoped_ptr<blink::WebServiceWorkerClientInfo> web_client;
526 if (!client.IsEmpty()) {
527 DCHECK(client.IsValid());
528 web_client.reset(new blink::WebServiceWorkerClientInfo(
529 ToWebServiceWorkerClientInfo(client)));
531 callbacks->onSuccess(web_client.release());
532 pending_client_callbacks_.Remove(request_id);
535 void ServiceWorkerScriptContext::OnOpenWindowError(
536 int request_id,
537 const std::string& message) {
538 TRACE_EVENT0("ServiceWorker",
539 "ServiceWorkerScriptContext::OnOpenWindowError");
540 blink::WebServiceWorkerClientCallbacks* callbacks =
541 pending_client_callbacks_.Lookup(request_id);
542 if (!callbacks) {
543 NOTREACHED() << "Got stray response: " << request_id;
544 return;
546 scoped_ptr<blink::WebServiceWorkerError> error(
547 new blink::WebServiceWorkerError(
548 blink::WebServiceWorkerError::ErrorTypeUnknown,
549 blink::WebString::fromUTF8(message)));
550 callbacks->onError(error.release());
551 pending_client_callbacks_.Remove(request_id);
554 void ServiceWorkerScriptContext::OnFocusClientResponse(
555 int request_id, const ServiceWorkerClientInfo& client) {
556 TRACE_EVENT0("ServiceWorker",
557 "ServiceWorkerScriptContext::OnFocusClientResponse");
558 blink::WebServiceWorkerClientCallbacks* callback =
559 pending_client_callbacks_.Lookup(request_id);
560 if (!callback) {
561 NOTREACHED() << "Got stray response: " << request_id;
562 return;
564 if (!client.IsEmpty()) {
565 DCHECK(client.IsValid());
566 scoped_ptr<blink::WebServiceWorkerClientInfo> web_client (
567 new blink::WebServiceWorkerClientInfo(
568 ToWebServiceWorkerClientInfo(client)));
569 callback->onSuccess(web_client.release());
570 } else {
571 scoped_ptr<blink::WebServiceWorkerError> error(
572 new blink::WebServiceWorkerError(
573 blink::WebServiceWorkerError::ErrorTypeNotFound,
574 "The WindowClient was not found."));
575 callback->onError(error.release());
578 pending_client_callbacks_.Remove(request_id);
581 void ServiceWorkerScriptContext::OnDidSkipWaiting(int request_id) {
582 TRACE_EVENT0("ServiceWorker",
583 "ServiceWorkerScriptContext::OnDidSkipWaiting");
584 blink::WebServiceWorkerSkipWaitingCallbacks* callbacks =
585 pending_skip_waiting_callbacks_.Lookup(request_id);
586 if (!callbacks) {
587 NOTREACHED() << "Got stray response: " << request_id;
588 return;
590 callbacks->onSuccess();
591 pending_skip_waiting_callbacks_.Remove(request_id);
594 void ServiceWorkerScriptContext::OnDidClaimClients(int request_id) {
595 TRACE_EVENT0("ServiceWorker",
596 "ServiceWorkerScriptContext::OnDidClaimClients");
597 blink::WebServiceWorkerClientsClaimCallbacks* callbacks =
598 pending_claim_clients_callbacks_.Lookup(request_id);
599 if (!callbacks) {
600 NOTREACHED() << "Got stray response: " << request_id;
601 return;
603 callbacks->onSuccess();
604 pending_claim_clients_callbacks_.Remove(request_id);
607 void ServiceWorkerScriptContext::OnClaimClientsError(
608 int request_id,
609 blink::WebServiceWorkerError::ErrorType error_type,
610 const base::string16& message) {
611 TRACE_EVENT0("ServiceWorker",
612 "ServiceWorkerScriptContext::OnClaimClientsError");
613 blink::WebServiceWorkerClientsClaimCallbacks* callbacks =
614 pending_claim_clients_callbacks_.Lookup(request_id);
615 if (!callbacks) {
616 NOTREACHED() << "Got stray response: " << request_id;
617 return;
619 scoped_ptr<blink::WebServiceWorkerError> error(
620 new blink::WebServiceWorkerError(error_type, message));
621 callbacks->onError(error.release());
622 pending_claim_clients_callbacks_.Remove(request_id);
625 void ServiceWorkerScriptContext::OnPing() {
626 Send(new ServiceWorkerHostMsg_Pong(GetRoutingID()));
629 } // namespace content