Line data Source code
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 file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsIScriptSecurityManager.h"
8 : #include "TCPServerSocket.h"
9 : #include "TCPServerSocketParent.h"
10 : #include "nsJSUtils.h"
11 : #include "TCPSocketParent.h"
12 : #include "mozilla/Unused.h"
13 : #include "mozilla/dom/ContentParent.h"
14 : #include "mozilla/dom/TabParent.h"
15 :
16 : namespace mozilla {
17 : namespace dom {
18 :
19 0 : NS_IMPL_CYCLE_COLLECTION(TCPServerSocketParent, mServerSocket)
20 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPServerSocketParent)
21 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPServerSocketParent)
22 :
23 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPServerSocketParent)
24 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
25 0 : NS_INTERFACE_MAP_END
26 :
27 : void
28 0 : TCPServerSocketParent::ReleaseIPDLReference()
29 : {
30 0 : MOZ_ASSERT(mIPCOpen);
31 0 : mIPCOpen = false;
32 0 : this->Release();
33 0 : }
34 :
35 : void
36 0 : TCPServerSocketParent::AddIPDLReference()
37 : {
38 0 : MOZ_ASSERT(!mIPCOpen);
39 0 : mIPCOpen = true;
40 0 : this->AddRef();
41 0 : }
42 :
43 0 : TCPServerSocketParent::TCPServerSocketParent(PNeckoParent* neckoParent,
44 : uint16_t aLocalPort,
45 : uint16_t aBacklog,
46 0 : bool aUseArrayBuffers)
47 : : mNeckoParent(neckoParent)
48 0 : , mIPCOpen(false)
49 : {
50 0 : mServerSocket = new TCPServerSocket(nullptr, aLocalPort, aUseArrayBuffers, aBacklog);
51 0 : mServerSocket->SetServerBridgeParent(this);
52 0 : }
53 :
54 0 : TCPServerSocketParent::~TCPServerSocketParent()
55 : {
56 0 : }
57 :
58 : void
59 0 : TCPServerSocketParent::Init()
60 : {
61 0 : NS_ENSURE_SUCCESS_VOID(mServerSocket->Init());
62 : }
63 :
64 : uint32_t
65 0 : TCPServerSocketParent::GetAppId()
66 : {
67 0 : return nsIScriptSecurityManager::UNKNOWN_APP_ID;
68 : }
69 :
70 : bool
71 0 : TCPServerSocketParent::GetInIsolatedMozBrowser()
72 : {
73 0 : const PContentParent *content = Manager()->Manager();
74 0 : if (PBrowserParent* browser = SingleManagedOrNull(content->ManagedPBrowserParent())) {
75 0 : TabParent *tab = TabParent::GetFrom(browser);
76 0 : return tab->IsIsolatedMozBrowserElement();
77 : } else {
78 0 : return false;
79 : }
80 : }
81 :
82 : nsresult
83 0 : TCPServerSocketParent::SendCallbackAccept(TCPSocketParent *socket)
84 : {
85 0 : socket->AddIPDLReference();
86 :
87 : nsresult rv;
88 :
89 0 : nsString host;
90 0 : rv = socket->GetHost(host);
91 0 : if (NS_FAILED(rv)) {
92 0 : NS_ERROR("Failed to get host from nsITCPSocketParent");
93 0 : return NS_ERROR_FAILURE;
94 : }
95 :
96 : uint16_t port;
97 0 : rv = socket->GetPort(&port);
98 0 : if (NS_FAILED(rv)) {
99 0 : NS_ERROR("Failed to get port from nsITCPSocketParent");
100 0 : return NS_ERROR_FAILURE;
101 : }
102 :
103 0 : if (mNeckoParent) {
104 0 : if (mNeckoParent->SendPTCPSocketConstructor(socket, host, port)) {
105 0 : mozilla::Unused << PTCPServerSocketParent::SendCallbackAccept(socket);
106 : }
107 : else {
108 0 : NS_ERROR("Sending data from PTCPSocketParent was failed.");
109 : }
110 : }
111 : else {
112 0 : NS_ERROR("The member value for NeckoParent is wrong.");
113 : }
114 0 : return NS_OK;
115 : }
116 :
117 : mozilla::ipc::IPCResult
118 0 : TCPServerSocketParent::RecvClose()
119 : {
120 0 : NS_ENSURE_TRUE(mServerSocket, IPC_OK());
121 0 : mServerSocket->Close();
122 0 : return IPC_OK();
123 : }
124 :
125 : void
126 0 : TCPServerSocketParent::ActorDestroy(ActorDestroyReason why)
127 : {
128 0 : if (mServerSocket) {
129 0 : mServerSocket->Close();
130 0 : mServerSocket = nullptr;
131 : }
132 0 : mNeckoParent = nullptr;
133 0 : }
134 :
135 : mozilla::ipc::IPCResult
136 0 : TCPServerSocketParent::RecvRequestDelete()
137 : {
138 0 : mozilla::Unused << Send__delete__(this);
139 0 : return IPC_OK();
140 : }
141 :
142 : void
143 0 : TCPServerSocketParent::OnConnect(TCPServerSocketEvent* event)
144 : {
145 0 : RefPtr<TCPSocket> socket = event->Socket();
146 0 : socket->SetAppIdAndBrowser(GetAppId(), GetInIsolatedMozBrowser());
147 :
148 0 : RefPtr<TCPSocketParent> socketParent = new TCPSocketParent();
149 0 : socketParent->SetSocket(socket);
150 :
151 0 : socket->SetSocketBridgeParent(socketParent);
152 :
153 0 : SendCallbackAccept(socketParent);
154 0 : }
155 :
156 : } // namespace dom
157 : } // namespace mozilla
|