Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/protocol/websocket/WebSocketEventService.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 mozilla_net_WebSocketEventService_h
8
#define mozilla_net_WebSocketEventService_h
9
10
#include "mozilla/AlreadyAddRefed.h"
11
#include "mozilla/Atomics.h"
12
#include "nsIWebSocketEventService.h"
13
#include "nsAutoPtr.h"
14
#include "nsCOMPtr.h"
15
#include "nsClassHashtable.h"
16
#include "nsHashKeys.h"
17
#include "nsIObserver.h"
18
#include "nsISupportsImpl.h"
19
#include "nsTArray.h"
20
21
namespace mozilla {
22
namespace net {
23
24
class WebSocketFrame;
25
class WebSocketEventListenerChild;
26
27
class WebSocketEventService final : public nsIWebSocketEventService
28
                                  , public nsIObserver
29
{
30
  friend class WebSocketBaseRunnable;
31
32
public:
33
  NS_DECL_ISUPPORTS
34
  NS_DECL_NSIOBSERVER
35
  NS_DECL_NSIWEBSOCKETEVENTSERVICE
36
37
  static already_AddRefed<WebSocketEventService> Get();
38
  static already_AddRefed<WebSocketEventService> GetOrCreate();
39
40
  void WebSocketCreated(uint32_t aWebSocketSerialID,
41
                        uint64_t aInnerWindowID,
42
                        const nsAString& aURI,
43
                        const nsACString& aProtocols,
44
                        nsIEventTarget* aTarget = nullptr);
45
46
  void WebSocketOpened(uint32_t aWebSocketSerialID,
47
                       uint64_t aInnerWindowID,
48
                       const nsAString& aEffectiveURI,
49
                       const nsACString& aProtocols,
50
                       const nsACString& aExtensions,
51
                       nsIEventTarget* aTarget = nullptr);
52
53
  void WebSocketMessageAvailable(uint32_t aWebSocketSerialID,
54
                                 uint64_t aInnerWindowID,
55
                                 const nsACString& aData,
56
                                 uint16_t aMessageType,
57
                                 nsIEventTarget* aTarget = nullptr);
58
59
  void WebSocketClosed(uint32_t aWebSocketSerialID,
60
                       uint64_t aInnerWindowID,
61
                       bool aWasClean,
62
                       uint16_t aCode,
63
                       const nsAString& aReason,
64
                       nsIEventTarget* aTarget = nullptr);
65
66
  void FrameReceived(uint32_t aWebSocketSerialID,
67
                     uint64_t aInnerWindowID,
68
                     already_AddRefed<WebSocketFrame> aFrame,
69
                     nsIEventTarget* aTarget = nullptr);
70
71
  void  FrameSent(uint32_t aWebSocketSerialID,
72
                  uint64_t aInnerWindowID,
73
                  already_AddRefed<WebSocketFrame> aFrame,
74
                  nsIEventTarget* aTarget = nullptr);
75
76
  already_AddRefed<WebSocketFrame>
77
  CreateFrameIfNeeded(bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3,
78
                      uint8_t aOpCode, bool aMaskBit, uint32_t aMask,
79
                      const nsCString& aPayload);
80
81
  already_AddRefed<WebSocketFrame>
82
  CreateFrameIfNeeded(bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3,
83
                      uint8_t aOpCode, bool aMaskBit, uint32_t aMask,
84
                      uint8_t* aPayload, uint32_t aPayloadLength);
85
86
  already_AddRefed<WebSocketFrame>
87
  CreateFrameIfNeeded(bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3,
88
                      uint8_t aOpCode, bool aMaskBit, uint32_t aMask,
89
                      uint8_t* aPayloadInHdr, uint32_t aPayloadInHdrLength,
90
                      uint8_t* aPayload, uint32_t aPayloadLength);
91
92
private:
93
  WebSocketEventService();
94
  ~WebSocketEventService();
95
96
  bool HasListeners() const;
97
  void Shutdown();
98
99
  typedef nsTArray<nsCOMPtr<nsIWebSocketEventListener>> WindowListeners;
100
101
  struct WindowListener
102
  {
103
    WindowListeners mListeners;
104
    RefPtr<WebSocketEventListenerChild> mActor;
105
  };
106
107
  void GetListeners(uint64_t aInnerWindowID,
108
                    WindowListeners& aListeners) const;
109
110
  void ShutdownActorListener(WindowListener* aListener);
111
112
  // Used only on the main-thread.
113
  nsClassHashtable<nsUint64HashKey, WindowListener> mWindows;
114
115
  Atomic<uint64_t> mCountListeners;
116
};
117
118
} // net namespace
119
} // mozilla namespace
120
121
/**
122
 * Casting WebSocketEventService to nsISupports is ambiguous.
123
 * This method handles that.
124
 */
125
inline nsISupports*
126
ToSupports(mozilla::net::WebSocketEventService* p)
127
0
{
128
0
  return NS_ISUPPORTS_CAST(nsIWebSocketEventService*, p);
129
0
}
130
131
#endif // mozilla_net_WebSocketEventService_h