1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "TCPServerSocketParent.h"
7 #include "TCPSocketParent.h"
8 #include "mozilla/unused.h"
9 #include "mozilla/AppProcessChecker.h"
10 #include "mozilla/dom/ContentParent.h"
11 #include "mozilla/dom/TabParent.h"
17 FireInteralError(mozilla::net::PTCPServerSocketParent
* aActor
,
21 aActor
->SendCallbackError(NS_LITERAL_STRING("Internal error"),
22 NS_LITERAL_STRING(__FILE__
), aLineNo
, 0);
25 NS_IMPL_CYCLE_COLLECTION(TCPServerSocketParent
, mServerSocket
, mIntermediary
)
26 NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPServerSocketParent
)
27 NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPServerSocketParent
)
29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPServerSocketParent
)
30 NS_INTERFACE_MAP_ENTRY(nsITCPServerSocketParent
)
31 NS_INTERFACE_MAP_ENTRY(nsISupports
)
35 TCPServerSocketParent::ReleaseIPDLReference()
43 TCPServerSocketParent::AddIPDLReference()
45 MOZ_ASSERT(!mIPCOpen
);
51 TCPServerSocketParent::Init(PNeckoParent
* neckoParent
, const uint16_t& aLocalPort
,
52 const uint16_t& aBacklog
, const nsString
& aBinaryType
)
54 mNeckoParent
= neckoParent
;
57 mIntermediary
= do_CreateInstance("@mozilla.org/tcp-socket-intermediary;1", &rv
);
59 FireInteralError(this, __LINE__
);
63 rv
= mIntermediary
->Listen(this, aLocalPort
, aBacklog
, aBinaryType
, GetAppId(),
64 GetInBrowser(), getter_AddRefs(mServerSocket
));
65 if (NS_FAILED(rv
) || !mServerSocket
) {
66 FireInteralError(this, __LINE__
);
73 TCPServerSocketParent::GetAppId()
75 uint32_t appId
= nsIScriptSecurityManager::UNKNOWN_APP_ID
;
76 const PContentParent
*content
= Manager()->Manager();
77 const InfallibleTArray
<PBrowserParent
*>& browsers
= content
->ManagedPBrowserParent();
78 if (browsers
.Length() > 0) {
79 TabParent
*tab
= static_cast<TabParent
*>(browsers
[0]);
80 appId
= tab
->OwnAppId();
86 TCPServerSocketParent::GetInBrowser()
88 bool inBrowser
= false;
89 const PContentParent
*content
= Manager()->Manager();
90 const InfallibleTArray
<PBrowserParent
*>& browsers
= content
->ManagedPBrowserParent();
91 if (browsers
.Length() > 0) {
92 TabParent
*tab
= static_cast<TabParent
*>(browsers
[0]);
93 inBrowser
= tab
->IsBrowserElement();
99 TCPServerSocketParent::SendCallbackAccept(nsITCPSocketParent
*socket
)
101 TCPSocketParent
* _socket
= static_cast<TCPSocketParent
*>(socket
);
102 PTCPSocketParent
* _psocket
= static_cast<PTCPSocketParent
*>(_socket
);
104 _socket
->AddIPDLReference();
109 rv
= socket
->GetHost(host
);
111 NS_ERROR("Failed to get host from nsITCPSocketParent");
112 return NS_ERROR_FAILURE
;
116 rv
= socket
->GetPort(&port
);
118 NS_ERROR("Failed to get port from nsITCPSocketParent");
119 return NS_ERROR_FAILURE
;
123 if (mNeckoParent
->SendPTCPSocketConstructor(_psocket
, host
, port
)) {
124 mozilla::unused
<< PTCPServerSocketParent::SendCallbackAccept(_psocket
);
127 NS_ERROR("Sending data from PTCPSocketParent was failed.");
131 NS_ERROR("The member value for NeckoParent is wrong.");
137 TCPServerSocketParent::SendCallbackError(const nsAString
& message
,
138 const nsAString
& filename
,
140 uint32_t columnNumber
)
143 PTCPServerSocketParent::SendCallbackError(nsString(message
), nsString(filename
),
144 lineNumber
, columnNumber
);
149 TCPServerSocketParent::RecvClose()
151 NS_ENSURE_TRUE(mServerSocket
, true);
152 mServerSocket
->Close();
157 TCPServerSocketParent::ActorDestroy(ActorDestroyReason why
)
160 mServerSocket
->Close();
161 mServerSocket
= nullptr;
163 mNeckoParent
= nullptr;
164 mIntermediary
= nullptr;
168 TCPServerSocketParent::RecvRequestDelete()
170 mozilla::unused
<< Send__delete__(this);
175 } // namespace mozilla