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
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_dispatcher_host.h"
7 #include "base/debug/trace_event.h"
8 #include "base/logging.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/browser/message_port_message_filter.h"
11 #include "content/browser/message_port_service.h"
12 #include "content/browser/service_worker/embedded_worker_registry.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/browser/service_worker/service_worker_context_wrapper.h"
15 #include "content/browser/service_worker/service_worker_handle.h"
16 #include "content/browser/service_worker/service_worker_registration.h"
17 #include "content/browser/service_worker/service_worker_registration_handle.h"
18 #include "content/browser/service_worker/service_worker_utils.h"
19 #include "content/common/service_worker/embedded_worker_messages.h"
20 #include "content/common/service_worker/service_worker_messages.h"
21 #include "content/public/browser/content_browser_client.h"
22 #include "content/public/common/content_client.h"
23 #include "ipc/ipc_message_macros.h"
24 #include "net/base/net_util.h"
25 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h"
28 using blink::WebServiceWorkerError;
34 const char kShutdownErrorMessage[] =
35 "The Service Worker system has shutdown.";
36 const char kDisabledErrorMessage[] = "The browser has disabled Service Worker.";
38 const uint32 kFilteredMessageClasses[] = {
39 ServiceWorkerMsgStart,
40 EmbeddedWorkerMsgStart,
43 bool AllOriginsMatch(const GURL& url_a, const GURL& url_b, const GURL& url_c) {
44 return url_a.GetOrigin() == url_b.GetOrigin() &&
45 url_a.GetOrigin() == url_c.GetOrigin();
48 // TODO(dominicc): When crbug.com/362214 is fixed use that to be
49 // consistent with Blink's
50 // SecurityOrigin::canAccessFeatureRequiringSecureOrigin.
51 bool OriginCanAccessServiceWorkers(const GURL& url) {
52 return url.SchemeIsSecure() || net::IsLocalhost(url.host());
55 bool CheckPatternIsUnderTheScriptDirectory(const GURL& pattern,
56 const GURL& script_url) {
57 size_t slash_pos = script_url.spec().rfind('/');
58 if (slash_pos == std::string::npos)
60 return pattern.spec().compare(
61 0, slash_pos + 1, script_url.spec(), 0, slash_pos + 1) == 0;
64 bool CanRegisterServiceWorker(const GURL& document_url,
66 const GURL& script_url) {
67 DCHECK(document_url.is_valid());
68 DCHECK(pattern.is_valid());
69 DCHECK(script_url.is_valid());
70 return AllOriginsMatch(document_url, pattern, script_url) &&
71 OriginCanAccessServiceWorkers(document_url) &&
72 CheckPatternIsUnderTheScriptDirectory(pattern, script_url);
75 bool CanUnregisterServiceWorker(const GURL& document_url,
76 const GURL& pattern) {
77 DCHECK(document_url.is_valid());
78 DCHECK(pattern.is_valid());
79 return document_url.GetOrigin() == pattern.GetOrigin() &&
80 OriginCanAccessServiceWorkers(document_url);
83 bool CanGetRegistration(const GURL& document_url,
84 const GURL& given_document_url) {
85 DCHECK(document_url.is_valid());
86 DCHECK(given_document_url.is_valid());
87 return document_url.GetOrigin() == given_document_url.GetOrigin() &&
88 OriginCanAccessServiceWorkers(document_url);
93 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost(
94 int render_process_id,
95 MessagePortMessageFilter* message_port_message_filter,
96 ResourceContext* resource_context)
97 : BrowserMessageFilter(kFilteredMessageClasses,
98 arraysize(kFilteredMessageClasses)),
99 render_process_id_(render_process_id),
100 message_port_message_filter_(message_port_message_filter),
101 resource_context_(resource_context),
102 channel_ready_(false) {
105 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() {
107 GetContext()->RemoveAllProviderHostsForProcess(render_process_id_);
108 GetContext()->embedded_worker_registry()->RemoveChildProcessSender(
113 void ServiceWorkerDispatcherHost::Init(
114 ServiceWorkerContextWrapper* context_wrapper) {
115 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
116 BrowserThread::PostTask(
117 BrowserThread::IO, FROM_HERE,
118 base::Bind(&ServiceWorkerDispatcherHost::Init,
119 this, make_scoped_refptr(context_wrapper)));
123 context_wrapper_ = context_wrapper;
124 GetContext()->embedded_worker_registry()->AddChildProcessSender(
125 render_process_id_, this);
128 void ServiceWorkerDispatcherHost::OnFilterAdded(IPC::Sender* sender) {
129 TRACE_EVENT0("ServiceWorker",
130 "ServiceWorkerDispatcherHost::OnFilterAdded");
131 channel_ready_ = true;
132 std::vector<IPC::Message*> messages;
133 pending_messages_.release(&messages);
134 for (size_t i = 0; i < messages.size(); ++i) {
135 BrowserMessageFilter::Send(messages[i]);
139 void ServiceWorkerDispatcherHost::OnFilterRemoved() {
140 // Don't wait until the destructor to teardown since a new dispatcher host
141 // for this process might be created before then.
143 GetContext()->RemoveAllProviderHostsForProcess(render_process_id_);
144 GetContext()->embedded_worker_registry()->RemoveChildProcessSender(
147 context_wrapper_ = nullptr;
148 channel_ready_ = false;
151 void ServiceWorkerDispatcherHost::OnDestruct() const {
152 BrowserThread::DeleteOnIOThread::Destruct(this);
155 bool ServiceWorkerDispatcherHost::OnMessageReceived(
156 const IPC::Message& message) {
158 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message)
159 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker,
160 OnRegisterServiceWorker)
161 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker,
162 OnUnregisterServiceWorker)
163 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration,
165 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated,
167 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed,
169 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetVersionId,
170 OnSetHostedVersionId)
171 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToWorker,
172 OnPostMessageToWorker)
173 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerReadyForInspection,
174 OnWorkerReadyForInspection)
175 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerScriptLoaded,
176 OnWorkerScriptLoaded)
177 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerScriptLoadFailed,
178 OnWorkerScriptLoadFailed)
179 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerScriptEvaluated,
180 OnWorkerScriptEvaluated)
181 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStarted,
183 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStopped,
185 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_DidPauseAfterDownload,
186 OnPausedAfterDownload)
187 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_ReportException,
189 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_ReportConsoleMessage,
190 OnReportConsoleMessage)
191 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_IncrementServiceWorkerRefCount,
192 OnIncrementServiceWorkerRefCount)
193 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount,
194 OnDecrementServiceWorkerRefCount)
195 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_IncrementRegistrationRefCount,
196 OnIncrementRegistrationRefCount)
197 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_DecrementRegistrationRefCount,
198 OnDecrementRegistrationRefCount)
199 IPC_MESSAGE_UNHANDLED(handled = false)
200 IPC_END_MESSAGE_MAP()
202 if (!handled && GetContext()) {
204 GetContext()->embedded_worker_registry()->OnMessageReceived(message);
206 BadMessageReceived();
212 bool ServiceWorkerDispatcherHost::Send(IPC::Message* message) {
213 if (channel_ready_) {
214 BrowserMessageFilter::Send(message);
215 // Don't bother passing through Send()'s result: it's not reliable.
219 pending_messages_.push_back(message);
223 ServiceWorkerRegistrationHandle*
224 ServiceWorkerDispatcherHost::GetOrCreateRegistrationHandle(
226 ServiceWorkerRegistration* registration) {
227 ServiceWorkerRegistrationHandle* handle =
228 FindRegistrationHandle(provider_id, registration->id());
230 handle->IncrementRefCount();
234 scoped_ptr<ServiceWorkerRegistrationHandle> new_handle(
235 new ServiceWorkerRegistrationHandle(
236 GetContext()->AsWeakPtr(), this, provider_id, registration));
237 handle = new_handle.get();
238 RegisterServiceWorkerRegistrationHandle(new_handle.Pass());
242 void ServiceWorkerDispatcherHost::RegisterServiceWorkerHandle(
243 scoped_ptr<ServiceWorkerHandle> handle) {
244 int handle_id = handle->handle_id();
245 handles_.AddWithID(handle.release(), handle_id);
248 void ServiceWorkerDispatcherHost::RegisterServiceWorkerRegistrationHandle(
249 scoped_ptr<ServiceWorkerRegistrationHandle> handle) {
250 int handle_id = handle->handle_id();
251 registration_handles_.AddWithID(handle.release(), handle_id);
254 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker(
259 const GURL& script_url) {
260 TRACE_EVENT0("ServiceWorker",
261 "ServiceWorkerDispatcherHost::OnRegisterServiceWorker");
263 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError(
266 WebServiceWorkerError::ErrorTypeAbort,
267 base::ASCIIToUTF16(kShutdownErrorMessage)));
270 if (!pattern.is_valid() || !script_url.is_valid()) {
271 BadMessageReceived();
275 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost(
276 render_process_id_, provider_id);
277 if (!provider_host) {
278 BadMessageReceived();
281 if (!provider_host->IsContextAlive()) {
282 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError(
285 WebServiceWorkerError::ErrorTypeAbort,
286 base::ASCIIToUTF16(kShutdownErrorMessage)));
290 if (!CanRegisterServiceWorker(
291 provider_host->document_url(), pattern, script_url)) {
292 BadMessageReceived();
296 if (!GetContentClient()->browser()->AllowServiceWorker(
297 pattern, provider_host->topmost_frame_url(), resource_context_)) {
298 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError(
301 WebServiceWorkerError::ErrorTypeDisabled,
302 base::ASCIIToUTF16(kDisabledErrorMessage)));
306 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker",
307 "ServiceWorkerDispatcherHost::RegisterServiceWorker",
309 "Pattern", pattern.spec(),
310 "Script URL", script_url.spec());
311 GetContext()->RegisterServiceWorker(
315 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete,
322 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker(
326 const GURL& pattern) {
327 TRACE_EVENT0("ServiceWorker",
328 "ServiceWorkerDispatcherHost::OnUnregisterServiceWorker");
330 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError(
333 blink::WebServiceWorkerError::ErrorTypeAbort,
334 base::ASCIIToUTF16(kShutdownErrorMessage)));
337 if (!pattern.is_valid()) {
338 BadMessageReceived();
342 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost(
343 render_process_id_, provider_id);
344 if (!provider_host) {
345 BadMessageReceived();
348 if (!provider_host->IsContextAlive()) {
349 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError(
352 blink::WebServiceWorkerError::ErrorTypeAbort,
353 base::ASCIIToUTF16(kShutdownErrorMessage)));
357 if (!CanUnregisterServiceWorker(provider_host->document_url(), pattern)) {
358 BadMessageReceived();
362 if (!GetContentClient()->browser()->AllowServiceWorker(
363 pattern, provider_host->topmost_frame_url(), resource_context_)) {
364 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError(
367 WebServiceWorkerError::ErrorTypeDisabled,
368 base::ASCIIToUTF16(kDisabledErrorMessage)));
372 TRACE_EVENT_ASYNC_BEGIN1(
374 "ServiceWorkerDispatcherHost::UnregisterServiceWorker",
376 "Pattern", pattern.spec());
377 GetContext()->UnregisterServiceWorker(
379 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete,
385 void ServiceWorkerDispatcherHost::OnGetRegistration(
389 const GURL& document_url) {
390 TRACE_EVENT0("ServiceWorker",
391 "ServiceWorkerDispatcherHost::OnGetRegistration");
393 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError(
396 blink::WebServiceWorkerError::ErrorTypeAbort,
397 base::ASCIIToUTF16(kShutdownErrorMessage)));
400 if (!document_url.is_valid()) {
401 BadMessageReceived();
405 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost(
406 render_process_id_, provider_id);
407 if (!provider_host) {
408 BadMessageReceived();
411 if (!provider_host->IsContextAlive()) {
412 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError(
415 blink::WebServiceWorkerError::ErrorTypeAbort,
416 base::ASCIIToUTF16(kShutdownErrorMessage)));
420 if (!CanGetRegistration(provider_host->document_url(), document_url)) {
421 BadMessageReceived();
425 if (!GetContentClient()->browser()->AllowServiceWorker(
426 provider_host->document_url(),
427 provider_host->topmost_frame_url(),
428 resource_context_)) {
429 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError(
432 WebServiceWorkerError::ErrorTypeDisabled,
433 base::ASCIIToUTF16(kDisabledErrorMessage)));
437 DCHECK_CURRENTLY_ON(BrowserThread::IO);
438 if (GetContext()->storage()->IsDisabled()) {
439 SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT);
443 TRACE_EVENT_ASYNC_BEGIN1(
445 "ServiceWorkerDispatcherHost::GetRegistration",
447 "Document URL", document_url.spec());
449 GetContext()->storage()->FindRegistrationForDocument(
451 base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationComplete,
458 void ServiceWorkerDispatcherHost::OnPostMessageToWorker(
460 const base::string16& message,
461 const std::vector<int>& sent_message_port_ids) {
462 TRACE_EVENT0("ServiceWorker",
463 "ServiceWorkerDispatcherHost::OnPostMessageToWorker");
467 ServiceWorkerHandle* handle = handles_.Lookup(handle_id);
469 BadMessageReceived();
473 std::vector<int> new_routing_ids;
474 message_port_message_filter_->UpdateMessagePortsWithNewRoutes(
475 sent_message_port_ids, &new_routing_ids);
476 handle->version()->SendMessage(
477 ServiceWorkerMsg_MessageToWorker(message,
478 sent_message_port_ids,
480 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
483 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) {
484 TRACE_EVENT0("ServiceWorker",
485 "ServiceWorkerDispatcherHost::OnProviderCreated");
488 if (GetContext()->GetProviderHost(render_process_id_, provider_id)) {
489 BadMessageReceived();
492 scoped_ptr<ServiceWorkerProviderHost> provider_host(
493 new ServiceWorkerProviderHost(
494 render_process_id_, provider_id, GetContext()->AsWeakPtr(), this));
495 GetContext()->AddProviderHost(provider_host.Pass());
498 void ServiceWorkerDispatcherHost::OnProviderDestroyed(int provider_id) {
499 TRACE_EVENT0("ServiceWorker",
500 "ServiceWorkerDispatcherHost::OnProviderDestroyed");
503 if (!GetContext()->GetProviderHost(render_process_id_, provider_id)) {
504 BadMessageReceived();
507 GetContext()->RemoveProviderHost(render_process_id_, provider_id);
510 void ServiceWorkerDispatcherHost::OnSetHostedVersionId(
511 int provider_id, int64 version_id) {
512 TRACE_EVENT0("ServiceWorker",
513 "ServiceWorkerDispatcherHost::OnSetHostedVersionId");
516 ServiceWorkerProviderHost* provider_host =
517 GetContext()->GetProviderHost(render_process_id_, provider_id);
518 if (!provider_host) {
519 BadMessageReceived();
522 if (!provider_host->IsContextAlive())
524 if (!provider_host->SetHostedVersionId(version_id))
525 BadMessageReceived();
528 ServiceWorkerRegistrationHandle*
529 ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id,
530 int64 registration_id) {
531 for (IDMap<ServiceWorkerRegistrationHandle, IDMapOwnPointer>::iterator
532 iter(®istration_handles_);
535 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue();
537 if (handle->provider_id() == provider_id && handle->registration() &&
538 handle->registration()->id() == registration_id) {
545 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes(
547 ServiceWorkerRegistration* registration,
548 ServiceWorkerRegistrationObjectInfo* info,
549 ServiceWorkerVersionAttributes* attrs) {
550 ServiceWorkerRegistrationHandle* handle =
551 GetOrCreateRegistrationHandle(provider_id, registration);
552 *info = handle->GetObjectInfo();
554 attrs->installing = handle->CreateServiceWorkerHandleAndPass(
555 registration->installing_version());
556 attrs->waiting = handle->CreateServiceWorkerHandleAndPass(
557 registration->waiting_version());
558 attrs->active = handle->CreateServiceWorkerHandleAndPass(
559 registration->active_version());
562 void ServiceWorkerDispatcherHost::RegistrationComplete(
566 ServiceWorkerStatusCode status,
567 int64 registration_id) {
571 if (status != SERVICE_WORKER_OK) {
572 SendRegistrationError(thread_id, request_id, status);
576 ServiceWorkerRegistration* registration =
577 GetContext()->GetLiveRegistration(registration_id);
578 DCHECK(registration);
580 ServiceWorkerRegistrationObjectInfo info;
581 ServiceWorkerVersionAttributes attrs;
582 GetRegistrationObjectInfoAndVersionAttributes(
583 provider_id, registration, &info, &attrs);
585 Send(new ServiceWorkerMsg_ServiceWorkerRegistered(
586 thread_id, request_id, info, attrs));
587 TRACE_EVENT_ASYNC_END1("ServiceWorker",
588 "ServiceWorkerDispatcherHost::RegisterServiceWorker",
594 void ServiceWorkerDispatcherHost::OnWorkerReadyForInspection(
595 int embedded_worker_id) {
596 TRACE_EVENT0("ServiceWorker",
597 "ServiceWorkerDispatcherHost::OnWorkerReadyForInspection");
600 EmbeddedWorkerRegistry* registry = GetContext()->embedded_worker_registry();
601 if (!registry->CanHandle(embedded_worker_id))
603 registry->OnWorkerReadyForInspection(render_process_id_, embedded_worker_id);
606 void ServiceWorkerDispatcherHost::OnWorkerScriptLoaded(
607 int embedded_worker_id,
609 TRACE_EVENT0("ServiceWorker",
610 "ServiceWorkerDispatcherHost::OnWorkerScriptLoaded");
613 EmbeddedWorkerRegistry* registry = GetContext()->embedded_worker_registry();
614 if (!registry->CanHandle(embedded_worker_id))
616 registry->OnWorkerScriptLoaded(
617 render_process_id_, thread_id, embedded_worker_id);
620 void ServiceWorkerDispatcherHost::OnWorkerScriptLoadFailed(
621 int embedded_worker_id) {
622 TRACE_EVENT0("ServiceWorker",
623 "ServiceWorkerDispatcherHost::OnWorkerScriptLoadFailed");
626 EmbeddedWorkerRegistry* registry = GetContext()->embedded_worker_registry();
627 if (!registry->CanHandle(embedded_worker_id))
629 registry->OnWorkerScriptLoadFailed(render_process_id_, embedded_worker_id);
632 void ServiceWorkerDispatcherHost::OnWorkerScriptEvaluated(
633 int embedded_worker_id,
635 TRACE_EVENT0("ServiceWorker",
636 "ServiceWorkerDispatcherHost::OnWorkerScriptEvaluated");
639 EmbeddedWorkerRegistry* registry = GetContext()->embedded_worker_registry();
640 if (!registry->CanHandle(embedded_worker_id))
642 registry->OnWorkerScriptEvaluated(
643 render_process_id_, embedded_worker_id, success);
646 void ServiceWorkerDispatcherHost::OnWorkerStarted(int embedded_worker_id) {
647 TRACE_EVENT0("ServiceWorker",
648 "ServiceWorkerDispatcherHost::OnWorkerStarted");
651 EmbeddedWorkerRegistry* registry = GetContext()->embedded_worker_registry();
652 if (!registry->CanHandle(embedded_worker_id))
654 registry->OnWorkerStarted(render_process_id_, embedded_worker_id);
657 void ServiceWorkerDispatcherHost::OnWorkerStopped(int embedded_worker_id) {
658 TRACE_EVENT0("ServiceWorker",
659 "ServiceWorkerDispatcherHost::OnWorkerStopped");
662 EmbeddedWorkerRegistry* registry = GetContext()->embedded_worker_registry();
663 if (!registry->CanHandle(embedded_worker_id))
665 registry->OnWorkerStopped(render_process_id_, embedded_worker_id);
668 void ServiceWorkerDispatcherHost::OnPausedAfterDownload(
669 int embedded_worker_id) {
670 TRACE_EVENT0("ServiceWorker",
671 "ServiceWorkerDispatcherHost::OnPausedAfterDownload");
674 GetContext()->embedded_worker_registry()->OnPausedAfterDownload(
675 render_process_id_, embedded_worker_id);
678 void ServiceWorkerDispatcherHost::OnReportException(
679 int embedded_worker_id,
680 const base::string16& error_message,
683 const GURL& source_url) {
684 TRACE_EVENT0("ServiceWorker",
685 "ServiceWorkerDispatcherHost::OnReportException");
688 EmbeddedWorkerRegistry* registry = GetContext()->embedded_worker_registry();
689 if (!registry->CanHandle(embedded_worker_id))
691 registry->OnReportException(embedded_worker_id,
698 void ServiceWorkerDispatcherHost::OnReportConsoleMessage(
699 int embedded_worker_id,
700 const EmbeddedWorkerHostMsg_ReportConsoleMessage_Params& params) {
701 TRACE_EVENT0("ServiceWorker",
702 "ServiceWorkerDispatcherHost::OnReportConsoleMessage");
705 EmbeddedWorkerRegistry* registry = GetContext()->embedded_worker_registry();
706 if (!registry->CanHandle(embedded_worker_id))
708 registry->OnReportConsoleMessage(embedded_worker_id,
709 params.source_identifier,
710 params.message_level,
716 void ServiceWorkerDispatcherHost::OnIncrementServiceWorkerRefCount(
718 TRACE_EVENT0("ServiceWorker",
719 "ServiceWorkerDispatcherHost::OnIncrementServiceWorkerRefCount");
720 ServiceWorkerHandle* handle = handles_.Lookup(handle_id);
722 BadMessageReceived();
725 handle->IncrementRefCount();
728 void ServiceWorkerDispatcherHost::OnDecrementServiceWorkerRefCount(
730 TRACE_EVENT0("ServiceWorker",
731 "ServiceWorkerDispatcherHost::OnDecrementServiceWorkerRefCount");
732 ServiceWorkerHandle* handle = handles_.Lookup(handle_id);
734 BadMessageReceived();
737 handle->DecrementRefCount();
738 if (handle->HasNoRefCount())
739 handles_.Remove(handle_id);
742 void ServiceWorkerDispatcherHost::OnIncrementRegistrationRefCount(
743 int registration_handle_id) {
744 TRACE_EVENT0("ServiceWorker",
745 "ServiceWorkerDispatcherHost::OnIncrementRegistrationRefCount");
746 ServiceWorkerRegistrationHandle* handle =
747 registration_handles_.Lookup(registration_handle_id);
749 BadMessageReceived();
752 handle->IncrementRefCount();
755 void ServiceWorkerDispatcherHost::OnDecrementRegistrationRefCount(
756 int registration_handle_id) {
757 TRACE_EVENT0("ServiceWorker",
758 "ServiceWorkerDispatcherHost::OnDecrementRegistrationRefCount");
759 ServiceWorkerRegistrationHandle* handle =
760 registration_handles_.Lookup(registration_handle_id);
762 BadMessageReceived();
765 handle->DecrementRefCount();
766 if (handle->HasNoRefCount())
767 registration_handles_.Remove(registration_handle_id);
770 void ServiceWorkerDispatcherHost::UnregistrationComplete(
773 ServiceWorkerStatusCode status) {
774 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) {
775 SendUnregistrationError(thread_id, request_id, status);
778 const bool is_success = (status == SERVICE_WORKER_OK);
779 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id,
782 TRACE_EVENT_ASYNC_END1(
784 "ServiceWorkerDispatcherHost::UnregisterServiceWorker",
789 void ServiceWorkerDispatcherHost::GetRegistrationComplete(
793 ServiceWorkerStatusCode status,
794 const scoped_refptr<ServiceWorkerRegistration>& registration) {
795 TRACE_EVENT_ASYNC_END1("ServiceWorker",
796 "ServiceWorkerDispatcherHost::GetRegistration",
799 registration.get() ? registration->id()
800 : kInvalidServiceWorkerRegistrationId);
805 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) {
806 SendGetRegistrationError(thread_id, request_id, status);
810 ServiceWorkerRegistrationObjectInfo info;
811 ServiceWorkerVersionAttributes attrs;
812 if (status == SERVICE_WORKER_OK) {
813 DCHECK(registration.get());
814 if (!registration->is_uninstalling()) {
815 GetRegistrationObjectInfoAndVersionAttributes(
816 provider_id, registration.get(), &info, &attrs);
820 Send(new ServiceWorkerMsg_DidGetRegistration(
821 thread_id, request_id, info, attrs));
824 void ServiceWorkerDispatcherHost::SendRegistrationError(
827 ServiceWorkerStatusCode status) {
828 base::string16 error_message;
829 blink::WebServiceWorkerError::ErrorType error_type;
830 GetServiceWorkerRegistrationStatusResponse(
831 status, &error_type, &error_message);
832 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError(
833 thread_id, request_id, error_type, error_message));
836 void ServiceWorkerDispatcherHost::SendUnregistrationError(
839 ServiceWorkerStatusCode status) {
840 base::string16 error_message;
841 blink::WebServiceWorkerError::ErrorType error_type;
842 GetServiceWorkerRegistrationStatusResponse(
843 status, &error_type, &error_message);
844 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError(
845 thread_id, request_id, error_type, error_message));
848 void ServiceWorkerDispatcherHost::SendGetRegistrationError(
851 ServiceWorkerStatusCode status) {
852 base::string16 error_message;
853 blink::WebServiceWorkerError::ErrorType error_type;
854 GetServiceWorkerRegistrationStatusResponse(
855 status, &error_type, &error_message);
856 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError(
857 thread_id, request_id, error_type, error_message));
860 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() {
861 if (!context_wrapper_.get())
863 return context_wrapper_->context();
866 } // namespace content