2010-06-17 11:34:24 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Wellington Fernando de Macedo.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Wellington Fernando de Macedo <wfernandom2004@gmail.com> (original author)
|
2011-05-21 18:27:52 -07:00
|
|
|
* Patrick McManus <mcmanus@ducksong.com>
|
2010-06-17 11:34:24 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
#include "nsWebSocket.h"
|
2011-05-23 09:46:36 -07:00
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsEventDispatcher.h"
|
|
|
|
#include "nsDOMError.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2011-10-03 12:11:31 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2010-06-17 11:34:24 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIPrivateDOMEvent.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIDOMMessageEvent.h"
|
|
|
|
#include "nsIPromptFactory.h"
|
|
|
|
#include "nsIWindowWatcher.h"
|
|
|
|
#include "nsIPrompt.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsLayoutStatics.h"
|
2010-06-17 11:36:01 -07:00
|
|
|
#include "nsIDOMCloseEvent.h"
|
|
|
|
#include "nsICryptoHash.h"
|
2010-12-21 09:05:34 -08:00
|
|
|
#include "jsdbgapi.h"
|
|
|
|
#include "nsIJSContextStack.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsIScriptError.h"
|
2011-05-21 18:27:52 -07:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsILoadGroup.h"
|
2011-05-24 23:31:59 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-08-03 09:42:02 -07:00
|
|
|
#include "nsDOMLists.h"
|
2011-09-29 09:06:36 -07:00
|
|
|
#include "xpcpublic.h"
|
2010-06-17 11:34:24 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
#define UTF_8_REPLACEMENT_CHAR static_cast<PRUnichar>(0xFFFD)
|
|
|
|
|
|
|
|
#define ENSURE_TRUE_AND_FAIL_IF_FAILED(x, ret) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (NS_UNLIKELY(!(x))) { \
|
|
|
|
NS_WARNING("ENSURE_TRUE_AND_FAIL_IF_FAILED(" #x ") failed"); \
|
|
|
|
FailConnection(); \
|
|
|
|
return ret; \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
|
|
|
#define ENSURE_SUCCESS_AND_FAIL_IF_FAILED(res, ret) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
nsresult __rv = res; \
|
|
|
|
if (NS_FAILED(__rv)) { \
|
|
|
|
NS_ENSURE_SUCCESS_BODY(res, ret) \
|
|
|
|
FailConnection(); \
|
|
|
|
return ret; \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsresult
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::PrintErrorOnConsole(const char *aBundleURI,
|
|
|
|
const PRUnichar *aError,
|
|
|
|
const PRUnichar **aFormatStrings,
|
|
|
|
PRUint32 aFormatStringsLen)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:34:24 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsCOMPtr<nsIStringBundle> strBundle;
|
|
|
|
rv = bundleService->CreateBundle(aBundleURI, getter_AddRefs(strBundle));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsCOMPtr<nsIConsoleService> console(
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsCOMPtr<nsIScriptError2> errorObject(
|
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
// Localize the error message
|
|
|
|
nsXPIDLString message;
|
|
|
|
if (aFormatStrings) {
|
|
|
|
rv = strBundle->FormatStringFromName(aError, aFormatStrings,
|
|
|
|
aFormatStringsLen,
|
|
|
|
getter_Copies(message));
|
2010-06-17 11:34:24 -07:00
|
|
|
} else {
|
2011-05-21 18:27:52 -07:00
|
|
|
rv = strBundle->GetStringFromName(aError, getter_Copies(message));
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
errorObject->InitWithWindowID(message.get(),
|
|
|
|
NS_ConvertUTF8toUTF16(mScriptFile).get(),
|
|
|
|
nsnull, mScriptLine, 0,
|
|
|
|
nsIScriptError::errorFlag, "Web Socket",
|
|
|
|
mInnerWindowID);
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
// print the error message directly to the JS console
|
|
|
|
nsCOMPtr<nsIScriptError> logError(do_QueryInterface(errorObject));
|
|
|
|
rv = console->LogMessage(logError);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
// when this is called the browser side wants no more part of it
|
|
|
|
nsresult
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::CloseConnection()
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2011-10-20 01:27:13 -07:00
|
|
|
if (mDisconnected)
|
2011-06-08 05:27:11 -07:00
|
|
|
return NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
// Disconnect() can release this object, so we keep a
|
|
|
|
// reference until the end of the method
|
2011-10-20 01:27:13 -07:00
|
|
|
nsRefPtr<nsWebSocket> kungfuDeathGrip = this;
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
if (mReadyState == nsIMozWebSocket::CONNECTING) {
|
|
|
|
SetReadyState(nsIMozWebSocket::CLOSED);
|
2011-11-22 15:42:59 -08:00
|
|
|
if (mWebSocketChannel)
|
|
|
|
mWebSocketChannel->Close(mClientReasonCode, mClientReason);
|
2011-05-21 18:27:52 -07:00
|
|
|
Disconnect();
|
|
|
|
return NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
SetReadyState(nsIMozWebSocket::CLOSING);
|
2011-05-21 18:27:52 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
if (mDisconnected) {
|
|
|
|
SetReadyState(nsIMozWebSocket::CLOSED);
|
2011-05-21 18:27:52 -07:00
|
|
|
Disconnect();
|
|
|
|
return NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
return mWebSocketChannel->Close(mClientReasonCode, mClientReason);
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsresult
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::ConsoleError()
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
|
|
|
nsresult rv;
|
2011-10-20 01:27:13 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsCAutoString targetSpec;
|
2011-10-20 01:27:13 -07:00
|
|
|
rv = mURI->GetSpec(targetSpec);
|
2011-05-21 18:27:52 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to get targetSpec");
|
|
|
|
} else {
|
|
|
|
NS_ConvertUTF8toUTF16 specUTF16(targetSpec);
|
|
|
|
const PRUnichar *formatStrings[] = { specUTF16.get() };
|
2011-10-20 01:27:13 -07:00
|
|
|
|
|
|
|
if (mReadyState < nsIMozWebSocket::OPEN) {
|
2011-05-21 18:27:52 -07:00
|
|
|
PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
|
|
|
NS_LITERAL_STRING("connectionFailure").get(),
|
2011-10-10 22:50:08 -07:00
|
|
|
formatStrings, ArrayLength(formatStrings));
|
2011-05-21 18:27:52 -07:00
|
|
|
} else {
|
|
|
|
PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
|
|
|
NS_LITERAL_STRING("netInterrupt").get(),
|
2011-10-10 22:50:08 -07:00
|
|
|
formatStrings, ArrayLength(formatStrings));
|
2011-05-21 18:27:52 -07:00
|
|
|
}
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
2011-08-02 16:55:17 -07:00
|
|
|
/// todo some specific errors - like for message too large
|
2011-05-21 18:27:52 -07:00
|
|
|
return rv;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsresult
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::FailConnection()
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2011-10-20 01:27:13 -07:00
|
|
|
ConsoleError();
|
|
|
|
|
|
|
|
nsresult rv = CloseConnection();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("error"));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_WARNING("Failed to dispatch the error event");
|
|
|
|
|
|
|
|
return NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsresult
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::Disconnect()
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
if (mDisconnected)
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
2011-10-20 01:27:13 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
if (loadGroup)
|
|
|
|
loadGroup->RemoveRequest(this, nsnull, NS_OK);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
// DontKeepAliveAnyMore() can release the object. So hold a reference to this
|
|
|
|
// until the end of the method.
|
|
|
|
nsRefPtr<nsWebSocket> kungfuDeathGrip = this;
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
DontKeepAliveAnyMore();
|
|
|
|
mWebSocketChannel = nsnull;
|
|
|
|
mDisconnected = true;
|
2011-06-08 05:27:11 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
return NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-20 01:27:13 -07:00
|
|
|
// nsWebSocket::nsIWebSocketListener methods:
|
2010-06-17 11:34:24 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::OnMessageAvailable(nsISupports *aContext, const nsACString & aMsg)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2011-10-20 01:27:13 -07:00
|
|
|
|
|
|
|
NS_ABORT_IF_FALSE(!mDisconnected, "Received message after disconnecting");
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
// Dispatch New Message
|
2011-10-20 01:27:13 -07:00
|
|
|
nsresult rv = CreateAndDispatchMessageEvent(aMsg);
|
2011-05-21 18:27:52 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the message event");
|
2010-06-17 11:36:01 -07:00
|
|
|
}
|
2011-05-21 18:27:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-17 11:36:01 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::OnBinaryMessageAvailable(nsISupports *aContext,
|
|
|
|
const nsACString & aMsg)
|
2011-05-21 18:27:52 -07:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::OnStart(nsISupports *aContext)
|
2011-05-21 18:27:52 -07:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2011-10-20 01:27:13 -07:00
|
|
|
if (mDisconnected)
|
2011-05-21 18:27:52 -07:00
|
|
|
return NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
if (!mRequestedProtocolList.IsEmpty())
|
|
|
|
mWebSocketChannel->GetProtocol(mEstablishedProtocol);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
mWebSocketChannel->GetExtensions(mEstablishedExtensions);
|
2011-08-03 20:46:13 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
SetReadyState(nsIMozWebSocket::OPEN);
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::OnStop(nsISupports *aContext, nsresult aStatusCode)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2011-10-20 01:27:13 -07:00
|
|
|
if (mDisconnected)
|
2011-05-21 18:27:52 -07:00
|
|
|
return NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
mClosedCleanly = NS_SUCCEEDED(aStatusCode);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
if (aStatusCode == NS_BASE_STREAM_CLOSED &&
|
|
|
|
mReadyState >= nsIMozWebSocket::CLOSING) {
|
2011-05-21 18:27:52 -07:00
|
|
|
// don't generate an error event just because of an unclean close
|
|
|
|
aStatusCode = NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
if (NS_FAILED(aStatusCode)) {
|
|
|
|
ConsoleError();
|
2011-10-20 01:27:13 -07:00
|
|
|
nsresult rv = CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("error"));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_WARNING("Failed to dispatch the error event");
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
SetReadyState(nsIMozWebSocket::CLOSED);
|
|
|
|
Disconnect();
|
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::OnAcknowledge(nsISupports *aContext, PRUint32 aSize)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
if (aSize > mOutgoingBufferedAmount)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2011-10-20 01:27:13 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
mOutgoingBufferedAmount -= aSize;
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::OnServerClose(nsISupports *aContext, PRUint16 aCode,
|
|
|
|
const nsACString &aReason)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2011-10-20 01:27:13 -07:00
|
|
|
mServerReasonCode = aCode;
|
|
|
|
CopyUTF8toUTF16(aReason, mServerReason);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
CloseConnection(); /* reciprocate! */
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-20 01:27:13 -07:00
|
|
|
// nsWebSocket::nsIInterfaceRequestor
|
2010-06-17 11:34:24 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::GetInterface(const nsIID &aIID, void **aResult)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
if (mDisconnected)
|
2011-06-08 05:27:11 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIAuthPrompt)) ||
|
|
|
|
aIID.Equals(NS_GET_IID(nsIAuthPrompt2))) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc =
|
2011-10-20 01:27:13 -07:00
|
|
|
nsContentUtils::GetDocumentFromScriptContext(mScriptContext);
|
|
|
|
if (!doc)
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPromptFactory> wwatch =
|
|
|
|
do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> outerWindow = doc->GetWindow();
|
|
|
|
return wwatch->GetPrompt(outerWindow, aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsWebSocket
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
nsWebSocket::nsWebSocket() : mKeepingAlive(false),
|
|
|
|
mCheckMustKeepAlive(true),
|
|
|
|
mTriggeredCloseEvent(false),
|
2011-10-20 01:27:13 -07:00
|
|
|
mClosedCleanly(false),
|
|
|
|
mDisconnected(false),
|
2011-08-03 12:15:25 -07:00
|
|
|
mClientReasonCode(0),
|
|
|
|
mServerReasonCode(nsIWebSocketChannel::CLOSE_ABNORMAL),
|
2011-07-26 06:30:14 -07:00
|
|
|
mReadyState(nsIMozWebSocket::CONNECTING),
|
2010-12-21 09:05:34 -08:00
|
|
|
mOutgoingBufferedAmount(0),
|
|
|
|
mScriptLine(0),
|
2011-08-24 13:44:35 -07:00
|
|
|
mInnerWindowID(0)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2011-10-20 01:27:13 -07:00
|
|
|
nsLayoutStatics::AddRef();
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsWebSocket::~nsWebSocket()
|
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:34:24 -07:00
|
|
|
if (mListenerManager) {
|
|
|
|
mListenerManager->Disconnect();
|
|
|
|
mListenerManager = nsnull;
|
|
|
|
}
|
2011-10-20 01:27:13 -07:00
|
|
|
Disconnect();
|
|
|
|
nsLayoutStatics::Release();
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsWebSocket)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsWebSocket,
|
|
|
|
nsDOMEventTargetWrapperCache)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnOpenListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnMessageListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnCloseListener)
|
2010-06-19 02:00:57 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnErrorListener)
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPrincipal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mURI)
|
2011-10-20 01:27:13 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mWebSocketChannel)
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsWebSocket,
|
|
|
|
nsDOMEventTargetWrapperCache)
|
2011-10-20 01:27:13 -07:00
|
|
|
tmp->Disconnect();
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnOpenListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnMessageListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnCloseListener)
|
2010-06-19 02:00:57 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnErrorListener)
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPrincipal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mURI)
|
2011-10-20 01:27:13 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mWebSocketChannel)
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
DOMCI_DATA(MozWebSocket, nsWebSocket)
|
2010-06-17 11:34:24 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsWebSocket)
|
2011-07-26 06:30:14 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMozWebSocket)
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIJSNativeInitializer)
|
2011-10-20 01:27:13 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIWebSocketListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequest)
|
2011-07-26 06:30:14 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozWebSocket)
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetWrapperCache)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsWebSocket, nsDOMEventTargetWrapperCache)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsWebSocket, nsDOMEventTargetWrapperCache)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsWebSocket::nsIJSNativeInitializer methods:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Initialize method is called from XPConnect via nsIJSNativeInitializer.
|
|
|
|
* It is used for constructing our nsWebSocket from JavaScript. It expects a URL
|
2011-08-03 09:42:02 -07:00
|
|
|
* string parameter and an optional protocol parameter which may be a string or
|
|
|
|
* an array of strings. It also initializes the principal, the script context and
|
|
|
|
* the window owner.
|
2010-06-17 11:34:24 -07:00
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWebSocket::Initialize(nsISupports* aOwner,
|
2010-06-17 11:36:01 -07:00
|
|
|
JSContext* aContext,
|
|
|
|
JSObject* aObject,
|
|
|
|
PRUint32 aArgc,
|
|
|
|
jsval* aArgv)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2011-08-03 09:42:02 -07:00
|
|
|
nsAutoString urlParam;
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2010-12-08 14:12:51 -08:00
|
|
|
if (!PrefEnabled()) {
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
if (aArgc != 1 && aArgc != 2) {
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
JSAutoRequest ar(aContext);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
JSString* jsstr = JS_ValueToString(aContext, aArgv[0]);
|
2010-06-17 11:34:24 -07:00
|
|
|
if (!jsstr) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
2010-12-03 00:24:17 -08:00
|
|
|
|
2011-05-05 14:16:44 -07:00
|
|
|
JS::Anchor<JSString *> deleteProtector(jsstr);
|
2010-12-03 00:24:17 -08:00
|
|
|
size_t length;
|
|
|
|
const jschar *chars = JS_GetStringCharsAndLength(aContext, jsstr, &length);
|
|
|
|
if (!chars) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
urlParam.Assign(chars, length);
|
2011-05-05 14:16:44 -07:00
|
|
|
deleteProtector.clear();
|
2010-06-17 11:34:24 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> ownerWindow = do_QueryInterface(aOwner);
|
|
|
|
NS_ENSURE_STATE(ownerWindow);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aOwner);
|
|
|
|
NS_ENSURE_STATE(sgo);
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
|
|
|
|
NS_ENSURE_STATE(scriptContext);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal(do_QueryInterface(aOwner));
|
|
|
|
NS_ENSURE_STATE(scriptPrincipal);
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = scriptPrincipal->GetPrincipal();
|
|
|
|
NS_ENSURE_STATE(principal);
|
|
|
|
|
2011-08-03 09:42:02 -07:00
|
|
|
nsTArray<nsString> protocolArray;
|
|
|
|
|
|
|
|
if (aArgc == 2) {
|
|
|
|
JSObject *jsobj;
|
|
|
|
|
|
|
|
if (JSVAL_IS_OBJECT(aArgv[1]) &&
|
|
|
|
(jsobj = JSVAL_TO_OBJECT(aArgv[1])) &&
|
|
|
|
JS_IsArrayObject(aContext, jsobj)) {
|
|
|
|
jsuint len;
|
|
|
|
JS_GetArrayLength(aContext, jsobj, &len);
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < len; ++index) {
|
|
|
|
jsval value;
|
|
|
|
|
|
|
|
if (!JS_GetElement(aContext, jsobj, index, &value))
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
|
|
|
|
jsstr = JS_ValueToString(aContext, value);
|
|
|
|
if (!jsstr)
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
|
|
|
|
deleteProtector.set(jsstr);
|
|
|
|
chars = JS_GetStringCharsAndLength(aContext, jsstr, &length);
|
|
|
|
if (!chars)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsDependentString protocolElement(chars, length);
|
|
|
|
if (protocolElement.IsEmpty())
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
if (protocolArray.Contains(protocolElement))
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
if (protocolElement.FindChar(',') != -1) /* interferes w/list */
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
protocolArray.AppendElement(protocolElement);
|
|
|
|
deleteProtector.clear();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
jsstr = JS_ValueToString(aContext, aArgv[1]);
|
|
|
|
if (!jsstr)
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
|
|
|
|
deleteProtector.set(jsstr);
|
|
|
|
chars = JS_GetStringCharsAndLength(aContext, jsstr, &length);
|
|
|
|
if (!chars)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsDependentString protocolElement(chars, length);
|
|
|
|
if (protocolElement.IsEmpty())
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
if (protocolElement.FindChar(',') != -1) /* interferes w/list */
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
protocolArray.AppendElement(protocolElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Init(principal, scriptContext, ownerWindow, urlParam, protocolArray);
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsWebSocket methods:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
class nsAutoCloseWS
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsAutoCloseWS(nsWebSocket *aWebSocket)
|
|
|
|
: mWebSocket(aWebSocket)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~nsAutoCloseWS()
|
|
|
|
{
|
|
|
|
if (!mWebSocket->mWebSocketChannel) {
|
|
|
|
mWebSocket->CloseConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsWebSocket> mWebSocket;
|
|
|
|
};
|
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
nsresult
|
|
|
|
nsWebSocket::EstablishConnection()
|
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2011-10-20 01:27:13 -07:00
|
|
|
NS_ABORT_IF_FALSE(!mWebSocketChannel, "mWebSocketChannel should be null");
|
2010-06-17 11:34:24 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
nsCOMPtr<nsIWebSocketChannel> wsChannel;
|
|
|
|
nsAutoCloseWS autoClose(this);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
if (mSecure) {
|
|
|
|
wsChannel =
|
|
|
|
do_CreateInstance("@mozilla.org/network/protocol;1?name=wss", &rv);
|
|
|
|
} else {
|
|
|
|
wsChannel =
|
|
|
|
do_CreateInstance("@mozilla.org/network/protocol;1?name=ws", &rv);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = wsChannel->SetNotificationCallbacks(this);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// add ourselves to the document's load group and
|
|
|
|
// provide the http stack the loadgroup info too
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
rv = GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
if (loadGroup) {
|
|
|
|
rv = wsChannel->SetLoadGroup(loadGroup);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = loadGroup->AddRequest(this, nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRequestedProtocolList.IsEmpty()) {
|
|
|
|
rv = wsChannel->SetProtocol(mRequestedProtocolList);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-08-02 16:55:17 -07:00
|
|
|
}
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
nsCString asciiOrigin;
|
|
|
|
rv = nsContentUtils::GetASCIIOrigin(mPrincipal, asciiOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
ToLowerCase(asciiOrigin);
|
|
|
|
|
|
|
|
rv = wsChannel->AsyncOpen(mURI, asciiOrigin, this, nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mWebSocketChannel = wsChannel;
|
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
class nsWSCloseEvent : public nsRunnable
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2010-06-17 11:36:01 -07:00
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
nsWSCloseEvent(nsWebSocket *aWebSocket, bool aWasClean,
|
2011-08-03 12:15:25 -07:00
|
|
|
PRUint16 aCode, const nsString &aReason)
|
2010-06-17 11:36:01 -07:00
|
|
|
: mWebSocket(aWebSocket),
|
2011-08-03 12:15:25 -07:00
|
|
|
mWasClean(aWasClean),
|
|
|
|
mCode(aCode),
|
|
|
|
mReason(aReason)
|
2010-06-17 11:36:01 -07:00
|
|
|
{}
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2011-08-03 12:15:25 -07:00
|
|
|
nsresult rv = mWebSocket->CreateAndDispatchCloseEvent(mWasClean,
|
|
|
|
mCode, mReason);
|
2010-06-27 14:09:29 -07:00
|
|
|
mWebSocket->UpdateMustKeepAlive();
|
|
|
|
return rv;
|
2010-06-17 11:36:01 -07:00
|
|
|
}
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
private:
|
|
|
|
nsRefPtr<nsWebSocket> mWebSocket;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mWasClean;
|
2011-08-03 12:15:25 -07:00
|
|
|
PRUint16 mCode;
|
|
|
|
nsString mReason;
|
2010-06-17 11:36:01 -07:00
|
|
|
};
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
nsresult
|
|
|
|
nsWebSocket::CreateAndDispatchSimpleEvent(const nsString& aName)
|
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:34:24 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = CheckInnerWindowCorrectness();
|
2010-06-17 11:36:01 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
rv = NS_NewDOMEvent(getter_AddRefs(event), nsnull, nsnull);
|
2010-06-17 11:36:01 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = event->InitEvent(aName, false, false);
|
2010-06-17 11:36:01 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = privateEvent->SetTrusted(true);
|
2010-06-17 11:36:01 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return DispatchDOMEvent(nsnull, event, nsnull, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-05-21 18:27:52 -07:00
|
|
|
nsWebSocket::CreateAndDispatchMessageEvent(const nsACString& aData)
|
2010-06-17 11:36:01 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:36:01 -07:00
|
|
|
nsresult rv;
|
2011-10-20 01:27:13 -07:00
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
2011-05-23 11:53:31 -07:00
|
|
|
// Let's play get the JSContext
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(mOwner);
|
|
|
|
NS_ENSURE_TRUE(sgo, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsIScriptContext* scriptContext = sgo->GetContext();
|
|
|
|
NS_ENSURE_TRUE(scriptContext, NS_ERROR_FAILURE);
|
|
|
|
|
2011-09-18 02:22:17 -07:00
|
|
|
JSContext* cx = scriptContext->GetNativeContext();
|
2011-05-23 11:53:31 -07:00
|
|
|
NS_ENSURE_TRUE(cx, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// Now we can turn our string into a jsval
|
|
|
|
|
|
|
|
jsval jsData;
|
|
|
|
{
|
|
|
|
NS_ConvertUTF8toUTF16 utf16Data(aData);
|
|
|
|
JSString* jsString;
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
jsString = JS_NewUCStringCopyN(cx,
|
|
|
|
utf16Data.get(),
|
|
|
|
utf16Data.Length());
|
|
|
|
NS_ENSURE_TRUE(jsString, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
jsData = STRING_TO_JSVAL(jsString);
|
|
|
|
}
|
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
// create an event that uses the MessageEvent interface,
|
|
|
|
// which does not bubble, is not cancelable, and has no default action
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
rv = NS_NewDOMMessageEvent(getter_AddRefs(event), nsnull, nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMMessageEvent> messageEvent = do_QueryInterface(event);
|
|
|
|
rv = messageEvent->InitMessageEvent(NS_LITERAL_STRING("message"),
|
2011-10-17 07:59:28 -07:00
|
|
|
false, false,
|
2011-05-23 11:53:31 -07:00
|
|
|
jsData,
|
2011-05-21 18:27:52 -07:00
|
|
|
mUTF16Origin,
|
2010-06-17 11:36:01 -07:00
|
|
|
EmptyString(), nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = privateEvent->SetTrusted(true);
|
2010-06-17 11:36:01 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
return DispatchDOMEvent(nsnull, event, nsnull, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsWebSocket::CreateAndDispatchCloseEvent(bool aWasClean,
|
2011-08-03 12:15:25 -07:00
|
|
|
PRUint16 aCode,
|
|
|
|
const nsString &aReason)
|
2010-06-17 11:36:01 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:36:01 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mTriggeredCloseEvent = true;
|
2010-07-21 09:05:56 -07:00
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an event that uses the CloseEvent interface,
|
|
|
|
// which does not bubble, is not cancelable, and has no default action
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
rv = NS_NewDOMCloseEvent(getter_AddRefs(event), nsnull, nsnull);
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
nsCOMPtr<nsIDOMCloseEvent> closeEvent = do_QueryInterface(event);
|
|
|
|
rv = closeEvent->InitCloseEvent(NS_LITERAL_STRING("close"),
|
2011-10-17 07:59:28 -07:00
|
|
|
false, false,
|
2011-08-03 12:15:25 -07:00
|
|
|
aWasClean, aCode, aReason);
|
2010-06-17 11:36:01 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = privateEvent->SetTrusted(true);
|
2010-06-17 11:36:01 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return DispatchDOMEvent(nsnull, event, nsnull, nsnull);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-12-08 14:12:51 -08:00
|
|
|
nsWebSocket::PrefEnabled()
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
return Preferences::GetBool("network.websocket.enabled", true);
|
2010-12-08 14:12:51 -08:00
|
|
|
}
|
|
|
|
|
2010-06-17 11:36:01 -07:00
|
|
|
void
|
|
|
|
nsWebSocket::SetReadyState(PRUint16 aNewReadyState)
|
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:36:01 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mReadyState == aNewReadyState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
NS_ABORT_IF_FALSE((aNewReadyState == nsIMozWebSocket::OPEN) ||
|
|
|
|
(aNewReadyState == nsIMozWebSocket::CLOSING) ||
|
|
|
|
(aNewReadyState == nsIMozWebSocket::CLOSED),
|
2011-05-21 18:27:52 -07:00
|
|
|
"unexpected readyState");
|
2010-06-17 11:36:01 -07:00
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
if (aNewReadyState == nsIMozWebSocket::OPEN) {
|
|
|
|
NS_ABORT_IF_FALSE(mReadyState == nsIMozWebSocket::CONNECTING,
|
2011-05-21 18:27:52 -07:00
|
|
|
"unexpected readyState transition");
|
2010-06-17 11:36:01 -07:00
|
|
|
mReadyState = aNewReadyState;
|
|
|
|
|
|
|
|
rv = CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("open"));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the open event");
|
|
|
|
}
|
2010-06-27 14:09:29 -07:00
|
|
|
UpdateMustKeepAlive();
|
2010-06-17 11:36:01 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
if (aNewReadyState == nsIMozWebSocket::CLOSING) {
|
|
|
|
NS_ABORT_IF_FALSE((mReadyState == nsIMozWebSocket::CONNECTING) ||
|
|
|
|
(mReadyState == nsIMozWebSocket::OPEN),
|
2011-05-21 18:27:52 -07:00
|
|
|
"unexpected readyState transition");
|
2010-06-17 11:36:01 -07:00
|
|
|
mReadyState = aNewReadyState;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
if (aNewReadyState == nsIMozWebSocket::CLOSED) {
|
2010-06-17 11:36:01 -07:00
|
|
|
mReadyState = aNewReadyState;
|
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
// The close event must be dispatched asynchronously.
|
|
|
|
rv = NS_DispatchToMainThread(new nsWSCloseEvent(this, mClosedCleanly,
|
|
|
|
mServerReasonCode,
|
|
|
|
mServerReason),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the close event");
|
|
|
|
mTriggeredCloseEvent = true;
|
|
|
|
UpdateMustKeepAlive();
|
2010-06-17 11:36:01 -07:00
|
|
|
}
|
|
|
|
}
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWebSocket::ParseURL(const nsString& aURL)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(!aURL.IsEmpty(), NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), aURL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> parsedURL(do_QueryInterface(uri, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
nsCAutoString fragment;
|
|
|
|
rv = parsedURL->GetRef(fragment);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && fragment.IsEmpty(),
|
|
|
|
NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
nsCAutoString scheme;
|
|
|
|
rv = parsedURL->GetScheme(scheme);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !scheme.IsEmpty(),
|
|
|
|
NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
nsCAutoString host;
|
|
|
|
rv = parsedURL->GetAsciiHost(host);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !host.IsEmpty(), NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
PRInt32 port;
|
|
|
|
rv = parsedURL->GetPort(&port);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
rv = NS_CheckPortSafety(port, scheme.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
nsCAutoString filePath;
|
|
|
|
rv = parsedURL->GetFilePath(filePath);
|
|
|
|
if (filePath.IsEmpty()) {
|
|
|
|
filePath.AssignLiteral("/");
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
nsCAutoString query;
|
|
|
|
rv = parsedURL->GetQuery(query);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
if (scheme.LowerCaseEqualsLiteral("ws")) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mSecure = false;
|
2010-06-17 11:34:24 -07:00
|
|
|
mPort = (port == -1) ? DEFAULT_WS_SCHEME_PORT : port;
|
|
|
|
} else if (scheme.LowerCaseEqualsLiteral("wss")) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mSecure = true;
|
2010-06-17 11:34:24 -07:00
|
|
|
mPort = (port == -1) ? DEFAULT_WSS_SCHEME_PORT : port;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
2011-08-03 17:32:23 -07:00
|
|
|
rv = nsContentUtils::GetUTFOrigin(parsedURL, mUTF16Origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
mAsciiHost = host;
|
|
|
|
ToLowerCase(mAsciiHost);
|
|
|
|
|
|
|
|
mResource = filePath;
|
|
|
|
if (!query.IsEmpty()) {
|
|
|
|
mResource.AppendLiteral("?");
|
|
|
|
mResource.Append(query);
|
|
|
|
}
|
|
|
|
PRUint32 length = mResource.Length();
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < length; ++i) {
|
|
|
|
if (mResource[i] < static_cast<PRUnichar>(0x0021) ||
|
|
|
|
mResource[i] > static_cast<PRUnichar>(0x007E)) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mOriginalURL = aURL;
|
|
|
|
mURI = parsedURL;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-27 14:09:29 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2010-07-21 09:05:56 -07:00
|
|
|
// Methods that keep alive the WebSocket object when:
|
|
|
|
// 1. the object has registered event listeners that can be triggered
|
|
|
|
// ("strong event listeners");
|
|
|
|
// 2. there are outgoing not sent messages.
|
2010-06-27 14:09:29 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWebSocket::UpdateMustKeepAlive()
|
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-07-21 09:05:56 -07:00
|
|
|
if (!mCheckMustKeepAlive) {
|
2010-06-27 14:09:29 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool shouldKeepAlive = false;
|
2010-07-21 09:05:56 -07:00
|
|
|
|
|
|
|
if (mListenerManager) {
|
|
|
|
switch (mReadyState)
|
|
|
|
{
|
2011-07-26 06:30:14 -07:00
|
|
|
case nsIMozWebSocket::CONNECTING:
|
2010-07-21 09:05:56 -07:00
|
|
|
{
|
|
|
|
if (mListenerManager->HasListenersFor(NS_LITERAL_STRING("open")) ||
|
|
|
|
mListenerManager->HasListenersFor(NS_LITERAL_STRING("message")) ||
|
2011-10-20 01:27:13 -07:00
|
|
|
mListenerManager->HasListenersFor(NS_LITERAL_STRING("error")) ||
|
2010-07-21 09:05:56 -07:00
|
|
|
mListenerManager->HasListenersFor(NS_LITERAL_STRING("close"))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
shouldKeepAlive = true;
|
2010-07-21 09:05:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
case nsIMozWebSocket::OPEN:
|
|
|
|
case nsIMozWebSocket::CLOSING:
|
2010-07-21 09:05:56 -07:00
|
|
|
{
|
|
|
|
if (mListenerManager->HasListenersFor(NS_LITERAL_STRING("message")) ||
|
2011-10-20 01:27:13 -07:00
|
|
|
mListenerManager->HasListenersFor(NS_LITERAL_STRING("error")) ||
|
2010-07-21 09:05:56 -07:00
|
|
|
mListenerManager->HasListenersFor(NS_LITERAL_STRING("close")) ||
|
2011-10-20 01:27:13 -07:00
|
|
|
mOutgoingBufferedAmount != 0) {
|
2011-10-17 07:59:28 -07:00
|
|
|
shouldKeepAlive = true;
|
2010-07-21 09:05:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
case nsIMozWebSocket::CLOSED:
|
2010-07-21 09:05:56 -07:00
|
|
|
{
|
|
|
|
shouldKeepAlive =
|
|
|
|
(!mTriggeredCloseEvent &&
|
|
|
|
mListenerManager->HasListenersFor(NS_LITERAL_STRING("close")));
|
|
|
|
}
|
2010-06-27 14:09:29 -07:00
|
|
|
}
|
|
|
|
}
|
2010-07-21 09:05:56 -07:00
|
|
|
|
|
|
|
if (mKeepingAlive && !shouldKeepAlive) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mKeepingAlive = false;
|
2011-06-23 19:18:00 -07:00
|
|
|
static_cast<nsIDOMEventTarget*>(this)->Release();
|
2010-07-21 09:05:56 -07:00
|
|
|
} else if (!mKeepingAlive && shouldKeepAlive) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mKeepingAlive = true;
|
2011-06-23 19:18:00 -07:00
|
|
|
static_cast<nsIDOMEventTarget*>(this)->AddRef();
|
2010-07-21 09:05:56 -07:00
|
|
|
}
|
2010-06-27 14:09:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWebSocket::DontKeepAliveAnyMore()
|
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-07-21 09:05:56 -07:00
|
|
|
if (mKeepingAlive) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mKeepingAlive = false;
|
2011-06-23 19:18:00 -07:00
|
|
|
static_cast<nsIDOMEventTarget*>(this)->Release();
|
2010-06-27 14:09:29 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mCheckMustKeepAlive = false;
|
2010-06-27 14:09:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWebSocket::RemoveEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener* aListener,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aUseCapture)
|
2010-06-27 14:09:29 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-27 14:09:29 -07:00
|
|
|
nsresult rv = nsDOMEventTargetHelper::RemoveEventListener(aType,
|
|
|
|
aListener,
|
|
|
|
aUseCapture);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWebSocket::AddEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener *aListener,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aUseCapture,
|
|
|
|
bool aWantsUntrusted,
|
2010-06-27 14:09:29 -07:00
|
|
|
PRUint8 optional_argc)
|
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-27 14:09:29 -07:00
|
|
|
nsresult rv = nsDOMEventTargetHelper::AddEventListener(aType,
|
|
|
|
aListener,
|
|
|
|
aUseCapture,
|
|
|
|
aWantsUntrusted,
|
|
|
|
optional_argc);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-07-26 06:30:14 -07:00
|
|
|
// nsWebSocket::nsIMozWebSocket methods:
|
2010-06-17 11:34:24 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-21 12:56:03 -07:00
|
|
|
nsWebSocket::GetUrl(nsAString& aURL)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
|
|
|
aURL = mOriginalURL;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-03 20:46:13 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWebSocket::GetExtensions(nsAString& aExtensions)
|
|
|
|
{
|
|
|
|
CopyUTF8toUTF16(mEstablishedExtensions, aExtensions);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWebSocket::GetProtocol(nsAString& aProtocol)
|
|
|
|
{
|
2011-08-03 09:42:02 -07:00
|
|
|
CopyUTF8toUTF16(mEstablishedProtocol, aProtocol);
|
2011-05-21 18:27:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_IMETHODIMP
|
2010-06-17 11:36:01 -07:00
|
|
|
nsWebSocket::GetReadyState(PRUint16 *aReadyState)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
|
|
|
*aReadyState = mReadyState;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWebSocket::GetBufferedAmount(PRUint32 *aBufferedAmount)
|
|
|
|
{
|
2011-10-20 01:27:13 -07:00
|
|
|
*aBufferedAmount = mOutgoingBufferedAmount;
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NS_WEBSOCKET_IMPL_DOMEVENTLISTENER(_eventlistenername, _eventlistener) \
|
|
|
|
NS_IMETHODIMP \
|
|
|
|
nsWebSocket::GetOn##_eventlistenername(nsIDOMEventListener * *aEventListener)\
|
|
|
|
{ \
|
|
|
|
return GetInnerEventListener(_eventlistener, aEventListener); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
NS_IMETHODIMP \
|
|
|
|
nsWebSocket::SetOn##_eventlistenername(nsIDOMEventListener * aEventListener) \
|
|
|
|
{ \
|
|
|
|
return RemoveAddEventListener(NS_LITERAL_STRING(#_eventlistenername), \
|
|
|
|
_eventlistener, aEventListener); \
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WEBSOCKET_IMPL_DOMEVENTLISTENER(open, mOnOpenListener)
|
2010-06-17 11:36:01 -07:00
|
|
|
NS_WEBSOCKET_IMPL_DOMEVENTLISTENER(error, mOnErrorListener)
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_WEBSOCKET_IMPL_DOMEVENTLISTENER(message, mOnMessageListener)
|
|
|
|
NS_WEBSOCKET_IMPL_DOMEVENTLISTENER(close, mOnCloseListener)
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2011-08-03 12:15:25 -07:00
|
|
|
ContainsUnpairedSurrogates(const nsAString& aData)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-04 06:36:23 -07:00
|
|
|
// Check for unpaired surrogates.
|
2010-06-17 11:34:24 -07:00
|
|
|
PRUint32 i, length = aData.Length();
|
|
|
|
for (i = 0; i < length; ++i) {
|
|
|
|
if (NS_IS_LOW_SURROGATE(aData[i])) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
if (NS_IS_HIGH_SURROGATE(aData[i])) {
|
|
|
|
++i;
|
2011-08-03 12:15:25 -07:00
|
|
|
if (i == length || !NS_IS_LOW_SURROGATE(aData[i])) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2011-08-03 12:15:25 -07:00
|
|
|
}
|
2010-06-17 11:34:24 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-08-03 12:15:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWebSocket::Send(const nsAString& aData)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
|
|
|
|
|
|
|
if (mReadyState == nsIMozWebSocket::CONNECTING) {
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ContainsUnpairedSurrogates(aData))
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
if (mReadyState == nsIMozWebSocket::CLOSING ||
|
|
|
|
mReadyState == nsIMozWebSocket::CLOSED) {
|
2010-06-17 11:34:24 -07:00
|
|
|
mOutgoingBufferedAmount += NS_ConvertUTF16toUTF8(aData).Length();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
nsString message = PromiseFlatString(aData);
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> ccm =
|
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
ENSURE_SUCCESS_AND_FAIL_IF_FAILED(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeEncoder> converter;
|
|
|
|
rv = ccm->GetUnicodeEncoder("UTF-8", getter_AddRefs(converter));
|
|
|
|
ENSURE_SUCCESS_AND_FAIL_IF_FAILED(rv, rv);
|
|
|
|
|
|
|
|
rv = converter->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace,
|
|
|
|
nsnull, UTF_8_REPLACEMENT_CHAR);
|
|
|
|
ENSURE_SUCCESS_AND_FAIL_IF_FAILED(rv, rv);
|
|
|
|
|
|
|
|
PRInt32 inLen = message.Length();
|
|
|
|
PRInt32 maxLen;
|
|
|
|
rv = converter->GetMaxLength(message.BeginReading(), inLen, &maxLen);
|
|
|
|
ENSURE_SUCCESS_AND_FAIL_IF_FAILED(rv, rv);
|
|
|
|
|
|
|
|
nsCString buf;
|
|
|
|
buf.SetLength(maxLen);
|
|
|
|
ENSURE_TRUE_AND_FAIL_IF_FAILED(buf.Length() == static_cast<PRUint32>(maxLen),
|
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
char* start = buf.BeginWriting();
|
|
|
|
|
|
|
|
PRInt32 outLen = maxLen;
|
|
|
|
rv = converter->Convert(message.BeginReading(), &inLen, start, &outLen);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRInt32 outLen2 = maxLen - outLen;
|
|
|
|
rv = converter->Finish(start + outLen, &outLen2);
|
|
|
|
outLen += outLen2;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv) || rv == NS_ERROR_UENC_NOMAPPING) {
|
|
|
|
// Yes, NS_ERROR_UENC_NOMAPPING is a success code
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.SetLength(outLen);
|
|
|
|
ENSURE_TRUE_AND_FAIL_IF_FAILED(buf.Length() == static_cast<PRUint32>(outLen),
|
|
|
|
NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
mOutgoingBufferedAmount += buf.Length();
|
|
|
|
mWebSocketChannel->SendMsg(buf);
|
|
|
|
|
|
|
|
UpdateMustKeepAlive();
|
2010-06-17 11:36:01 -07:00
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-08-03 12:15:25 -07:00
|
|
|
nsWebSocket::Close(PRUint16 code, const nsAString & reason, PRUint8 argc)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2011-08-03 12:15:25 -07:00
|
|
|
|
|
|
|
// the reason code is optional, but if provided it must be in a specific range
|
|
|
|
if (argc >= 1) {
|
|
|
|
if (code != 1000 && (code < 3000 || code > 4999))
|
|
|
|
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString utf8Reason;
|
|
|
|
if (argc >= 2) {
|
|
|
|
if (ContainsUnpairedSurrogates(reason))
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
|
|
|
|
CopyUTF16toUTF8(reason, utf8Reason);
|
|
|
|
|
|
|
|
// The API requires the UTF-8 string to be 123 or less bytes
|
|
|
|
if (utf8Reason.Length() > 123)
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format checks for reason and code both passed, they can now be assigned.
|
|
|
|
if (argc >= 1)
|
|
|
|
mClientReasonCode = code;
|
|
|
|
if (argc >= 2)
|
|
|
|
mClientReason = utf8Reason;
|
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
if (mReadyState == nsIMozWebSocket::CLOSING ||
|
|
|
|
mReadyState == nsIMozWebSocket::CLOSED) {
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
if (mReadyState == nsIMozWebSocket::CONNECTING) {
|
2010-07-21 09:05:56 -07:00
|
|
|
// FailConnection() can release the object, so we keep a reference
|
|
|
|
// before calling it
|
2010-06-27 14:09:29 -07:00
|
|
|
nsRefPtr<nsWebSocket> kungfuDeathGrip = this;
|
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
FailConnection();
|
2010-06-17 11:36:01 -07:00
|
|
|
return NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
2011-07-26 06:30:14 -07:00
|
|
|
// mReadyState == nsIMozWebSocket::OPEN
|
2011-10-20 01:27:13 -07:00
|
|
|
CloseConnection();
|
2010-06-17 11:34:24 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Init method should only be called by C++ consumers.
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWebSocket::Init(nsIPrincipal* aPrincipal,
|
|
|
|
nsIScriptContext* aScriptContext,
|
|
|
|
nsPIDOMWindow* aOwnerWindow,
|
|
|
|
const nsAString& aURL,
|
2011-08-03 09:42:02 -07:00
|
|
|
nsTArray<nsString> & protocolArray)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:34:24 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(aPrincipal);
|
|
|
|
|
2010-12-08 14:12:51 -08:00
|
|
|
if (!PrefEnabled()) {
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPrincipal = aPrincipal;
|
|
|
|
mScriptContext = aScriptContext;
|
|
|
|
if (aOwnerWindow) {
|
2010-09-21 02:17:30 -07:00
|
|
|
mOwner = aOwnerWindow->IsOuterWindow() ?
|
|
|
|
aOwnerWindow->GetCurrentInnerWindow() : aOwnerWindow;
|
2011-10-20 01:27:13 -07:00
|
|
|
} else {
|
2010-06-17 11:34:24 -07:00
|
|
|
mOwner = nsnull;
|
|
|
|
}
|
|
|
|
|
2010-12-21 09:05:34 -08:00
|
|
|
nsCOMPtr<nsIJSContextStack> stack =
|
|
|
|
do_GetService("@mozilla.org/js/xpc/ContextStack;1");
|
|
|
|
JSContext* cx = nsnull;
|
|
|
|
if (stack && NS_SUCCEEDED(stack->Peek(&cx)) && cx) {
|
|
|
|
JSStackFrame *fp = JS_GetScriptedCaller(cx, NULL);
|
|
|
|
if (fp) {
|
|
|
|
JSScript *script = JS_GetFrameScript(cx, fp);
|
|
|
|
if (script) {
|
|
|
|
mScriptFile = JS_GetScriptFilename(cx, script);
|
|
|
|
}
|
|
|
|
|
|
|
|
jsbytecode *pc = JS_GetFramePC(cx, fp);
|
|
|
|
if (script && pc) {
|
|
|
|
mScriptLine = JS_PCToLineNumber(cx, script, pc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 13:44:35 -07:00
|
|
|
mInnerWindowID = nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(cx);
|
2010-12-21 09:05:34 -08:00
|
|
|
}
|
|
|
|
|
2010-06-17 11:34:24 -07:00
|
|
|
// parses the url
|
|
|
|
rv = ParseURL(PromiseFlatString(aURL));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-07-10 09:31:41 -07:00
|
|
|
// Don't allow https:// to open ws://
|
2011-10-20 01:27:13 -07:00
|
|
|
if (!mSecure &&
|
2011-07-10 09:31:41 -07:00
|
|
|
!Preferences::GetBool("network.websocket.allowInsecureFromHTTPS",
|
2011-09-28 23:19:26 -07:00
|
|
|
false)) {
|
2011-07-19 06:17:39 -07:00
|
|
|
// Confirmed we are opening plain ws:// and want to prevent this from a
|
|
|
|
// secure context (e.g. https). Check the security context of the document
|
|
|
|
// associated with this script, which is the same as associated with mOwner.
|
2011-10-31 08:39:46 -07:00
|
|
|
nsCOMPtr<nsIDocument> originDoc =
|
|
|
|
nsContentUtils::GetDocumentFromScriptContext(mScriptContext);
|
2011-07-19 06:17:39 -07:00
|
|
|
if (originDoc && originDoc->GetSecurityInfo())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
2011-07-10 09:31:41 -07:00
|
|
|
}
|
|
|
|
|
2011-08-03 09:42:02 -07:00
|
|
|
// Assign the sub protocol list and scan it for illegal values
|
|
|
|
for (PRUint32 index = 0; index < protocolArray.Length(); ++index) {
|
|
|
|
for (PRUint32 i = 0; i < protocolArray[index].Length(); ++i) {
|
|
|
|
if (protocolArray[index][i] < static_cast<PRUnichar>(0x0021) ||
|
|
|
|
protocolArray[index][i] > static_cast<PRUnichar>(0x007E))
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRequestedProtocolList.IsEmpty())
|
|
|
|
mRequestedProtocolList.Append(NS_LITERAL_CSTRING(", "));
|
|
|
|
AppendUTF16toUTF8(protocolArray[index], mRequestedProtocolList);
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// the constructor should throw a SYNTAX_ERROR only if it fails to parse the
|
|
|
|
// url parameter, so we don't care about the EstablishConnection result.
|
|
|
|
EstablishConnection();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-20 01:27:13 -07:00
|
|
|
// nsWebSocket::nsIRequest
|
2011-05-21 18:27:52 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2010-06-17 11:34:24 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::GetName(nsACString &aName)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-10-20 01:27:13 -07:00
|
|
|
CopyUTF16toUTF8(mOriginalURL, aName);
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::IsPending(bool *aValue)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-10-20 01:27:13 -07:00
|
|
|
*aValue = !mDisconnected;
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::GetStatus(nsresult *aStatus)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
*aStatus = NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-07 06:44:19 -07:00
|
|
|
// Window closed, stop/reload button pressed, user navigated away from page, etc.
|
2010-06-17 11:34:24 -07:00
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::Cancel(nsresult aStatus)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-10-20 01:27:13 -07:00
|
|
|
if (mDisconnected)
|
2011-05-21 18:27:52 -07:00
|
|
|
return NS_OK;
|
2010-06-17 11:34:24 -07:00
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
ConsoleError();
|
2011-10-20 01:27:13 -07:00
|
|
|
return CloseConnection();
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::Suspend()
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::Resume()
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2010-06-17 11:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::GetLoadGroup(nsILoadGroup **aLoadGroup)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
*aLoadGroup = nsnull;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc =
|
2011-10-20 01:27:13 -07:00
|
|
|
nsContentUtils::GetDocumentFromScriptContext(mScriptContext);
|
2011-05-21 18:27:52 -07:00
|
|
|
|
|
|
|
if (doc) {
|
|
|
|
*aLoadGroup = doc->GetDocumentLoadGroup().get(); // already_AddRefed
|
|
|
|
}
|
2010-06-17 11:34:24 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:27:52 -07:00
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::SetLoadGroup(nsILoadGroup *aLoadGroup)
|
2011-05-21 18:27:52 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2010-06-17 11:34:24 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::GetLoadFlags(nsLoadFlags *aLoadFlags)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
*aLoadFlags = nsIRequest::LOAD_BACKGROUND;
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-20 01:27:13 -07:00
|
|
|
nsWebSocket::SetLoadFlags(nsLoadFlags aLoadFlags)
|
2010-06-17 11:34:24 -07:00
|
|
|
{
|
2011-05-21 18:27:52 -07:00
|
|
|
// we won't change the load flags at all.
|
2010-06-17 11:34:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|