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 "content/common/service_worker/service_worker_types.h"
16 blink::WebServiceWorkerState
17 GetWebServiceWorkerState(ServiceWorkerVersion
* version
) {
19 switch (version
->status()) {
20 case ServiceWorkerVersion::NEW
:
21 return blink::WebServiceWorkerStateUnknown
;
22 case ServiceWorkerVersion::INSTALLING
:
23 return blink::WebServiceWorkerStateInstalling
;
24 case ServiceWorkerVersion::INSTALLED
:
25 return blink::WebServiceWorkerStateInstalled
;
26 case ServiceWorkerVersion::ACTIVATING
:
27 return blink::WebServiceWorkerStateActivating
;
28 case ServiceWorkerVersion::ACTIVATED
:
29 return blink::WebServiceWorkerStateActivated
;
30 case ServiceWorkerVersion::REDUNDANT
:
31 return blink::WebServiceWorkerStateRedundant
;
33 NOTREACHED() << version
->status();
34 return blink::WebServiceWorkerStateUnknown
;
39 scoped_ptr
<ServiceWorkerHandle
> ServiceWorkerHandle::Create(
40 base::WeakPtr
<ServiceWorkerContextCore
> context
,
41 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
42 ServiceWorkerVersion
* version
) {
43 if (!context
|| !provider_host
|| !version
)
44 return scoped_ptr
<ServiceWorkerHandle
>();
45 DCHECK(context
->GetLiveRegistration(version
->registration_id()));
46 return make_scoped_ptr(new ServiceWorkerHandle(
47 context
, provider_host
, version
));
50 ServiceWorkerHandle::ServiceWorkerHandle(
51 base::WeakPtr
<ServiceWorkerContextCore
> context
,
52 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
53 ServiceWorkerVersion
* version
)
55 provider_host_(provider_host
),
56 provider_id_(provider_host
? provider_host
->provider_id()
57 : kInvalidServiceWorkerProviderId
),
58 handle_id_(context
.get() ? context
->GetNewServiceWorkerHandleId() : -1),
61 version_
->AddListener(this);
64 ServiceWorkerHandle::~ServiceWorkerHandle() {
65 version_
->RemoveListener(this);
68 void ServiceWorkerHandle::OnVersionStateChanged(ServiceWorkerVersion
* version
) {
71 provider_host_
->SendServiceWorkerStateChangedMessage(
72 handle_id_
, GetWebServiceWorkerState(version
));
75 ServiceWorkerObjectInfo
ServiceWorkerHandle::GetObjectInfo() {
76 ServiceWorkerObjectInfo info
;
77 info
.handle_id
= handle_id_
;
78 info
.url
= version_
->script_url();
79 info
.state
= GetWebServiceWorkerState(version_
.get());
80 info
.version_id
= version_
->version_id();
84 void ServiceWorkerHandle::IncrementRefCount() {
85 DCHECK_GT(ref_count_
, 0);
89 void ServiceWorkerHandle::DecrementRefCount() {
90 DCHECK_GE(ref_count_
, 0);
94 } // namespace content