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

Side by Side Diff: content/child/service_worker/service_worker_dispatcher.cc

Issue 1011643002: ServiceWorker: Introduce SWMessageSender in order to mock IPC messaging (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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
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/child/service_worker/service_worker_dispatcher.h" 5 #include "content/child/service_worker/service_worker_dispatcher.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "base/threading/thread_local.h" 9 #include "base/threading/thread_local.h"
10 #include "base/trace_event/trace_event.h" 10 #include "base/trace_event/trace_event.h"
11 #include "content/child/child_thread_impl.h" 11 #include "content/child/child_thread_impl.h"
12 #include "content/child/service_worker/service_worker_handle_reference.h" 12 #include "content/child/service_worker/service_worker_handle_reference.h"
13 #include "content/child/service_worker/service_worker_message_sender.h"
13 #include "content/child/service_worker/service_worker_provider_context.h" 14 #include "content/child/service_worker/service_worker_provider_context.h"
14 #include "content/child/service_worker/service_worker_registration_handle_refere nce.h" 15 #include "content/child/service_worker/service_worker_registration_handle_refere nce.h"
15 #include "content/child/service_worker/web_service_worker_impl.h" 16 #include "content/child/service_worker/web_service_worker_impl.h"
16 #include "content/child/service_worker/web_service_worker_registration_impl.h" 17 #include "content/child/service_worker/web_service_worker_registration_impl.h"
17 #include "content/child/thread_safe_sender.h" 18 #include "content/child/thread_safe_sender.h"
18 #include "content/child/webmessageportchannel_impl.h" 19 #include "content/child/webmessageportchannel_impl.h"
19 #include "content/common/service_worker/service_worker_messages.h" 20 #include "content/common/service_worker/service_worker_messages.h"
20 #include "content/common/service_worker/service_worker_types.h" 21 #include "content/common/service_worker/service_worker_types.h"
21 #include "content/public/common/url_utils.h" 22 #include "content/public/common/url_utils.h"
22 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h" 23 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h"
(...skipping 16 matching lines...) Expand all
39 reinterpret_cast<ServiceWorkerDispatcher*>(0x1); 40 reinterpret_cast<ServiceWorkerDispatcher*>(0x1);
40 41
41 int CurrentWorkerId() { 42 int CurrentWorkerId() {
42 return WorkerTaskRunner::Instance()->CurrentWorkerId(); 43 return WorkerTaskRunner::Instance()->CurrentWorkerId();
43 } 44 }
44 45
45 } // namespace 46 } // namespace
46 47
47 ServiceWorkerDispatcher::ServiceWorkerDispatcher( 48 ServiceWorkerDispatcher::ServiceWorkerDispatcher(
48 ThreadSafeSender* thread_safe_sender) 49 ThreadSafeSender* thread_safe_sender)
49 : thread_safe_sender_(thread_safe_sender) { 50 : sender_(new ServiceWorkerMessageSender(thread_safe_sender)) {
50 g_dispatcher_tls.Pointer()->Set(this); 51 g_dispatcher_tls.Pointer()->Set(this);
51 } 52 }
52 53
54 ServiceWorkerDispatcher::ServiceWorkerDispatcher(
55 ServiceWorkerMessageSender* sender)
56 : sender_(sender) {
57 g_dispatcher_tls.Pointer()->Set(this);
58 }
59
53 ServiceWorkerDispatcher::~ServiceWorkerDispatcher() { 60 ServiceWorkerDispatcher::~ServiceWorkerDispatcher() {
54 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); 61 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted);
55 } 62 }
56 63
57 void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { 64 void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) {
58 bool handled = true; 65 bool handled = true;
59 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) 66 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg)
60 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_AssociateRegistrationWithServiceWorker, 67 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_AssociateRegistrationWithServiceWorker,
61 OnAssociateRegistrationWithServiceWorker) 68 OnAssociateRegistrationWithServiceWorker)
62 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_AssociateRegistration, 69 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_AssociateRegistration,
(...skipping 21 matching lines...) Expand all
84 OnUpdateFound) 91 OnUpdateFound)
85 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetControllerServiceWorker, 92 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetControllerServiceWorker,
86 OnSetControllerServiceWorker) 93 OnSetControllerServiceWorker)
87 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToDocument, 94 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToDocument,
88 OnPostMessage) 95 OnPostMessage)
89 IPC_MESSAGE_UNHANDLED(handled = false) 96 IPC_MESSAGE_UNHANDLED(handled = false)
90 IPC_END_MESSAGE_MAP() 97 IPC_END_MESSAGE_MAP()
91 DCHECK(handled) << "Unhandled message:" << msg.type(); 98 DCHECK(handled) << "Unhandled message:" << msg.type();
92 } 99 }
93 100
94 bool ServiceWorkerDispatcher::Send(IPC::Message* msg) {
95 return thread_safe_sender_->Send(msg);
96 }
97
98 void ServiceWorkerDispatcher::RegisterServiceWorker( 101 void ServiceWorkerDispatcher::RegisterServiceWorker(
99 int provider_id, 102 int provider_id,
100 const GURL& pattern, 103 const GURL& pattern,
101 const GURL& script_url, 104 const GURL& script_url,
102 WebServiceWorkerRegistrationCallbacks* callbacks) { 105 WebServiceWorkerRegistrationCallbacks* callbacks) {
103 DCHECK(callbacks); 106 DCHECK(callbacks);
104 107
105 if (pattern.possibly_invalid_spec().size() > GetMaxURLChars() || 108 if (pattern.possibly_invalid_spec().size() > GetMaxURLChars() ||
106 script_url.possibly_invalid_spec().size() > GetMaxURLChars()) { 109 script_url.possibly_invalid_spec().size() > GetMaxURLChars()) {
107 scoped_ptr<WebServiceWorkerRegistrationCallbacks> 110 scoped_ptr<WebServiceWorkerRegistrationCallbacks>
108 owned_callbacks(callbacks); 111 owned_callbacks(callbacks);
109 std::string error_message(kServiceWorkerRegisterErrorPrefix); 112 std::string error_message(kServiceWorkerRegisterErrorPrefix);
110 error_message += "The provided scriptURL or scope is too long."; 113 error_message += "The provided scriptURL or scope is too long.";
111 scoped_ptr<WebServiceWorkerError> error( 114 scoped_ptr<WebServiceWorkerError> error(
112 new WebServiceWorkerError(WebServiceWorkerError::ErrorTypeSecurity, 115 new WebServiceWorkerError(WebServiceWorkerError::ErrorTypeSecurity,
113 blink::WebString::fromUTF8(error_message))); 116 blink::WebString::fromUTF8(error_message)));
114 callbacks->onError(error.release()); 117 callbacks->onError(error.release());
115 return; 118 return;
116 } 119 }
117 120
118 int request_id = pending_registration_callbacks_.Add(callbacks); 121 int request_id = pending_registration_callbacks_.Add(callbacks);
119 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", 122 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker",
120 "ServiceWorkerDispatcher::RegisterServiceWorker", 123 "ServiceWorkerDispatcher::RegisterServiceWorker",
121 request_id, 124 request_id,
122 "Scope", pattern.spec(), 125 "Scope", pattern.spec(),
123 "Script URL", script_url.spec()); 126 "Script URL", script_url.spec());
124 thread_safe_sender_->Send(new ServiceWorkerHostMsg_RegisterServiceWorker( 127 sender_->Send(new ServiceWorkerHostMsg_RegisterServiceWorker(
125 CurrentWorkerId(), request_id, provider_id, pattern, script_url)); 128 CurrentWorkerId(), request_id, provider_id, pattern, script_url));
126 } 129 }
127 130
128 void ServiceWorkerDispatcher::UnregisterServiceWorker( 131 void ServiceWorkerDispatcher::UnregisterServiceWorker(
129 int provider_id, 132 int provider_id,
130 const GURL& pattern, 133 const GURL& pattern,
131 WebServiceWorkerUnregistrationCallbacks* callbacks) { 134 WebServiceWorkerUnregistrationCallbacks* callbacks) {
132 DCHECK(callbacks); 135 DCHECK(callbacks);
133 136
134 if (pattern.possibly_invalid_spec().size() > GetMaxURLChars()) { 137 if (pattern.possibly_invalid_spec().size() > GetMaxURLChars()) {
135 scoped_ptr<WebServiceWorkerUnregistrationCallbacks> 138 scoped_ptr<WebServiceWorkerUnregistrationCallbacks>
136 owned_callbacks(callbacks); 139 owned_callbacks(callbacks);
137 std::string error_message(kServiceWorkerUnregisterErrorPrefix); 140 std::string error_message(kServiceWorkerUnregisterErrorPrefix);
138 error_message += "The provided scope is too long."; 141 error_message += "The provided scope is too long.";
139 scoped_ptr<WebServiceWorkerError> error( 142 scoped_ptr<WebServiceWorkerError> error(
140 new WebServiceWorkerError(WebServiceWorkerError::ErrorTypeSecurity, 143 new WebServiceWorkerError(WebServiceWorkerError::ErrorTypeSecurity,
141 blink::WebString::fromUTF8(error_message))); 144 blink::WebString::fromUTF8(error_message)));
142 callbacks->onError(error.release()); 145 callbacks->onError(error.release());
143 return; 146 return;
144 } 147 }
145 148
146 int request_id = pending_unregistration_callbacks_.Add(callbacks); 149 int request_id = pending_unregistration_callbacks_.Add(callbacks);
147 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", 150 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker",
148 "ServiceWorkerDispatcher::UnregisterServiceWorker", 151 "ServiceWorkerDispatcher::UnregisterServiceWorker",
149 request_id, 152 request_id,
150 "Scope", pattern.spec()); 153 "Scope", pattern.spec());
151 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker( 154 sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker(
152 CurrentWorkerId(), request_id, provider_id, pattern)); 155 CurrentWorkerId(), request_id, provider_id, pattern));
153 } 156 }
154 157
155 void ServiceWorkerDispatcher::GetRegistration( 158 void ServiceWorkerDispatcher::GetRegistration(
156 int provider_id, 159 int provider_id,
157 const GURL& document_url, 160 const GURL& document_url,
158 WebServiceWorkerRegistrationCallbacks* callbacks) { 161 WebServiceWorkerRegistrationCallbacks* callbacks) {
159 DCHECK(callbacks); 162 DCHECK(callbacks);
160 163
161 if (document_url.possibly_invalid_spec().size() > GetMaxURLChars()) { 164 if (document_url.possibly_invalid_spec().size() > GetMaxURLChars()) {
162 scoped_ptr<WebServiceWorkerRegistrationCallbacks> 165 scoped_ptr<WebServiceWorkerRegistrationCallbacks>
163 owned_callbacks(callbacks); 166 owned_callbacks(callbacks);
164 std::string error_message(kServiceWorkerGetRegistrationErrorPrefix); 167 std::string error_message(kServiceWorkerGetRegistrationErrorPrefix);
165 error_message += "The provided documentURL is too long."; 168 error_message += "The provided documentURL is too long.";
166 scoped_ptr<WebServiceWorkerError> error( 169 scoped_ptr<WebServiceWorkerError> error(
167 new WebServiceWorkerError(WebServiceWorkerError::ErrorTypeSecurity, 170 new WebServiceWorkerError(WebServiceWorkerError::ErrorTypeSecurity,
168 blink::WebString::fromUTF8(error_message))); 171 blink::WebString::fromUTF8(error_message)));
169 callbacks->onError(error.release()); 172 callbacks->onError(error.release());
170 return; 173 return;
171 } 174 }
172 175
173 int request_id = pending_get_registration_callbacks_.Add(callbacks); 176 int request_id = pending_get_registration_callbacks_.Add(callbacks);
174 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", 177 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker",
175 "ServiceWorkerDispatcher::GetRegistration", 178 "ServiceWorkerDispatcher::GetRegistration",
176 request_id, 179 request_id,
177 "Document URL", document_url.spec()); 180 "Document URL", document_url.spec());
178 thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetRegistration( 181 sender_->Send(new ServiceWorkerHostMsg_GetRegistration(
179 CurrentWorkerId(), request_id, provider_id, document_url)); 182 CurrentWorkerId(), request_id, provider_id, document_url));
180 } 183 }
181 184
182 void ServiceWorkerDispatcher::GetRegistrationForReady( 185 void ServiceWorkerDispatcher::GetRegistrationForReady(
183 int provider_id, 186 int provider_id,
184 WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks) { 187 WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks) {
185 int request_id = get_for_ready_callbacks_.Add(callbacks); 188 int request_id = get_for_ready_callbacks_.Add(callbacks);
186 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker", 189 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker",
187 "ServiceWorkerDispatcher::GetRegistrationForReady", 190 "ServiceWorkerDispatcher::GetRegistrationForReady",
188 request_id); 191 request_id);
189 thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetRegistrationForReady( 192 sender_->Send(new ServiceWorkerHostMsg_GetRegistrationForReady(
190 CurrentWorkerId(), request_id, provider_id)); 193 CurrentWorkerId(), request_id, provider_id));
191 } 194 }
192 195
193 void ServiceWorkerDispatcher::AddProviderContext( 196 void ServiceWorkerDispatcher::AddProviderContext(
194 ServiceWorkerProviderContext* provider_context) { 197 ServiceWorkerProviderContext* provider_context) {
195 DCHECK(provider_context); 198 DCHECK(provider_context);
196 int provider_id = provider_context->provider_id(); 199 int provider_id = provider_context->provider_id();
197 DCHECK(!ContainsKey(provider_contexts_, provider_id)); 200 DCHECK(!ContainsKey(provider_contexts_, provider_id));
198 provider_contexts_[provider_id] = provider_context; 201 provider_contexts_[provider_id] = provider_context;
199 } 202 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 if (info.handle_id == kInvalidServiceWorkerHandleId) 259 if (info.handle_id == kInvalidServiceWorkerHandleId)
257 return NULL; 260 return NULL;
258 261
259 WorkerObjectMap::iterator existing_worker = 262 WorkerObjectMap::iterator existing_worker =
260 service_workers_.find(info.handle_id); 263 service_workers_.find(info.handle_id);
261 264
262 if (existing_worker != service_workers_.end()) { 265 if (existing_worker != service_workers_.end()) {
263 if (adopt_handle) { 266 if (adopt_handle) {
264 // We are instructed to adopt a handle but we already have one, so 267 // We are instructed to adopt a handle but we already have one, so
265 // adopt and destroy a handle ref. 268 // adopt and destroy a handle ref.
266 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); 269 ServiceWorkerHandleReference::Adopt(info, sender_.get());
267 } 270 }
268 return existing_worker->second; 271 return existing_worker->second;
269 } 272 }
270 273
271 scoped_ptr<ServiceWorkerHandleReference> handle_ref = 274 scoped_ptr<ServiceWorkerHandleReference> handle_ref =
272 adopt_handle 275 adopt_handle
273 ? ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()) 276 ? ServiceWorkerHandleReference::Adopt(info, sender_.get())
274 : ServiceWorkerHandleReference::Create(info, 277 : ServiceWorkerHandleReference::Create(info, sender_.get());
275 thread_safe_sender_.get());
276 // WebServiceWorkerImpl constructor calls AddServiceWorker. 278 // WebServiceWorkerImpl constructor calls AddServiceWorker.
277 return new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_.get()); 279 return new WebServiceWorkerImpl(handle_ref.Pass(), sender_.get());
278 } 280 }
279 281
280 WebServiceWorkerRegistrationImpl* 282 WebServiceWorkerRegistrationImpl*
281 ServiceWorkerDispatcher::FindServiceWorkerRegistration( 283 ServiceWorkerDispatcher::FindServiceWorkerRegistration(
282 const ServiceWorkerRegistrationObjectInfo& info, 284 const ServiceWorkerRegistrationObjectInfo& info,
283 bool adopt_handle) { 285 bool adopt_handle) {
284 RegistrationObjectMap::iterator registration = 286 RegistrationObjectMap::iterator registration =
285 registrations_.find(info.handle_id); 287 registrations_.find(info.handle_id);
286 if (registration == registrations_.end()) 288 if (registration == registrations_.end())
287 return NULL; 289 return NULL;
288 if (adopt_handle) { 290 if (adopt_handle) {
289 // We are instructed to adopt a handle but we already have one, so 291 // We are instructed to adopt a handle but we already have one, so
290 // adopt and destroy a handle ref. 292 // adopt and destroy a handle ref.
291 ServiceWorkerRegistrationHandleReference::Adopt( 293 ServiceWorkerRegistrationHandleReference::Adopt(info, sender_.get());
292 info, thread_safe_sender_.get());
293 } 294 }
294 return registration->second; 295 return registration->second;
295 } 296 }
296 297
297 WebServiceWorkerRegistrationImpl* 298 WebServiceWorkerRegistrationImpl*
298 ServiceWorkerDispatcher::CreateServiceWorkerRegistration( 299 ServiceWorkerDispatcher::CreateServiceWorkerRegistration(
299 const ServiceWorkerRegistrationObjectInfo& info, 300 const ServiceWorkerRegistrationObjectInfo& info,
300 bool adopt_handle) { 301 bool adopt_handle) {
301 DCHECK(!FindServiceWorkerRegistration(info, adopt_handle)); 302 DCHECK(!FindServiceWorkerRegistration(info, adopt_handle));
302 if (info.handle_id == kInvalidServiceWorkerRegistrationHandleId) 303 if (info.handle_id == kInvalidServiceWorkerRegistrationHandleId)
303 return NULL; 304 return NULL;
304 305
305 scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref = 306 scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref =
306 adopt_handle ? ServiceWorkerRegistrationHandleReference::Adopt( 307 adopt_handle ? ServiceWorkerRegistrationHandleReference::Adopt(
307 info, thread_safe_sender_.get()) 308 info, sender_.get())
308 : ServiceWorkerRegistrationHandleReference::Create( 309 : ServiceWorkerRegistrationHandleReference::Create(
309 info, thread_safe_sender_.get()); 310 info, sender_.get());
310 311
311 // WebServiceWorkerRegistrationImpl constructor calls 312 // WebServiceWorkerRegistrationImpl constructor calls
312 // AddServiceWorkerRegistration. 313 // AddServiceWorkerRegistration.
313 return new WebServiceWorkerRegistrationImpl(handle_ref.Pass()); 314 return new WebServiceWorkerRegistrationImpl(handle_ref.Pass());
314 } 315 }
315 316
316 // We can assume that this message handler is called before the worker context 317 // We can assume that this message handler is called before the worker context
317 // starts because script loading happens after this association. 318 // starts because script loading happens after this association.
318 // TODO(nhiroki): This association information could be pushed into 319 // TODO(nhiroki): This association information could be pushed into
319 // EmbeddedWorkerMsg_StartWorker message and handed over to the worker thread 320 // EmbeddedWorkerMsg_StartWorker message and handed over to the worker thread
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
691 WebServiceWorkerRegistrationImpl* registration = 692 WebServiceWorkerRegistrationImpl* registration =
692 FindServiceWorkerRegistration(info, true); 693 FindServiceWorkerRegistration(info, true);
693 if (!registration) { 694 if (!registration) {
694 registration = CreateServiceWorkerRegistration(info, true); 695 registration = CreateServiceWorkerRegistration(info, true);
695 registration->SetInstalling(GetServiceWorker(attrs.installing, true)); 696 registration->SetInstalling(GetServiceWorker(attrs.installing, true));
696 registration->SetWaiting(GetServiceWorker(attrs.waiting, true)); 697 registration->SetWaiting(GetServiceWorker(attrs.waiting, true));
697 registration->SetActive(GetServiceWorker(attrs.active, true)); 698 registration->SetActive(GetServiceWorker(attrs.active, true));
698 } else { 699 } else {
699 // |registration| must already have version attributes, so adopt and destroy 700 // |registration| must already have version attributes, so adopt and destroy
700 // handle refs for them. 701 // handle refs for them.
701 ServiceWorkerHandleReference::Adopt( 702 ServiceWorkerHandleReference::Adopt(attrs.installing, sender_.get());
702 attrs.installing, thread_safe_sender_.get()); 703 ServiceWorkerHandleReference::Adopt(attrs.waiting, sender_.get());
703 ServiceWorkerHandleReference::Adopt( 704 ServiceWorkerHandleReference::Adopt(attrs.active, sender_.get());
704 attrs.waiting, thread_safe_sender_.get());
705 ServiceWorkerHandleReference::Adopt(
706 attrs.active, thread_safe_sender_.get());
707 } 705 }
708 return registration; 706 return registration;
709 } 707 }
710 708
711 } // namespace content 709 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698