Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_registration.h
blob7d9ca1a41d2daf13237cf405208ae6b5278a7adb
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_
8 #include <string>
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"
18 #include "url/gurl.h"
20 namespace content {
22 class ServiceWorkerVersion;
23 struct ServiceWorkerRegistrationInfo;
25 // Represents the core of a service worker registration object. Other
26 // registration derivatives (WebServiceWorkerRegistration etc) ultimately refer
27 // to this class. This is refcounted via ServiceWorkerRegistrationHandle to
28 // facilitate multiple controllees being associated with the same registration.
29 class CONTENT_EXPORT ServiceWorkerRegistration
30 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerRegistration>),
31 public ServiceWorkerVersion::Listener {
32 public:
33 typedef base::Callback<void(ServiceWorkerStatusCode status)> StatusCallback;
34 typedef base::Callback<void(
35 const std::string& data,
36 ServiceWorkerStatusCode status)> GetUserDataCallback;
38 class Listener {
39 public:
40 virtual void OnVersionAttributesChanged(
41 ServiceWorkerRegistration* registration,
42 ChangedVersionAttributesMask changed_mask,
43 const ServiceWorkerRegistrationInfo& info) {}
44 virtual void OnRegistrationFailed(
45 ServiceWorkerRegistration* registration) {}
46 virtual void OnRegistrationFinishedUninstalling(
47 ServiceWorkerRegistration* registration) {}
48 virtual void OnUpdateFound(
49 ServiceWorkerRegistration* registration) {}
50 virtual void OnSkippedWaiting(ServiceWorkerRegistration* registation) {}
53 ServiceWorkerRegistration(const GURL& pattern,
54 int64 registration_id,
55 base::WeakPtr<ServiceWorkerContextCore> context);
57 int64 id() const { return registration_id_; }
58 const GURL& pattern() const { return pattern_; }
60 bool is_deleted() const { return is_deleted_; }
61 void set_is_deleted(bool deleted) { is_deleted_ = deleted; }
63 bool is_uninstalling() const { return is_uninstalling_; }
65 void set_is_uninstalled(bool uninstalled) { is_uninstalled_ = uninstalled; }
66 bool is_uninstalled() const { return is_uninstalled_; }
68 int64_t resources_total_size_bytes() const {
69 return resources_total_size_bytes_;
72 void set_resources_total_size_bytes(int64_t resources_total_size_bytes) {
73 resources_total_size_bytes_ = resources_total_size_bytes;
76 ServiceWorkerVersion* active_version() const {
77 return active_version_.get();
80 ServiceWorkerVersion* waiting_version() const {
81 return waiting_version_.get();
84 ServiceWorkerVersion* installing_version() const {
85 return installing_version_.get();
88 ServiceWorkerVersion* GetNewestVersion() const;
90 void AddListener(Listener* listener);
91 void RemoveListener(Listener* listener);
92 void NotifyRegistrationFailed();
93 void NotifyUpdateFound();
95 ServiceWorkerRegistrationInfo GetInfo();
97 // Sets the corresposding version attribute and resets the position
98 // (if any) left vacant (ie. by a waiting version being promoted).
99 // Also notifies listeners via OnVersionAttributesChanged.
100 void SetActiveVersion(const scoped_refptr<ServiceWorkerVersion>& version);
101 void SetWaitingVersion(const scoped_refptr<ServiceWorkerVersion>& version);
102 void SetInstallingVersion(const scoped_refptr<ServiceWorkerVersion>& version);
104 // If version is the installing, waiting, active version of this
105 // registation, the method will reset that field to NULL, and notify
106 // listeners via OnVersionAttributesChanged.
107 void UnsetVersion(ServiceWorkerVersion* version);
109 // Triggers the [[Activate]] algorithm when the currently active version
110 // has no controllees. If there are no controllees at the time the method
111 // is called or when version's skip waiting flag is set, activation is
112 // initiated immediately.
113 void ActivateWaitingVersionWhenReady();
115 // Takes over control of provider hosts which are currently not controlled or
116 // controlled by other registrations.
117 void ClaimClients();
119 // Triggers the [[ClearRegistration]] algorithm when the currently
120 // active version has no controllees. Deletes this registration
121 // from storage immediately.
122 void ClearWhenReady();
124 // Restores this registration in storage and cancels the pending
125 // [[ClearRegistration]] algorithm.
126 void AbortPendingClear(const StatusCallback& callback);
128 // The time of the most recent update check.
129 base::Time last_update_check() const { return last_update_check_; }
130 void set_last_update_check(base::Time last) { last_update_check_ = last; }
132 // Provide a storage mechanism to read/write arbitrary data associated with
133 // this registration in the storage. Stored data is deleted when this
134 // registration is deleted from the storage.
135 void GetUserData(const std::string& key,
136 const GetUserDataCallback& callback);
137 void StoreUserData(const std::string& key,
138 const std::string& data,
139 const StatusCallback& callback);
140 void ClearUserData(const std::string& key,
141 const StatusCallback& callback);
143 // Unsets the version and deletes its resources. Also deletes this
144 // registration from storage if there is no longer a stored version.
145 void DeleteVersion(const scoped_refptr<ServiceWorkerVersion>& version);
147 bool force_update_on_page_load() const { return force_update_on_page_load_; }
148 void set_force_update_on_page_load(bool force_update_on_page_load) {
149 force_update_on_page_load_ = force_update_on_page_load;
152 private:
153 friend class base::RefCounted<ServiceWorkerRegistration>;
155 ~ServiceWorkerRegistration() override;
157 void UnsetVersionInternal(
158 ServiceWorkerVersion* version,
159 ChangedVersionAttributesMask* mask);
161 // ServiceWorkerVersion::Listener override.
162 void OnNoControllees(ServiceWorkerVersion* version) override;
164 // This method corresponds to the [[Activate]] algorithm.
165 void ActivateWaitingVersion();
166 void OnActivateEventFinished(
167 ServiceWorkerVersion* activating_version,
168 ServiceWorkerStatusCode status);
169 void OnDeleteFinished(ServiceWorkerStatusCode status);
171 // This method corresponds to the [[ClearRegistration]] algorithm.
172 void Clear();
174 void OnRestoreFinished(const StatusCallback& callback,
175 scoped_refptr<ServiceWorkerVersion> version,
176 ServiceWorkerStatusCode status);
178 const GURL pattern_;
179 const int64 registration_id_;
180 bool is_deleted_;
181 bool is_uninstalling_;
182 bool is_uninstalled_;
183 bool should_activate_when_ready_;
184 bool force_update_on_page_load_;
185 base::Time last_update_check_;
186 int64_t resources_total_size_bytes_;
188 // This registration is the primary owner of these versions.
189 scoped_refptr<ServiceWorkerVersion> active_version_;
190 scoped_refptr<ServiceWorkerVersion> waiting_version_;
191 scoped_refptr<ServiceWorkerVersion> installing_version_;
193 base::ObserverList<Listener> listeners_;
194 base::WeakPtr<ServiceWorkerContextCore> context_;
196 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration);
199 } // namespace content
201 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_