michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set sw=2 ts=8 et tw=80 : */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "WebSocketLog.h" michael@0: #include "BaseWebSocketChannel.h" michael@0: #include "MainThreadUtils.h" michael@0: #include "nsILoadGroup.h" michael@0: #include "nsIInterfaceRequestor.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsStandardURL.h" michael@0: michael@0: #if defined(PR_LOGGING) michael@0: PRLogModuleInfo *webSocketLog = nullptr; michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: BaseWebSocketChannel::BaseWebSocketChannel() michael@0: : mEncrypted(0) michael@0: , mWasOpened(0) michael@0: , mClientSetPingInterval(0) michael@0: , mClientSetPingTimeout(0) michael@0: , mPingInterval(0) michael@0: , mPingResponseTimeout(10000) michael@0: { michael@0: #if defined(PR_LOGGING) michael@0: if (!webSocketLog) michael@0: webSocketLog = PR_NewLogModule("nsWebSocket"); michael@0: #endif michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // BaseWebSocketChannel::nsIWebSocketChannel michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::GetOriginalURI(nsIURI **aOriginalURI) michael@0: { michael@0: LOG(("BaseWebSocketChannel::GetOriginalURI() %p\n", this)); michael@0: michael@0: if (!mOriginalURI) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: NS_ADDREF(*aOriginalURI = mOriginalURI); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::GetURI(nsIURI **aURI) michael@0: { michael@0: LOG(("BaseWebSocketChannel::GetURI() %p\n", this)); michael@0: michael@0: if (!mOriginalURI) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: if (mURI) michael@0: NS_ADDREF(*aURI = mURI); michael@0: else michael@0: NS_ADDREF(*aURI = mOriginalURI); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel:: michael@0: GetNotificationCallbacks(nsIInterfaceRequestor **aNotificationCallbacks) michael@0: { michael@0: LOG(("BaseWebSocketChannel::GetNotificationCallbacks() %p\n", this)); michael@0: NS_IF_ADDREF(*aNotificationCallbacks = mCallbacks); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel:: michael@0: SetNotificationCallbacks(nsIInterfaceRequestor *aNotificationCallbacks) michael@0: { michael@0: LOG(("BaseWebSocketChannel::SetNotificationCallbacks() %p\n", this)); michael@0: mCallbacks = aNotificationCallbacks; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::GetLoadGroup(nsILoadGroup **aLoadGroup) michael@0: { michael@0: LOG(("BaseWebSocketChannel::GetLoadGroup() %p\n", this)); michael@0: NS_IF_ADDREF(*aLoadGroup = mLoadGroup); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::SetLoadGroup(nsILoadGroup *aLoadGroup) michael@0: { michael@0: LOG(("BaseWebSocketChannel::SetLoadGroup() %p\n", this)); michael@0: mLoadGroup = aLoadGroup; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::GetExtensions(nsACString &aExtensions) michael@0: { michael@0: LOG(("BaseWebSocketChannel::GetExtensions() %p\n", this)); michael@0: aExtensions = mNegotiatedExtensions; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::GetProtocol(nsACString &aProtocol) michael@0: { michael@0: LOG(("BaseWebSocketChannel::GetProtocol() %p\n", this)); michael@0: aProtocol = mProtocol; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::SetProtocol(const nsACString &aProtocol) michael@0: { michael@0: LOG(("BaseWebSocketChannel::SetProtocol() %p\n", this)); michael@0: mProtocol = aProtocol; /* the sub protocol */ michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::GetPingInterval(uint32_t *aSeconds) michael@0: { michael@0: // stored in ms but should only have second resolution michael@0: MOZ_ASSERT(!(mPingInterval % 1000)); michael@0: michael@0: *aSeconds = mPingInterval / 1000; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::SetPingInterval(uint32_t aSeconds) michael@0: { michael@0: if (mWasOpened) { michael@0: return NS_ERROR_IN_PROGRESS; michael@0: } michael@0: michael@0: mPingInterval = aSeconds * 1000; michael@0: mClientSetPingInterval = 1; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::GetPingTimeout(uint32_t *aSeconds) michael@0: { michael@0: // stored in ms but should only have second resolution michael@0: MOZ_ASSERT(!(mPingResponseTimeout % 1000)); michael@0: michael@0: *aSeconds = mPingResponseTimeout / 1000; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::SetPingTimeout(uint32_t aSeconds) michael@0: { michael@0: if (mWasOpened) { michael@0: return NS_ERROR_IN_PROGRESS; michael@0: } michael@0: michael@0: mPingResponseTimeout = aSeconds * 1000; michael@0: mClientSetPingTimeout = 1; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // BaseWebSocketChannel::nsIProtocolHandler michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::GetScheme(nsACString &aScheme) michael@0: { michael@0: LOG(("BaseWebSocketChannel::GetScheme() %p\n", this)); michael@0: michael@0: if (mEncrypted) michael@0: aScheme.AssignLiteral("wss"); michael@0: else michael@0: aScheme.AssignLiteral("ws"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::GetDefaultPort(int32_t *aDefaultPort) michael@0: { michael@0: LOG(("BaseWebSocketChannel::GetDefaultPort() %p\n", this)); michael@0: michael@0: if (mEncrypted) michael@0: *aDefaultPort = kDefaultWSSPort; michael@0: else michael@0: *aDefaultPort = kDefaultWSPort; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::GetProtocolFlags(uint32_t *aProtocolFlags) michael@0: { michael@0: LOG(("BaseWebSocketChannel::GetProtocolFlags() %p\n", this)); michael@0: michael@0: *aProtocolFlags = URI_NORELATIVE | URI_NON_PERSISTABLE | ALLOWS_PROXY | michael@0: ALLOWS_PROXY_HTTP | URI_DOES_NOT_RETURN_DATA | URI_DANGEROUS_TO_LOAD; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::NewURI(const nsACString & aSpec, const char *aOriginCharset, michael@0: nsIURI *aBaseURI, nsIURI **_retval) michael@0: { michael@0: LOG(("BaseWebSocketChannel::NewURI() %p\n", this)); michael@0: michael@0: int32_t port; michael@0: nsresult rv = GetDefaultPort(&port); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsRefPtr url = new nsStandardURL(); michael@0: rv = url->Init(nsIStandardURL::URLTYPE_AUTHORITY, port, aSpec, michael@0: aOriginCharset, aBaseURI); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: NS_ADDREF(*_retval = url); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::NewChannel(nsIURI *aURI, nsIChannel **_retval) michael@0: { michael@0: LOG(("BaseWebSocketChannel::NewChannel() %p\n", this)); michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::AllowPort(int32_t port, const char *scheme, michael@0: bool *_retval) michael@0: { michael@0: LOG(("BaseWebSocketChannel::AllowPort() %p\n", this)); michael@0: michael@0: // do not override any blacklisted ports michael@0: *_retval = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // BaseWebSocketChannel::nsIThreadRetargetableRequest michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMETHODIMP michael@0: BaseWebSocketChannel::RetargetDeliveryTo(nsIEventTarget* aTargetThread) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(aTargetThread); michael@0: MOZ_ASSERT(!mTargetThread, "Delivery target should be set once, before AsyncOpen"); michael@0: MOZ_ASSERT(!mWasOpened, "Should not be called after AsyncOpen!"); michael@0: michael@0: mTargetThread = do_QueryInterface(aTargetThread); michael@0: MOZ_ASSERT(mTargetThread); michael@0: return NS_OK; michael@0: } michael@0: michael@0: } // namespace net michael@0: } // namespace mozilla