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/thread_safe_sender.h" |
9 #include "content/child/webmessageportchannel_impl.h" | 9 #include "content/child/webmessageportchannel_impl.h" |
10 #include "content/common/service_worker/service_worker_messages.h" | 10 #include "content/common/service_worker/service_worker_messages.h" |
11 #include "content/renderer/service_worker/embedded_worker_context_client.h" | 11 #include "content/renderer/service_worker/embedded_worker_context_client.h" |
12 #include "ipc/ipc_message.h" | 12 #include "ipc/ipc_message.h" |
13 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h" | 13 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h" |
14 #include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h" | 14 #include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h" |
| 15 #include "third_party/WebKit/public/web/WebServiceWorkerPushEventCallback.h" |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 void SendPostMessageToDocumentOnMainThread( | 21 void SendPostMessageToDocumentOnMainThread( |
21 ThreadSafeSender* sender, | 22 ThreadSafeSender* sender, |
22 int routing_id, | 23 int routing_id, |
23 int client_id, | 24 int client_id, |
24 const base::string16& message, | 25 const base::string16& message, |
25 scoped_ptr<blink::WebMessagePortChannelArray> channels) { | 26 scoped_ptr<blink::WebMessagePortChannelArray> channels) { |
26 sender->Send(new ServiceWorkerHostMsg_PostMessageToDocument( | 27 sender->Send(new ServiceWorkerHostMsg_PostMessageToDocument( |
27 routing_id, client_id, message, | 28 routing_id, client_id, message, |
28 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels.release()))); | 29 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels.release()))); |
29 } | 30 } |
30 | 31 |
| 32 class PushEventCallback : public blink::WebServiceWorkerPushEventCallback { |
| 33 public: |
| 34 PushEventCallback(ServiceWorkerScriptContext* context) |
| 35 : context_(context) { |
| 36 } |
| 37 |
| 38 virtual void didHandlePushEvent(int eventID) const OVERRIDE { |
| 39 context_->DidHandlePushEvent(eventID); |
| 40 } |
| 41 |
| 42 private: |
| 43 ServiceWorkerScriptContext* context_; |
| 44 }; |
| 45 |
31 } // namespace | 46 } // namespace |
32 | 47 |
33 ServiceWorkerScriptContext::ServiceWorkerScriptContext( | 48 ServiceWorkerScriptContext::ServiceWorkerScriptContext( |
34 EmbeddedWorkerContextClient* embedded_context, | 49 EmbeddedWorkerContextClient* embedded_context, |
35 blink::WebServiceWorkerContextProxy* proxy) | 50 blink::WebServiceWorkerContextProxy* proxy) |
36 : embedded_context_(embedded_context), | 51 : embedded_context_(embedded_context), |
37 proxy_(proxy) { | 52 proxy_(proxy) { |
38 } | 53 } |
39 | 54 |
40 ServiceWorkerScriptContext::~ServiceWorkerScriptContext() {} | 55 ServiceWorkerScriptContext::~ServiceWorkerScriptContext() {} |
41 | 56 |
42 void ServiceWorkerScriptContext::OnMessageReceived( | 57 void ServiceWorkerScriptContext::OnMessageReceived( |
43 const IPC::Message& message) { | 58 const IPC::Message& message) { |
44 bool handled = true; | 59 bool handled = true; |
45 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerScriptContext, message) | 60 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerScriptContext, message) |
46 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) | 61 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) |
47 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) | 62 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) |
48 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) | 63 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) |
49 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent) | 64 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent) |
| 65 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) |
50 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage) | 66 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage) |
51 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClientDocuments, | 67 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClientDocuments, |
52 OnDidGetClientDocuments) | 68 OnDidGetClientDocuments) |
53 IPC_MESSAGE_UNHANDLED(handled = false) | 69 IPC_MESSAGE_UNHANDLED(handled = false) |
54 IPC_END_MESSAGE_MAP() | 70 IPC_END_MESSAGE_MAP() |
55 DCHECK(handled); | 71 DCHECK(handled); |
56 } | 72 } |
57 | 73 |
58 void ServiceWorkerScriptContext::DidHandleActivateEvent( | 74 void ServiceWorkerScriptContext::DidHandleActivateEvent( |
59 int request_id, | 75 int request_id, |
(...skipping 15 matching lines...) Expand all Loading... |
75 const ServiceWorkerResponse& response) { | 91 const ServiceWorkerResponse& response) { |
76 Send(new ServiceWorkerHostMsg_FetchEventFinished( | 92 Send(new ServiceWorkerHostMsg_FetchEventFinished( |
77 GetRoutingID(), request_id, result, response)); | 93 GetRoutingID(), request_id, result, response)); |
78 } | 94 } |
79 | 95 |
80 void ServiceWorkerScriptContext::DidHandleSyncEvent(int request_id) { | 96 void ServiceWorkerScriptContext::DidHandleSyncEvent(int request_id) { |
81 Send(new ServiceWorkerHostMsg_SyncEventFinished( | 97 Send(new ServiceWorkerHostMsg_SyncEventFinished( |
82 GetRoutingID(), request_id)); | 98 GetRoutingID(), request_id)); |
83 } | 99 } |
84 | 100 |
| 101 void ServiceWorkerScriptContext::DidHandlePushEvent(int request_id) { |
| 102 Send(new ServiceWorkerHostMsg_PushEventFinished( |
| 103 GetRoutingID(), request_id)); |
| 104 } |
| 105 |
85 void ServiceWorkerScriptContext::GetClientDocuments( | 106 void ServiceWorkerScriptContext::GetClientDocuments( |
86 blink::WebServiceWorkerClientsCallbacks* callbacks) { | 107 blink::WebServiceWorkerClientsCallbacks* callbacks) { |
87 DCHECK(callbacks); | 108 DCHECK(callbacks); |
88 int request_id = pending_clients_callbacks_.Add(callbacks); | 109 int request_id = pending_clients_callbacks_.Add(callbacks); |
89 Send(new ServiceWorkerHostMsg_GetClientDocuments( | 110 Send(new ServiceWorkerHostMsg_GetClientDocuments( |
90 GetRoutingID(), request_id)); | 111 GetRoutingID(), request_id)); |
91 } | 112 } |
92 | 113 |
93 void ServiceWorkerScriptContext::PostMessageToDocument( | 114 void ServiceWorkerScriptContext::PostMessageToDocument( |
94 int client_id, | 115 int client_id, |
(...skipping 27 matching lines...) Expand all Loading... |
122 int request_id, | 143 int request_id, |
123 const ServiceWorkerFetchRequest& request) { | 144 const ServiceWorkerFetchRequest& request) { |
124 // TODO(falken): Pass in the request. | 145 // TODO(falken): Pass in the request. |
125 proxy_->dispatchFetchEvent(request_id); | 146 proxy_->dispatchFetchEvent(request_id); |
126 } | 147 } |
127 | 148 |
128 void ServiceWorkerScriptContext::OnSyncEvent(int request_id) { | 149 void ServiceWorkerScriptContext::OnSyncEvent(int request_id) { |
129 proxy_->dispatchSyncEvent(request_id); | 150 proxy_->dispatchSyncEvent(request_id); |
130 } | 151 } |
131 | 152 |
| 153 void ServiceWorkerScriptContext::OnPushEvent(int request_id) { |
| 154 PushEventCallback callback(this); |
| 155 proxy_->dispatchPushEvent(request_id, "ServiceWorkerScriptContext", callback); |
| 156 } |
| 157 |
132 void ServiceWorkerScriptContext::OnPostMessage( | 158 void ServiceWorkerScriptContext::OnPostMessage( |
133 const base::string16& message, | 159 const base::string16& message, |
134 const std::vector<int>& sent_message_port_ids, | 160 const std::vector<int>& sent_message_port_ids, |
135 const std::vector<int>& new_routing_ids) { | 161 const std::vector<int>& new_routing_ids) { |
136 std::vector<WebMessagePortChannelImpl*> ports; | 162 std::vector<WebMessagePortChannelImpl*> ports; |
137 if (!sent_message_port_ids.empty()) { | 163 if (!sent_message_port_ids.empty()) { |
138 base::MessageLoopProxy* loop_proxy = embedded_context_->main_thread_proxy(); | 164 base::MessageLoopProxy* loop_proxy = embedded_context_->main_thread_proxy(); |
139 ports.resize(sent_message_port_ids.size()); | 165 ports.resize(sent_message_port_ids.size()); |
140 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { | 166 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { |
141 ports[i] = new WebMessagePortChannelImpl( | 167 ports[i] = new WebMessagePortChannelImpl( |
(...skipping 17 matching lines...) Expand all Loading... |
159 info->clientIDs = client_ids; | 185 info->clientIDs = client_ids; |
160 callbacks->onSuccess(info.release()); | 186 callbacks->onSuccess(info.release()); |
161 pending_clients_callbacks_.Remove(request_id); | 187 pending_clients_callbacks_.Remove(request_id); |
162 } | 188 } |
163 | 189 |
164 int ServiceWorkerScriptContext::GetRoutingID() const { | 190 int ServiceWorkerScriptContext::GetRoutingID() const { |
165 return embedded_context_->embedded_worker_id(); | 191 return embedded_context_->embedded_worker_id(); |
166 } | 192 } |
167 | 193 |
168 } // namespace content | 194 } // namespace content |
OLD | NEW |