Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/nsDOMDataChannel.h
Line
Count
Source (jump to first uncovered line)
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
#include "nsIInputStream.h"
16
17
18
namespace mozilla {
19
namespace dom {
20
class Blob;
21
}
22
23
class DataChannel;
24
};
25
26
class nsDOMDataChannel final : public mozilla::DOMEventTargetHelper,
27
                               public mozilla::DataChannelListener
28
{
29
public:
30
  nsDOMDataChannel(already_AddRefed<mozilla::DataChannel>& aDataChannel,
31
                   nsPIDOMWindowInner* aWindow);
32
33
  nsresult Init(nsPIDOMWindowInner* aDOMWindow);
34
35
  NS_DECL_ISUPPORTS_INHERITED
36
37
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsDOMDataChannel,
38
                                           mozilla::DOMEventTargetHelper)
39
40
  // EventTarget
41
  using EventTarget::EventListenerAdded;
42
  virtual void EventListenerAdded(nsAtom* aType) override;
43
44
  using EventTarget::EventListenerRemoved;
45
  virtual void EventListenerRemoved(nsAtom* aType) override;
46
47
  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
48
    override;
49
  nsPIDOMWindowInner* GetParentObject() const
50
0
  {
51
0
    return GetOwner();
52
0
  }
53
54
  // WebIDL
55
  void GetLabel(nsAString& aLabel);
56
  void GetProtocol(nsAString& aProtocol);
57
  bool Reliable() const;
58
  mozilla::dom::Nullable<uint16_t> GetMaxPacketLifeTime() const;
59
  mozilla::dom::Nullable<uint16_t> GetMaxRetransmits() const;
60
  mozilla::dom::RTCDataChannelState ReadyState() const;
61
  uint32_t BufferedAmount() const;
62
  uint32_t BufferedAmountLowThreshold() const;
63
  void SetBufferedAmountLowThreshold(uint32_t aThreshold);
64
  IMPL_EVENT_HANDLER(open)
65
  IMPL_EVENT_HANDLER(error)
66
  IMPL_EVENT_HANDLER(close)
67
  void Close();
68
  IMPL_EVENT_HANDLER(message)
69
  IMPL_EVENT_HANDLER(bufferedamountlow)
70
  mozilla::dom::RTCDataChannelType BinaryType() const
71
0
  {
72
0
    return static_cast<mozilla::dom::RTCDataChannelType>(
73
0
      static_cast<int>(mBinaryType));
74
0
  }
75
  void SetBinaryType(mozilla::dom::RTCDataChannelType aType)
76
0
  {
77
0
    mBinaryType = static_cast<DataChannelBinaryType>(
78
0
      static_cast<int>(aType));
79
0
  }
80
  void Send(const nsAString& aData, mozilla::ErrorResult& aRv);
81
  void Send(mozilla::dom::Blob& aData, mozilla::ErrorResult& aRv);
82
  void Send(const mozilla::dom::ArrayBuffer& aData, mozilla::ErrorResult& aRv);
83
  void Send(const mozilla::dom::ArrayBufferView& aData,
84
            mozilla::ErrorResult& aRv);
85
86
  bool Ordered() const;
87
  uint16_t Id() const;
88
89
  nsresult
90
  DoOnMessageAvailable(const nsACString& aMessage, bool aBinary);
91
92
  virtual nsresult
93
  OnMessageAvailable(nsISupports* aContext, const nsACString& aMessage) override;
94
95
  virtual nsresult
96
  OnBinaryMessageAvailable(nsISupports* aContext, const nsACString& aMessage) override;
97
98
  virtual nsresult OnSimpleEvent(nsISupports* aContext, const nsAString& aName);
99
100
  virtual nsresult
101
  OnChannelConnected(nsISupports* aContext) override;
102
103
  virtual nsresult
104
  OnChannelClosed(nsISupports* aContext) override;
105
106
  virtual nsresult
107
  OnBufferLow(nsISupports* aContext) override;
108
109
  virtual nsresult
110
  NotBuffered(nsISupports* aContext) override;
111
112
  virtual void
113
  AppReady();
114
115
  // if there are "strong event listeners" or outgoing not sent messages
116
  // then this method keeps the object alive when js doesn't have strong
117
  // references to it.
118
  void UpdateMustKeepAlive();
119
  // ATTENTION, when calling this method the object can be released
120
  // (and possibly collected).
121
  void DontKeepAliveAnyMore();
122
123
protected:
124
  ~nsDOMDataChannel();
125
126
private:
127
  void Send(nsIInputStream* aMsgStream, const nsACString& aMsgString,
128
            bool aIsBinary, mozilla::ErrorResult& aRv);
129
130
  void ReleaseSelf();
131
132
  // to keep us alive while we have listeners
133
  RefPtr<nsDOMDataChannel> mSelfRef;
134
  // Owning reference
135
  RefPtr<mozilla::DataChannel> mDataChannel;
136
  nsString  mOrigin;
137
  enum DataChannelBinaryType {
138
    DC_BINARY_TYPE_ARRAYBUFFER,
139
    DC_BINARY_TYPE_BLOB,
140
  };
141
  DataChannelBinaryType mBinaryType;
142
  bool mCheckMustKeepAlive;
143
  bool mSentClose;
144
};
145
146
#endif // nsDOMDataChannel_h