Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_ServiceWorkerRegistration_h
8
#define mozilla_dom_ServiceWorkerRegistration_h
9
10
#include "mozilla/DOMEventTargetHelper.h"
11
#include "mozilla/dom/ServiceWorkerBinding.h"
12
#include "mozilla/dom/ServiceWorkerRegistrationBinding.h"
13
#include "mozilla/dom/ServiceWorkerRegistrationDescriptor.h"
14
#include "mozilla/dom/ServiceWorkerUtils.h"
15
16
// Support for Notification API extension.
17
#include "mozilla/dom/NotificationBinding.h"
18
19
class nsIGlobalObject;
20
21
namespace mozilla {
22
namespace dom {
23
24
class Promise;
25
class PushManager;
26
class WorkerPrivate;
27
class ServiceWorker;
28
29
#define NS_DOM_SERVICEWORKERREGISTRATION_IID \
30
{ \
31
0x4578a90e, 0xa427, 0x4237, { \
32
0x98, 0x4a, 0xbd, 0x98, 0xe4, 0xcd, 0x5f, 0x3a \
33
} \
34
}
35
36
class ServiceWorkerRegistration final : public DOMEventTargetHelper {
37
public:
38
class Inner {
39
public:
40
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
41
42
virtual void SetServiceWorkerRegistration(
43
ServiceWorkerRegistration* aReg) = 0;
44
45
virtual void ClearServiceWorkerRegistration(
46
ServiceWorkerRegistration* aReg) = 0;
47
48
virtual void Update(ServiceWorkerRegistrationCallback&& aSuccessCB,
49
ServiceWorkerFailureCallback&& aFailureCB) = 0;
50
51
virtual void Unregister(ServiceWorkerBoolCallback&& aSuccessCB,
52
ServiceWorkerFailureCallback&& aFailureCB) = 0;
53
};
54
55
NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_SERVICEWORKERREGISTRATION_IID)
56
NS_DECL_ISUPPORTS_INHERITED
57
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerRegistration,
58
DOMEventTargetHelper)
59
60
IMPL_EVENT_HANDLER(updatefound)
61
62
static already_AddRefed<ServiceWorkerRegistration> CreateForMainThread(
63
nsPIDOMWindowInner* aWindow,
64
const ServiceWorkerRegistrationDescriptor& aDescriptor);
65
66
static already_AddRefed<ServiceWorkerRegistration> CreateForWorker(
67
WorkerPrivate* aWorkerPrivate, nsIGlobalObject* aGlobal,
68
const ServiceWorkerRegistrationDescriptor& aDescriptor);
69
70
JSObject* WrapObject(JSContext* aCx,
71
JS::Handle<JSObject*> aGivenProto) override;
72
73
void DisconnectFromOwner() override;
74
75
void RegistrationCleared();
76
77
already_AddRefed<ServiceWorker> GetInstalling() const;
78
79
already_AddRefed<ServiceWorker> GetWaiting() const;
80
81
already_AddRefed<ServiceWorker> GetActive() const;
82
83
void UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor);
84
85
bool MatchesDescriptor(
86
const ServiceWorkerRegistrationDescriptor& aDescriptor) const;
87
88
void GetScope(nsAString& aScope) const;
89
90
ServiceWorkerUpdateViaCache GetUpdateViaCache(ErrorResult& aRv) const;
91
92
already_AddRefed<Promise> Update(ErrorResult& aRv);
93
94
already_AddRefed<Promise> Unregister(ErrorResult& aRv);
95
96
already_AddRefed<PushManager> GetPushManager(JSContext* aCx,
97
ErrorResult& aRv);
98
99
already_AddRefed<Promise> ShowNotification(
100
JSContext* aCx, const nsAString& aTitle,
101
const NotificationOptions& aOptions, ErrorResult& aRv);
102
103
already_AddRefed<Promise> GetNotifications(
104
const GetNotificationOptions& aOptions, ErrorResult& aRv);
105
106
const ServiceWorkerRegistrationDescriptor& Descriptor() const;
107
108
void WhenVersionReached(uint64_t aVersion,
109
ServiceWorkerBoolCallback&& aCallback);
110
111
void MaybeDispatchUpdateFoundRunnable();
112
113
private:
114
ServiceWorkerRegistration(
115
nsIGlobalObject* aGlobal,
116
const ServiceWorkerRegistrationDescriptor& aDescriptor, Inner* aInner);
117
118
~ServiceWorkerRegistration();
119
120
void UpdateStateInternal(const Maybe<ServiceWorkerDescriptor>& aInstalling,
121
const Maybe<ServiceWorkerDescriptor>& aWaiting,
122
const Maybe<ServiceWorkerDescriptor>& aActive);
123
124
void MaybeScheduleUpdateFound(
125
const Maybe<ServiceWorkerDescriptor>& aInstallingDescriptor);
126
127
void MaybeDispatchUpdateFound();
128
129
ServiceWorkerRegistrationDescriptor mDescriptor;
130
RefPtr<Inner> mInner;
131
132
RefPtr<ServiceWorker> mInstallingWorker;
133
RefPtr<ServiceWorker> mWaitingWorker;
134
RefPtr<ServiceWorker> mActiveWorker;
135
RefPtr<PushManager> mPushManager;
136
137
struct VersionCallback {
138
uint64_t mVersion;
139
ServiceWorkerBoolCallback mFunc;
140
141
VersionCallback(uint64_t aVersion, ServiceWorkerBoolCallback&& aFunc)
142
: mVersion(aVersion), mFunc(std::move(aFunc)) {
143
MOZ_DIAGNOSTIC_ASSERT(mFunc);
144
}
145
};
146
nsTArray<UniquePtr<VersionCallback>> mVersionCallbackList;
147
148
uint64_t mScheduledUpdateFoundId;
149
uint64_t mDispatchedUpdateFoundId;
150
};
151
152
NS_DEFINE_STATIC_IID_ACCESSOR(ServiceWorkerRegistration,
153
NS_DOM_SERVICEWORKERREGISTRATION_IID)
154
155
} // namespace dom
156
} // namespace mozilla
157
158
#endif /* mozilla_dom_ServiceWorkerRegistration_h */