Rename isSystemLocationEnabled to isLocationEnabled, as per internal review (185995).
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_registration.h
blob6a8d11fcac988e4ed81c51d19563e83879575f50
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 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 {
31 public:
32 typedef base::Callback<void(ServiceWorkerStatusCode status)> StatusCallback;
33 typedef base::Callback<void(
34 const std::string& data,
35 ServiceWorkerStatusCode status)> GetUserDataCallback;
37 class Listener {
38 public:
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 // Triggers the [[ClearRegistration]] algorithm when the currently
113 // active version has no controllees. Deletes this registration
114 // from storage immediately.
115 void ClearWhenReady();
117 // Restores this registration in storage and cancels the pending
118 // [[ClearRegistration]] algorithm.
119 void AbortPendingClear(const StatusCallback& callback);
121 // The time of the most recent update check.
122 base::Time last_update_check() const { return last_update_check_; }
123 void set_last_update_check(base::Time last) { last_update_check_ = last; }
125 // Provide a storage mechanism to read/write arbitrary data associated with
126 // this registration in the storage. Stored data is deleted when this
127 // registration is deleted from the storage.
128 void GetUserData(const std::string& key,
129 const GetUserDataCallback& callback);
130 void StoreUserData(const std::string& key,
131 const std::string& data,
132 const StatusCallback& callback);
133 void ClearUserData(const std::string& key,
134 const StatusCallback& callback);
136 private:
137 friend class base::RefCounted<ServiceWorkerRegistration>;
139 ~ServiceWorkerRegistration() override;
141 void SetVersionInternal(
142 ServiceWorkerVersion* version,
143 scoped_refptr<ServiceWorkerVersion>* data_member,
144 int change_flag);
145 void UnsetVersionInternal(
146 ServiceWorkerVersion* version,
147 ChangedVersionAttributesMask* mask);
149 // ServiceWorkerVersion::Listener override.
150 void OnNoControllees(ServiceWorkerVersion* version) override;
152 // This method corresponds to the [[Activate]] algorithm.
153 void ActivateWaitingVersion();
154 void OnActivateEventFinished(
155 ServiceWorkerVersion* activating_version,
156 ServiceWorkerStatusCode status);
157 void OnDeleteFinished(ServiceWorkerStatusCode status);
159 // This method corresponds to the [[ClearRegistration]] algorithm.
160 void Clear();
162 void OnRestoreFinished(const StatusCallback& callback,
163 scoped_refptr<ServiceWorkerVersion> version,
164 ServiceWorkerStatusCode status);
166 const GURL pattern_;
167 const int64 registration_id_;
168 bool is_deleted_;
169 bool is_uninstalling_;
170 bool is_uninstalled_;
171 bool should_activate_when_ready_;
172 base::Time last_update_check_;
173 int64_t resources_total_size_bytes_;
174 scoped_refptr<ServiceWorkerVersion> active_version_;
175 scoped_refptr<ServiceWorkerVersion> waiting_version_;
176 scoped_refptr<ServiceWorkerVersion> installing_version_;
177 ObserverList<Listener> listeners_;
178 base::WeakPtr<ServiceWorkerContextCore> context_;
180 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration);
183 } // namespace content
185 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_