|
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/. */ |
|
4 |
|
5 #include "TCPServerSocketParent.h" |
|
6 #include "nsJSUtils.h" |
|
7 #include "TCPSocketParent.h" |
|
8 #include "mozilla/unused.h" |
|
9 #include "mozilla/AppProcessChecker.h" |
|
10 |
|
11 namespace mozilla { |
|
12 namespace dom { |
|
13 |
|
14 static void |
|
15 FireInteralError(mozilla::net::PTCPServerSocketParent* aActor, |
|
16 uint32_t aLineNo) |
|
17 { |
|
18 mozilla::unused << |
|
19 aActor->SendCallbackError(NS_LITERAL_STRING("Internal error"), |
|
20 NS_LITERAL_STRING(__FILE__), aLineNo, 0); |
|
21 } |
|
22 |
|
23 NS_IMPL_CYCLE_COLLECTION(TCPServerSocketParent, mServerSocket, mIntermediary) |
|
24 NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPServerSocketParent) |
|
25 NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPServerSocketParent) |
|
26 |
|
27 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPServerSocketParent) |
|
28 NS_INTERFACE_MAP_ENTRY(nsITCPServerSocketParent) |
|
29 NS_INTERFACE_MAP_ENTRY(nsISupports) |
|
30 NS_INTERFACE_MAP_END |
|
31 |
|
32 void |
|
33 TCPServerSocketParent::ReleaseIPDLReference() |
|
34 { |
|
35 MOZ_ASSERT(mIPCOpen); |
|
36 mIPCOpen = false; |
|
37 this->Release(); |
|
38 } |
|
39 |
|
40 void |
|
41 TCPServerSocketParent::AddIPDLReference() |
|
42 { |
|
43 MOZ_ASSERT(!mIPCOpen); |
|
44 mIPCOpen = true; |
|
45 this->AddRef(); |
|
46 } |
|
47 |
|
48 bool |
|
49 TCPServerSocketParent::Init(PNeckoParent* neckoParent, const uint16_t& aLocalPort, |
|
50 const uint16_t& aBacklog, const nsString& aBinaryType) |
|
51 { |
|
52 mNeckoParent = neckoParent; |
|
53 |
|
54 nsresult rv; |
|
55 mIntermediary = do_CreateInstance("@mozilla.org/tcp-socket-intermediary;1", &rv); |
|
56 if (NS_FAILED(rv)) { |
|
57 FireInteralError(this, __LINE__); |
|
58 return true; |
|
59 } |
|
60 |
|
61 rv = mIntermediary->Listen(this, aLocalPort, aBacklog, aBinaryType, getter_AddRefs(mServerSocket)); |
|
62 if (NS_FAILED(rv) || !mServerSocket) { |
|
63 FireInteralError(this, __LINE__); |
|
64 return true; |
|
65 } |
|
66 return true; |
|
67 } |
|
68 |
|
69 NS_IMETHODIMP |
|
70 TCPServerSocketParent::SendCallbackAccept(nsITCPSocketParent *socket) |
|
71 { |
|
72 TCPSocketParent* _socket = static_cast<TCPSocketParent*>(socket); |
|
73 PTCPSocketParent* _psocket = static_cast<PTCPSocketParent*>(_socket); |
|
74 |
|
75 _socket->AddIPDLReference(); |
|
76 |
|
77 if (mNeckoParent) { |
|
78 if (mNeckoParent->SendPTCPSocketConstructor(_psocket)) { |
|
79 mozilla::unused << PTCPServerSocketParent::SendCallbackAccept(_psocket); |
|
80 } |
|
81 else { |
|
82 NS_ERROR("Sending data from PTCPSocketParent was failed."); |
|
83 }; |
|
84 } |
|
85 else { |
|
86 NS_ERROR("The member value for NeckoParent is wrong."); |
|
87 } |
|
88 return NS_OK; |
|
89 } |
|
90 |
|
91 NS_IMETHODIMP |
|
92 TCPServerSocketParent::SendCallbackError(const nsAString& message, |
|
93 const nsAString& filename, |
|
94 uint32_t lineNumber, |
|
95 uint32_t columnNumber) |
|
96 { |
|
97 mozilla::unused << |
|
98 PTCPServerSocketParent::SendCallbackError(nsString(message), nsString(filename), |
|
99 lineNumber, columnNumber); |
|
100 return NS_OK; |
|
101 } |
|
102 |
|
103 bool |
|
104 TCPServerSocketParent::RecvClose() |
|
105 { |
|
106 NS_ENSURE_TRUE(mServerSocket, true); |
|
107 mServerSocket->Close(); |
|
108 return true; |
|
109 } |
|
110 |
|
111 void |
|
112 TCPServerSocketParent::ActorDestroy(ActorDestroyReason why) |
|
113 { |
|
114 if (mServerSocket) { |
|
115 mServerSocket->Close(); |
|
116 mServerSocket = nullptr; |
|
117 } |
|
118 mNeckoParent = nullptr; |
|
119 mIntermediary = nullptr; |
|
120 } |
|
121 |
|
122 bool |
|
123 TCPServerSocketParent::RecvRequestDelete() |
|
124 { |
|
125 mozilla::unused << Send__delete__(this); |
|
126 return true; |
|
127 } |
|
128 |
|
129 } // namespace dom |
|
130 } // namespace mozilla |