OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/renderer/service_worker/service_worker_script_context.h" | 5 #include "content/renderer/service_worker/service_worker_script_context.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "content/child/thread_safe_sender.h" | |
8 #include "content/child/webmessageportchannel_impl.h" | 9 #include "content/child/webmessageportchannel_impl.h" |
9 #include "content/common/service_worker/service_worker_messages.h" | 10 #include "content/common/service_worker/service_worker_messages.h" |
10 #include "content/renderer/service_worker/embedded_worker_context_client.h" | 11 #include "content/renderer/service_worker/embedded_worker_context_client.h" |
11 #include "ipc/ipc_message.h" | 12 #include "ipc/ipc_message.h" |
12 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h" | 13 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h" |
13 #include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h" | 14 #include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h" |
14 | 15 |
15 namespace content { | 16 namespace content { |
16 | 17 |
18 namespace { | |
19 | |
20 void SendPostMessageToDocumentOnMainThread( | |
21 ThreadSafeSender* sender, | |
22 int routing_id, | |
23 int client_id, | |
24 const base::string16& message, | |
25 const std::vector<int>& message_port_ids) { | |
26 sender->Send(new ServiceWorkerHostMsg_PostMessageToDocument( | |
27 routing_id, client_id, message, message_port_ids)); | |
28 } | |
29 | |
30 } // namespace | |
31 | |
17 ServiceWorkerScriptContext::ServiceWorkerScriptContext( | 32 ServiceWorkerScriptContext::ServiceWorkerScriptContext( |
18 EmbeddedWorkerContextClient* embedded_context, | 33 EmbeddedWorkerContextClient* embedded_context, |
19 blink::WebServiceWorkerContextProxy* proxy) | 34 blink::WebServiceWorkerContextProxy* proxy) |
20 : embedded_context_(embedded_context), | 35 : embedded_context_(embedded_context), |
21 proxy_(proxy) { | 36 proxy_(proxy) { |
22 } | 37 } |
23 | 38 |
24 ServiceWorkerScriptContext::~ServiceWorkerScriptContext() {} | 39 ServiceWorkerScriptContext::~ServiceWorkerScriptContext() {} |
25 | 40 |
26 void ServiceWorkerScriptContext::OnMessageReceived( | 41 void ServiceWorkerScriptContext::OnMessageReceived( |
27 const IPC::Message& message) { | 42 const IPC::Message& message) { |
28 bool handled = true; | 43 bool handled = true; |
29 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerScriptContext, message) | 44 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerScriptContext, message) |
30 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) | 45 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) |
31 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) | 46 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) |
32 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) | 47 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) |
33 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent) | 48 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent) |
34 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_Message, OnPostMessage) | 49 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage) |
35 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClientDocuments, | 50 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClientDocuments, |
36 OnDidGetClientDocuments) | 51 OnDidGetClientDocuments) |
37 IPC_MESSAGE_UNHANDLED(handled = false) | 52 IPC_MESSAGE_UNHANDLED(handled = false) |
38 IPC_END_MESSAGE_MAP() | 53 IPC_END_MESSAGE_MAP() |
39 DCHECK(handled); | 54 DCHECK(handled); |
40 } | 55 } |
41 | 56 |
42 void ServiceWorkerScriptContext::DidHandleActivateEvent( | 57 void ServiceWorkerScriptContext::DidHandleActivateEvent( |
43 int request_id, | 58 int request_id, |
44 blink::WebServiceWorkerEventResult result) { | 59 blink::WebServiceWorkerEventResult result) { |
(...skipping 22 matching lines...) Expand all Loading... | |
67 } | 82 } |
68 | 83 |
69 void ServiceWorkerScriptContext::GetClientDocuments( | 84 void ServiceWorkerScriptContext::GetClientDocuments( |
70 blink::WebServiceWorkerClientsCallbacks* callbacks) { | 85 blink::WebServiceWorkerClientsCallbacks* callbacks) { |
71 DCHECK(callbacks); | 86 DCHECK(callbacks); |
72 int request_id = pending_clients_callbacks_.Add(callbacks); | 87 int request_id = pending_clients_callbacks_.Add(callbacks); |
73 Send(new ServiceWorkerHostMsg_GetClientDocuments( | 88 Send(new ServiceWorkerHostMsg_GetClientDocuments( |
74 GetRoutingID(), request_id)); | 89 GetRoutingID(), request_id)); |
75 } | 90 } |
76 | 91 |
92 void ServiceWorkerScriptContext::PostMessageToDocument( | |
93 int client_id, | |
94 const base::string16& message, | |
95 const std::vector<int>& message_port_ids) { | |
96 // This may send IDs for MessagePorts, and all internal book-keeping | |
97 // messages for MessagePort (e.g. QueueMessages) are sent from main thread | |
98 // (with thread hopping), so we need to do the same thread hopping here not | |
99 // to overtake those messages. | |
michaeln
2014/05/08 00:00:27
ouch, that's a subtle ordering dependency
| |
100 embedded_context_->main_thread_proxy()->PostTask( | |
101 FROM_HERE, | |
102 base::Bind(&SendPostMessageToDocumentOnMainThread, | |
103 make_scoped_refptr(embedded_context_->thread_safe_sender()), | |
104 GetRoutingID(), client_id, message, message_port_ids)); | |
105 } | |
106 | |
77 void ServiceWorkerScriptContext::Send(IPC::Message* message) { | 107 void ServiceWorkerScriptContext::Send(IPC::Message* message) { |
78 embedded_context_->Send(message); | 108 embedded_context_->Send(message); |
79 } | 109 } |
80 | 110 |
81 void ServiceWorkerScriptContext::OnActivateEvent(int request_id) { | 111 void ServiceWorkerScriptContext::OnActivateEvent(int request_id) { |
82 proxy_->dispatchActivateEvent(request_id); | 112 proxy_->dispatchActivateEvent(request_id); |
83 } | 113 } |
84 | 114 |
85 void ServiceWorkerScriptContext::OnInstallEvent(int request_id, | 115 void ServiceWorkerScriptContext::OnInstallEvent(int request_id, |
86 int active_version_id) { | 116 int active_version_id) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 info->clientIDs = client_ids; | 158 info->clientIDs = client_ids; |
129 callbacks->onSuccess(info.release()); | 159 callbacks->onSuccess(info.release()); |
130 pending_clients_callbacks_.Remove(request_id); | 160 pending_clients_callbacks_.Remove(request_id); |
131 } | 161 } |
132 | 162 |
133 int ServiceWorkerScriptContext::GetRoutingID() const { | 163 int ServiceWorkerScriptContext::GetRoutingID() const { |
134 return embedded_context_->embedded_worker_id(); | 164 return embedded_context_->embedded_worker_id(); |
135 } | 165 } |
136 | 166 |
137 } // namespace content | 167 } // namespace content |
OLD | NEW |