Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_registration_handle.cc
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.
4
5 #include "content/browser/service_worker/service_worker_registration_handle.h"
6
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"
11
12 namespace content {
13
14 static const int kDocumentMainThreadId = 0;
15
16 ServiceWorkerRegistrationHandle::ServiceWorkerRegistrationHandle(
17     base::WeakPtr<ServiceWorkerContextCore> context,
18     ServiceWorkerDispatcherHost* dispatcher_host,
19     int provider_id,
20     ServiceWorkerRegistration* registration)
21     : context_(context),
22       dispatcher_host_(dispatcher_host),
23       provider_id_(provider_id),
24       handle_id_(context ? context->GetNewRegistrationHandleId()
25                          : kInvalidServiceWorkerRegistrationHandleId),
26       ref_count_(1),
27       registration_(registration) {
28   DCHECK(registration_.get());
29   SetVersionAttributes(registration->installing_version(),
30                        registration->waiting_version(),
31                        registration->active_version());
32   registration_->AddListener(this);
33 }
34
35 ServiceWorkerRegistrationHandle::~ServiceWorkerRegistrationHandle() {
36   DCHECK(registration_.get());
37   registration_->RemoveListener(this);
38 }
39
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();
46   return info;
47 }
48
49 ServiceWorkerObjectInfo
50 ServiceWorkerRegistrationHandle::CreateServiceWorkerHandleAndPass(
51     ServiceWorkerVersion* version) {
52   ServiceWorkerObjectInfo info;
53   if (context_ && version) {
54     scoped_ptr<ServiceWorkerHandle> handle =
55         ServiceWorkerHandle::Create(context_,
56                                     dispatcher_host_,
57                                     kDocumentMainThreadId,
58                                     provider_id_,
59                                     version);
60     info = handle->GetObjectInfo();
61     dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass());
62   }
63   return info;
64 }
65
66 void ServiceWorkerRegistrationHandle::IncrementRefCount() {
67   DCHECK_GT(ref_count_, 0);
68   ++ref_count_;
69 }
70
71 void ServiceWorkerRegistrationHandle::DecrementRefCount() {
72   DCHECK_GT(ref_count_, 0);
73   --ref_count_;
74 }
75
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());
84 }
85
86 void ServiceWorkerRegistrationHandle::OnRegistrationFailed(
87     ServiceWorkerRegistration* registration) {
88   DCHECK_EQ(registration->id(), registration_->id());
89   ClearVersionAttributes();
90 }
91
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()));
98 }
99
100 void ServiceWorkerRegistrationHandle::SetVersionAttributes(
101     ServiceWorkerVersion* installing_version,
102     ServiceWorkerVersion* waiting_version,
103     ServiceWorkerVersion* active_version) {
104   ChangedVersionAttributesMask mask;
105
106   if (installing_version != installing_version_.get()) {
107     installing_version_ = installing_version;
108     mask.add(ChangedVersionAttributesMask::INSTALLING_VERSION);
109   }
110   if (waiting_version != waiting_version_.get()) {
111     waiting_version_ = waiting_version;
112     mask.add(ChangedVersionAttributesMask::WAITING_VERSION);
113   }
114   if (active_version != active_version_.get()) {
115     active_version_ = active_version;
116     mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION);
117   }
118
119   if (!dispatcher_host_)
120     return;  // Could be NULL in some tests.
121   if (!mask.changed())
122     return;
123
124   ServiceWorkerVersionAttributes attributes;
125   if (mask.installing_changed()) {
126     attributes.installing =
127         CreateServiceWorkerHandleAndPass(installing_version);
128   }
129   if (mask.waiting_changed()) {
130     attributes.waiting =
131         CreateServiceWorkerHandleAndPass(waiting_version);
132   }
133   if (mask.active_changed()) {
134     attributes.active =
135         CreateServiceWorkerHandleAndPass(active_version);
136   }
137
138   dispatcher_host_->Send(new ServiceWorkerMsg_SetVersionAttributes(
139       kDocumentMainThreadId, provider_id_, handle_id_,
140       mask.changed(), attributes));
141 }
142
143 void ServiceWorkerRegistrationHandle::ClearVersionAttributes() {
144   SetVersionAttributes(NULL, NULL, NULL);
145 }
146
147 }  // namespace content