Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_registration.h
blobb386eecfad3fa265444694eb99c850339b07ebc3
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 "base/basictypes.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "content/browser/service_worker/service_worker_version.h"
14 #include "content/common/content_export.h"
15 #include "content/common/service_worker/service_worker_types.h"
16 #include "url/gurl.h"
18 namespace content {
20 class ServiceWorkerRegistrationInfo;
21 class ServiceWorkerVersion;
23 // This class represents a Service Worker registration. The scope is constant
24 // for the life of the persistent registration. It's refcounted to facilitate
25 // multiple controllees being associated with the same registration.
26 class CONTENT_EXPORT ServiceWorkerRegistration
27 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerRegistration>),
28 public ServiceWorkerVersion::Listener {
29 public:
30 typedef base::Callback<void(ServiceWorkerStatusCode status)> StatusCallback;
32 class Listener {
33 public:
34 virtual void OnVersionAttributesChanged(
35 ServiceWorkerRegistration* registration,
36 ChangedVersionAttributesMask changed_mask,
37 const ServiceWorkerRegistrationInfo& info) {}
38 virtual void OnRegistrationFailed(
39 ServiceWorkerRegistration* registration) {}
40 virtual void OnRegistrationFinishedUninstalling(
41 ServiceWorkerRegistration* registration) {}
42 virtual void OnUpdateFound(
43 ServiceWorkerRegistration* registration) {}
46 ServiceWorkerRegistration(const GURL& pattern,
47 int64 registration_id,
48 base::WeakPtr<ServiceWorkerContextCore> context);
50 int64 id() const { return registration_id_; }
51 const GURL& pattern() const { return pattern_; }
53 bool is_deleted() const { return is_deleted_; }
54 void set_is_deleted(bool deleted) { is_deleted_ = deleted; }
56 bool is_uninstalling() const { return is_uninstalling_; }
57 bool is_uninstalled() const { return is_uninstalled_; }
59 int64_t resources_total_size_bytes() const {
60 return resources_total_size_bytes_;
63 void set_resources_total_size_bytes(int64_t resources_total_size_bytes) {
64 resources_total_size_bytes_ = resources_total_size_bytes;
67 ServiceWorkerVersion* active_version() const {
68 return active_version_.get();
71 ServiceWorkerVersion* waiting_version() const {
72 return waiting_version_.get();
75 ServiceWorkerVersion* installing_version() const {
76 return installing_version_.get();
79 ServiceWorkerVersion* GetNewestVersion() const;
81 void AddListener(Listener* listener);
82 void RemoveListener(Listener* listener);
83 void NotifyRegistrationFailed();
84 void NotifyUpdateFound();
86 ServiceWorkerRegistrationInfo GetInfo();
88 // Sets the corresposding version attribute and resets the position
89 // (if any) left vacant (ie. by a waiting version being promoted).
90 // Also notifies listeners via OnVersionAttributesChanged.
91 void SetActiveVersion(ServiceWorkerVersion* version);
92 void SetWaitingVersion(ServiceWorkerVersion* version);
93 void SetInstallingVersion(ServiceWorkerVersion* version);
95 // If version is the installing, waiting, active version of this
96 // registation, the method will reset that field to NULL, and notify
97 // listeners via OnVersionAttributesChanged.
98 void UnsetVersion(ServiceWorkerVersion* version);
100 // Triggers the [[Activate]] algorithm when the currently active version
101 // has no controllees. If there are no controllees at the time the method
102 // is called, activation is initiated immediately.
103 void ActivateWaitingVersionWhenReady();
105 // Triggers the [[ClearRegistration]] algorithm when the currently
106 // active version has no controllees. Deletes this registration
107 // from storage immediately.
108 void ClearWhenReady();
110 // Restores this registration in storage and cancels the pending
111 // [[ClearRegistration]] algorithm.
112 void AbortPendingClear(const StatusCallback& callback);
114 // The time of the most recent update check.
115 base::Time last_update_check() const { return last_update_check_; }
116 void set_last_update_check(base::Time last) { last_update_check_ = last; }
118 private:
119 friend class base::RefCounted<ServiceWorkerRegistration>;
121 ~ServiceWorkerRegistration() override;
123 void SetVersionInternal(
124 ServiceWorkerVersion* version,
125 scoped_refptr<ServiceWorkerVersion>* data_member,
126 int change_flag);
127 void UnsetVersionInternal(
128 ServiceWorkerVersion* version,
129 ChangedVersionAttributesMask* mask);
131 // ServiceWorkerVersion::Listener override.
132 void OnNoControllees(ServiceWorkerVersion* version) override;
134 // This method corresponds to the [[Activate]] algorithm.
135 void ActivateWaitingVersion();
136 void OnActivateEventFinished(
137 ServiceWorkerVersion* activating_version,
138 ServiceWorkerStatusCode status);
139 void OnDeleteFinished(ServiceWorkerStatusCode status);
141 // This method corresponds to the [[ClearRegistration]] algorithm.
142 void Clear();
144 void OnRestoreFinished(const StatusCallback& callback,
145 scoped_refptr<ServiceWorkerVersion> version,
146 ServiceWorkerStatusCode status);
148 const GURL pattern_;
149 const int64 registration_id_;
150 bool is_deleted_;
151 bool is_uninstalling_;
152 bool is_uninstalled_;
153 bool should_activate_when_ready_;
154 base::Time last_update_check_;
155 int64_t resources_total_size_bytes_;
156 scoped_refptr<ServiceWorkerVersion> active_version_;
157 scoped_refptr<ServiceWorkerVersion> waiting_version_;
158 scoped_refptr<ServiceWorkerVersion> installing_version_;
159 ObserverList<Listener> listeners_;
160 base::WeakPtr<ServiceWorkerContextCore> context_;
162 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration);
165 } // namespace content
167 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_