EventSourceEventService.h (2390B)
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_EventSourceEventService_h 8 #define mozilla_dom_EventSourceEventService_h 9 10 #include "mozilla/AlreadyAddRefed.h" 11 #include "mozilla/Atomics.h" 12 #include "nsCOMPtr.h" 13 #include "nsClassHashtable.h" 14 #include "nsHashKeys.h" 15 #include "nsIEventSourceEventService.h" 16 #include "nsIObserver.h" 17 #include "nsISupportsImpl.h" 18 #include "nsTArray.h" 19 20 namespace mozilla::dom { 21 22 class EventSourceEventService final : public nsIEventSourceEventService, 23 public nsIObserver { 24 friend class EventSourceBaseRunnable; 25 26 public: 27 NS_DECL_THREADSAFE_ISUPPORTS 28 NS_DECL_NSIOBSERVER 29 NS_DECL_NSIEVENTSOURCEEVENTSERVICE 30 31 static already_AddRefed<EventSourceEventService> GetOrCreate(); 32 33 void EventSourceConnectionOpened(uint64_t aHttpChannelId, 34 uint64_t aInnerWindowID); 35 36 void EventSourceConnectionClosed(uint64_t aHttpChannelId, 37 uint64_t aInnerWindowID); 38 39 void EventReceived(uint64_t aHttpChannelId, uint64_t aInnerWindowID, 40 const nsAString& aEventName, const nsAString& aLastEventID, 41 const nsAString& aData, uint32_t aRetry, 42 DOMHighResTimeStamp aTimeStamp); 43 44 private: 45 EventSourceEventService(); 46 ~EventSourceEventService(); 47 48 bool HasListeners() const; 49 void Shutdown(); 50 51 using EventSourceListeners = nsTArray<nsCOMPtr<nsIEventSourceEventListener>>; 52 53 struct WindowListener { 54 EventSourceListeners mListeners; 55 }; 56 57 void GetListeners(uint64_t aInnerWindowID, 58 EventSourceListeners& aListeners) const; 59 60 // Used only on the main-thread. 61 nsClassHashtable<nsUint64HashKey, WindowListener> mWindows; 62 63 Atomic<uint64_t> mCountListeners; 64 }; 65 66 } // namespace mozilla::dom 67 68 /** 69 * Casting EventSourceEventService to nsISupports is ambiguous. 70 * This method handles that. 71 */ 72 inline nsISupports* ToSupports(mozilla::dom::EventSourceEventService* p) { 73 return NS_ISUPPORTS_CAST(nsIEventSourceEventService*, p); 74 } 75 76 #endif // mozilla_dom_EventSourceEventService_h