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
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_handle.h"
7 #include "content/browser/service_worker/service_worker_context_core.h"
8 #include "content/browser/service_worker/service_worker_registration.h"
9 #include "content/common/service_worker/service_worker_messages.h"
10 #include "ipc/ipc_sender.h"
16 blink::WebServiceWorkerState
17 GetWebServiceWorkerState(ServiceWorkerVersion* version) {
19 switch (version->status()) {
20 case ServiceWorkerVersion::NEW:
21 if (version->running_status() == ServiceWorkerVersion::RUNNING)
22 return blink::WebServiceWorkerStateParsed;
24 return blink::WebServiceWorkerStateUnknown;
25 case ServiceWorkerVersion::INSTALLING:
26 return blink::WebServiceWorkerStateInstalling;
27 case ServiceWorkerVersion::INSTALLED:
28 return blink::WebServiceWorkerStateInstalled;
29 case ServiceWorkerVersion::ACTIVATING:
30 return blink::WebServiceWorkerStateActivating;
31 case ServiceWorkerVersion::ACTIVE:
32 return blink::WebServiceWorkerStateActive;
33 case ServiceWorkerVersion::DEACTIVATED:
34 return blink::WebServiceWorkerStateDeactivated;
36 NOTREACHED() << version->status();
37 return blink::WebServiceWorkerStateUnknown;
42 scoped_ptr<ServiceWorkerHandle> ServiceWorkerHandle::Create(
43 base::WeakPtr<ServiceWorkerContextCore> context,
46 ServiceWorkerVersion* version) {
47 if (!context || !version)
48 return scoped_ptr<ServiceWorkerHandle>();
49 ServiceWorkerRegistration* registration =
50 context->GetLiveRegistration(version->registration_id());
51 return make_scoped_ptr(
52 new ServiceWorkerHandle(context, sender, thread_id,
53 registration, version));
56 ServiceWorkerHandle::ServiceWorkerHandle(
57 base::WeakPtr<ServiceWorkerContextCore> context,
60 ServiceWorkerRegistration* registration,
61 ServiceWorkerVersion* version)
64 thread_id_(thread_id),
65 handle_id_(context.get() ? context->GetNewServiceWorkerHandleId() : -1),
67 registration_(registration),
69 version_->AddListener(this);
72 ServiceWorkerHandle::~ServiceWorkerHandle() {
73 version_->RemoveListener(this);
74 // TODO(kinuko): At this point we can discard the registration if
75 // all documents/handles that have a reference to the registration is
76 // closed or freed up, but could also keep it alive in cache
77 // (e.g. in context_) for a while with some timer so that we don't
78 // need to re-load the same registration from disk over and over.
81 void ServiceWorkerHandle::OnWorkerStarted(ServiceWorkerVersion* version) {
84 void ServiceWorkerHandle::OnWorkerStopped(ServiceWorkerVersion* version) {
87 void ServiceWorkerHandle::OnErrorReported(ServiceWorkerVersion* version,
88 const base::string16& error_message,
91 const GURL& source_url) {
94 void ServiceWorkerHandle::OnReportConsoleMessage(ServiceWorkerVersion* version,
95 int source_identifier,
97 const base::string16& message,
99 const GURL& source_url) {
102 void ServiceWorkerHandle::OnVersionStateChanged(ServiceWorkerVersion* version) {
103 sender_->Send(new ServiceWorkerMsg_ServiceWorkerStateChanged(
104 thread_id_, handle_id_, GetWebServiceWorkerState(version)));
107 ServiceWorkerObjectInfo ServiceWorkerHandle::GetObjectInfo() {
108 ServiceWorkerObjectInfo info;
109 info.handle_id = handle_id_;
110 info.scope = registration_->pattern();
111 info.url = registration_->script_url();
112 info.state = GetWebServiceWorkerState(version_);
116 void ServiceWorkerHandle::IncrementRefCount() {
117 DCHECK_GT(ref_count_, 0);
121 void ServiceWorkerHandle::DecrementRefCount() {
122 DCHECK_GE(ref_count_, 0);
126 } // namespace content