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::ACTIVATED
:
32 return blink::WebServiceWorkerStateActivated
;
33 case ServiceWorkerVersion::REDUNDANT
:
34 return blink::WebServiceWorkerStateRedundant
;
36 NOTREACHED() << version
->status();
37 return blink::WebServiceWorkerStateUnknown
;
42 scoped_ptr
<ServiceWorkerHandle
> ServiceWorkerHandle::Create(
43 base::WeakPtr
<ServiceWorkerContextCore
> context
,
47 ServiceWorkerVersion
* version
) {
48 if (!context
|| !version
)
49 return scoped_ptr
<ServiceWorkerHandle
>();
50 ServiceWorkerRegistration
* registration
=
51 context
->GetLiveRegistration(version
->registration_id());
52 return make_scoped_ptr(new ServiceWorkerHandle(
53 context
, sender
, thread_id
, provider_id
, registration
, version
));
56 ServiceWorkerHandle::ServiceWorkerHandle(
57 base::WeakPtr
<ServiceWorkerContextCore
> context
,
61 ServiceWorkerRegistration
* registration
,
62 ServiceWorkerVersion
* version
)
65 thread_id_(thread_id
),
66 provider_id_(provider_id
),
67 handle_id_(context
.get() ? context
->GetNewServiceWorkerHandleId() : -1),
69 registration_(registration
),
71 version_
->AddListener(this);
74 ServiceWorkerHandle::~ServiceWorkerHandle() {
75 version_
->RemoveListener(this);
76 // TODO(kinuko): At this point we can discard the registration if
77 // all documents/handles that have a reference to the registration is
78 // closed or freed up, but could also keep it alive in cache
79 // (e.g. in context_) for a while with some timer so that we don't
80 // need to re-load the same registration from disk over and over.
83 void ServiceWorkerHandle::OnWorkerStarted(ServiceWorkerVersion
* version
) {
86 void ServiceWorkerHandle::OnWorkerStopped(ServiceWorkerVersion
* version
) {
89 void ServiceWorkerHandle::OnErrorReported(ServiceWorkerVersion
* version
,
90 const base::string16
& error_message
,
93 const GURL
& source_url
) {
96 void ServiceWorkerHandle::OnReportConsoleMessage(ServiceWorkerVersion
* version
,
97 int source_identifier
,
99 const base::string16
& message
,
101 const GURL
& source_url
) {
104 void ServiceWorkerHandle::OnVersionStateChanged(ServiceWorkerVersion
* version
) {
105 sender_
->Send(new ServiceWorkerMsg_ServiceWorkerStateChanged(
106 thread_id_
, handle_id_
, GetWebServiceWorkerState(version
)));
109 ServiceWorkerObjectInfo
ServiceWorkerHandle::GetObjectInfo() {
110 ServiceWorkerObjectInfo info
;
111 info
.handle_id
= handle_id_
;
112 info
.scope
= registration_
->pattern();
113 info
.url
= version_
->script_url();
114 info
.state
= GetWebServiceWorkerState(version_
.get());
118 void ServiceWorkerHandle::IncrementRefCount() {
119 DCHECK_GT(ref_count_
, 0);
123 void ServiceWorkerHandle::DecrementRefCount() {
124 DCHECK_GE(ref_count_
, 0);
128 } // namespace content