Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_registration.h
blob55d527e8296cfaf6caf13fd05593415b7b83ccb0
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) {}
44 ServiceWorkerRegistration(const GURL& pattern,
45 int64 registration_id,
46 base::WeakPtr<ServiceWorkerContextCore> context);
48 int64 id() const { return registration_id_; }
49 const GURL& pattern() const { return pattern_; }
51 bool is_deleted() const { return is_deleted_; }
52 void set_is_deleted(bool deleted) { is_deleted_ = deleted; }
54 bool is_uninstalling() const { return is_uninstalling_; }
56 ServiceWorkerVersion* active_version() const {
57 return active_version_.get();
60 ServiceWorkerVersion* waiting_version() const {
61 return waiting_version_.get();
64 ServiceWorkerVersion* installing_version() const {
65 return installing_version_.get();
68 ServiceWorkerVersion* GetNewestVersion() const;
70 void AddListener(Listener* listener);
71 void RemoveListener(Listener* listener);
72 void NotifyRegistrationFailed();
74 ServiceWorkerRegistrationInfo GetInfo();
76 // Sets the corresposding version attribute and resets the position
77 // (if any) left vacant (ie. by a waiting version being promoted).
78 // Also notifies listeners via OnVersionAttributesChanged.
79 void SetActiveVersion(ServiceWorkerVersion* version);
80 void SetWaitingVersion(ServiceWorkerVersion* version);
81 void SetInstallingVersion(ServiceWorkerVersion* version);
83 // If version is the installing, waiting, active version of this
84 // registation, the method will reset that field to NULL, and notify
85 // listeners via OnVersionAttributesChanged.
86 void UnsetVersion(ServiceWorkerVersion* version);
88 // Triggers the [[Activate]] algorithm when the currently active version
89 // has no controllees. If there are no controllees at the time the method
90 // is called, activation is initiated immediately.
91 void ActivateWaitingVersionWhenReady();
93 // Triggers the [[ClearRegistration]] algorithm when the currently
94 // active version has no controllees. Deletes this registration
95 // from storage immediately.
96 void ClearWhenReady();
98 // Restores this registration in storage and cancels the pending
99 // [[ClearRegistration]] algorithm.
100 void AbortPendingClear(const StatusCallback& callback);
102 // The time of the most recent update check.
103 base::Time last_update_check() const { return last_update_check_; }
104 void set_last_update_check(base::Time last) { last_update_check_ = last; }
106 private:
107 friend class base::RefCounted<ServiceWorkerRegistration>;
109 virtual ~ServiceWorkerRegistration();
111 void SetVersionInternal(
112 ServiceWorkerVersion* version,
113 scoped_refptr<ServiceWorkerVersion>* data_member,
114 int change_flag);
115 void UnsetVersionInternal(
116 ServiceWorkerVersion* version,
117 ChangedVersionAttributesMask* mask);
119 // ServiceWorkerVersion::Listener override.
120 virtual void OnNoControllees(ServiceWorkerVersion* version) OVERRIDE;
122 // This method corresponds to the [[Activate]] algorithm.
123 void ActivateWaitingVersion();
124 void OnActivateEventFinished(
125 ServiceWorkerVersion* activating_version,
126 ServiceWorkerStatusCode status);
127 void OnDeleteFinished(ServiceWorkerStatusCode status);
129 // This method corresponds to the [[ClearRegistration]] algorithm.
130 void Clear();
132 void OnRestoreFinished(const StatusCallback& callback,
133 scoped_refptr<ServiceWorkerVersion> version,
134 ServiceWorkerStatusCode status);
136 const GURL pattern_;
137 const int64 registration_id_;
138 bool is_deleted_;
139 bool is_uninstalling_;
140 bool should_activate_when_ready_;
141 base::Time last_update_check_;
142 scoped_refptr<ServiceWorkerVersion> active_version_;
143 scoped_refptr<ServiceWorkerVersion> waiting_version_;
144 scoped_refptr<ServiceWorkerVersion> installing_version_;
145 ObserverList<Listener> listeners_;
146 base::WeakPtr<ServiceWorkerContextCore> context_;
148 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration);
151 } // namespace content
153 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_