summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/websocket/WebSocketEventListenerParent.cpp
blob: bb21483fb95b8980fa94ee570e9257e9aaa4adbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* -*- 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/. */

#include "WebSocketEventService.h"
#include "WebSocketEventListenerParent.h"
#include "mozilla/Unused.h"

namespace mozilla {
namespace net {

NS_INTERFACE_MAP_BEGIN(WebSocketEventListenerParent)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebSocketEventListener)
  NS_INTERFACE_MAP_ENTRY(nsIWebSocketEventListener)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(WebSocketEventListenerParent)
NS_IMPL_RELEASE(WebSocketEventListenerParent)

WebSocketEventListenerParent::WebSocketEventListenerParent(
    uint64_t aInnerWindowID)
    : mService(WebSocketEventService::GetOrCreate()),
      mInnerWindowID(aInnerWindowID) {
  DebugOnly<nsresult> rv = mService->AddListener(mInnerWindowID, this);
  MOZ_ASSERT(NS_SUCCEEDED(rv));
}

WebSocketEventListenerParent::~WebSocketEventListenerParent() {
  MOZ_ASSERT(!mService);
}

mozilla::ipc::IPCResult WebSocketEventListenerParent::RecvClose() {
  if (mService) {
    UnregisterListener();
    Unused << Send__delete__(this);
  }

  return IPC_OK();
}

void WebSocketEventListenerParent::ActorDestroy(ActorDestroyReason aWhy) {
  UnregisterListener();
}

void WebSocketEventListenerParent::UnregisterListener() {
  if (mService) {
    DebugOnly<nsresult> rv = mService->RemoveListener(mInnerWindowID, this);
    MOZ_ASSERT(NS_SUCCEEDED(rv));
    mService = nullptr;
  }
}

NS_IMETHODIMP
WebSocketEventListenerParent::WebSocketCreated(uint32_t aWebSocketSerialID,
                                               const nsAString& aURI,
                                               const nsACString& aProtocols) {
  Unused << SendWebSocketCreated(aWebSocketSerialID, nsString(aURI),
                                 nsCString(aProtocols));
  return NS_OK;
}

NS_IMETHODIMP
WebSocketEventListenerParent::WebSocketOpened(uint32_t aWebSocketSerialID,
                                              const nsAString& aEffectiveURI,
                                              const nsACString& aProtocols,
                                              const nsACString& aExtensions,
                                              uint64_t aHttpChannelId) {
  Unused << SendWebSocketOpened(aWebSocketSerialID, nsString(aEffectiveURI),
                                nsCString(aProtocols), nsCString(aExtensions),
                                aHttpChannelId);
  return NS_OK;
}

NS_IMETHODIMP
WebSocketEventListenerParent::WebSocketClosed(uint32_t aWebSocketSerialID,
                                              bool aWasClean, uint16_t aCode,
                                              const nsAString& aReason) {
  Unused << SendWebSocketClosed(aWebSocketSerialID, aWasClean, aCode,
                                nsString(aReason));
  return NS_OK;
}

NS_IMETHODIMP
WebSocketEventListenerParent::WebSocketMessageAvailable(
    uint32_t aWebSocketSerialID, const nsACString& aData,
    uint16_t aMessageType) {
  Unused << SendWebSocketMessageAvailable(aWebSocketSerialID, nsCString(aData),
                                          aMessageType);
  return NS_OK;
}

NS_IMETHODIMP
WebSocketEventListenerParent::FrameReceived(uint32_t aWebSocketSerialID,
                                            nsIWebSocketFrame* aFrame) {
  if (!aFrame) {
    return NS_ERROR_FAILURE;
  }

  WebSocketFrame* frame = static_cast<WebSocketFrame*>(aFrame);
  Unused << SendFrameReceived(aWebSocketSerialID, frame->Data());
  return NS_OK;
}

NS_IMETHODIMP
WebSocketEventListenerParent::FrameSent(uint32_t aWebSocketSerialID,
                                        nsIWebSocketFrame* aFrame) {
  if (!aFrame) {
    return NS_ERROR_FAILURE;
  }

  WebSocketFrame* frame = static_cast<WebSocketFrame*>(aFrame);
  Unused << SendFrameSent(aWebSocketSerialID, frame->Data());
  return NS_OK;
}

}  // namespace net
}  // namespace mozilla