Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / nsDOMDataChannel.h
blobc717983c813e70bb95285a40ede207406c610663
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 #ifndef nsDOMDataChannel_h
8 #define nsDOMDataChannel_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/DOMEventTargetHelper.h"
12 #include "mozilla/dom/RTCDataChannelBinding.h"
13 #include "mozilla/dom/TypedArray.h"
14 #include "mozilla/net/DataChannelListener.h"
16 namespace mozilla {
17 namespace dom {
18 class Blob;
21 class DataChannel;
22 }; // namespace mozilla
24 class nsDOMDataChannel final : public mozilla::DOMEventTargetHelper,
25 public mozilla::DataChannelListener {
26 public:
27 nsDOMDataChannel(already_AddRefed<mozilla::DataChannel>& aDataChannel,
28 nsPIDOMWindowInner* aWindow);
30 nsresult Init(nsPIDOMWindowInner* aDOMWindow);
32 NS_DECL_ISUPPORTS_INHERITED
34 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsDOMDataChannel,
35 mozilla::DOMEventTargetHelper)
37 // EventTarget
38 using EventTarget::EventListenerAdded;
39 virtual void EventListenerAdded(nsAtom* aType) override;
41 using EventTarget::EventListenerRemoved;
42 virtual void EventListenerRemoved(nsAtom* aType) override;
44 virtual JSObject* WrapObject(JSContext* aCx,
45 JS::Handle<JSObject*> aGivenProto) override;
46 nsPIDOMWindowInner* GetParentObject() const { return GetOwner(); }
48 // WebIDL
49 void GetLabel(nsAString& aLabel);
50 void GetProtocol(nsAString& aProtocol);
51 bool Reliable() const;
52 mozilla::dom::Nullable<uint16_t> GetMaxPacketLifeTime() const;
53 mozilla::dom::Nullable<uint16_t> GetMaxRetransmits() const;
54 mozilla::dom::RTCDataChannelState ReadyState() const;
55 uint32_t BufferedAmount() const;
56 uint32_t BufferedAmountLowThreshold() const;
57 void SetBufferedAmountLowThreshold(uint32_t aThreshold);
58 IMPL_EVENT_HANDLER(open)
59 IMPL_EVENT_HANDLER(error)
60 IMPL_EVENT_HANDLER(close)
61 void Close();
62 IMPL_EVENT_HANDLER(message)
63 IMPL_EVENT_HANDLER(bufferedamountlow)
64 mozilla::dom::RTCDataChannelType BinaryType() const {
65 return static_cast<mozilla::dom::RTCDataChannelType>(
66 static_cast<int>(mBinaryType));
68 void SetBinaryType(mozilla::dom::RTCDataChannelType aType) {
69 mBinaryType = static_cast<DataChannelBinaryType>(static_cast<int>(aType));
71 void Send(const nsAString& aData, mozilla::ErrorResult& aRv);
72 void Send(mozilla::dom::Blob& aData, mozilla::ErrorResult& aRv);
73 void Send(const mozilla::dom::ArrayBuffer& aData, mozilla::ErrorResult& aRv);
74 void Send(const mozilla::dom::ArrayBufferView& aData,
75 mozilla::ErrorResult& aRv);
77 bool Negotiated() const;
78 bool Ordered() const;
79 mozilla::dom::Nullable<uint16_t> GetId() const;
81 nsresult DoOnMessageAvailable(const nsACString& aMessage, bool aBinary);
83 virtual nsresult OnMessageAvailable(nsISupports* aContext,
84 const nsACString& aMessage) override;
86 virtual nsresult OnBinaryMessageAvailable(
87 nsISupports* aContext, const nsACString& aMessage) override;
89 virtual nsresult OnSimpleEvent(nsISupports* aContext, const nsAString& aName);
91 virtual nsresult OnChannelConnected(nsISupports* aContext) override;
93 virtual nsresult OnChannelClosed(nsISupports* aContext) override;
95 virtual nsresult OnBufferLow(nsISupports* aContext) override;
97 virtual nsresult NotBuffered(nsISupports* aContext) override;
99 // if there are "strong event listeners" or outgoing not sent messages
100 // then this method keeps the object alive when js doesn't have strong
101 // references to it.
102 void UpdateMustKeepAlive();
103 // ATTENTION, when calling this method the object can be released
104 // (and possibly collected).
105 void DontKeepAliveAnyMore();
107 protected:
108 ~nsDOMDataChannel();
110 private:
111 bool CheckReadyState(mozilla::ErrorResult& aRv);
113 void ReleaseSelf();
115 // to keep us alive while we have listeners
116 RefPtr<nsDOMDataChannel> mSelfRef;
117 // Owning reference
118 RefPtr<mozilla::DataChannel> mDataChannel;
119 nsString mOrigin;
120 enum DataChannelBinaryType {
121 DC_BINARY_TYPE_ARRAYBUFFER,
122 DC_BINARY_TYPE_BLOB,
124 DataChannelBinaryType mBinaryType;
125 bool mCheckMustKeepAlive;
126 bool mSentClose;
129 #endif // nsDOMDataChannel_h