WebSocketEventService.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_net_WebSocketEventService_h
  6. #define mozilla_net_WebSocketEventService_h
  7. #include "mozilla/AlreadyAddRefed.h"
  8. #include "mozilla/Atomics.h"
  9. #include "nsIWebSocketEventService.h"
  10. #include "nsAutoPtr.h"
  11. #include "nsCOMPtr.h"
  12. #include "nsClassHashtable.h"
  13. #include "nsHashKeys.h"
  14. #include "nsIObserver.h"
  15. #include "nsISupportsImpl.h"
  16. #include "nsTArray.h"
  17. namespace mozilla {
  18. namespace net {
  19. class WebSocketFrame;
  20. class WebSocketEventListenerChild;
  21. class WebSocketEventService final : public nsIWebSocketEventService
  22. , public nsIObserver
  23. {
  24. friend class WebSocketBaseRunnable;
  25. public:
  26. NS_DECL_ISUPPORTS
  27. NS_DECL_NSIOBSERVER
  28. NS_DECL_NSIWEBSOCKETEVENTSERVICE
  29. static already_AddRefed<WebSocketEventService> GetOrCreate();
  30. void WebSocketCreated(uint32_t aWebSocketSerialID,
  31. uint64_t aInnerWindowID,
  32. const nsAString& aURI,
  33. const nsACString& aProtocols);
  34. void WebSocketOpened(uint32_t aWebSocketSerialID,
  35. uint64_t aInnerWindowID,
  36. const nsAString& aEffectiveURI,
  37. const nsACString& aProtocols,
  38. const nsACString& aExtensions);
  39. void WebSocketMessageAvailable(uint32_t aWebSocketSerialID,
  40. uint64_t aInnerWindowID,
  41. const nsACString& aData,
  42. uint16_t aMessageType);
  43. void WebSocketClosed(uint32_t aWebSocketSerialID,
  44. uint64_t aInnerWindowID,
  45. bool aWasClean,
  46. uint16_t aCode,
  47. const nsAString& aReason);
  48. void FrameReceived(uint32_t aWebSocketSerialID,
  49. uint64_t aInnerWindowID,
  50. already_AddRefed<WebSocketFrame> aFrame);
  51. void FrameSent(uint32_t aWebSocketSerialID,
  52. uint64_t aInnerWindowID,
  53. already_AddRefed<WebSocketFrame> aFrame);
  54. already_AddRefed<WebSocketFrame>
  55. CreateFrameIfNeeded(bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3,
  56. uint8_t aOpCode, bool aMaskBit, uint32_t aMask,
  57. const nsCString& aPayload);
  58. already_AddRefed<WebSocketFrame>
  59. CreateFrameIfNeeded(bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3,
  60. uint8_t aOpCode, bool aMaskBit, uint32_t aMask,
  61. uint8_t* aPayload, uint32_t aPayloadLength);
  62. already_AddRefed<WebSocketFrame>
  63. CreateFrameIfNeeded(bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3,
  64. uint8_t aOpCode, bool aMaskBit, uint32_t aMask,
  65. uint8_t* aPayloadInHdr, uint32_t aPayloadInHdrLength,
  66. uint8_t* aPayload, uint32_t aPayloadLength);
  67. private:
  68. WebSocketEventService();
  69. ~WebSocketEventService();
  70. bool HasListeners() const;
  71. void Shutdown();
  72. typedef nsTArray<nsCOMPtr<nsIWebSocketEventListener>> WindowListeners;
  73. struct WindowListener
  74. {
  75. WindowListeners mListeners;
  76. RefPtr<WebSocketEventListenerChild> mActor;
  77. };
  78. void GetListeners(uint64_t aInnerWindowID,
  79. WindowListeners& aListeners) const;
  80. void ShutdownActorListener(WindowListener* aListener);
  81. // Used only on the main-thread.
  82. nsClassHashtable<nsUint64HashKey, WindowListener> mWindows;
  83. Atomic<uint64_t> mCountListeners;
  84. };
  85. } // net namespace
  86. } // mozilla namespace
  87. /**
  88. * Casting WebSocketEventService to nsISupports is ambiguous.
  89. * This method handles that.
  90. */
  91. inline nsISupports*
  92. ToSupports(mozilla::net::WebSocketEventService* p)
  93. {
  94. return NS_ISUPPORTS_CAST(nsIWebSocketEventService*, p);
  95. }
  96. #endif // mozilla_net_WebSocketEventService_h