1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/protocol/websocket/BaseWebSocketChannel.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,264 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set sw=2 ts=8 et tw=80 : */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "WebSocketLog.h" 1.11 +#include "BaseWebSocketChannel.h" 1.12 +#include "MainThreadUtils.h" 1.13 +#include "nsILoadGroup.h" 1.14 +#include "nsIInterfaceRequestor.h" 1.15 +#include "nsAutoPtr.h" 1.16 +#include "nsStandardURL.h" 1.17 + 1.18 +#if defined(PR_LOGGING) 1.19 +PRLogModuleInfo *webSocketLog = nullptr; 1.20 +#endif 1.21 + 1.22 +namespace mozilla { 1.23 +namespace net { 1.24 + 1.25 +BaseWebSocketChannel::BaseWebSocketChannel() 1.26 + : mEncrypted(0) 1.27 + , mWasOpened(0) 1.28 + , mClientSetPingInterval(0) 1.29 + , mClientSetPingTimeout(0) 1.30 + , mPingInterval(0) 1.31 + , mPingResponseTimeout(10000) 1.32 +{ 1.33 +#if defined(PR_LOGGING) 1.34 + if (!webSocketLog) 1.35 + webSocketLog = PR_NewLogModule("nsWebSocket"); 1.36 +#endif 1.37 +} 1.38 + 1.39 +//----------------------------------------------------------------------------- 1.40 +// BaseWebSocketChannel::nsIWebSocketChannel 1.41 +//----------------------------------------------------------------------------- 1.42 + 1.43 +NS_IMETHODIMP 1.44 +BaseWebSocketChannel::GetOriginalURI(nsIURI **aOriginalURI) 1.45 +{ 1.46 + LOG(("BaseWebSocketChannel::GetOriginalURI() %p\n", this)); 1.47 + 1.48 + if (!mOriginalURI) 1.49 + return NS_ERROR_NOT_INITIALIZED; 1.50 + NS_ADDREF(*aOriginalURI = mOriginalURI); 1.51 + return NS_OK; 1.52 +} 1.53 + 1.54 +NS_IMETHODIMP 1.55 +BaseWebSocketChannel::GetURI(nsIURI **aURI) 1.56 +{ 1.57 + LOG(("BaseWebSocketChannel::GetURI() %p\n", this)); 1.58 + 1.59 + if (!mOriginalURI) 1.60 + return NS_ERROR_NOT_INITIALIZED; 1.61 + if (mURI) 1.62 + NS_ADDREF(*aURI = mURI); 1.63 + else 1.64 + NS_ADDREF(*aURI = mOriginalURI); 1.65 + return NS_OK; 1.66 +} 1.67 + 1.68 +NS_IMETHODIMP 1.69 +BaseWebSocketChannel:: 1.70 +GetNotificationCallbacks(nsIInterfaceRequestor **aNotificationCallbacks) 1.71 +{ 1.72 + LOG(("BaseWebSocketChannel::GetNotificationCallbacks() %p\n", this)); 1.73 + NS_IF_ADDREF(*aNotificationCallbacks = mCallbacks); 1.74 + return NS_OK; 1.75 +} 1.76 + 1.77 +NS_IMETHODIMP 1.78 +BaseWebSocketChannel:: 1.79 +SetNotificationCallbacks(nsIInterfaceRequestor *aNotificationCallbacks) 1.80 +{ 1.81 + LOG(("BaseWebSocketChannel::SetNotificationCallbacks() %p\n", this)); 1.82 + mCallbacks = aNotificationCallbacks; 1.83 + return NS_OK; 1.84 +} 1.85 + 1.86 +NS_IMETHODIMP 1.87 +BaseWebSocketChannel::GetLoadGroup(nsILoadGroup **aLoadGroup) 1.88 +{ 1.89 + LOG(("BaseWebSocketChannel::GetLoadGroup() %p\n", this)); 1.90 + NS_IF_ADDREF(*aLoadGroup = mLoadGroup); 1.91 + return NS_OK; 1.92 +} 1.93 + 1.94 +NS_IMETHODIMP 1.95 +BaseWebSocketChannel::SetLoadGroup(nsILoadGroup *aLoadGroup) 1.96 +{ 1.97 + LOG(("BaseWebSocketChannel::SetLoadGroup() %p\n", this)); 1.98 + mLoadGroup = aLoadGroup; 1.99 + return NS_OK; 1.100 +} 1.101 + 1.102 +NS_IMETHODIMP 1.103 +BaseWebSocketChannel::GetExtensions(nsACString &aExtensions) 1.104 +{ 1.105 + LOG(("BaseWebSocketChannel::GetExtensions() %p\n", this)); 1.106 + aExtensions = mNegotiatedExtensions; 1.107 + return NS_OK; 1.108 +} 1.109 + 1.110 +NS_IMETHODIMP 1.111 +BaseWebSocketChannel::GetProtocol(nsACString &aProtocol) 1.112 +{ 1.113 + LOG(("BaseWebSocketChannel::GetProtocol() %p\n", this)); 1.114 + aProtocol = mProtocol; 1.115 + return NS_OK; 1.116 +} 1.117 + 1.118 +NS_IMETHODIMP 1.119 +BaseWebSocketChannel::SetProtocol(const nsACString &aProtocol) 1.120 +{ 1.121 + LOG(("BaseWebSocketChannel::SetProtocol() %p\n", this)); 1.122 + mProtocol = aProtocol; /* the sub protocol */ 1.123 + return NS_OK; 1.124 +} 1.125 + 1.126 +NS_IMETHODIMP 1.127 +BaseWebSocketChannel::GetPingInterval(uint32_t *aSeconds) 1.128 +{ 1.129 + // stored in ms but should only have second resolution 1.130 + MOZ_ASSERT(!(mPingInterval % 1000)); 1.131 + 1.132 + *aSeconds = mPingInterval / 1000; 1.133 + return NS_OK; 1.134 +} 1.135 + 1.136 +NS_IMETHODIMP 1.137 +BaseWebSocketChannel::SetPingInterval(uint32_t aSeconds) 1.138 +{ 1.139 + if (mWasOpened) { 1.140 + return NS_ERROR_IN_PROGRESS; 1.141 + } 1.142 + 1.143 + mPingInterval = aSeconds * 1000; 1.144 + mClientSetPingInterval = 1; 1.145 + 1.146 + return NS_OK; 1.147 +} 1.148 + 1.149 +NS_IMETHODIMP 1.150 +BaseWebSocketChannel::GetPingTimeout(uint32_t *aSeconds) 1.151 +{ 1.152 + // stored in ms but should only have second resolution 1.153 + MOZ_ASSERT(!(mPingResponseTimeout % 1000)); 1.154 + 1.155 + *aSeconds = mPingResponseTimeout / 1000; 1.156 + return NS_OK; 1.157 +} 1.158 + 1.159 +NS_IMETHODIMP 1.160 +BaseWebSocketChannel::SetPingTimeout(uint32_t aSeconds) 1.161 +{ 1.162 + if (mWasOpened) { 1.163 + return NS_ERROR_IN_PROGRESS; 1.164 + } 1.165 + 1.166 + mPingResponseTimeout = aSeconds * 1000; 1.167 + mClientSetPingTimeout = 1; 1.168 + 1.169 + return NS_OK; 1.170 +} 1.171 + 1.172 +//----------------------------------------------------------------------------- 1.173 +// BaseWebSocketChannel::nsIProtocolHandler 1.174 +//----------------------------------------------------------------------------- 1.175 + 1.176 + 1.177 +NS_IMETHODIMP 1.178 +BaseWebSocketChannel::GetScheme(nsACString &aScheme) 1.179 +{ 1.180 + LOG(("BaseWebSocketChannel::GetScheme() %p\n", this)); 1.181 + 1.182 + if (mEncrypted) 1.183 + aScheme.AssignLiteral("wss"); 1.184 + else 1.185 + aScheme.AssignLiteral("ws"); 1.186 + return NS_OK; 1.187 +} 1.188 + 1.189 +NS_IMETHODIMP 1.190 +BaseWebSocketChannel::GetDefaultPort(int32_t *aDefaultPort) 1.191 +{ 1.192 + LOG(("BaseWebSocketChannel::GetDefaultPort() %p\n", this)); 1.193 + 1.194 + if (mEncrypted) 1.195 + *aDefaultPort = kDefaultWSSPort; 1.196 + else 1.197 + *aDefaultPort = kDefaultWSPort; 1.198 + return NS_OK; 1.199 +} 1.200 + 1.201 +NS_IMETHODIMP 1.202 +BaseWebSocketChannel::GetProtocolFlags(uint32_t *aProtocolFlags) 1.203 +{ 1.204 + LOG(("BaseWebSocketChannel::GetProtocolFlags() %p\n", this)); 1.205 + 1.206 + *aProtocolFlags = URI_NORELATIVE | URI_NON_PERSISTABLE | ALLOWS_PROXY | 1.207 + ALLOWS_PROXY_HTTP | URI_DOES_NOT_RETURN_DATA | URI_DANGEROUS_TO_LOAD; 1.208 + return NS_OK; 1.209 +} 1.210 + 1.211 +NS_IMETHODIMP 1.212 +BaseWebSocketChannel::NewURI(const nsACString & aSpec, const char *aOriginCharset, 1.213 + nsIURI *aBaseURI, nsIURI **_retval) 1.214 +{ 1.215 + LOG(("BaseWebSocketChannel::NewURI() %p\n", this)); 1.216 + 1.217 + int32_t port; 1.218 + nsresult rv = GetDefaultPort(&port); 1.219 + if (NS_FAILED(rv)) 1.220 + return rv; 1.221 + 1.222 + nsRefPtr<nsStandardURL> url = new nsStandardURL(); 1.223 + rv = url->Init(nsIStandardURL::URLTYPE_AUTHORITY, port, aSpec, 1.224 + aOriginCharset, aBaseURI); 1.225 + if (NS_FAILED(rv)) 1.226 + return rv; 1.227 + NS_ADDREF(*_retval = url); 1.228 + return NS_OK; 1.229 +} 1.230 + 1.231 +NS_IMETHODIMP 1.232 +BaseWebSocketChannel::NewChannel(nsIURI *aURI, nsIChannel **_retval) 1.233 +{ 1.234 + LOG(("BaseWebSocketChannel::NewChannel() %p\n", this)); 1.235 + return NS_ERROR_NOT_IMPLEMENTED; 1.236 +} 1.237 + 1.238 +NS_IMETHODIMP 1.239 +BaseWebSocketChannel::AllowPort(int32_t port, const char *scheme, 1.240 + bool *_retval) 1.241 +{ 1.242 + LOG(("BaseWebSocketChannel::AllowPort() %p\n", this)); 1.243 + 1.244 + // do not override any blacklisted ports 1.245 + *_retval = false; 1.246 + return NS_OK; 1.247 +} 1.248 + 1.249 +//----------------------------------------------------------------------------- 1.250 +// BaseWebSocketChannel::nsIThreadRetargetableRequest 1.251 +//----------------------------------------------------------------------------- 1.252 + 1.253 +NS_IMETHODIMP 1.254 +BaseWebSocketChannel::RetargetDeliveryTo(nsIEventTarget* aTargetThread) 1.255 +{ 1.256 + MOZ_ASSERT(NS_IsMainThread()); 1.257 + MOZ_ASSERT(aTargetThread); 1.258 + MOZ_ASSERT(!mTargetThread, "Delivery target should be set once, before AsyncOpen"); 1.259 + MOZ_ASSERT(!mWasOpened, "Should not be called after AsyncOpen!"); 1.260 + 1.261 + mTargetThread = do_QueryInterface(aTargetThread); 1.262 + MOZ_ASSERT(mTargetThread); 1.263 + return NS_OK; 1.264 +} 1.265 + 1.266 +} // namespace net 1.267 +} // namespace mozilla