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 ServiceWorkerVersion
;
23 struct ServiceWorkerRegistrationInfo
;
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 OnRegistrationFinishedUninstalling(
46 ServiceWorkerRegistration
* registration
) {}
47 virtual void OnUpdateFound(
48 ServiceWorkerRegistration
* registration
) {}
49 virtual void OnSkippedWaiting(ServiceWorkerRegistration
* registation
) {}
52 ServiceWorkerRegistration(const GURL
& pattern
,
53 int64 registration_id
,
54 base::WeakPtr
<ServiceWorkerContextCore
> context
);
56 int64
id() const { return registration_id_
; }
57 const GURL
& pattern() const { return pattern_
; }
59 bool is_deleted() const { return is_deleted_
; }
60 void set_is_deleted(bool deleted
) { is_deleted_
= deleted
; }
62 bool is_uninstalling() const { return is_uninstalling_
; }
64 void set_is_uninstalled(bool uninstalled
) { is_uninstalled_
= uninstalled
; }
65 bool is_uninstalled() const { return is_uninstalled_
; }
67 int64_t resources_total_size_bytes() const {
68 return resources_total_size_bytes_
;
71 void set_resources_total_size_bytes(int64_t resources_total_size_bytes
) {
72 resources_total_size_bytes_
= resources_total_size_bytes
;
75 ServiceWorkerVersion
* active_version() const {
76 return active_version_
.get();
79 ServiceWorkerVersion
* waiting_version() const {
80 return waiting_version_
.get();
83 ServiceWorkerVersion
* installing_version() const {
84 return installing_version_
.get();
87 ServiceWorkerVersion
* GetNewestVersion() const;
89 void AddListener(Listener
* listener
);
90 void RemoveListener(Listener
* listener
);
91 void NotifyRegistrationFailed();
92 void NotifyUpdateFound();
94 ServiceWorkerRegistrationInfo
GetInfo();
96 // Sets the corresposding version attribute and resets the position
97 // (if any) left vacant (ie. by a waiting version being promoted).
98 // Also notifies listeners via OnVersionAttributesChanged.
99 void SetActiveVersion(const scoped_refptr
<ServiceWorkerVersion
>& version
);
100 void SetWaitingVersion(const scoped_refptr
<ServiceWorkerVersion
>& version
);
101 void SetInstallingVersion(const scoped_refptr
<ServiceWorkerVersion
>& version
);
103 // If version is the installing, waiting, active version of this
104 // registation, the method will reset that field to NULL, and notify
105 // listeners via OnVersionAttributesChanged.
106 void UnsetVersion(ServiceWorkerVersion
* version
);
108 // Triggers the [[Activate]] algorithm when the currently active version
109 // has no controllees. If there are no controllees at the time the method
110 // is called or when version's skip waiting flag is set, activation is
111 // initiated immediately.
112 void ActivateWaitingVersionWhenReady();
114 // Takes over control of provider hosts which are currently not controlled or
115 // controlled by other registrations.
118 // Triggers the [[ClearRegistration]] algorithm when the currently
119 // active version has no controllees. Deletes this registration
120 // from storage immediately.
121 void ClearWhenReady();
123 // Restores this registration in storage and cancels the pending
124 // [[ClearRegistration]] algorithm.
125 void AbortPendingClear(const StatusCallback
& callback
);
127 // The time of the most recent update check.
128 base::Time
last_update_check() const { return last_update_check_
; }
129 void set_last_update_check(base::Time last
) { last_update_check_
= last
; }
131 // Provide a storage mechanism to read/write arbitrary data associated with
132 // this registration in the storage. Stored data is deleted when this
133 // registration is deleted from the storage.
134 void GetUserData(const std::string
& key
,
135 const GetUserDataCallback
& callback
);
136 void StoreUserData(const std::string
& key
,
137 const std::string
& data
,
138 const StatusCallback
& callback
);
139 void ClearUserData(const std::string
& key
,
140 const StatusCallback
& callback
);
142 // Unsets the version and deletes its resources. Also deletes this
143 // registration from storage if there is no longer a stored version.
144 void DeleteVersion(const scoped_refptr
<ServiceWorkerVersion
>& version
);
147 friend class base::RefCounted
<ServiceWorkerRegistration
>;
149 ~ServiceWorkerRegistration() override
;
151 void UnsetVersionInternal(
152 ServiceWorkerVersion
* version
,
153 ChangedVersionAttributesMask
* mask
);
155 // ServiceWorkerVersion::Listener override.
156 void OnNoControllees(ServiceWorkerVersion
* version
) override
;
158 // This method corresponds to the [[Activate]] algorithm.
159 void ActivateWaitingVersion();
160 void OnActivateEventFinished(
161 ServiceWorkerVersion
* activating_version
,
162 ServiceWorkerStatusCode status
);
163 void OnDeleteFinished(ServiceWorkerStatusCode status
);
165 // This method corresponds to the [[ClearRegistration]] algorithm.
168 void OnRestoreFinished(const StatusCallback
& callback
,
169 scoped_refptr
<ServiceWorkerVersion
> version
,
170 ServiceWorkerStatusCode status
);
173 const int64 registration_id_
;
175 bool is_uninstalling_
;
176 bool is_uninstalled_
;
177 bool should_activate_when_ready_
;
178 base::Time last_update_check_
;
179 int64_t resources_total_size_bytes_
;
180 scoped_refptr
<ServiceWorkerVersion
> active_version_
;
181 scoped_refptr
<ServiceWorkerVersion
> waiting_version_
;
182 scoped_refptr
<ServiceWorkerVersion
> installing_version_
;
183 base::ObserverList
<Listener
> listeners_
;
184 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
186 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration
);
189 } // namespace content
191 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_