netwerk/protocol/websocket/BaseWebSocketChannel.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial