1 // Copyright 2013 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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/logging.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "content/browser/service_worker/service_worker_version.h"
16 #include "content/common/content_export.h"
17 #include "content/common/service_worker/service_worker_types.h"
22 class ServiceWorkerRegistrationInfo
;
23 class ServiceWorkerVersion
;
25 // This class represents a Service Worker registration. The scope is constant
26 // for the life of the persistent registration. It's refcounted to facilitate
27 // multiple controllees being associated with the same registration.
28 class CONTENT_EXPORT ServiceWorkerRegistration
29 : NON_EXPORTED_BASE(public base::RefCounted
<ServiceWorkerRegistration
>),
30 public ServiceWorkerVersion::Listener
{
32 typedef base::Callback
<void(ServiceWorkerStatusCode status
)> StatusCallback
;
33 typedef base::Callback
<void(
34 const std::string
& data
,
35 ServiceWorkerStatusCode status
)> GetUserDataCallback
;
39 virtual void OnVersionAttributesChanged(
40 ServiceWorkerRegistration
* registration
,
41 ChangedVersionAttributesMask changed_mask
,
42 const ServiceWorkerRegistrationInfo
& info
) {}
43 virtual void OnRegistrationFailed(
44 ServiceWorkerRegistration
* registration
) {}
45 virtual void OnUpdateFound(
46 ServiceWorkerRegistration
* registration
) {}
47 virtual void OnSkippedWaiting(ServiceWorkerRegistration
* registation
) {}
50 ServiceWorkerRegistration(const GURL
& pattern
,
51 int64 registration_id
,
52 base::WeakPtr
<ServiceWorkerContextCore
> context
);
54 int64
id() const { return registration_id_
; }
55 const GURL
& pattern() const { return pattern_
; }
57 bool is_deleted() const { return is_deleted_
; }
58 void set_is_deleted(bool deleted
) { is_deleted_
= deleted
; }
60 bool is_uninstalling() const { return is_uninstalling_
; }
61 bool is_uninstalled() const { return is_uninstalled_
; }
63 int64_t resources_total_size_bytes() const {
64 return resources_total_size_bytes_
;
67 void set_resources_total_size_bytes(int64_t resources_total_size_bytes
) {
68 resources_total_size_bytes_
= resources_total_size_bytes
;
71 ServiceWorkerVersion
* active_version() const {
72 return active_version_
.get();
75 ServiceWorkerVersion
* waiting_version() const {
76 return waiting_version_
.get();
79 ServiceWorkerVersion
* installing_version() const {
80 return installing_version_
.get();
83 ServiceWorkerVersion
* GetNewestVersion() const;
85 void AddListener(Listener
* listener
);
86 void RemoveListener(Listener
* listener
);
87 void NotifyRegistrationFailed();
88 void NotifyUpdateFound();
90 ServiceWorkerRegistrationInfo
GetInfo();
92 // Sets the corresposding version attribute and resets the position
93 // (if any) left vacant (ie. by a waiting version being promoted).
94 // Also notifies listeners via OnVersionAttributesChanged.
95 void SetActiveVersion(ServiceWorkerVersion
* version
);
96 void SetWaitingVersion(ServiceWorkerVersion
* version
);
97 void SetInstallingVersion(ServiceWorkerVersion
* version
);
99 // If version is the installing, waiting, active version of this
100 // registation, the method will reset that field to NULL, and notify
101 // listeners via OnVersionAttributesChanged.
102 void UnsetVersion(ServiceWorkerVersion
* version
);
104 // Triggers the [[Activate]] algorithm when the currently active version
105 // has no controllees. If there are no controllees at the time the method
106 // is called or when version's skip waiting flag is set, activation is
107 // initiated immediately.
108 void ActivateWaitingVersionWhenReady();
110 // Takes over control of provider hosts which are currently not controlled or
111 // controlled by other registrations.
112 void ClaimClients(const StatusCallback
& callback
);
114 // Triggers the [[ClearRegistration]] algorithm when the currently
115 // active version has no controllees. Deletes this registration
116 // from storage immediately.
117 void ClearWhenReady();
119 // Restores this registration in storage and cancels the pending
120 // [[ClearRegistration]] algorithm.
121 void AbortPendingClear(const StatusCallback
& callback
);
123 // The time of the most recent update check.
124 base::Time
last_update_check() const { return last_update_check_
; }
125 void set_last_update_check(base::Time last
) { last_update_check_
= last
; }
127 // Provide a storage mechanism to read/write arbitrary data associated with
128 // this registration in the storage. Stored data is deleted when this
129 // registration is deleted from the storage.
130 void GetUserData(const std::string
& key
,
131 const GetUserDataCallback
& callback
);
132 void StoreUserData(const std::string
& key
,
133 const std::string
& data
,
134 const StatusCallback
& callback
);
135 void ClearUserData(const std::string
& key
,
136 const StatusCallback
& callback
);
139 friend class base::RefCounted
<ServiceWorkerRegistration
>;
141 ~ServiceWorkerRegistration() override
;
143 void SetVersionInternal(
144 ServiceWorkerVersion
* version
,
145 scoped_refptr
<ServiceWorkerVersion
>* data_member
,
147 void UnsetVersionInternal(
148 ServiceWorkerVersion
* version
,
149 ChangedVersionAttributesMask
* mask
);
151 // ServiceWorkerVersion::Listener override.
152 void OnNoControllees(ServiceWorkerVersion
* version
) override
;
154 // This method corresponds to the [[Activate]] algorithm.
155 void ActivateWaitingVersion();
156 void OnActivateEventFinished(
157 ServiceWorkerVersion
* activating_version
,
158 ServiceWorkerStatusCode status
);
159 void OnDeleteFinished(ServiceWorkerStatusCode status
);
161 // This method corresponds to the [[ClearRegistration]] algorithm.
164 void OnRestoreFinished(const StatusCallback
& callback
,
165 scoped_refptr
<ServiceWorkerVersion
> version
,
166 ServiceWorkerStatusCode status
);
168 void DidGetRegistrationsForClaimClients(
169 const StatusCallback
& callback
,
170 scoped_refptr
<ServiceWorkerVersion
> version
,
171 const std::vector
<ServiceWorkerRegistrationInfo
>& registrations
);
173 ServiceWorkerProviderHost
* provider_host
,
174 const std::vector
<ServiceWorkerRegistrationInfo
>& registration_infos
);
177 const int64 registration_id_
;
179 bool is_uninstalling_
;
180 bool is_uninstalled_
;
181 bool should_activate_when_ready_
;
182 base::Time last_update_check_
;
183 int64_t resources_total_size_bytes_
;
184 scoped_refptr
<ServiceWorkerVersion
> active_version_
;
185 scoped_refptr
<ServiceWorkerVersion
> waiting_version_
;
186 scoped_refptr
<ServiceWorkerVersion
> installing_version_
;
187 ObserverList
<Listener
> listeners_
;
188 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
190 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration
);
193 } // namespace content
195 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_