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"
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 {
30 typedef base::Callback<void(ServiceWorkerStatusCode status)> StatusCallback;
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 ServiceWorkerVersion* active_version() const {
60 return active_version_.get();
63 ServiceWorkerVersion* waiting_version() const {
64 return waiting_version_.get();
67 ServiceWorkerVersion* installing_version() const {
68 return installing_version_.get();
71 ServiceWorkerVersion* GetNewestVersion() const;
73 void AddListener(Listener* listener);
74 void RemoveListener(Listener* listener);
75 void NotifyRegistrationFailed();
76 void NotifyUpdateFound();
78 ServiceWorkerRegistrationInfo GetInfo();
80 // Sets the corresposding version attribute and resets the position
81 // (if any) left vacant (ie. by a waiting version being promoted).
82 // Also notifies listeners via OnVersionAttributesChanged.
83 void SetActiveVersion(ServiceWorkerVersion* version);
84 void SetWaitingVersion(ServiceWorkerVersion* version);
85 void SetInstallingVersion(ServiceWorkerVersion* version);
87 // If version is the installing, waiting, active version of this
88 // registation, the method will reset that field to NULL, and notify
89 // listeners via OnVersionAttributesChanged.
90 void UnsetVersion(ServiceWorkerVersion* version);
92 // Triggers the [[Activate]] algorithm when the currently active version
93 // has no controllees. If there are no controllees at the time the method
94 // is called, activation is initiated immediately.
95 void ActivateWaitingVersionWhenReady();
97 // Triggers the [[ClearRegistration]] algorithm when the currently
98 // active version has no controllees. Deletes this registration
99 // from storage immediately.
100 void ClearWhenReady();
102 // Restores this registration in storage and cancels the pending
103 // [[ClearRegistration]] algorithm.
104 void AbortPendingClear(const StatusCallback& callback);
106 // The time of the most recent update check.
107 base::Time last_update_check() const { return last_update_check_; }
108 void set_last_update_check(base::Time last) { last_update_check_ = last; }
111 friend class base::RefCounted<ServiceWorkerRegistration>;
113 virtual ~ServiceWorkerRegistration();
115 void SetVersionInternal(
116 ServiceWorkerVersion* version,
117 scoped_refptr<ServiceWorkerVersion>* data_member,
119 void UnsetVersionInternal(
120 ServiceWorkerVersion* version,
121 ChangedVersionAttributesMask* mask);
123 // ServiceWorkerVersion::Listener override.
124 virtual void OnNoControllees(ServiceWorkerVersion* version) OVERRIDE;
126 // This method corresponds to the [[Activate]] algorithm.
127 void ActivateWaitingVersion();
128 void OnActivateEventFinished(
129 ServiceWorkerVersion* activating_version,
130 ServiceWorkerStatusCode status);
131 void OnDeleteFinished(ServiceWorkerStatusCode status);
133 // This method corresponds to the [[ClearRegistration]] algorithm.
136 void OnRestoreFinished(const StatusCallback& callback,
137 scoped_refptr<ServiceWorkerVersion> version,
138 ServiceWorkerStatusCode status);
141 const int64 registration_id_;
143 bool is_uninstalling_;
144 bool is_uninstalled_;
145 bool should_activate_when_ready_;
146 base::Time last_update_check_;
147 scoped_refptr<ServiceWorkerVersion> active_version_;
148 scoped_refptr<ServiceWorkerVersion> waiting_version_;
149 scoped_refptr<ServiceWorkerVersion> installing_version_;
150 ObserverList<Listener> listeners_;
151 base::WeakPtr<ServiceWorkerContextCore> context_;
153 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration);
156 } // namespace content
158 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_