Bumping manifests a=b2g-bump
[gecko.git] / dom / network / TCPServerSocketParent.cpp
blobae470fa723bb59aaa3ab2daea25291bb05188d71
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"
6 #include "nsJSUtils.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"
13 namespace mozilla {
14 namespace dom {
16 static void
17 FireInteralError(mozilla::net::PTCPServerSocketParent* aActor,
18 uint32_t aLineNo)
20 mozilla::unused <<
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)
32 NS_INTERFACE_MAP_END
34 void
35 TCPServerSocketParent::ReleaseIPDLReference()
37 MOZ_ASSERT(mIPCOpen);
38 mIPCOpen = false;
39 this->Release();
42 void
43 TCPServerSocketParent::AddIPDLReference()
45 MOZ_ASSERT(!mIPCOpen);
46 mIPCOpen = true;
47 this->AddRef();
50 bool
51 TCPServerSocketParent::Init(PNeckoParent* neckoParent, const uint16_t& aLocalPort,
52 const uint16_t& aBacklog, const nsString& aBinaryType)
54 mNeckoParent = neckoParent;
56 nsresult rv;
57 mIntermediary = do_CreateInstance("@mozilla.org/tcp-socket-intermediary;1", &rv);
58 if (NS_FAILED(rv)) {
59 FireInteralError(this, __LINE__);
60 return true;
63 rv = mIntermediary->Listen(this, aLocalPort, aBacklog, aBinaryType, GetAppId(),
64 GetInBrowser(), getter_AddRefs(mServerSocket));
65 if (NS_FAILED(rv) || !mServerSocket) {
66 FireInteralError(this, __LINE__);
67 return true;
69 return true;
72 uint32_t
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();
82 return appId;
85 bool
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();
95 return inBrowser;
98 NS_IMETHODIMP
99 TCPServerSocketParent::SendCallbackAccept(nsITCPSocketParent *socket)
101 TCPSocketParent* _socket = static_cast<TCPSocketParent*>(socket);
102 PTCPSocketParent* _psocket = static_cast<PTCPSocketParent*>(_socket);
104 _socket->AddIPDLReference();
106 nsresult rv;
108 nsString host;
109 rv = socket->GetHost(host);
110 if (NS_FAILED(rv)) {
111 NS_ERROR("Failed to get host from nsITCPSocketParent");
112 return NS_ERROR_FAILURE;
115 uint16_t port;
116 rv = socket->GetPort(&port);
117 if (NS_FAILED(rv)) {
118 NS_ERROR("Failed to get port from nsITCPSocketParent");
119 return NS_ERROR_FAILURE;
122 if (mNeckoParent) {
123 if (mNeckoParent->SendPTCPSocketConstructor(_psocket, host, port)) {
124 mozilla::unused << PTCPServerSocketParent::SendCallbackAccept(_psocket);
126 else {
127 NS_ERROR("Sending data from PTCPSocketParent was failed.");
130 else {
131 NS_ERROR("The member value for NeckoParent is wrong.");
133 return NS_OK;
136 NS_IMETHODIMP
137 TCPServerSocketParent::SendCallbackError(const nsAString& message,
138 const nsAString& filename,
139 uint32_t lineNumber,
140 uint32_t columnNumber)
142 mozilla::unused <<
143 PTCPServerSocketParent::SendCallbackError(nsString(message), nsString(filename),
144 lineNumber, columnNumber);
145 return NS_OK;
148 bool
149 TCPServerSocketParent::RecvClose()
151 NS_ENSURE_TRUE(mServerSocket, true);
152 mServerSocket->Close();
153 return true;
156 void
157 TCPServerSocketParent::ActorDestroy(ActorDestroyReason why)
159 if (mServerSocket) {
160 mServerSocket->Close();
161 mServerSocket = nullptr;
163 mNeckoParent = nullptr;
164 mIntermediary = nullptr;
167 bool
168 TCPServerSocketParent::RecvRequestDelete()
170 mozilla::unused << Send__delete__(this);
171 return true;
174 } // namespace dom
175 } // namespace mozilla