|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set sw=2 ts=8 et 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 |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "WebSocketLog.h" |
|
8 #include "BaseWebSocketChannel.h" |
|
9 #include "MainThreadUtils.h" |
|
10 #include "nsILoadGroup.h" |
|
11 #include "nsIInterfaceRequestor.h" |
|
12 #include "nsAutoPtr.h" |
|
13 #include "nsStandardURL.h" |
|
14 |
|
15 #if defined(PR_LOGGING) |
|
16 PRLogModuleInfo *webSocketLog = nullptr; |
|
17 #endif |
|
18 |
|
19 namespace mozilla { |
|
20 namespace net { |
|
21 |
|
22 BaseWebSocketChannel::BaseWebSocketChannel() |
|
23 : mEncrypted(0) |
|
24 , mWasOpened(0) |
|
25 , mClientSetPingInterval(0) |
|
26 , mClientSetPingTimeout(0) |
|
27 , mPingInterval(0) |
|
28 , mPingResponseTimeout(10000) |
|
29 { |
|
30 #if defined(PR_LOGGING) |
|
31 if (!webSocketLog) |
|
32 webSocketLog = PR_NewLogModule("nsWebSocket"); |
|
33 #endif |
|
34 } |
|
35 |
|
36 //----------------------------------------------------------------------------- |
|
37 // BaseWebSocketChannel::nsIWebSocketChannel |
|
38 //----------------------------------------------------------------------------- |
|
39 |
|
40 NS_IMETHODIMP |
|
41 BaseWebSocketChannel::GetOriginalURI(nsIURI **aOriginalURI) |
|
42 { |
|
43 LOG(("BaseWebSocketChannel::GetOriginalURI() %p\n", this)); |
|
44 |
|
45 if (!mOriginalURI) |
|
46 return NS_ERROR_NOT_INITIALIZED; |
|
47 NS_ADDREF(*aOriginalURI = mOriginalURI); |
|
48 return NS_OK; |
|
49 } |
|
50 |
|
51 NS_IMETHODIMP |
|
52 BaseWebSocketChannel::GetURI(nsIURI **aURI) |
|
53 { |
|
54 LOG(("BaseWebSocketChannel::GetURI() %p\n", this)); |
|
55 |
|
56 if (!mOriginalURI) |
|
57 return NS_ERROR_NOT_INITIALIZED; |
|
58 if (mURI) |
|
59 NS_ADDREF(*aURI = mURI); |
|
60 else |
|
61 NS_ADDREF(*aURI = mOriginalURI); |
|
62 return NS_OK; |
|
63 } |
|
64 |
|
65 NS_IMETHODIMP |
|
66 BaseWebSocketChannel:: |
|
67 GetNotificationCallbacks(nsIInterfaceRequestor **aNotificationCallbacks) |
|
68 { |
|
69 LOG(("BaseWebSocketChannel::GetNotificationCallbacks() %p\n", this)); |
|
70 NS_IF_ADDREF(*aNotificationCallbacks = mCallbacks); |
|
71 return NS_OK; |
|
72 } |
|
73 |
|
74 NS_IMETHODIMP |
|
75 BaseWebSocketChannel:: |
|
76 SetNotificationCallbacks(nsIInterfaceRequestor *aNotificationCallbacks) |
|
77 { |
|
78 LOG(("BaseWebSocketChannel::SetNotificationCallbacks() %p\n", this)); |
|
79 mCallbacks = aNotificationCallbacks; |
|
80 return NS_OK; |
|
81 } |
|
82 |
|
83 NS_IMETHODIMP |
|
84 BaseWebSocketChannel::GetLoadGroup(nsILoadGroup **aLoadGroup) |
|
85 { |
|
86 LOG(("BaseWebSocketChannel::GetLoadGroup() %p\n", this)); |
|
87 NS_IF_ADDREF(*aLoadGroup = mLoadGroup); |
|
88 return NS_OK; |
|
89 } |
|
90 |
|
91 NS_IMETHODIMP |
|
92 BaseWebSocketChannel::SetLoadGroup(nsILoadGroup *aLoadGroup) |
|
93 { |
|
94 LOG(("BaseWebSocketChannel::SetLoadGroup() %p\n", this)); |
|
95 mLoadGroup = aLoadGroup; |
|
96 return NS_OK; |
|
97 } |
|
98 |
|
99 NS_IMETHODIMP |
|
100 BaseWebSocketChannel::GetExtensions(nsACString &aExtensions) |
|
101 { |
|
102 LOG(("BaseWebSocketChannel::GetExtensions() %p\n", this)); |
|
103 aExtensions = mNegotiatedExtensions; |
|
104 return NS_OK; |
|
105 } |
|
106 |
|
107 NS_IMETHODIMP |
|
108 BaseWebSocketChannel::GetProtocol(nsACString &aProtocol) |
|
109 { |
|
110 LOG(("BaseWebSocketChannel::GetProtocol() %p\n", this)); |
|
111 aProtocol = mProtocol; |
|
112 return NS_OK; |
|
113 } |
|
114 |
|
115 NS_IMETHODIMP |
|
116 BaseWebSocketChannel::SetProtocol(const nsACString &aProtocol) |
|
117 { |
|
118 LOG(("BaseWebSocketChannel::SetProtocol() %p\n", this)); |
|
119 mProtocol = aProtocol; /* the sub protocol */ |
|
120 return NS_OK; |
|
121 } |
|
122 |
|
123 NS_IMETHODIMP |
|
124 BaseWebSocketChannel::GetPingInterval(uint32_t *aSeconds) |
|
125 { |
|
126 // stored in ms but should only have second resolution |
|
127 MOZ_ASSERT(!(mPingInterval % 1000)); |
|
128 |
|
129 *aSeconds = mPingInterval / 1000; |
|
130 return NS_OK; |
|
131 } |
|
132 |
|
133 NS_IMETHODIMP |
|
134 BaseWebSocketChannel::SetPingInterval(uint32_t aSeconds) |
|
135 { |
|
136 if (mWasOpened) { |
|
137 return NS_ERROR_IN_PROGRESS; |
|
138 } |
|
139 |
|
140 mPingInterval = aSeconds * 1000; |
|
141 mClientSetPingInterval = 1; |
|
142 |
|
143 return NS_OK; |
|
144 } |
|
145 |
|
146 NS_IMETHODIMP |
|
147 BaseWebSocketChannel::GetPingTimeout(uint32_t *aSeconds) |
|
148 { |
|
149 // stored in ms but should only have second resolution |
|
150 MOZ_ASSERT(!(mPingResponseTimeout % 1000)); |
|
151 |
|
152 *aSeconds = mPingResponseTimeout / 1000; |
|
153 return NS_OK; |
|
154 } |
|
155 |
|
156 NS_IMETHODIMP |
|
157 BaseWebSocketChannel::SetPingTimeout(uint32_t aSeconds) |
|
158 { |
|
159 if (mWasOpened) { |
|
160 return NS_ERROR_IN_PROGRESS; |
|
161 } |
|
162 |
|
163 mPingResponseTimeout = aSeconds * 1000; |
|
164 mClientSetPingTimeout = 1; |
|
165 |
|
166 return NS_OK; |
|
167 } |
|
168 |
|
169 //----------------------------------------------------------------------------- |
|
170 // BaseWebSocketChannel::nsIProtocolHandler |
|
171 //----------------------------------------------------------------------------- |
|
172 |
|
173 |
|
174 NS_IMETHODIMP |
|
175 BaseWebSocketChannel::GetScheme(nsACString &aScheme) |
|
176 { |
|
177 LOG(("BaseWebSocketChannel::GetScheme() %p\n", this)); |
|
178 |
|
179 if (mEncrypted) |
|
180 aScheme.AssignLiteral("wss"); |
|
181 else |
|
182 aScheme.AssignLiteral("ws"); |
|
183 return NS_OK; |
|
184 } |
|
185 |
|
186 NS_IMETHODIMP |
|
187 BaseWebSocketChannel::GetDefaultPort(int32_t *aDefaultPort) |
|
188 { |
|
189 LOG(("BaseWebSocketChannel::GetDefaultPort() %p\n", this)); |
|
190 |
|
191 if (mEncrypted) |
|
192 *aDefaultPort = kDefaultWSSPort; |
|
193 else |
|
194 *aDefaultPort = kDefaultWSPort; |
|
195 return NS_OK; |
|
196 } |
|
197 |
|
198 NS_IMETHODIMP |
|
199 BaseWebSocketChannel::GetProtocolFlags(uint32_t *aProtocolFlags) |
|
200 { |
|
201 LOG(("BaseWebSocketChannel::GetProtocolFlags() %p\n", this)); |
|
202 |
|
203 *aProtocolFlags = URI_NORELATIVE | URI_NON_PERSISTABLE | ALLOWS_PROXY | |
|
204 ALLOWS_PROXY_HTTP | URI_DOES_NOT_RETURN_DATA | URI_DANGEROUS_TO_LOAD; |
|
205 return NS_OK; |
|
206 } |
|
207 |
|
208 NS_IMETHODIMP |
|
209 BaseWebSocketChannel::NewURI(const nsACString & aSpec, const char *aOriginCharset, |
|
210 nsIURI *aBaseURI, nsIURI **_retval) |
|
211 { |
|
212 LOG(("BaseWebSocketChannel::NewURI() %p\n", this)); |
|
213 |
|
214 int32_t port; |
|
215 nsresult rv = GetDefaultPort(&port); |
|
216 if (NS_FAILED(rv)) |
|
217 return rv; |
|
218 |
|
219 nsRefPtr<nsStandardURL> url = new nsStandardURL(); |
|
220 rv = url->Init(nsIStandardURL::URLTYPE_AUTHORITY, port, aSpec, |
|
221 aOriginCharset, aBaseURI); |
|
222 if (NS_FAILED(rv)) |
|
223 return rv; |
|
224 NS_ADDREF(*_retval = url); |
|
225 return NS_OK; |
|
226 } |
|
227 |
|
228 NS_IMETHODIMP |
|
229 BaseWebSocketChannel::NewChannel(nsIURI *aURI, nsIChannel **_retval) |
|
230 { |
|
231 LOG(("BaseWebSocketChannel::NewChannel() %p\n", this)); |
|
232 return NS_ERROR_NOT_IMPLEMENTED; |
|
233 } |
|
234 |
|
235 NS_IMETHODIMP |
|
236 BaseWebSocketChannel::AllowPort(int32_t port, const char *scheme, |
|
237 bool *_retval) |
|
238 { |
|
239 LOG(("BaseWebSocketChannel::AllowPort() %p\n", this)); |
|
240 |
|
241 // do not override any blacklisted ports |
|
242 *_retval = false; |
|
243 return NS_OK; |
|
244 } |
|
245 |
|
246 //----------------------------------------------------------------------------- |
|
247 // BaseWebSocketChannel::nsIThreadRetargetableRequest |
|
248 //----------------------------------------------------------------------------- |
|
249 |
|
250 NS_IMETHODIMP |
|
251 BaseWebSocketChannel::RetargetDeliveryTo(nsIEventTarget* aTargetThread) |
|
252 { |
|
253 MOZ_ASSERT(NS_IsMainThread()); |
|
254 MOZ_ASSERT(aTargetThread); |
|
255 MOZ_ASSERT(!mTargetThread, "Delivery target should be set once, before AsyncOpen"); |
|
256 MOZ_ASSERT(!mWasOpened, "Should not be called after AsyncOpen!"); |
|
257 |
|
258 mTargetThread = do_QueryInterface(aTargetThread); |
|
259 MOZ_ASSERT(mTargetThread); |
|
260 return NS_OK; |
|
261 } |
|
262 |
|
263 } // namespace net |
|
264 } // namespace mozilla |