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;
23 BaseWebSocketChannel::BaseWebSocketChannel()
26 , mClientSetPingInterval(0)
27 , mClientSetPingTimeout(0)
30 , mPingResponseTimeout(10000)
32 #if defined(PR_LOGGING)
34 webSocketLog
= PR_NewLogModule("nsWebSocket");
38 //-----------------------------------------------------------------------------
39 // BaseWebSocketChannel::nsIWebSocketChannel
40 //-----------------------------------------------------------------------------
43 BaseWebSocketChannel::GetOriginalURI(nsIURI
**aOriginalURI
)
45 LOG(("BaseWebSocketChannel::GetOriginalURI() %p\n", this));
48 return NS_ERROR_NOT_INITIALIZED
;
49 NS_ADDREF(*aOriginalURI
= mOriginalURI
);
54 BaseWebSocketChannel::GetURI(nsIURI
**aURI
)
56 LOG(("BaseWebSocketChannel::GetURI() %p\n", this));
59 return NS_ERROR_NOT_INITIALIZED
;
61 NS_ADDREF(*aURI
= mURI
);
63 NS_ADDREF(*aURI
= mOriginalURI
);
68 BaseWebSocketChannel::
69 GetNotificationCallbacks(nsIInterfaceRequestor
**aNotificationCallbacks
)
71 LOG(("BaseWebSocketChannel::GetNotificationCallbacks() %p\n", this));
72 NS_IF_ADDREF(*aNotificationCallbacks
= mCallbacks
);
77 BaseWebSocketChannel::
78 SetNotificationCallbacks(nsIInterfaceRequestor
*aNotificationCallbacks
)
80 LOG(("BaseWebSocketChannel::SetNotificationCallbacks() %p\n", this));
81 mCallbacks
= aNotificationCallbacks
;
86 BaseWebSocketChannel::GetLoadGroup(nsILoadGroup
**aLoadGroup
)
88 LOG(("BaseWebSocketChannel::GetLoadGroup() %p\n", this));
89 NS_IF_ADDREF(*aLoadGroup
= mLoadGroup
);
94 BaseWebSocketChannel::SetLoadGroup(nsILoadGroup
*aLoadGroup
)
96 LOG(("BaseWebSocketChannel::SetLoadGroup() %p\n", this));
97 mLoadGroup
= aLoadGroup
;
102 BaseWebSocketChannel::SetLoadInfo(nsILoadInfo
* aLoadInfo
)
104 mLoadInfo
= aLoadInfo
;
109 BaseWebSocketChannel::GetLoadInfo(nsILoadInfo
** aLoadInfo
)
111 NS_IF_ADDREF(*aLoadInfo
= mLoadInfo
);
116 BaseWebSocketChannel::GetExtensions(nsACString
&aExtensions
)
118 LOG(("BaseWebSocketChannel::GetExtensions() %p\n", this));
119 aExtensions
= mNegotiatedExtensions
;
124 BaseWebSocketChannel::GetProtocol(nsACString
&aProtocol
)
126 LOG(("BaseWebSocketChannel::GetProtocol() %p\n", this));
127 aProtocol
= mProtocol
;
132 BaseWebSocketChannel::SetProtocol(const nsACString
&aProtocol
)
134 LOG(("BaseWebSocketChannel::SetProtocol() %p\n", this));
135 mProtocol
= aProtocol
; /* the sub protocol */
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;
150 BaseWebSocketChannel::SetPingInterval(uint32_t aSeconds
)
153 return NS_ERROR_IN_PROGRESS
;
156 mPingInterval
= aSeconds
* 1000;
157 mClientSetPingInterval
= 1;
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;
173 BaseWebSocketChannel::SetPingTimeout(uint32_t aSeconds
)
176 return NS_ERROR_IN_PROGRESS
;
179 mPingResponseTimeout
= aSeconds
* 1000;
180 mClientSetPingTimeout
= 1;
185 //-----------------------------------------------------------------------------
186 // BaseWebSocketChannel::nsIProtocolHandler
187 //-----------------------------------------------------------------------------
191 BaseWebSocketChannel::GetScheme(nsACString
&aScheme
)
193 LOG(("BaseWebSocketChannel::GetScheme() %p\n", this));
196 aScheme
.AssignLiteral("wss");
198 aScheme
.AssignLiteral("ws");
203 BaseWebSocketChannel::GetDefaultPort(int32_t *aDefaultPort
)
205 LOG(("BaseWebSocketChannel::GetDefaultPort() %p\n", this));
208 *aDefaultPort
= kDefaultWSSPort
;
210 *aDefaultPort
= kDefaultWSPort
;
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
;
225 BaseWebSocketChannel::NewURI(const nsACString
& aSpec
, const char *aOriginCharset
,
226 nsIURI
*aBaseURI
, nsIURI
**_retval
)
228 LOG(("BaseWebSocketChannel::NewURI() %p\n", this));
231 nsresult rv
= GetDefaultPort(&port
);
235 nsRefPtr
<nsStandardURL
> url
= new nsStandardURL();
236 rv
= url
->Init(nsIStandardURL::URLTYPE_AUTHORITY
, port
, aSpec
,
237 aOriginCharset
, aBaseURI
);
240 NS_ADDREF(*_retval
= url
);
245 BaseWebSocketChannel::NewChannel2(nsIURI
* aURI
,
246 nsILoadInfo
* aLoadInfo
,
247 nsIChannel
** outChannel
)
249 LOG(("BaseWebSocketChannel::NewChannel2() %p\n", this));
250 return NS_ERROR_NOT_IMPLEMENTED
;
254 BaseWebSocketChannel::NewChannel(nsIURI
*aURI
, nsIChannel
**_retval
)
256 LOG(("BaseWebSocketChannel::NewChannel() %p\n", this));
257 return NS_ERROR_NOT_IMPLEMENTED
;
261 BaseWebSocketChannel::AllowPort(int32_t port
, const char *scheme
,
264 LOG(("BaseWebSocketChannel::AllowPort() %p\n", this));
266 // do not override any blacklisted ports
271 //-----------------------------------------------------------------------------
272 // BaseWebSocketChannel::nsIThreadRetargetableRequest
273 //-----------------------------------------------------------------------------
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
);
288 BaseWebSocketChannel::ListenerAndContextContainer::ListenerAndContextContainer(
289 nsIWebSocketListener
* aListener
,
290 nsISupports
* aContext
)
291 : mListener(aListener
)
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);
310 } // namespace mozilla