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_
; }
63 bool is_uninstalled() const { return is_uninstalled_
; }
65 int64_t resources_total_size_bytes() const {
66 return resources_total_size_bytes_
;
69 void set_resources_total_size_bytes(int64_t resources_total_size_bytes
) {
70 resources_total_size_bytes_
= resources_total_size_bytes
;
73 ServiceWorkerVersion
* active_version() const {
74 return active_version_
.get();
77 ServiceWorkerVersion
* waiting_version() const {
78 return waiting_version_
.get();
81 ServiceWorkerVersion
* installing_version() const {
82 return installing_version_
.get();
85 ServiceWorkerVersion
* GetNewestVersion() const;
87 void AddListener(Listener
* listener
);
88 void RemoveListener(Listener
* listener
);
89 void NotifyRegistrationFailed();
90 void NotifyUpdateFound();
92 ServiceWorkerRegistrationInfo
GetInfo();
94 // Sets the corresposding version attribute and resets the position
95 // (if any) left vacant (ie. by a waiting version being promoted).
96 // Also notifies listeners via OnVersionAttributesChanged.
97 void SetActiveVersion(ServiceWorkerVersion
* version
);
98 void SetWaitingVersion(ServiceWorkerVersion
* version
);
99 void SetInstallingVersion(ServiceWorkerVersion
* version
);
101 // If version is the installing, waiting, active version of this
102 // registation, the method will reset that field to NULL, and notify
103 // listeners via OnVersionAttributesChanged.
104 void UnsetVersion(ServiceWorkerVersion
* version
);
106 // Triggers the [[Activate]] algorithm when the currently active version
107 // has no controllees. If there are no controllees at the time the method
108 // is called or when version's skip waiting flag is set, activation is
109 // initiated immediately.
110 void ActivateWaitingVersionWhenReady();
112 // Takes over control of provider hosts which are currently not controlled or
113 // controlled by other registrations.
116 // Triggers the [[ClearRegistration]] algorithm when the currently
117 // active version has no controllees. Deletes this registration
118 // from storage immediately.
119 void ClearWhenReady();
121 // Restores this registration in storage and cancels the pending
122 // [[ClearRegistration]] algorithm.
123 void AbortPendingClear(const StatusCallback
& callback
);
125 // The time of the most recent update check.
126 base::Time
last_update_check() const { return last_update_check_
; }
127 void set_last_update_check(base::Time last
) { last_update_check_
= last
; }
129 // Provide a storage mechanism to read/write arbitrary data associated with
130 // this registration in the storage. Stored data is deleted when this
131 // registration is deleted from the storage.
132 void GetUserData(const std::string
& key
,
133 const GetUserDataCallback
& callback
);
134 void StoreUserData(const std::string
& key
,
135 const std::string
& data
,
136 const StatusCallback
& callback
);
137 void ClearUserData(const std::string
& key
,
138 const StatusCallback
& callback
);
141 friend class base::RefCounted
<ServiceWorkerRegistration
>;
143 ~ServiceWorkerRegistration() override
;
145 void SetVersionInternal(
146 ServiceWorkerVersion
* version
,
147 scoped_refptr
<ServiceWorkerVersion
>* data_member
,
149 void UnsetVersionInternal(
150 ServiceWorkerVersion
* version
,
151 ChangedVersionAttributesMask
* mask
);
153 // ServiceWorkerVersion::Listener override.
154 void OnNoControllees(ServiceWorkerVersion
* version
) override
;
156 // This method corresponds to the [[Activate]] algorithm.
157 void ActivateWaitingVersion();
158 void OnActivateEventFinished(
159 ServiceWorkerVersion
* activating_version
,
160 ServiceWorkerStatusCode status
);
161 void OnDeleteFinished(ServiceWorkerStatusCode status
);
163 // This method corresponds to the [[ClearRegistration]] algorithm.
166 void OnRestoreFinished(const StatusCallback
& callback
,
167 scoped_refptr
<ServiceWorkerVersion
> version
,
168 ServiceWorkerStatusCode status
);
171 const int64 registration_id_
;
173 bool is_uninstalling_
;
174 bool is_uninstalled_
;
175 bool should_activate_when_ready_
;
176 base::Time last_update_check_
;
177 int64_t resources_total_size_bytes_
;
178 scoped_refptr
<ServiceWorkerVersion
> active_version_
;
179 scoped_refptr
<ServiceWorkerVersion
> waiting_version_
;
180 scoped_refptr
<ServiceWorkerVersion
> installing_version_
;
181 ObserverList
<Listener
> listeners_
;
182 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
184 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration
);
187 } // namespace content
189 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_