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_serviceworker_h__
8
#define mozilla_dom_serviceworker_h__
9
10
#include "mozilla/DOMEventTargetHelper.h"
11
#include "mozilla/dom/BindingDeclarations.h"
12
#include "mozilla/dom/ServiceWorkerDescriptor.h"
13
#include "mozilla/dom/ServiceWorkerUtils.h"
14
15
#ifdef XP_WIN
16
# undef PostMessage
17
#endif
18
19
class nsIGlobalObject;
20
21
namespace mozilla {
22
namespace dom {
23
24
struct PostMessageOptions;
25
class ServiceWorkerCloneData;
26
27
#define NS_DOM_SERVICEWORKER_IID \
28
{ \
29
0xd42e0611, 0x3647, 0x4319, { \
30
0xae, 0x05, 0x19, 0x89, 0x59, 0xba, 0x99, 0x5e \
31
} \
32
}
33
34
bool ServiceWorkerVisible(JSContext* aCx, JSObject* aObj);
35
36
class ServiceWorker final : public DOMEventTargetHelper {
37
public:
38
// Abstract interface for the internal representation of the
39
// ServiceWorker object.
40
class Inner {
41
public:
42
// This will be called when a DOM ServiceWorker object is
43
// created and takes a strong ref to the Inner object.
44
// RemoveServiceWorker() is guaranteed to be called on the
45
// current thread before the ServiceWorker is destroyed.
46
//
47
// In addition, the Inner object should check to see if
48
// the ServiceWorker's state is correct. If not, it should
49
// be updated automatically by calling SetState(). This is
50
// necessary to handle race conditions where the DOM
51
// ServiceWorker object is created while the state is being
52
// updated in another process.
53
virtual void AddServiceWorker(ServiceWorker* aWorker) = 0;
54
55
// This is called when the DOM ServiceWorker object is
56
// destroyed and drops its ref to the Inner object.
57
virtual void RemoveServiceWorker(ServiceWorker* aWorker) = 0;
58
59
// Get the associated registration for this ServiceWorker. The success
60
// callback should always be called asynchronously.
61
virtual void GetRegistration(ServiceWorkerRegistrationCallback&& aSuccessCB,
62
ServiceWorkerFailureCallback&& aFailureCB) = 0;
63
64
virtual void PostMessage(RefPtr<ServiceWorkerCloneData>&& aData,
65
const ClientInfo& aClientInfo,
66
const ClientState& aClientState) = 0;
67
68
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
69
};
70
71
NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_SERVICEWORKER_IID)
72
NS_DECL_ISUPPORTS_INHERITED
73
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorker, DOMEventTargetHelper)
74
75
IMPL_EVENT_HANDLER(statechange)
76
IMPL_EVENT_HANDLER(error)
77
78
static already_AddRefed<ServiceWorker> Create(
79
nsIGlobalObject* aOwner, const ServiceWorkerDescriptor& aDescriptor);
80
81
virtual JSObject* WrapObject(JSContext* aCx,
82
JS::Handle<JSObject*> aGivenProto) override;
83
84
ServiceWorkerState State() const;
85
86
void SetState(ServiceWorkerState aState);
87
88
void MaybeDispatchStateChangeEvent();
89
90
void GetScriptURL(nsString& aURL) const;
91
92
void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
93
const Sequence<JSObject*>& aTransferable, ErrorResult& aRv);
94
95
void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
96
const PostMessageOptions& aOptions, ErrorResult& aRv);
97
98
const ServiceWorkerDescriptor& Descriptor() const;
99
100
void DisconnectFromOwner() override;
101
102
private:
103
ServiceWorker(nsIGlobalObject* aWindow,
104
const ServiceWorkerDescriptor& aDescriptor, Inner* aInner);
105
106
// This class is reference-counted and will be destroyed from Release().
107
~ServiceWorker();
108
109
void MaybeAttachToRegistration(ServiceWorkerRegistration* aRegistration);
110
111
ServiceWorkerDescriptor mDescriptor;
112
113
RefPtr<Inner> mInner;
114
RefPtr<ServiceWorkerRegistration> mRegistration;
115
ServiceWorkerState mLastNotifiedState;
116
};
117
118
NS_DEFINE_STATIC_IID_ACCESSOR(ServiceWorker, NS_DOM_SERVICEWORKER_IID)
119
120
} // namespace dom
121
} // namespace mozilla
122
123
#endif // mozilla_dom_serviceworker_h__