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_registration_handle.h"
7 #include "content/browser/service_worker/service_worker_context_core.h"
8 #include "content/browser/service_worker/service_worker_dispatcher_host.h"
9 #include "content/browser/service_worker/service_worker_handle.h"
10 #include "content/common/service_worker/service_worker_messages.h"
14 static const int kDocumentMainThreadId
= 0;
16 ServiceWorkerRegistrationHandle::ServiceWorkerRegistrationHandle(
17 base::WeakPtr
<ServiceWorkerContextCore
> context
,
18 ServiceWorkerDispatcherHost
* dispatcher_host
,
20 ServiceWorkerRegistration
* registration
)
22 dispatcher_host_(dispatcher_host
),
23 provider_id_(provider_id
),
24 handle_id_(context
? context
->GetNewRegistrationHandleId()
25 : kInvalidServiceWorkerRegistrationHandleId
),
27 registration_(registration
) {
28 DCHECK(registration_
.get());
29 SetVersionAttributes(registration
->installing_version(),
30 registration
->waiting_version(),
31 registration
->active_version());
32 registration_
->AddListener(this);
35 ServiceWorkerRegistrationHandle::~ServiceWorkerRegistrationHandle() {
36 DCHECK(registration_
.get());
37 registration_
->RemoveListener(this);
40 ServiceWorkerRegistrationObjectInfo
41 ServiceWorkerRegistrationHandle::GetObjectInfo() {
42 ServiceWorkerRegistrationObjectInfo info
;
43 info
.handle_id
= handle_id_
;
44 info
.scope
= registration_
->pattern();
45 info
.registration_id
= registration_
->id();
49 ServiceWorkerObjectInfo
50 ServiceWorkerRegistrationHandle::CreateServiceWorkerHandleAndPass(
51 ServiceWorkerVersion
* version
) {
52 ServiceWorkerObjectInfo info
;
53 if (context_
&& version
) {
54 scoped_ptr
<ServiceWorkerHandle
> handle
=
55 ServiceWorkerHandle::Create(context_
,
57 kDocumentMainThreadId
,
60 info
= handle
->GetObjectInfo();
61 dispatcher_host_
->RegisterServiceWorkerHandle(handle
.Pass());
66 void ServiceWorkerRegistrationHandle::IncrementRefCount() {
67 DCHECK_GT(ref_count_
, 0);
71 void ServiceWorkerRegistrationHandle::DecrementRefCount() {
72 DCHECK_GT(ref_count_
, 0);
76 void ServiceWorkerRegistrationHandle::OnVersionAttributesChanged(
77 ServiceWorkerRegistration
* registration
,
78 ChangedVersionAttributesMask changed_mask
,
79 const ServiceWorkerRegistrationInfo
& info
) {
80 DCHECK_EQ(registration
->id(), registration_
->id());
81 SetVersionAttributes(registration
->installing_version(),
82 registration
->waiting_version(),
83 registration
->active_version());
86 void ServiceWorkerRegistrationHandle::OnRegistrationFailed(
87 ServiceWorkerRegistration
* registration
) {
88 DCHECK_EQ(registration
->id(), registration_
->id());
89 ClearVersionAttributes();
92 void ServiceWorkerRegistrationHandle::OnUpdateFound(
93 ServiceWorkerRegistration
* registration
) {
94 if (!dispatcher_host_
)
95 return; // Could be NULL in some tests.
96 dispatcher_host_
->Send(new ServiceWorkerMsg_UpdateFound(
97 kDocumentMainThreadId
, GetObjectInfo()));
100 void ServiceWorkerRegistrationHandle::SetVersionAttributes(
101 ServiceWorkerVersion
* installing_version
,
102 ServiceWorkerVersion
* waiting_version
,
103 ServiceWorkerVersion
* active_version
) {
104 ChangedVersionAttributesMask mask
;
106 if (installing_version
!= installing_version_
.get()) {
107 installing_version_
= installing_version
;
108 mask
.add(ChangedVersionAttributesMask::INSTALLING_VERSION
);
110 if (waiting_version
!= waiting_version_
.get()) {
111 waiting_version_
= waiting_version
;
112 mask
.add(ChangedVersionAttributesMask::WAITING_VERSION
);
114 if (active_version
!= active_version_
.get()) {
115 active_version_
= active_version
;
116 mask
.add(ChangedVersionAttributesMask::ACTIVE_VERSION
);
119 if (!dispatcher_host_
)
120 return; // Could be NULL in some tests.
124 ServiceWorkerVersionAttributes attributes
;
125 if (mask
.installing_changed()) {
126 attributes
.installing
=
127 CreateServiceWorkerHandleAndPass(installing_version
);
129 if (mask
.waiting_changed()) {
131 CreateServiceWorkerHandleAndPass(waiting_version
);
133 if (mask
.active_changed()) {
135 CreateServiceWorkerHandleAndPass(active_version
);
138 dispatcher_host_
->Send(new ServiceWorkerMsg_SetVersionAttributes(
139 kDocumentMainThreadId
, provider_id_
, handle_id_
,
140 mask
.changed(), attributes
));
143 void ServiceWorkerRegistrationHandle::ClearVersionAttributes() {
144 SetVersionAttributes(NULL
, NULL
, NULL
);
147 } // namespace content