Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / netwerk / protocol / websocket / BaseWebSocketChannel.cpp
blobe3a8241c6530451007855ac03b90ebe2854ecce2
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/. */
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 "nsProxyRelease.h"
14 #include "nsStandardURL.h"
16 #if defined(PR_LOGGING)
17 PRLogModuleInfo *webSocketLog = nullptr;
18 #endif
20 namespace mozilla {
21 namespace net {
23 BaseWebSocketChannel::BaseWebSocketChannel()
24 : mEncrypted(0)
25 , mWasOpened(0)
26 , mClientSetPingInterval(0)
27 , mClientSetPingTimeout(0)
28 , mPingForced(0)
29 , mPingInterval(0)
30 , mPingResponseTimeout(10000)
32 #if defined(PR_LOGGING)
33 if (!webSocketLog)
34 webSocketLog = PR_NewLogModule("nsWebSocket");
35 #endif
38 //-----------------------------------------------------------------------------
39 // BaseWebSocketChannel::nsIWebSocketChannel
40 //-----------------------------------------------------------------------------
42 NS_IMETHODIMP
43 BaseWebSocketChannel::GetOriginalURI(nsIURI **aOriginalURI)
45 LOG(("BaseWebSocketChannel::GetOriginalURI() %p\n", this));
47 if (!mOriginalURI)
48 return NS_ERROR_NOT_INITIALIZED;
49 NS_ADDREF(*aOriginalURI = mOriginalURI);
50 return NS_OK;
53 NS_IMETHODIMP
54 BaseWebSocketChannel::GetURI(nsIURI **aURI)
56 LOG(("BaseWebSocketChannel::GetURI() %p\n", this));
58 if (!mOriginalURI)
59 return NS_ERROR_NOT_INITIALIZED;
60 if (mURI)
61 NS_ADDREF(*aURI = mURI);
62 else
63 NS_ADDREF(*aURI = mOriginalURI);
64 return NS_OK;
67 NS_IMETHODIMP
68 BaseWebSocketChannel::
69 GetNotificationCallbacks(nsIInterfaceRequestor **aNotificationCallbacks)
71 LOG(("BaseWebSocketChannel::GetNotificationCallbacks() %p\n", this));
72 NS_IF_ADDREF(*aNotificationCallbacks = mCallbacks);
73 return NS_OK;
76 NS_IMETHODIMP
77 BaseWebSocketChannel::
78 SetNotificationCallbacks(nsIInterfaceRequestor *aNotificationCallbacks)
80 LOG(("BaseWebSocketChannel::SetNotificationCallbacks() %p\n", this));
81 mCallbacks = aNotificationCallbacks;
82 return NS_OK;
85 NS_IMETHODIMP
86 BaseWebSocketChannel::GetLoadGroup(nsILoadGroup **aLoadGroup)
88 LOG(("BaseWebSocketChannel::GetLoadGroup() %p\n", this));
89 NS_IF_ADDREF(*aLoadGroup = mLoadGroup);
90 return NS_OK;
93 NS_IMETHODIMP
94 BaseWebSocketChannel::SetLoadGroup(nsILoadGroup *aLoadGroup)
96 LOG(("BaseWebSocketChannel::SetLoadGroup() %p\n", this));
97 mLoadGroup = aLoadGroup;
98 return NS_OK;
101 NS_IMETHODIMP
102 BaseWebSocketChannel::SetLoadInfo(nsILoadInfo* aLoadInfo)
104 mLoadInfo = aLoadInfo;
105 return NS_OK;
108 NS_IMETHODIMP
109 BaseWebSocketChannel::GetLoadInfo(nsILoadInfo** aLoadInfo)
111 NS_IF_ADDREF(*aLoadInfo = mLoadInfo);
112 return NS_OK;
115 NS_IMETHODIMP
116 BaseWebSocketChannel::GetExtensions(nsACString &aExtensions)
118 LOG(("BaseWebSocketChannel::GetExtensions() %p\n", this));
119 aExtensions = mNegotiatedExtensions;
120 return NS_OK;
123 NS_IMETHODIMP
124 BaseWebSocketChannel::GetProtocol(nsACString &aProtocol)
126 LOG(("BaseWebSocketChannel::GetProtocol() %p\n", this));
127 aProtocol = mProtocol;
128 return NS_OK;
131 NS_IMETHODIMP
132 BaseWebSocketChannel::SetProtocol(const nsACString &aProtocol)
134 LOG(("BaseWebSocketChannel::SetProtocol() %p\n", this));
135 mProtocol = aProtocol; /* the sub protocol */
136 return NS_OK;
139 NS_IMETHODIMP
140 BaseWebSocketChannel::GetPingInterval(uint32_t *aSeconds)
142 // stored in ms but should only have second resolution
143 MOZ_ASSERT(!(mPingInterval % 1000));
145 *aSeconds = mPingInterval / 1000;
146 return NS_OK;
149 NS_IMETHODIMP
150 BaseWebSocketChannel::SetPingInterval(uint32_t aSeconds)
152 if (mWasOpened) {
153 return NS_ERROR_IN_PROGRESS;
156 mPingInterval = aSeconds * 1000;
157 mClientSetPingInterval = 1;
159 return NS_OK;
162 NS_IMETHODIMP
163 BaseWebSocketChannel::GetPingTimeout(uint32_t *aSeconds)
165 // stored in ms but should only have second resolution
166 MOZ_ASSERT(!(mPingResponseTimeout % 1000));
168 *aSeconds = mPingResponseTimeout / 1000;
169 return NS_OK;
172 NS_IMETHODIMP
173 BaseWebSocketChannel::SetPingTimeout(uint32_t aSeconds)
175 if (mWasOpened) {
176 return NS_ERROR_IN_PROGRESS;
179 mPingResponseTimeout = aSeconds * 1000;
180 mClientSetPingTimeout = 1;
182 return NS_OK;
185 //-----------------------------------------------------------------------------
186 // BaseWebSocketChannel::nsIProtocolHandler
187 //-----------------------------------------------------------------------------
190 NS_IMETHODIMP
191 BaseWebSocketChannel::GetScheme(nsACString &aScheme)
193 LOG(("BaseWebSocketChannel::GetScheme() %p\n", this));
195 if (mEncrypted)
196 aScheme.AssignLiteral("wss");
197 else
198 aScheme.AssignLiteral("ws");
199 return NS_OK;
202 NS_IMETHODIMP
203 BaseWebSocketChannel::GetDefaultPort(int32_t *aDefaultPort)
205 LOG(("BaseWebSocketChannel::GetDefaultPort() %p\n", this));
207 if (mEncrypted)
208 *aDefaultPort = kDefaultWSSPort;
209 else
210 *aDefaultPort = kDefaultWSPort;
211 return NS_OK;
214 NS_IMETHODIMP
215 BaseWebSocketChannel::GetProtocolFlags(uint32_t *aProtocolFlags)
217 LOG(("BaseWebSocketChannel::GetProtocolFlags() %p\n", this));
219 *aProtocolFlags = URI_NORELATIVE | URI_NON_PERSISTABLE | ALLOWS_PROXY |
220 ALLOWS_PROXY_HTTP | URI_DOES_NOT_RETURN_DATA | URI_DANGEROUS_TO_LOAD;
221 return NS_OK;
224 NS_IMETHODIMP
225 BaseWebSocketChannel::NewURI(const nsACString & aSpec, const char *aOriginCharset,
226 nsIURI *aBaseURI, nsIURI **_retval)
228 LOG(("BaseWebSocketChannel::NewURI() %p\n", this));
230 int32_t port;
231 nsresult rv = GetDefaultPort(&port);
232 if (NS_FAILED(rv))
233 return rv;
235 nsRefPtr<nsStandardURL> url = new nsStandardURL();
236 rv = url->Init(nsIStandardURL::URLTYPE_AUTHORITY, port, aSpec,
237 aOriginCharset, aBaseURI);
238 if (NS_FAILED(rv))
239 return rv;
240 NS_ADDREF(*_retval = url);
241 return NS_OK;
244 NS_IMETHODIMP
245 BaseWebSocketChannel::NewChannel2(nsIURI* aURI,
246 nsILoadInfo* aLoadInfo,
247 nsIChannel** outChannel)
249 LOG(("BaseWebSocketChannel::NewChannel2() %p\n", this));
250 return NS_ERROR_NOT_IMPLEMENTED;
253 NS_IMETHODIMP
254 BaseWebSocketChannel::NewChannel(nsIURI *aURI, nsIChannel **_retval)
256 LOG(("BaseWebSocketChannel::NewChannel() %p\n", this));
257 return NS_ERROR_NOT_IMPLEMENTED;
260 NS_IMETHODIMP
261 BaseWebSocketChannel::AllowPort(int32_t port, const char *scheme,
262 bool *_retval)
264 LOG(("BaseWebSocketChannel::AllowPort() %p\n", this));
266 // do not override any blacklisted ports
267 *_retval = false;
268 return NS_OK;
271 //-----------------------------------------------------------------------------
272 // BaseWebSocketChannel::nsIThreadRetargetableRequest
273 //-----------------------------------------------------------------------------
275 NS_IMETHODIMP
276 BaseWebSocketChannel::RetargetDeliveryTo(nsIEventTarget* aTargetThread)
278 MOZ_ASSERT(NS_IsMainThread());
279 MOZ_ASSERT(aTargetThread);
280 MOZ_ASSERT(!mTargetThread, "Delivery target should be set once, before AsyncOpen");
281 MOZ_ASSERT(!mWasOpened, "Should not be called after AsyncOpen!");
283 mTargetThread = do_QueryInterface(aTargetThread);
284 MOZ_ASSERT(mTargetThread);
285 return NS_OK;
288 BaseWebSocketChannel::ListenerAndContextContainer::ListenerAndContextContainer(
289 nsIWebSocketListener* aListener,
290 nsISupports* aContext)
291 : mListener(aListener)
292 , mContext(aContext)
294 MOZ_ASSERT(NS_IsMainThread());
295 MOZ_ASSERT(mListener);
298 BaseWebSocketChannel::ListenerAndContextContainer::~ListenerAndContextContainer()
300 MOZ_ASSERT(mListener);
302 nsCOMPtr<nsIThread> mainThread;
303 NS_GetMainThread(getter_AddRefs(mainThread));
305 NS_ProxyRelease(mainThread, mListener, false);
306 NS_ProxyRelease(mainThread, mContext, false);
309 } // namespace net
310 } // namespace mozilla