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/. */
7 #include "EventSourceEventService.h"
8 #include "mozilla/StaticPtr.h"
9 #include "nsISupportsPrimitives.h"
10 #include "nsIObserverService.h"
11 #include "nsXULAppAPI.h"
12 #include "nsSocketTransportService2.h"
13 #include "nsThreadUtils.h"
14 #include "mozilla/Services.h"
16 namespace mozilla::dom
{
20 StaticRefPtr
<EventSourceEventService
> gEventSourceEventService
;
22 } // anonymous namespace
24 class EventSourceBaseRunnable
: public Runnable
{
26 EventSourceBaseRunnable(uint64_t aHttpChannelId
, uint64_t aInnerWindowID
)
27 : Runnable("dom::EventSourceBaseRunnable"),
28 mHttpChannelId(aHttpChannelId
),
29 mInnerWindowID(aInnerWindowID
) {}
31 NS_IMETHOD
Run() override
{
32 MOZ_ASSERT(NS_IsMainThread());
33 RefPtr
<EventSourceEventService
> service
=
34 EventSourceEventService::GetOrCreate();
37 EventSourceEventService::EventSourceListeners listeners
;
39 service
->GetListeners(mInnerWindowID
, listeners
);
41 for (uint32_t i
= 0; i
< listeners
.Length(); ++i
) {
49 ~EventSourceBaseRunnable() = default;
51 virtual void DoWork(nsIEventSourceEventListener
* aListener
) = 0;
53 uint64_t mHttpChannelId
;
54 uint64_t mInnerWindowID
;
57 class EventSourceConnectionOpenedRunnable final
58 : public EventSourceBaseRunnable
{
60 EventSourceConnectionOpenedRunnable(uint64_t aHttpChannelId
,
61 uint64_t aInnerWindowID
)
62 : EventSourceBaseRunnable(aHttpChannelId
, aInnerWindowID
) {}
65 virtual void DoWork(nsIEventSourceEventListener
* aListener
) override
{
66 DebugOnly
<nsresult
> rv
=
67 aListener
->EventSourceConnectionOpened(mHttpChannelId
);
68 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
),
69 "EventSourceConnectionOpened failed");
73 class EventSourceConnectionClosedRunnable final
74 : public EventSourceBaseRunnable
{
76 EventSourceConnectionClosedRunnable(uint64_t aHttpChannelId
,
77 uint64_t aInnerWindowID
)
78 : EventSourceBaseRunnable(aHttpChannelId
, aInnerWindowID
) {}
81 virtual void DoWork(nsIEventSourceEventListener
* aListener
) override
{
82 DebugOnly
<nsresult
> rv
=
83 aListener
->EventSourceConnectionClosed(mHttpChannelId
);
84 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
),
85 "EventSourceConnectionClosed failed");
89 class EventSourceEventRunnable final
: public EventSourceBaseRunnable
{
91 EventSourceEventRunnable(uint64_t aHttpChannelId
, uint64_t aInnerWindowID
,
92 const nsAString
& aEventName
,
93 const nsAString
& aLastEventID
,
94 const nsAString
& aData
, uint32_t aRetry
,
95 DOMHighResTimeStamp aTimeStamp
)
96 : EventSourceBaseRunnable(aHttpChannelId
, aInnerWindowID
),
97 mEventName(aEventName
),
98 mLastEventID(aLastEventID
),
101 mTimeStamp(aTimeStamp
) {}
104 virtual void DoWork(nsIEventSourceEventListener
* aListener
) override
{
105 DebugOnly
<nsresult
> rv
= aListener
->EventReceived(
106 mHttpChannelId
, mEventName
, mLastEventID
, mData
, mRetry
, mTimeStamp
);
108 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
), "Event op failed");
112 nsString mLastEventID
;
115 DOMHighResTimeStamp mTimeStamp
;
119 already_AddRefed
<EventSourceEventService
>
120 EventSourceEventService::GetOrCreate() {
121 MOZ_ASSERT(NS_IsMainThread());
123 if (!gEventSourceEventService
) {
124 gEventSourceEventService
= new EventSourceEventService();
127 RefPtr
<EventSourceEventService
> service
= gEventSourceEventService
.get();
128 return service
.forget();
131 NS_INTERFACE_MAP_BEGIN(EventSourceEventService
)
132 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIEventSourceEventService
)
133 NS_INTERFACE_MAP_ENTRY(nsIObserver
)
134 NS_INTERFACE_MAP_ENTRY(nsIEventSourceEventService
)
137 NS_IMPL_ADDREF(EventSourceEventService
)
138 NS_IMPL_RELEASE(EventSourceEventService
)
140 EventSourceEventService::EventSourceEventService() : mCountListeners(0) {
141 MOZ_ASSERT(NS_IsMainThread());
143 nsCOMPtr
<nsIObserverService
> obs
= mozilla::services::GetObserverService();
145 obs
->AddObserver(this, "xpcom-shutdown", false);
146 obs
->AddObserver(this, "inner-window-destroyed", false);
150 EventSourceEventService::~EventSourceEventService() {
151 MOZ_ASSERT(NS_IsMainThread());
154 void EventSourceEventService::EventSourceConnectionOpened(
155 uint64_t aHttpChannelId
, uint64_t aInnerWindowID
) {
156 // Let's continue only if we have some listeners.
157 if (!HasListeners()) {
161 RefPtr
<EventSourceConnectionOpenedRunnable
> runnable
=
162 new EventSourceConnectionOpenedRunnable(aHttpChannelId
, aInnerWindowID
);
163 DebugOnly
<nsresult
> rv
= NS_DispatchToMainThread(runnable
);
164 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
), "NS_DispatchToMainThread failed");
167 void EventSourceEventService::EventSourceConnectionClosed(
168 uint64_t aHttpChannelId
, uint64_t aInnerWindowID
) {
169 // Let's continue only if we have some listeners.
170 if (!HasListeners()) {
173 RefPtr
<EventSourceConnectionClosedRunnable
> runnable
=
174 new EventSourceConnectionClosedRunnable(aHttpChannelId
, aInnerWindowID
);
175 DebugOnly
<nsresult
> rv
= NS_DispatchToMainThread(runnable
);
176 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
), "NS_DispatchToMainThread failed");
179 void EventSourceEventService::EventReceived(
180 uint64_t aHttpChannelId
, uint64_t aInnerWindowID
,
181 const nsAString
& aEventName
, const nsAString
& aLastEventID
,
182 const nsAString
& aData
, uint32_t aRetry
, DOMHighResTimeStamp aTimeStamp
) {
183 // Let's continue only if we have some listeners.
184 if (!HasListeners()) {
188 RefPtr
<EventSourceEventRunnable
> runnable
=
189 new EventSourceEventRunnable(aHttpChannelId
, aInnerWindowID
, aEventName
,
190 aLastEventID
, aData
, aRetry
, aTimeStamp
);
191 DebugOnly
<nsresult
> rv
= NS_DispatchToMainThread(runnable
);
192 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
), "NS_DispatchToMainThread failed");
196 EventSourceEventService::AddListener(uint64_t aInnerWindowID
,
197 nsIEventSourceEventListener
* aListener
) {
198 MOZ_ASSERT(NS_IsMainThread());
200 return NS_ERROR_FAILURE
;
204 WindowListener
* listener
= mWindows
.GetOrInsertNew(aInnerWindowID
);
206 listener
->mListeners
.AppendElement(aListener
);
212 EventSourceEventService::RemoveListener(
213 uint64_t aInnerWindowID
, nsIEventSourceEventListener
* aListener
) {
214 MOZ_ASSERT(NS_IsMainThread());
217 return NS_ERROR_FAILURE
;
220 WindowListener
* listener
= mWindows
.Get(aInnerWindowID
);
222 return NS_ERROR_FAILURE
;
225 if (!listener
->mListeners
.RemoveElement(aListener
)) {
226 return NS_ERROR_FAILURE
;
229 // The last listener for this window.
230 if (listener
->mListeners
.IsEmpty()) {
231 mWindows
.Remove(aInnerWindowID
);
234 MOZ_ASSERT(mCountListeners
);
241 EventSourceEventService::HasListenerFor(uint64_t aInnerWindowID
,
243 MOZ_ASSERT(NS_IsMainThread());
245 *aResult
= mWindows
.Get(aInnerWindowID
);
251 EventSourceEventService::Observe(nsISupports
* aSubject
, const char* aTopic
,
252 const char16_t
* aData
) {
253 MOZ_ASSERT(NS_IsMainThread());
255 if (!strcmp(aTopic
, "xpcom-shutdown")) {
260 if (!strcmp(aTopic
, "inner-window-destroyed") && HasListeners()) {
261 nsCOMPtr
<nsISupportsPRUint64
> wrapper
= do_QueryInterface(aSubject
);
262 NS_ENSURE_TRUE(wrapper
, NS_ERROR_FAILURE
);
265 nsresult rv
= wrapper
->GetData(&innerID
);
266 NS_ENSURE_SUCCESS(rv
, rv
);
268 WindowListener
* listener
= mWindows
.Get(innerID
);
273 MOZ_ASSERT(mCountListeners
>= listener
->mListeners
.Length());
274 mCountListeners
-= listener
->mListeners
.Length();
275 mWindows
.Remove(innerID
);
278 // This should not happen.
279 return NS_ERROR_FAILURE
;
282 void EventSourceEventService::Shutdown() {
283 MOZ_ASSERT(NS_IsMainThread());
285 if (gEventSourceEventService
) {
286 nsCOMPtr
<nsIObserverService
> obs
= mozilla::services::GetObserverService();
288 obs
->RemoveObserver(gEventSourceEventService
, "xpcom-shutdown");
289 obs
->RemoveObserver(gEventSourceEventService
, "inner-window-destroyed");
293 gEventSourceEventService
= nullptr;
297 bool EventSourceEventService::HasListeners() const { return !!mCountListeners
; }
299 void EventSourceEventService::GetListeners(
300 uint64_t aInnerWindowID
,
301 EventSourceEventService::EventSourceListeners
& aListeners
) const {
304 WindowListener
* listener
= mWindows
.Get(aInnerWindowID
);
309 aListeners
.AppendElements(listener
->mListeners
);
312 } // namespace mozilla::dom