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 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
,
44 ServiceWorkerVersion
* version
) {
45 if (!context
|| !version
)
46 return scoped_ptr
<ServiceWorkerHandle
>();
47 ServiceWorkerRegistration
* registration
=
48 context
->GetLiveRegistration(version
->registration_id());
49 return make_scoped_ptr(new ServiceWorkerHandle(
50 context
, sender
, thread_id
, provider_id
, registration
, version
));
53 ServiceWorkerHandle::ServiceWorkerHandle(
54 base::WeakPtr
<ServiceWorkerContextCore
> context
,
58 ServiceWorkerRegistration
* registration
,
59 ServiceWorkerVersion
* version
)
62 thread_id_(thread_id
),
63 provider_id_(provider_id
),
64 handle_id_(context
.get() ? context
->GetNewServiceWorkerHandleId() : -1),
66 registration_(registration
),
68 version_
->AddListener(this);
71 ServiceWorkerHandle::~ServiceWorkerHandle() {
72 version_
->RemoveListener(this);
73 // TODO(kinuko): At this point we can discard the registration if
74 // all documents/handles that have a reference to the registration is
75 // closed or freed up, but could also keep it alive in cache
76 // (e.g. in context_) for a while with some timer so that we don't
77 // need to re-load the same registration from disk over and over.
80 void ServiceWorkerHandle::OnWorkerStarted(ServiceWorkerVersion
* version
) {
83 void ServiceWorkerHandle::OnWorkerStopped(ServiceWorkerVersion
* version
) {
86 void ServiceWorkerHandle::OnErrorReported(ServiceWorkerVersion
* version
,
87 const base::string16
& error_message
,
90 const GURL
& source_url
) {
93 void ServiceWorkerHandle::OnReportConsoleMessage(ServiceWorkerVersion
* version
,
94 int source_identifier
,
96 const base::string16
& message
,
98 const GURL
& source_url
) {
101 void ServiceWorkerHandle::OnVersionStateChanged(ServiceWorkerVersion
* version
) {
102 sender_
->Send(new ServiceWorkerMsg_ServiceWorkerStateChanged(
103 thread_id_
, handle_id_
, GetWebServiceWorkerState(version
)));
106 ServiceWorkerObjectInfo
ServiceWorkerHandle::GetObjectInfo() {
107 ServiceWorkerObjectInfo info
;
108 info
.handle_id
= handle_id_
;
109 info
.scope
= registration_
->pattern();
110 info
.url
= version_
->script_url();
111 info
.state
= GetWebServiceWorkerState(version_
.get());
112 info
.version_id
= version_
->version_id();
116 void ServiceWorkerHandle::IncrementRefCount() {
117 DCHECK_GT(ref_count_
, 0);
121 void ServiceWorkerHandle::DecrementRefCount() {
122 DCHECK_GE(ref_count_
, 0);
126 } // namespace content