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 nsDOMDataChannel_h 8 #define nsDOMDataChannel_h 9 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" 15 16 namespace mozilla { 17 namespace dom { 18 class Blob; 19 } 20 21 class DataChannel; 22 }; // namespace mozilla 23 24 class nsDOMDataChannel final : public mozilla::DOMEventTargetHelper, 25 public mozilla::DataChannelListener { 26 public: 27 nsDOMDataChannel(already_AddRefed<mozilla::DataChannel>& aDataChannel, 28 nsPIDOMWindowInner* aWindow); 29 30 nsresult Init(nsPIDOMWindowInner* aDOMWindow); 31 32 NS_DECL_ISUPPORTS_INHERITED 33 34 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsDOMDataChannel, 35 mozilla::DOMEventTargetHelper) 36 37 // EventTarget 38 using EventTarget::EventListenerAdded; 39 virtual void EventListenerAdded(nsAtom* aType) override; 40 41 using EventTarget::EventListenerRemoved; 42 virtual void EventListenerRemoved(nsAtom* aType) override; 43 44 virtual JSObject* WrapObject(JSContext* aCx, 45 JS::Handle<JSObject*> aGivenProto) override; GetParentObject()46 nsPIDOMWindowInner* GetParentObject() const { return GetOwner(); } 47 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) IMPL_EVENT_HANDLER(bufferedamountlow)63 IMPL_EVENT_HANDLER(bufferedamountlow) 64 mozilla::dom::RTCDataChannelType BinaryType() const { 65 return static_cast<mozilla::dom::RTCDataChannelType>( 66 static_cast<int>(mBinaryType)); 67 } SetBinaryType(mozilla::dom::RTCDataChannelType aType)68 void SetBinaryType(mozilla::dom::RTCDataChannelType aType) { 69 mBinaryType = static_cast<DataChannelBinaryType>(static_cast<int>(aType)); 70 } 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); 76 77 bool Negotiated() const; 78 bool Ordered() const; 79 mozilla::dom::Nullable<uint16_t> GetId() const; 80 81 nsresult DoOnMessageAvailable(const nsACString& aMessage, bool aBinary); 82 83 virtual nsresult OnMessageAvailable(nsISupports* aContext, 84 const nsACString& aMessage) override; 85 86 virtual nsresult OnBinaryMessageAvailable( 87 nsISupports* aContext, const nsACString& aMessage) override; 88 89 virtual nsresult OnSimpleEvent(nsISupports* aContext, const nsAString& aName); 90 91 virtual nsresult OnChannelConnected(nsISupports* aContext) override; 92 93 virtual nsresult OnChannelClosed(nsISupports* aContext) override; 94 95 virtual nsresult OnBufferLow(nsISupports* aContext) override; 96 97 virtual nsresult NotBuffered(nsISupports* aContext) override; 98 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(); 106 107 protected: 108 ~nsDOMDataChannel(); 109 110 private: 111 void Send(mozilla::dom::Blob* aMsgBlob, const nsACString* aMsgString, 112 bool aIsBinary, mozilla::ErrorResult& aRv); 113 114 void ReleaseSelf(); 115 116 // to keep us alive while we have listeners 117 RefPtr<nsDOMDataChannel> mSelfRef; 118 // Owning reference 119 RefPtr<mozilla::DataChannel> mDataChannel; 120 nsString mOrigin; 121 enum DataChannelBinaryType { 122 DC_BINARY_TYPE_ARRAYBUFFER, 123 DC_BINARY_TYPE_BLOB, 124 }; 125 DataChannelBinaryType mBinaryType; 126 bool mCheckMustKeepAlive; 127 bool mSentClose; 128 }; 129 130 #endif // nsDOMDataChannel_h 131