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_
; }
58 ServiceWorkerVersion
* active_version() const {
59 return active_version_
.get();
62 ServiceWorkerVersion
* waiting_version() const {
63 return waiting_version_
.get();
66 ServiceWorkerVersion
* installing_version() const {
67 return installing_version_
.get();
70 ServiceWorkerVersion
* GetNewestVersion() const;
72 void AddListener(Listener
* listener
);
73 void RemoveListener(Listener
* listener
);
74 void NotifyRegistrationFailed();
75 void NotifyUpdateFound();
77 ServiceWorkerRegistrationInfo
GetInfo();
79 // Sets the corresposding version attribute and resets the position
80 // (if any) left vacant (ie. by a waiting version being promoted).
81 // Also notifies listeners via OnVersionAttributesChanged.
82 void SetActiveVersion(ServiceWorkerVersion
* version
);
83 void SetWaitingVersion(ServiceWorkerVersion
* version
);
84 void SetInstallingVersion(ServiceWorkerVersion
* version
);
86 // If version is the installing, waiting, active version of this
87 // registation, the method will reset that field to NULL, and notify
88 // listeners via OnVersionAttributesChanged.
89 void UnsetVersion(ServiceWorkerVersion
* version
);
91 // Triggers the [[Activate]] algorithm when the currently active version
92 // has no controllees. If there are no controllees at the time the method
93 // is called, activation is initiated immediately.
94 void ActivateWaitingVersionWhenReady();
96 // Triggers the [[ClearRegistration]] algorithm when the currently
97 // active version has no controllees. Deletes this registration
98 // from storage immediately.
99 void ClearWhenReady();
101 // Restores this registration in storage and cancels the pending
102 // [[ClearRegistration]] algorithm.
103 void AbortPendingClear(const StatusCallback
& callback
);
105 // The time of the most recent update check.
106 base::Time
last_update_check() const { return last_update_check_
; }
107 void set_last_update_check(base::Time last
) { last_update_check_
= last
; }
110 friend class base::RefCounted
<ServiceWorkerRegistration
>;
112 virtual ~ServiceWorkerRegistration();
114 void SetVersionInternal(
115 ServiceWorkerVersion
* version
,
116 scoped_refptr
<ServiceWorkerVersion
>* data_member
,
118 void UnsetVersionInternal(
119 ServiceWorkerVersion
* version
,
120 ChangedVersionAttributesMask
* mask
);
122 // ServiceWorkerVersion::Listener override.
123 virtual void OnNoControllees(ServiceWorkerVersion
* version
) OVERRIDE
;
125 // This method corresponds to the [[Activate]] algorithm.
126 void ActivateWaitingVersion();
127 void OnActivateEventFinished(
128 ServiceWorkerVersion
* activating_version
,
129 ServiceWorkerStatusCode status
);
130 void OnDeleteFinished(ServiceWorkerStatusCode status
);
132 // This method corresponds to the [[ClearRegistration]] algorithm.
135 void OnRestoreFinished(const StatusCallback
& callback
,
136 scoped_refptr
<ServiceWorkerVersion
> version
,
137 ServiceWorkerStatusCode status
);
140 const int64 registration_id_
;
142 bool is_uninstalling_
;
143 bool should_activate_when_ready_
;
144 base::Time last_update_check_
;
145 scoped_refptr
<ServiceWorkerVersion
> active_version_
;
146 scoped_refptr
<ServiceWorkerVersion
> waiting_version_
;
147 scoped_refptr
<ServiceWorkerVersion
> installing_version_
;
148 ObserverList
<Listener
> listeners_
;
149 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
151 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegistration
);
154 } // namespace content
156 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTRATION_H_