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

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

Issue 189253002: Implement ServiceWorker::postMessage() [Chromium] (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review feedback Created 6 years, 9 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/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "content/browser/message_port_message_filter.h"
9 #include "content/browser/message_port_service.h"
8 #include "content/browser/service_worker/embedded_worker_registry.h" 10 #include "content/browser/service_worker/embedded_worker_registry.h"
9 #include "content/browser/service_worker/service_worker_context_core.h" 11 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_context_wrapper.h" 12 #include "content/browser/service_worker/service_worker_context_wrapper.h"
11 #include "content/browser/service_worker/service_worker_provider_host.h" 13 #include "content/browser/service_worker/service_worker_registration.h"
12 #include "content/browser/service_worker/service_worker_utils.h" 14 #include "content/browser/service_worker/service_worker_utils.h"
13 #include "content/common/service_worker/embedded_worker_messages.h" 15 #include "content/common/service_worker/embedded_worker_messages.h"
14 #include "content/common/service_worker/service_worker_messages.h" 16 #include "content/common/service_worker/service_worker_messages.h"
15 #include "ipc/ipc_message_macros.h" 17 #include "ipc/ipc_message_macros.h"
16 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" 18 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h"
17 #include "url/gurl.h" 19 #include "url/gurl.h"
18 20
19 using blink::WebServiceWorkerError; 21 using blink::WebServiceWorkerError;
20 22
21 namespace { 23 namespace {
22 24
23 const char kDisabledErrorMessage[] = 25 const char kDisabledErrorMessage[] =
24 "ServiceWorker is disabled"; 26 "ServiceWorker is disabled";
25 const char kDomainMismatchErrorMessage[] = 27 const char kDomainMismatchErrorMessage[] =
26 "Scope and scripts do not have the same origin"; 28 "Scope and scripts do not have the same origin";
27 29
28 const uint32 kFilteredMessageClasses[] = { 30 const uint32 kFilteredMessageClasses[] = {
29 ServiceWorkerMsgStart, 31 ServiceWorkerMsgStart,
30 EmbeddedWorkerMsgStart, 32 EmbeddedWorkerMsgStart,
31 }; 33 };
32 34
33 } // namespace 35 } // namespace
34 36
35 namespace content { 37 namespace content {
36 38
37 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( 39 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost(
38 int render_process_id) 40 int render_process_id,
39 : BrowserMessageFilter( 41 MessagePortMessageFilter* message_port_message_filter)
40 kFilteredMessageClasses, arraysize(kFilteredMessageClasses)), 42 : BrowserMessageFilter(kFilteredMessageClasses,
41 render_process_id_(render_process_id) { 43 arraysize(kFilteredMessageClasses)),
44 render_process_id_(render_process_id),
45 message_port_message_filter_(message_port_message_filter) {
42 } 46 }
43 47
44 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { 48 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() {
45 if (context_) { 49 if (context_) {
46 context_->RemoveAllProviderHostsForProcess(render_process_id_); 50 context_->RemoveAllProviderHostsForProcess(render_process_id_);
47 context_->embedded_worker_registry()->RemoveChildProcessSender( 51 context_->embedded_worker_registry()->RemoveChildProcessSender(
48 render_process_id_); 52 render_process_id_);
49 } 53 }
50 } 54 }
51 55
(...skipping 26 matching lines...) Expand all
78 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, 82 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker,
79 OnUnregisterServiceWorker) 83 OnUnregisterServiceWorker)
80 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, 84 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated,
81 OnProviderCreated) 85 OnProviderCreated)
82 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, 86 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed,
83 OnProviderDestroyed) 87 OnProviderDestroyed)
84 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_AddScriptClient, 88 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_AddScriptClient,
85 OnAddScriptClient) 89 OnAddScriptClient)
86 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RemoveScriptClient, 90 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RemoveScriptClient,
87 OnRemoveScriptClient) 91 OnRemoveScriptClient)
92 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessage,
93 OnPostMessage)
88 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStarted, 94 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStarted,
89 OnWorkerStarted) 95 OnWorkerStarted)
90 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStopped, 96 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStopped,
91 OnWorkerStopped) 97 OnWorkerStopped)
92 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_SendMessageToBrowser, 98 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_SendMessageToBrowser,
93 OnSendMessageToBrowser) 99 OnSendMessageToBrowser)
94 IPC_MESSAGE_UNHANDLED(handled = false) 100 IPC_MESSAGE_UNHANDLED(handled = false)
95 IPC_END_MESSAGE_MAP() 101 IPC_END_MESSAGE_MAP()
96 102
97 return handled; 103 return handled;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 157
152 context_->UnregisterServiceWorker( 158 context_->UnregisterServiceWorker(
153 pattern, 159 pattern,
154 render_process_id_, 160 render_process_id_,
155 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, 161 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete,
156 this, 162 this,
157 thread_id, 163 thread_id,
158 request_id)); 164 request_id));
159 } 165 }
160 166
167 void ServiceWorkerDispatcherHost::OnPostMessage(
168 int64 registration_id,
169 const base::string16& message,
170 const std::vector<int>& sent_message_port_ids) {
171 if (!context_ || !ServiceWorkerUtils::IsFeatureEnabled())
172 return;
173
174 std::vector<int> new_routing_ids(sent_message_port_ids.size());
175 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) {
176 new_routing_ids[i] = message_port_message_filter_->GetNextRoutingID();
177 MessagePortService::GetInstance()->UpdateMessagePort(
178 sent_message_port_ids[i],
179 message_port_message_filter_,
180 new_routing_ids[i]);
181 }
182
183 context_->storage()->FindRegistrationForId(
184 registration_id,
185 base::Bind(&ServiceWorkerDispatcherHost::PostMessageFoundRegistration,
186 message,
187 sent_message_port_ids,
188 new_routing_ids));
189 }
190
191 namespace {
192 void NoOpStatusCallback(ServiceWorkerStatusCode status) {}
193 } // namespace
194
195 // static
196 void ServiceWorkerDispatcherHost::PostMessageFoundRegistration(
197 const base::string16& message,
198 const std::vector<int>& sent_message_port_ids,
199 const std::vector<int>& new_routing_ids,
200 ServiceWorkerStatusCode status,
201 const scoped_refptr<ServiceWorkerRegistration>& result) {
202 if (status != SERVICE_WORKER_OK)
203 return;
204 DCHECK(result);
205
206 // TODO(jsbell): Route message to appropriate version. crbug.com/351797
207 ServiceWorkerVersion* version = result->active_version();
208 if (!version)
209 return;
210 version->SendMessage(
211 ServiceWorkerMsg_Message(message, sent_message_port_ids, new_routing_ids),
212 base::Bind(&NoOpStatusCallback));
213 }
214
161 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) { 215 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) {
162 if (!context_) 216 if (!context_)
163 return; 217 return;
164 if (context_->GetProviderHost(render_process_id_, provider_id)) { 218 if (context_->GetProviderHost(render_process_id_, provider_id)) {
165 BadMessageReceived(); 219 BadMessageReceived();
166 return; 220 return;
167 } 221 }
168 scoped_ptr<ServiceWorkerProviderHost> provider_host( 222 scoped_ptr<ServiceWorkerProviderHost> provider_host(
169 new ServiceWorkerProviderHost(render_process_id_, provider_id)); 223 new ServiceWorkerProviderHost(render_process_id_, provider_id));
170 context_->AddProviderHost(provider_host.Pass()); 224 context_->AddProviderHost(provider_host.Pass());
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 ServiceWorkerStatusCode status) { 313 ServiceWorkerStatusCode status) {
260 base::string16 error_message; 314 base::string16 error_message;
261 blink::WebServiceWorkerError::ErrorType error_type; 315 blink::WebServiceWorkerError::ErrorType error_type;
262 GetServiceWorkerRegistrationStatusResponse( 316 GetServiceWorkerRegistrationStatusResponse(
263 status, &error_type, &error_message); 317 status, &error_type, &error_message);
264 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( 318 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError(
265 thread_id, request_id, error_type, error_message)); 319 thread_id, request_id, error_type, error_message));
266 } 320 }
267 321
268 } // namespace content 322 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698