Upstream version 9.38.198.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_);
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_);
37   registration_->RemoveListener(this);
38 }
39
40 void ServiceWorkerRegistrationHandle::IncrementRefCount() {
41   DCHECK_GT(ref_count_, 0);
42   ++ref_count_;
43 }
44
45 void ServiceWorkerRegistrationHandle::DecrementRefCount() {
46   DCHECK_GT(ref_count_, 0);
47   --ref_count_;
48 }
49
50 void ServiceWorkerRegistrationHandle::OnVersionAttributesChanged(
51     ServiceWorkerRegistration* registration,
52     ChangedVersionAttributesMask changed_mask,
53     const ServiceWorkerRegistrationInfo& info) {
54   DCHECK_EQ(registration->id(), registration_->id());
55   SetVersionAttributes(registration->installing_version(),
56                        registration->waiting_version(),
57                        registration->active_version());
58 }
59
60 void ServiceWorkerRegistrationHandle::OnRegistrationFailed(
61     ServiceWorkerRegistration* registration) {
62   DCHECK_EQ(registration->id(), registration_->id());
63   ClearVersionAttributes();
64 }
65
66 void ServiceWorkerRegistrationHandle::SetVersionAttributes(
67     ServiceWorkerVersion* installing_version,
68     ServiceWorkerVersion* waiting_version,
69     ServiceWorkerVersion* active_version) {
70   ChangedVersionAttributesMask mask;
71
72   if (installing_version != installing_version_) {
73     installing_version_ = installing_version;
74     mask.add(ChangedVersionAttributesMask::INSTALLING_VERSION);
75   }
76   if (waiting_version != waiting_version_) {
77     waiting_version_ = waiting_version;
78     mask.add(ChangedVersionAttributesMask::WAITING_VERSION);
79   }
80   if (active_version != active_version_) {
81     active_version_ = active_version;
82     mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION);
83   }
84
85   if (!dispatcher_host_)
86     return;  // Could be NULL in some tests.
87   if (!mask.changed())
88     return;
89
90   ServiceWorkerVersionAttributes attributes;
91   if (mask.installing_changed()) {
92     attributes.installing =
93         CreateServiceWorkerHandleAndPass(installing_version);
94   }
95   if (mask.waiting_changed()) {
96     attributes.waiting =
97         CreateServiceWorkerHandleAndPass(waiting_version);
98   }
99   if (mask.active_changed()) {
100     attributes.active =
101         CreateServiceWorkerHandleAndPass(active_version);
102   }
103
104   dispatcher_host_->Send(new ServiceWorkerMsg_SetVersionAttributes(
105       kDocumentMainThreadId, provider_id_, handle_id_,
106       mask.changed(), attributes));
107 }
108
109 void ServiceWorkerRegistrationHandle::ClearVersionAttributes() {
110   SetVersionAttributes(NULL, NULL, NULL);
111 }
112
113 ServiceWorkerObjectInfo
114 ServiceWorkerRegistrationHandle::CreateServiceWorkerHandleAndPass(
115     ServiceWorkerVersion* version) {
116   ServiceWorkerObjectInfo info;
117   if (context_ && version) {
118     scoped_ptr<ServiceWorkerHandle> handle =
119         ServiceWorkerHandle::Create(context_,
120                                     dispatcher_host_,
121                                     kDocumentMainThreadId,
122                                     provider_id_,
123                                     version);
124     info = handle->GetObjectInfo();
125     dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass());
126   }
127   return info;
128 }
129
130 }  // namespace content