2015-10-26 18:30:11 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2015-10-28 22:10:20 +03:00
|
|
|
#ifndef mozilla_net_WebSocketEventService_h
|
|
|
|
#define mozilla_net_WebSocketEventService_h
|
2015-10-26 18:30:11 +03:00
|
|
|
|
2015-11-09 04:44:11 +03:00
|
|
|
#include "mozilla/AlreadyAddRefed.h"
|
2015-10-26 18:30:11 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2015-10-28 22:10:20 +03:00
|
|
|
#include "nsIWebSocketEventService.h"
|
2015-10-26 18:30:11 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2015-10-28 22:11:05 +03:00
|
|
|
#include "nsTArray.h"
|
2021-10-14 15:58:01 +03:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
|
|
|
#include "nsTHashMap.h"
|
2015-10-26 18:30:11 +03:00
|
|
|
|
2020-07-29 20:41:56 +03:00
|
|
|
class nsIWebSocketImpl;
|
2021-10-14 15:58:01 +03:00
|
|
|
class nsIEventTarget;
|
2020-07-29 20:41:56 +03:00
|
|
|
|
2015-10-26 18:30:11 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
class WebSocketFrame;
|
2015-10-28 22:10:20 +03:00
|
|
|
class WebSocketEventListenerChild;
|
2015-10-26 18:30:11 +03:00
|
|
|
|
2015-10-28 22:10:20 +03:00
|
|
|
class WebSocketEventService final : public nsIWebSocketEventService,
|
2015-10-26 18:30:11 +03:00
|
|
|
public nsIObserver {
|
2015-10-28 22:11:05 +03:00
|
|
|
friend class WebSocketBaseRunnable;
|
2015-10-26 18:30:11 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
2015-10-28 22:10:20 +03:00
|
|
|
NS_DECL_NSIWEBSOCKETEVENTSERVICE
|
2015-10-26 18:30:11 +03:00
|
|
|
|
2017-06-29 22:48:45 +03:00
|
|
|
static already_AddRefed<WebSocketEventService> Get();
|
2015-10-28 22:10:20 +03:00
|
|
|
static already_AddRefed<WebSocketEventService> GetOrCreate();
|
2015-10-26 18:30:11 +03:00
|
|
|
|
2015-10-28 22:11:05 +03:00
|
|
|
void WebSocketCreated(uint32_t aWebSocketSerialID, uint64_t aInnerWindowID,
|
2017-06-14 03:15:00 +03:00
|
|
|
const nsAString& aURI, const nsACString& aProtocols,
|
|
|
|
nsIEventTarget* aTarget = nullptr);
|
2015-10-28 22:11:05 +03:00
|
|
|
|
|
|
|
void WebSocketOpened(uint32_t aWebSocketSerialID, uint64_t aInnerWindowID,
|
|
|
|
const nsAString& aEffectiveURI,
|
|
|
|
const nsACString& aProtocols,
|
2019-05-03 20:25:44 +03:00
|
|
|
const nsACString& aExtensions, uint64_t aHttpChannelId,
|
2017-06-14 03:15:00 +03:00
|
|
|
nsIEventTarget* aTarget = nullptr);
|
2015-10-28 22:11:05 +03:00
|
|
|
|
2015-10-28 22:11:25 +03:00
|
|
|
void WebSocketMessageAvailable(uint32_t aWebSocketSerialID,
|
|
|
|
uint64_t aInnerWindowID,
|
|
|
|
const nsACString& aData, uint16_t aMessageType,
|
2017-06-14 03:15:00 +03:00
|
|
|
nsIEventTarget* aTarget = nullptr);
|
2015-10-28 22:11:25 +03:00
|
|
|
|
2015-10-28 22:11:05 +03:00
|
|
|
void WebSocketClosed(uint32_t aWebSocketSerialID, uint64_t aInnerWindowID,
|
2017-06-14 03:15:00 +03:00
|
|
|
bool aWasClean, uint16_t aCode, const nsAString& aReason,
|
|
|
|
nsIEventTarget* aTarget = nullptr);
|
2015-10-28 22:11:05 +03:00
|
|
|
|
2015-10-26 18:30:11 +03:00
|
|
|
void FrameReceived(uint32_t aWebSocketSerialID, uint64_t aInnerWindowID,
|
2017-06-14 03:15:00 +03:00
|
|
|
already_AddRefed<WebSocketFrame> aFrame,
|
|
|
|
nsIEventTarget* aTarget = nullptr);
|
2015-10-26 18:30:11 +03:00
|
|
|
|
|
|
|
void FrameSent(uint32_t aWebSocketSerialID, uint64_t aInnerWindowID,
|
2017-06-14 03:15:00 +03:00
|
|
|
already_AddRefed<WebSocketFrame> aFrame,
|
|
|
|
nsIEventTarget* aTarget = nullptr);
|
2015-10-26 18:30:11 +03:00
|
|
|
|
2020-07-29 20:41:56 +03:00
|
|
|
void AssociateWebSocketImplWithSerialID(nsIWebSocketImpl* aWebSocketImpl,
|
|
|
|
uint32_t aWebSocketSerialID);
|
|
|
|
|
2015-11-09 04:44:11 +03:00
|
|
|
already_AddRefed<WebSocketFrame> CreateFrameIfNeeded(
|
2015-10-26 18:30:11 +03:00
|
|
|
bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3,
|
|
|
|
uint8_t aOpCode, bool aMaskBit, uint32_t aMask,
|
|
|
|
const nsCString& aPayload);
|
|
|
|
|
2015-11-09 04:44:11 +03:00
|
|
|
already_AddRefed<WebSocketFrame> CreateFrameIfNeeded(
|
2015-10-26 18:30:11 +03:00
|
|
|
bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3,
|
|
|
|
uint8_t aOpCode, bool aMaskBit, uint32_t aMask, uint8_t* aPayload,
|
|
|
|
uint32_t aPayloadLength);
|
|
|
|
|
2015-11-09 04:44:11 +03:00
|
|
|
already_AddRefed<WebSocketFrame> CreateFrameIfNeeded(
|
2015-10-26 18:30:11 +03:00
|
|
|
bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3,
|
|
|
|
uint8_t aOpCode, bool aMaskBit, uint32_t aMask, uint8_t* aPayloadInHdr,
|
|
|
|
uint32_t aPayloadInHdrLength, uint8_t* aPayload, uint32_t aPayloadLength);
|
|
|
|
|
|
|
|
private:
|
2015-10-28 22:10:20 +03:00
|
|
|
WebSocketEventService();
|
|
|
|
~WebSocketEventService();
|
2015-10-26 18:30:11 +03:00
|
|
|
|
|
|
|
bool HasListeners() const;
|
|
|
|
void Shutdown();
|
|
|
|
|
2021-06-04 15:35:16 +03:00
|
|
|
using WindowListeners = nsTArray<nsCOMPtr<nsIWebSocketEventListener>>;
|
2015-10-26 18:30:11 +03:00
|
|
|
|
2021-03-10 13:47:47 +03:00
|
|
|
nsTHashMap<nsUint32HashKey, nsWeakPtr> mWebSocketImplMap;
|
2020-07-29 20:41:56 +03:00
|
|
|
|
2015-10-26 18:31:00 +03:00
|
|
|
struct WindowListener {
|
|
|
|
WindowListeners mListeners;
|
2015-10-28 22:10:20 +03:00
|
|
|
RefPtr<WebSocketEventListenerChild> mActor;
|
2015-10-26 18:31:00 +03:00
|
|
|
};
|
|
|
|
|
2015-10-28 22:11:05 +03:00
|
|
|
void GetListeners(uint64_t aInnerWindowID, WindowListeners& aListeners) const;
|
|
|
|
|
2015-10-26 18:31:00 +03:00
|
|
|
void ShutdownActorListener(WindowListener* aListener);
|
2015-10-26 18:30:11 +03:00
|
|
|
|
|
|
|
// Used only on the main-thread.
|
2015-10-26 18:31:00 +03:00
|
|
|
nsClassHashtable<nsUint64HashKey, WindowListener> mWindows;
|
2015-10-26 18:30:11 +03:00
|
|
|
|
|
|
|
Atomic<uint64_t> mCountListeners;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2016-06-30 01:56:41 +03:00
|
|
|
/**
|
|
|
|
* Casting WebSocketEventService to nsISupports is ambiguous.
|
|
|
|
* This method handles that.
|
|
|
|
*/
|
|
|
|
inline nsISupports* ToSupports(mozilla::net::WebSocketEventService* p) {
|
|
|
|
return NS_ISUPPORTS_CAST(nsIWebSocketEventService*, p);
|
|
|
|
}
|
|
|
|
|
2015-10-28 22:10:20 +03:00
|
|
|
#endif // mozilla_net_WebSocketEventService_h
|