summaryrefslogtreecommitdiffstats
path: root/dom/base/EventSourceEventService.cpp
blob: 46aba37f8ec5f6aaf3fb9b1ced53e3a516544fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "EventSourceEventService.h"
#include "mozilla/StaticPtr.h"
#include "nsISupportsPrimitives.h"
#include "nsIObserverService.h"
#include "nsXULAppAPI.h"
#include "nsSocketTransportService2.h"
#include "nsThreadUtils.h"
#include "mozilla/Services.h"

namespace mozilla::dom {

namespace {

StaticRefPtr<EventSourceEventService> gEventSourceEventService;

}  // anonymous namespace

class EventSourceBaseRunnable : public Runnable {
 public:
  EventSourceBaseRunnable(uint64_t aHttpChannelId, uint64_t aInnerWindowID)
      : Runnable("dom::EventSourceBaseRunnable"),
        mHttpChannelId(aHttpChannelId),
        mInnerWindowID(aInnerWindowID) {}

  NS_IMETHOD Run() override {
    MOZ_ASSERT(NS_IsMainThread());
    RefPtr<EventSourceEventService> service =
        EventSourceEventService::GetOrCreate();
    MOZ_ASSERT(service);

    EventSourceEventService::EventSourceListeners listeners;

    service->GetListeners(mInnerWindowID, listeners);

    for (uint32_t i = 0; i < listeners.Length(); ++i) {
      DoWork(listeners[i]);
    }

    return NS_OK;
  }

 protected:
  ~EventSourceBaseRunnable() = default;

  virtual void DoWork(nsIEventSourceEventListener* aListener) = 0;

  uint64_t mHttpChannelId;
  uint64_t mInnerWindowID;
};

class EventSourceConnectionOpenedRunnable final
    : public EventSourceBaseRunnable {
 public:
  EventSourceConnectionOpenedRunnable(uint64_t aHttpChannelId,
                                      uint64_t aInnerWindowID)
      : EventSourceBaseRunnable(aHttpChannelId, aInnerWindowID) {}

 private:
  virtual void DoWork(nsIEventSourceEventListener* aListener) override {
    DebugOnly<nsresult> rv =
        aListener->EventSourceConnectionOpened(mHttpChannelId);
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                         "EventSourceConnectionOpened failed");
  }
};

class EventSourceConnectionClosedRunnable final
    : public EventSourceBaseRunnable {
 public:
  EventSourceConnectionClosedRunnable(uint64_t aHttpChannelId,
                                      uint64_t aInnerWindowID)
      : EventSourceBaseRunnable(aHttpChannelId, aInnerWindowID) {}

 private:
  virtual void DoWork(nsIEventSourceEventListener* aListener) override {
    DebugOnly<nsresult> rv =
        aListener->EventSourceConnectionClosed(mHttpChannelId);
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                         "EventSourceConnectionClosed failed");
  }
};

class EventSourceEventRunnable final : public EventSourceBaseRunnable {
 public:
  EventSourceEventRunnable(uint64_t aHttpChannelId, uint64_t aInnerWindowID,
                           const nsAString& aEventName,
                           const nsAString& aLastEventID,
                           const nsAString& aData, uint32_t aRetry,
                           DOMHighResTimeStamp aTimeStamp)
      : EventSourceBaseRunnable(aHttpChannelId, aInnerWindowID),
        mEventName(aEventName),
        mLastEventID(aLastEventID),
        mData(aData),
        mRetry(aRetry),
        mTimeStamp(aTimeStamp) {}

 private:
  virtual void DoWork(nsIEventSourceEventListener* aListener) override {
    DebugOnly<nsresult> rv = aListener->EventReceived(
        mHttpChannelId, mEventName, mLastEventID, mData, mRetry, mTimeStamp);

    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Event op failed");
  }

  nsString mEventName;
  nsString mLastEventID;
  nsString mData;
  uint32_t mRetry;
  DOMHighResTimeStamp mTimeStamp;
};

/* static */
already_AddRefed<EventSourceEventService>
EventSourceEventService::GetOrCreate() {
  MOZ_ASSERT(NS_IsMainThread());

  if (!gEventSourceEventService) {
    gEventSourceEventService = new EventSourceEventService();
  }

  RefPtr<EventSourceEventService> service = gEventSourceEventService.get();
  return service.forget();
}

NS_INTERFACE_MAP_BEGIN(EventSourceEventService)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIEventSourceEventService)
  NS_INTERFACE_MAP_ENTRY(nsIObserver)
  NS_INTERFACE_MAP_ENTRY(nsIEventSourceEventService)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(EventSourceEventService)
NS_IMPL_RELEASE(EventSourceEventService)

EventSourceEventService::EventSourceEventService() : mCountListeners(0) {
  MOZ_ASSERT(NS_IsMainThread());

  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
  if (obs) {
    obs->AddObserver(this, "xpcom-shutdown", false);
    obs->AddObserver(this, "inner-window-destroyed", false);
  }
}

EventSourceEventService::~EventSourceEventService() {
  MOZ_ASSERT(NS_IsMainThread());
}

void EventSourceEventService::EventSourceConnectionOpened(
    uint64_t aHttpChannelId, uint64_t aInnerWindowID) {
  // Let's continue only if we have some listeners.
  if (!HasListeners()) {
    return;
  }

  RefPtr<EventSourceConnectionOpenedRunnable> runnable =
      new EventSourceConnectionOpenedRunnable(aHttpChannelId, aInnerWindowID);
  DebugOnly<nsresult> rv = NS_DispatchToMainThread(runnable);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread failed");
}

void EventSourceEventService::EventSourceConnectionClosed(
    uint64_t aHttpChannelId, uint64_t aInnerWindowID) {
  // Let's continue only if we have some listeners.
  if (!HasListeners()) {
    return;
  }
  RefPtr<EventSourceConnectionClosedRunnable> runnable =
      new EventSourceConnectionClosedRunnable(aHttpChannelId, aInnerWindowID);
  DebugOnly<nsresult> rv = NS_DispatchToMainThread(runnable);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread failed");
}

void EventSourceEventService::EventReceived(
    uint64_t aHttpChannelId, uint64_t aInnerWindowID,
    const nsAString& aEventName, const nsAString& aLastEventID,
    const nsAString& aData, uint32_t aRetry, DOMHighResTimeStamp aTimeStamp) {
  // Let's continue only if we have some listeners.
  if (!HasListeners()) {
    return;
  }

  RefPtr<EventSourceEventRunnable> runnable =
      new EventSourceEventRunnable(aHttpChannelId, aInnerWindowID, aEventName,
                                   aLastEventID, aData, aRetry, aTimeStamp);
  DebugOnly<nsresult> rv = NS_DispatchToMainThread(runnable);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread failed");
}

NS_IMETHODIMP
EventSourceEventService::AddListener(uint64_t aInnerWindowID,
                                     nsIEventSourceEventListener* aListener) {
  MOZ_ASSERT(NS_IsMainThread());
  if (!aListener) {
    return NS_ERROR_FAILURE;
  }
  ++mCountListeners;

  WindowListener* listener = mWindows.Get(aInnerWindowID);
  if (!listener) {
    listener = new WindowListener();
    mWindows.Put(aInnerWindowID, listener);
  }

  listener->mListeners.AppendElement(aListener);

  return NS_OK;
}

NS_IMETHODIMP
EventSourceEventService::RemoveListener(
    uint64_t aInnerWindowID, nsIEventSourceEventListener* aListener) {
  MOZ_ASSERT(NS_IsMainThread());

  if (!aListener) {
    return NS_ERROR_FAILURE;
  }

  WindowListener* listener = mWindows.Get(aInnerWindowID);
  if (!listener) {
    return NS_ERROR_FAILURE;
  }

  if (!listener->mListeners.RemoveElement(aListener)) {
    return NS_ERROR_FAILURE;
  }

  // The last listener for this window.
  if (listener->mListeners.IsEmpty()) {
    mWindows.Remove(aInnerWindowID);
  }

  MOZ_ASSERT(mCountListeners);
  --mCountListeners;

  return NS_OK;
}

NS_IMETHODIMP
EventSourceEventService::HasListenerFor(uint64_t aInnerWindowID,
                                        bool* aResult) {
  MOZ_ASSERT(NS_IsMainThread());

  *aResult = mWindows.Get(aInnerWindowID);

  return NS_OK;
}

NS_IMETHODIMP
EventSourceEventService::Observe(nsISupports* aSubject, const char* aTopic,
                                 const char16_t* aData) {
  MOZ_ASSERT(NS_IsMainThread());

  if (!strcmp(aTopic, "xpcom-shutdown")) {
    Shutdown();
    return NS_OK;
  }

  if (!strcmp(aTopic, "inner-window-destroyed") && HasListeners()) {
    nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
    NS_ENSURE_TRUE(wrapper, NS_ERROR_FAILURE);

    uint64_t innerID;
    nsresult rv = wrapper->GetData(&innerID);
    NS_ENSURE_SUCCESS(rv, rv);

    WindowListener* listener = mWindows.Get(innerID);
    if (!listener) {
      return NS_OK;
    }

    MOZ_ASSERT(mCountListeners >= listener->mListeners.Length());
    mCountListeners -= listener->mListeners.Length();
    mWindows.Remove(innerID);
  }

  // This should not happen.
  return NS_ERROR_FAILURE;
}

void EventSourceEventService::Shutdown() {
  MOZ_ASSERT(NS_IsMainThread());

  if (gEventSourceEventService) {
    nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
    if (obs) {
      obs->RemoveObserver(gEventSourceEventService, "xpcom-shutdown");
      obs->RemoveObserver(gEventSourceEventService, "inner-window-destroyed");
    }

    mWindows.Clear();
    gEventSourceEventService = nullptr;
  }
}

bool EventSourceEventService::HasListeners() const { return !!mCountListeners; }

void EventSourceEventService::GetListeners(
    uint64_t aInnerWindowID,
    EventSourceEventService::EventSourceListeners& aListeners) const {
  aListeners.Clear();

  WindowListener* listener = mWindows.Get(aInnerWindowID);
  if (!listener) {
    return;
  }

  aListeners.AppendElements(listener->mListeners);
}

}  // namespace mozilla::dom