2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
#include "mozilla/dom/EventSource.h"
|
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2012-12-14 23:58:45 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2014-07-10 06:56:37 +00:00
|
|
|
#include "mozilla/LoadInfo.h"
|
2014-04-01 06:13:50 +00:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2012-12-22 08:17:05 +00:00
|
|
|
#include "mozilla/dom/EventSourceBinding.h"
|
2014-02-27 10:51:14 +00:00
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
2016-01-30 17:05:36 +00:00
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
2014-05-09 07:07:30 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2016-06-07 20:10:18 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2011-05-22 21:30:07 +00:00
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 02:17:00 +00:00
|
|
|
#include "nsIAuthPrompt.h"
|
|
|
|
#include "nsIAuthPrompt2.h"
|
2015-09-29 04:31:56 +00:00
|
|
|
#include "nsIInputStream.h"
|
2015-07-07 02:17:00 +00:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2011-05-22 21:30:07 +00:00
|
|
|
#include "nsMimeTypes.h"
|
|
|
|
#include "nsIPromptFactory.h"
|
|
|
|
#include "nsIWindowWatcher.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
|
|
|
#include "nsIScriptError.h"
|
2013-11-26 07:31:52 +00:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2011-05-22 21:30:07 +00:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2011-08-11 13:29:50 +00:00
|
|
|
#include "nsContentUtils.h"
|
2011-05-25 06:31:59 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-09-29 16:06:36 +00:00
|
|
|
#include "xpcpublic.h"
|
2012-01-26 15:55:30 +00:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-06-19 02:30:09 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2011-05-25 06:31:59 +00:00
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
#define REPLACEMENT_CHAR (char16_t)0xFFFD
|
|
|
|
#define BOM_CHAR (char16_t)0xFEFF
|
|
|
|
#define SPACE_CHAR (char16_t)0x0020
|
|
|
|
#define CR_CHAR (char16_t)0x000D
|
|
|
|
#define LF_CHAR (char16_t)0x000A
|
|
|
|
#define COLON_CHAR (char16_t)0x003A
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
#define DEFAULT_BUFFER_SIZE 4096
|
|
|
|
|
|
|
|
// Reconnection time related values in milliseconds. The default one is equal
|
|
|
|
// to the default value of the pref dom.server-events.default-reconnection-time
|
|
|
|
#define MIN_RECONNECTION_TIME_VALUE 500
|
|
|
|
#define DEFAULT_RECONNECTION_TIME_VALUE 5000
|
|
|
|
#define MAX_RECONNECTION_TIME_VALUE PR_IntervalToMilliseconds(DELAY_INTERVAL_LIMIT)
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
EventSource::EventSource(nsPIDOMWindowInner* aOwnerWindow) :
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper(aOwnerWindow),
|
2011-05-22 21:30:07 +00:00
|
|
|
mStatus(PARSE_STATE_OFF),
|
2011-10-17 14:59:28 +00:00
|
|
|
mFrozen(false),
|
|
|
|
mErrorLoadOnRedirect(false),
|
|
|
|
mGoingToDispatchAllMessages(false),
|
2011-12-05 23:02:42 +00:00
|
|
|
mWithCredentials(false),
|
2012-02-16 16:45:25 +00:00
|
|
|
mWaitingForOnStopRequest(false),
|
2011-05-22 21:30:07 +00:00
|
|
|
mLastConvertionResult(NS_OK),
|
2012-12-22 08:17:05 +00:00
|
|
|
mReadyState(CONNECTING),
|
2011-05-22 21:30:07 +00:00
|
|
|
mScriptLine(0),
|
2015-09-02 00:56:03 +00:00
|
|
|
mScriptColumn(0),
|
2011-08-24 20:44:35 +00:00
|
|
|
mInnerWindowID(0)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::~EventSource()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-12-22 08:17:05 +00:00
|
|
|
// EventSource::nsISupports
|
2011-05-22 21:30:07 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-08-02 01:29:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(EventSource)
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(EventSource)
|
2012-02-16 16:45:25 +00:00
|
|
|
bool isBlack = tmp->IsBlack();
|
|
|
|
if (isBlack || tmp->mWaitingForOnStopRequest) {
|
2012-01-26 15:55:30 +00:00
|
|
|
if (tmp->mListenerManager) {
|
2012-10-17 01:22:02 +00:00
|
|
|
tmp->mListenerManager->MarkForCC();
|
2012-01-26 15:55:30 +00:00
|
|
|
}
|
2012-03-14 14:22:10 +00:00
|
|
|
if (!isBlack && tmp->PreservingWrapper()) {
|
2013-09-09 03:28:48 +00:00
|
|
|
// This marks the wrapper black.
|
|
|
|
tmp->GetWrapper();
|
2012-02-16 16:45:25 +00:00
|
|
|
}
|
2012-01-26 15:55:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(EventSource)
|
2012-01-26 15:55:30 +00:00
|
|
|
return tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(EventSource)
|
2012-01-26 15:55:30 +00:00
|
|
|
return tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(EventSource,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2012-01-26 15:55:30 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(EventSource,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSrc)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoadGroup)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mHttpChannel)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTimer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mUnicodeDecoder)
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(EventSource,
|
|
|
|
DOMEventTargetHelper)
|
2011-05-22 21:30:07 +00:00
|
|
|
tmp->Close();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(EventSource)
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIChannelEventSink)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(EventSource, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(EventSource, DOMEventTargetHelper)
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2012-03-13 00:56:07 +00:00
|
|
|
void
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::DisconnectFromOwner()
|
2012-03-13 00:56:07 +00:00
|
|
|
{
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2012-03-13 00:56:20 +00:00
|
|
|
Close();
|
2012-03-13 00:56:07 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
void
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::Close()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED) {
|
|
|
|
return;
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
os->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC);
|
|
|
|
os->RemoveObserver(this, DOM_WINDOW_FROZEN_TOPIC);
|
|
|
|
os->RemoveObserver(this, DOM_WINDOW_THAWED_TOPIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
2012-07-30 14:20:58 +00:00
|
|
|
mTimer = nullptr;
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ResetConnection();
|
|
|
|
|
|
|
|
ClearFields();
|
|
|
|
|
|
|
|
while (mMessagesToDispatch.GetSize() != 0) {
|
|
|
|
delete static_cast<Message*>(mMessagesToDispatch.PopFront());
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mSrc = nullptr;
|
2011-10-17 14:59:28 +00:00
|
|
|
mFrozen = false;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mUnicodeDecoder = nullptr;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
mReadyState = CLOSED;
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
nsresult
|
|
|
|
EventSource::Init(nsISupports* aOwner,
|
2012-12-22 08:17:05 +00:00
|
|
|
const nsAString& aURL,
|
|
|
|
bool aWithCredentials)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2016-05-03 08:21:54 +00:00
|
|
|
if (mReadyState != CONNECTING) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aOwner);
|
|
|
|
NS_ENSURE_STATE(sgo);
|
2016-04-20 22:04:37 +00:00
|
|
|
// XXXbz why are we checking this? This doesn't match anything in the spec.
|
2012-12-22 08:17:05 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
mPrincipal = principal;
|
2011-12-05 23:02:42 +00:00
|
|
|
mWithCredentials = aWithCredentials;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2013-04-29 18:16:17 +00:00
|
|
|
// The conditional here is historical and not necessarily sane.
|
|
|
|
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
|
2015-09-02 00:56:03 +00:00
|
|
|
nsJSUtils::GetCallingLocation(cx, mScriptFile, &mScriptLine,
|
|
|
|
&mScriptColumn);
|
2011-08-24 20:44:35 +00:00
|
|
|
mInnerWindowID = nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(cx);
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the load group for the page. When requesting we'll add ourselves to it.
|
|
|
|
// This way any pending requests will be automatically aborted if the user
|
|
|
|
// leaves the page.
|
2016-04-20 22:04:37 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocumentIfCurrent();
|
|
|
|
if (doc) {
|
|
|
|
mLoadGroup = doc->GetDocumentLoadGroup();
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
// get the src
|
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
2016-04-20 22:04:37 +00:00
|
|
|
nsresult rv = GetBaseURI(getter_AddRefs(baseURI));
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> srcURI;
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = NS_NewURI(getter_AddRefs(srcURI), aURL, nullptr, baseURI);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
// we observe when the window freezes and thaws
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
NS_ENSURE_STATE(os);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = os->AddObserver(this, DOM_WINDOW_DESTROYED_TOPIC, true);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = os->AddObserver(this, DOM_WINDOW_FROZEN_TOPIC, true);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = os->AddObserver(this, DOM_WINDOW_THAWED_TOPIC, true);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-05 23:02:42 +00:00
|
|
|
nsAutoString origin;
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(srcURI, origin);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString spec;
|
2011-05-22 21:30:07 +00:00
|
|
|
rv = srcURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mOriginalURL = NS_ConvertUTF8toUTF16(spec);
|
|
|
|
mSrc = srcURI;
|
|
|
|
mOrigin = origin;
|
|
|
|
|
|
|
|
mReconnectionTime =
|
2011-05-25 06:32:00 +00:00
|
|
|
Preferences::GetInt("dom.server-events.default-reconnection-time",
|
|
|
|
DEFAULT_RECONNECTION_TIME_VALUE);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2013-11-26 07:31:52 +00:00
|
|
|
mUnicodeDecoder = EncodingUtils::DecoderForEncoding("UTF-8");
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
// the constructor should throw a SYNTAX_ERROR only if it fails resolving the
|
|
|
|
// url parameter, so we don't care about the InitChannelAndRequestEventSource
|
|
|
|
// result.
|
|
|
|
InitChannelAndRequestEventSource();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
/* virtual */ JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 14:13:33 +00:00
|
|
|
EventSource::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 14:13:33 +00:00
|
|
|
return EventSourceBinding::Wrap(aCx, this, aGivenProto);
|
2012-12-22 08:17:05 +00:00
|
|
|
}
|
2011-12-05 23:02:42 +00:00
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
/* static */ already_AddRefed<EventSource>
|
2013-08-23 05:17:08 +00:00
|
|
|
EventSource::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aURL,
|
2012-12-22 08:17:05 +00:00
|
|
|
const EventSourceInit& aEventSourceInitDict,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> ownerWindow =
|
2014-01-07 02:53:23 +00:00
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!ownerWindow) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(ownerWindow->IsInnerWindow());
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<EventSource> eventSource = new EventSource(ownerWindow);
|
2013-08-23 05:17:08 +00:00
|
|
|
aRv = eventSource->Init(aGlobal.GetAsSupports(), aURL,
|
2012-12-03 16:07:49 +00:00
|
|
|
aEventSourceInitDict.mWithCredentials);
|
2012-12-22 08:17:05 +00:00
|
|
|
return eventSource.forget();
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-12-22 08:17:05 +00:00
|
|
|
// EventSource::nsIObserver
|
2011-05-22 21:30:07 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t* aData)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aSubject);
|
2012-03-13 00:56:07 +00:00
|
|
|
if (!GetOwner() || window != GetOwner()) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:14:33 +00:00
|
|
|
DebugOnly<nsresult> rv;
|
2011-05-22 21:30:07 +00:00
|
|
|
if (strcmp(aTopic, DOM_WINDOW_FROZEN_TOPIC) == 0) {
|
|
|
|
rv = Freeze();
|
2012-09-01 21:24:42 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Freeze() failed");
|
2011-05-22 21:30:07 +00:00
|
|
|
} else if (strcmp(aTopic, DOM_WINDOW_THAWED_TOPIC) == 0) {
|
|
|
|
rv = Thaw();
|
2012-09-01 21:24:42 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Thaw() failed");
|
2011-05-22 21:30:07 +00:00
|
|
|
} else if (strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC) == 0) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-12-22 08:17:05 +00:00
|
|
|
// EventSource::nsIStreamListener
|
2011-05-22 21:30:07 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::OnStartRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *ctxt)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
nsresult rv = CheckHealthOfRequestCallback(aRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-06-04 09:34:15 +00:00
|
|
|
nsresult status;
|
2014-11-08 14:51:38 +00:00
|
|
|
rv = aRequest->GetStatus(&status);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-06-04 09:34:15 +00:00
|
|
|
|
2014-11-08 14:51:38 +00:00
|
|
|
if (NS_FAILED(status)) {
|
|
|
|
// EventSource::OnStopRequest will evaluate if it shall either reestablish
|
|
|
|
// or fail the connection
|
|
|
|
return NS_ERROR_ABORT;
|
2013-05-21 12:29:31 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 09:34:15 +00:00
|
|
|
uint32_t httpStatus;
|
|
|
|
rv = httpChannel->GetResponseStatus(&httpStatus);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-06-04 09:34:15 +00:00
|
|
|
if (httpStatus != 200) {
|
2014-11-08 14:51:38 +00:00
|
|
|
DispatchFailConnection();
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString contentType;
|
|
|
|
rv = httpChannel->GetContentType(contentType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!contentType.EqualsLiteral(TEXT_EVENT_STREAM)) {
|
2011-05-22 21:30:07 +00:00
|
|
|
DispatchFailConnection();
|
2013-06-04 09:34:15 +00:00
|
|
|
return NS_ERROR_ABORT;
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 08:45:00 +00:00
|
|
|
rv = NS_DispatchToMainThread(NewRunnableMethod(this, &EventSource::AnnounceConnection));
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStatus = PARSE_STATE_BEGIN_OF_STREAM;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this method parses the characters as they become available instead of
|
|
|
|
// buffering them.
|
|
|
|
NS_METHOD
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::StreamReaderFunc(nsIInputStream *aInputStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char *aFromRawSegment,
|
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource* thisObject = static_cast<EventSource*>(aClosure);
|
2011-05-22 21:30:07 +00:00
|
|
|
if (!thisObject || !aWriteCount) {
|
2012-12-22 08:17:05 +00:00
|
|
|
NS_WARNING("EventSource cannot read from stream: no aClosure or aWriteCount");
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aWriteCount = 0;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t srcCount, outCount;
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t out[2];
|
2011-05-22 21:30:07 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
const char *p = aFromRawSegment,
|
|
|
|
*end = aFromRawSegment + aCount;
|
|
|
|
|
|
|
|
do {
|
|
|
|
srcCount = aCount - (p - aFromRawSegment);
|
|
|
|
outCount = 2;
|
|
|
|
|
|
|
|
thisObject->mLastConvertionResult =
|
|
|
|
thisObject->mUnicodeDecoder->Convert(p, &srcCount, out, &outCount);
|
2012-12-10 14:11:15 +00:00
|
|
|
MOZ_ASSERT(thisObject->mLastConvertionResult != NS_ERROR_ILLEGAL_INPUT);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2012-12-10 14:11:15 +00:00
|
|
|
for (int32_t i = 0; i < outCount; ++i) {
|
|
|
|
rv = thisObject->ParseCharacter(out[i]);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-12-10 14:11:15 +00:00
|
|
|
p = p + srcCount;
|
2011-05-22 21:30:07 +00:00
|
|
|
} while (p < end &&
|
|
|
|
thisObject->mLastConvertionResult != NS_PARTIAL_MORE_INPUT &&
|
|
|
|
thisObject->mLastConvertionResult != NS_OK);
|
|
|
|
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::OnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aInputStream,
|
|
|
|
uint64_t aOffset,
|
|
|
|
uint32_t aCount)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aInputStream);
|
|
|
|
|
|
|
|
nsresult rv = CheckHealthOfRequestCallback(aRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t totalRead;
|
2012-12-22 08:17:05 +00:00
|
|
|
return aInputStream->ReadSegments(EventSource::StreamReaderFunc, this,
|
2011-05-22 21:30:07 +00:00
|
|
|
aCount, &totalRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::OnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatusCode)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-02-16 16:45:25 +00:00
|
|
|
mWaitingForOnStopRequest = false;
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2014-11-08 14:51:38 +00:00
|
|
|
// "Network errors that prevents the connection from being established in the
|
|
|
|
// first place (e.g. DNS errors), must cause the user agent to asynchronously
|
|
|
|
// reestablish the connection.
|
|
|
|
//
|
|
|
|
// (...) the cancelation of the fetch algorithm by the user agent (e.g. in
|
|
|
|
// response to window.stop() or the user canceling the network connection
|
|
|
|
// manually) must cause the user agent to fail the connection.
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatusCode) &&
|
|
|
|
aStatusCode != NS_ERROR_CONNECTION_REFUSED &&
|
|
|
|
aStatusCode != NS_ERROR_NET_TIMEOUT &&
|
|
|
|
aStatusCode != NS_ERROR_NET_RESET &&
|
|
|
|
aStatusCode != NS_ERROR_NET_INTERRUPT &&
|
|
|
|
aStatusCode != NS_ERROR_PROXY_CONNECTION_REFUSED &&
|
|
|
|
aStatusCode != NS_ERROR_DNS_LOOKUP_QUEUE_FULL) {
|
2011-05-22 21:30:07 +00:00
|
|
|
DispatchFailConnection();
|
2014-11-08 14:51:38 +00:00
|
|
|
return NS_ERROR_ABORT;
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 14:51:38 +00:00
|
|
|
nsresult rv = CheckHealthOfRequestCallback(aRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2013-01-22 19:09:13 +00:00
|
|
|
ClearFields();
|
|
|
|
|
2016-05-05 08:45:00 +00:00
|
|
|
rv = NS_DispatchToMainThread(NewRunnableMethod(this, &EventSource::ReestablishConnection));
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-11-08 14:51:38 +00:00
|
|
|
return NS_OK;
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-12-22 08:17:05 +00:00
|
|
|
// EventSource::nsIChannelEventSink
|
2011-05-22 21:30:07 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::AsyncOnChannelRedirect(nsIChannel *aOldChannel,
|
|
|
|
nsIChannel *aNewChannel,
|
|
|
|
uint32_t aFlags,
|
|
|
|
nsIAsyncVerifyRedirectCallback *aCallback)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRequest> aOldRequest = do_QueryInterface(aOldChannel);
|
|
|
|
NS_PRECONDITION(aOldRequest, "Redirect from a null request?");
|
|
|
|
|
|
|
|
nsresult rv = CheckHealthOfRequestCallback(aOldRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_PRECONDITION(aNewChannel, "Redirect without a channel?");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> newURI;
|
|
|
|
rv = NS_GetFinalChannelURI(aNewChannel, getter_AddRefs(newURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-08-05 03:06:19 +00:00
|
|
|
bool isValidScheme =
|
|
|
|
(NS_SUCCEEDED(newURI->SchemeIs("http", &isValidScheme)) && isValidScheme) ||
|
|
|
|
(NS_SUCCEEDED(newURI->SchemeIs("https", &isValidScheme)) && isValidScheme);
|
|
|
|
|
|
|
|
rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv) || !isValidScheme) {
|
|
|
|
DispatchFailConnection();
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update our channel
|
|
|
|
|
2015-10-08 20:41:38 +00:00
|
|
|
mHttpChannel = do_QueryInterface(aNewChannel);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_STATE(mHttpChannel);
|
|
|
|
|
2016-04-20 22:04:37 +00:00
|
|
|
SetupHttpChannel();
|
|
|
|
// The HTTP impl already copies over the referrer and referrer policy on
|
|
|
|
// redirects, so we don't need to SetupReferrerPolicy().
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2015-10-08 20:41:38 +00:00
|
|
|
if ((aFlags & nsIChannelEventSink::REDIRECT_PERMANENT) != 0) {
|
2011-05-22 21:30:07 +00:00
|
|
|
rv = NS_GetFinalChannelURI(mHttpChannel, getter_AddRefs(mSrc));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2015-10-08 20:41:38 +00:00
|
|
|
aCallback->OnRedirectVerifyCallback(NS_OK);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-12-22 08:17:05 +00:00
|
|
|
// EventSource::nsIInterfaceRequestor
|
2011-05-22 21:30:07 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::GetInterface(const nsIID & aIID,
|
|
|
|
void **aResult)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
|
|
|
*aResult = static_cast<nsIChannelEventSink*>(this);
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIAuthPrompt)) ||
|
|
|
|
aIID.Equals(NS_GET_IID(nsIAuthPrompt2))) {
|
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPromptFactory> wwatch =
|
|
|
|
do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Get the an auth prompter for our window so that the parenting
|
|
|
|
// of the dialogs works as it should when using tabs.
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window;
|
2012-03-13 00:56:07 +00:00
|
|
|
if (GetOwner()) {
|
|
|
|
window = GetOwner()->GetOuterWindow();
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return wwatch->GetPrompt(window, aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::GetBaseURI(nsIURI **aBaseURI)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aBaseURI);
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*aBaseURI = nullptr;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
|
|
|
|
|
|
|
// first we try from document->GetBaseURI()
|
2016-04-20 22:04:37 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocumentIfCurrent();
|
2011-05-22 21:30:07 +00:00
|
|
|
if (doc) {
|
|
|
|
baseURI = doc->GetBaseURI();
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise we get from the doc's principal
|
|
|
|
if (!baseURI) {
|
2016-04-20 22:04:37 +00:00
|
|
|
nsresult rv = mPrincipal->GetURI(getter_AddRefs(baseURI));
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_STATE(baseURI);
|
|
|
|
|
|
|
|
baseURI.forget(aBaseURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-20 22:04:37 +00:00
|
|
|
void
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::SetupHttpChannel()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
mHttpChannel->SetRequestMethod(NS_LITERAL_CSTRING("GET"));
|
|
|
|
|
|
|
|
/* set the http request headers */
|
|
|
|
|
|
|
|
mHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_LITERAL_CSTRING(TEXT_EVENT_STREAM), false);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
// LOAD_BYPASS_CACHE already adds the Cache-Control: no-cache header
|
|
|
|
|
|
|
|
if (!mLastEventID.IsEmpty()) {
|
|
|
|
mHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Last-Event-ID"),
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ConvertUTF16toUTF8(mLastEventID), false);
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
2016-04-20 22:04:37 +00:00
|
|
|
}
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2016-04-20 22:04:37 +00:00
|
|
|
nsresult
|
|
|
|
EventSource::SetupReferrerPolicy()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocumentIfCurrent();
|
|
|
|
if (doc) {
|
|
|
|
nsresult rv = mHttpChannel->SetReferrerWithPolicy(doc->GetDocumentURI(),
|
|
|
|
doc->GetReferrerPolicy());
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::InitChannelAndRequestEventSource()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2015-08-05 03:06:19 +00:00
|
|
|
bool isValidScheme =
|
|
|
|
(NS_SUCCEEDED(mSrc->SchemeIs("http", &isValidScheme)) && isValidScheme) ||
|
|
|
|
(NS_SUCCEEDED(mSrc->SchemeIs("https", &isValidScheme)) && isValidScheme);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2015-08-05 03:06:19 +00:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv) || !isValidScheme) {
|
2011-05-22 21:30:07 +00:00
|
|
|
DispatchFailConnection();
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsLoadFlags loadFlags;
|
|
|
|
loadFlags = nsIRequest::LOAD_BACKGROUND | nsIRequest::LOAD_BYPASS_CACHE;
|
|
|
|
|
2016-04-20 22:04:37 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocumentIfCurrent();
|
2014-09-21 16:36:25 +00:00
|
|
|
|
2015-08-05 03:06:19 +00:00
|
|
|
nsSecurityFlags securityFlags =
|
|
|
|
nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
|
|
|
|
|
|
|
|
if (mWithCredentials) {
|
2015-12-06 23:33:15 +00:00
|
|
|
securityFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
|
2015-08-05 03:06:19 +00:00
|
|
|
}
|
|
|
|
|
2011-05-22 21:30:07 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2014-09-21 16:36:25 +00:00
|
|
|
// If we have the document, use it
|
|
|
|
if (doc) {
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
mSrc,
|
|
|
|
doc,
|
2015-08-05 03:06:19 +00:00
|
|
|
securityFlags,
|
2015-08-06 18:17:24 +00:00
|
|
|
nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE,
|
2014-09-21 16:36:25 +00:00
|
|
|
mLoadGroup, // loadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
loadFlags); // aLoadFlags
|
|
|
|
} else {
|
|
|
|
// otherwise use the principal
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
mSrc,
|
2014-09-22 22:34:26 +00:00
|
|
|
mPrincipal,
|
2015-08-05 03:06:19 +00:00
|
|
|
securityFlags,
|
2015-08-06 18:17:24 +00:00
|
|
|
nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE,
|
2014-09-21 16:36:25 +00:00
|
|
|
mLoadGroup, // loadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
loadFlags); // aLoadFlags
|
|
|
|
}
|
|
|
|
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mHttpChannel = do_QueryInterface(channel);
|
|
|
|
NS_ENSURE_TRUE(mHttpChannel, NS_ERROR_NO_INTERFACE);
|
|
|
|
|
2016-04-20 22:04:37 +00:00
|
|
|
SetupHttpChannel();
|
|
|
|
rv = SetupReferrerPolicy();
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-08 20:41:38 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> notificationCallbacks;
|
|
|
|
mHttpChannel->GetNotificationCallbacks(getter_AddRefs(notificationCallbacks));
|
|
|
|
MOZ_ASSERT(!notificationCallbacks);
|
2012-02-24 19:37:30 +00:00
|
|
|
}
|
2015-10-08 20:41:38 +00:00
|
|
|
#endif
|
|
|
|
mHttpChannel->SetNotificationCallbacks(this);
|
2012-02-24 19:37:30 +00:00
|
|
|
|
2011-05-22 21:30:07 +00:00
|
|
|
// Start reading from the channel
|
2015-08-05 03:06:19 +00:00
|
|
|
rv = mHttpChannel->AsyncOpen2(this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
DispatchFailConnection();
|
|
|
|
return rv;
|
2012-02-16 16:45:25 +00:00
|
|
|
}
|
2015-08-05 03:06:19 +00:00
|
|
|
mWaitingForOnStopRequest = true;
|
2012-02-16 16:45:25 +00:00
|
|
|
return rv;
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::AnnounceConnection()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState != CONNECTING) {
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_WARNING("Unexpected mReadyState!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a user agent is to announce the connection, the user agent must set
|
|
|
|
// the readyState attribute to OPEN and queue a task to fire a simple event
|
|
|
|
// named open at the EventSource object.
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
mReadyState = OPEN;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2015-11-13 00:09:42 +00:00
|
|
|
event->InitEvent(NS_LITERAL_STRING("open"), false, false);
|
2012-06-10 23:44:50 +00:00
|
|
|
event->SetTrusted(true);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2011-05-22 21:30:07 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the open event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::ResetConnection()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
if (mHttpChannel) {
|
|
|
|
mHttpChannel->Cancel(NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mUnicodeDecoder) {
|
|
|
|
mUnicodeDecoder->Reset();
|
|
|
|
}
|
|
|
|
mLastConvertionResult = NS_OK;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mHttpChannel = nullptr;
|
2011-05-22 21:30:07 +00:00
|
|
|
mStatus = PARSE_STATE_OFF;
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
mReadyState = CONNECTING;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::ReestablishConnection()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = ResetConnection();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to reset the connection!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2015-11-13 00:09:42 +00:00
|
|
|
event->InitEvent(NS_LITERAL_STRING("error"), false, false);
|
2012-06-10 23:44:50 +00:00
|
|
|
event->SetTrusted(true);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2011-05-22 21:30:07 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the error event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = SetReconnectionTimeout();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to set the timeout for reestablishing the connection!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::SetReconnectionTimeout()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the timer will be used whenever the requests are going finished.
|
|
|
|
if (!mTimer) {
|
|
|
|
mTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
NS_ENSURE_STATE(mTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mTimer->InitWithFuncCallback(TimerCallback, this,
|
|
|
|
mReconnectionTime,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::PrintErrorOnConsole(const char *aBundleURI,
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *aError,
|
|
|
|
const char16_t **aFormatStrings,
|
2012-12-22 08:17:05 +00:00
|
|
|
uint32_t aFormatStringsLen)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
mozilla::services::GetStringBundleService();
|
|
|
|
NS_ENSURE_STATE(bundleService);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> strBundle;
|
|
|
|
nsresult rv =
|
|
|
|
bundleService->CreateBundle(aBundleURI, getter_AddRefs(strBundle));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIConsoleService> console(
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-21 21:51:29 +00:00
|
|
|
nsCOMPtr<nsIScriptError> errObj(
|
2011-05-22 21:30:07 +00:00
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Localize the error message
|
|
|
|
nsXPIDLString message;
|
|
|
|
if (aFormatStrings) {
|
|
|
|
rv = strBundle->FormatStringFromName(aError, aFormatStrings,
|
|
|
|
aFormatStringsLen,
|
|
|
|
getter_Copies(message));
|
|
|
|
} else {
|
|
|
|
rv = strBundle->GetStringFromName(aError, getter_Copies(message));
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-09 23:29:12 +00:00
|
|
|
rv = errObj->InitWithWindowID(message,
|
|
|
|
mScriptFile,
|
|
|
|
EmptyString(),
|
2015-09-02 00:56:03 +00:00
|
|
|
mScriptLine, mScriptColumn,
|
2011-12-21 21:51:29 +00:00
|
|
|
nsIScriptError::errorFlag,
|
|
|
|
"Event Source", mInnerWindowID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
// print the error message directly to the JS console
|
2011-12-21 21:51:29 +00:00
|
|
|
rv = console->LogMessage(errObj);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::ConsoleError()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString targetSpec;
|
2011-05-22 21:30:07 +00:00
|
|
|
nsresult rv = mSrc->GetSpec(targetSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 specUTF16(targetSpec);
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *formatStrings[] = { specUTF16.get() };
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2014-11-08 14:51:38 +00:00
|
|
|
if (mReadyState == CONNECTING) {
|
2011-05-22 21:30:07 +00:00
|
|
|
rv = PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
2016-07-21 05:03:25 +00:00
|
|
|
u"connectionFailure",
|
2011-10-11 05:50:08 +00:00
|
|
|
formatStrings, ArrayLength(formatStrings));
|
2011-05-22 21:30:07 +00:00
|
|
|
} else {
|
|
|
|
rv = PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
2016-07-21 05:03:25 +00:00
|
|
|
u"netInterrupt",
|
2011-10-11 05:50:08 +00:00
|
|
|
formatStrings, ArrayLength(formatStrings));
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::DispatchFailConnection()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
|
2016-05-05 08:45:00 +00:00
|
|
|
return NS_DispatchToMainThread(NewRunnableMethod(this, &EventSource::FailConnection));
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::FailConnection()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = ConsoleError();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to print to the console error");
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a user agent is to fail the connection, the user agent must set the
|
|
|
|
// readyState attribute to CLOSED and queue a task to fire a simple event
|
|
|
|
// named error at the EventSource object.
|
|
|
|
|
|
|
|
Close(); // it sets mReadyState to CLOSED
|
|
|
|
|
|
|
|
rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2015-11-13 00:09:42 +00:00
|
|
|
event->InitEvent(NS_LITERAL_STRING("error"), false, false);
|
2012-06-10 23:44:50 +00:00
|
|
|
event->SetTrusted(true);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2011-05-22 21:30:07 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the error event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::TimerCallback(nsITimer* aTimer, void* aClosure)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<EventSource> thisObject = static_cast<EventSource*>(aClosure);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
if (thisObject->mReadyState == CLOSED) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_PRECONDITION(!thisObject->mHttpChannel,
|
|
|
|
"the channel hasn't been cancelled!!");
|
|
|
|
|
|
|
|
if (!thisObject->mFrozen) {
|
|
|
|
nsresult rv = thisObject->InitChannelAndRequestEventSource();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("thisObject->InitChannelAndRequestEventSource() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::Thaw()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED || !mFrozen) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mHttpChannel, "the connection hasn't been closed!!!");
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mFrozen = false;
|
2011-05-22 21:30:07 +00:00
|
|
|
nsresult rv;
|
|
|
|
if (!mGoingToDispatchAllMessages && mMessagesToDispatch.GetSize() > 0) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2016-05-05 08:45:00 +00:00
|
|
|
NewRunnableMethod(this, &EventSource::DispatchAllMessageEvents);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_STATE(event);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mGoingToDispatchAllMessages = true;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2014-05-23 19:53:17 +00:00
|
|
|
rv = NS_DispatchToMainThread(event);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = InitChannelAndRequestEventSource();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::Freeze()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED || mFrozen) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mHttpChannel, "the connection hasn't been closed!!!");
|
2011-10-17 14:59:28 +00:00
|
|
|
mFrozen = true;
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::DispatchCurrentMessageEvent()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
nsAutoPtr<Message> message(new Message());
|
|
|
|
*message = mCurrentMessage;
|
|
|
|
|
|
|
|
ClearFields();
|
|
|
|
|
|
|
|
if (message->mData.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// removes the trailing LF from mData
|
|
|
|
NS_ASSERTION(message->mData.CharAt(message->mData.Length() - 1) == LF_CHAR,
|
|
|
|
"Invalid trailing character! LF was expected instead.");
|
|
|
|
message->mData.SetLength(message->mData.Length() - 1);
|
|
|
|
|
|
|
|
if (message->mEventName.IsEmpty()) {
|
|
|
|
message->mEventName.AssignLiteral("message");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message->mLastEventID.IsEmpty() && !mLastEventID.IsEmpty()) {
|
|
|
|
message->mLastEventID.Assign(mLastEventID);
|
|
|
|
}
|
|
|
|
|
2015-11-15 13:48:08 +00:00
|
|
|
size_t sizeBefore = mMessagesToDispatch.GetSize();
|
2011-05-22 21:30:07 +00:00
|
|
|
mMessagesToDispatch.Push(message.forget());
|
|
|
|
NS_ENSURE_TRUE(mMessagesToDispatch.GetSize() == sizeBefore + 1,
|
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
|
|
|
|
if (!mGoingToDispatchAllMessages) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2016-05-05 08:45:00 +00:00
|
|
|
NewRunnableMethod(this, &EventSource::DispatchAllMessageEvents);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_ENSURE_STATE(event);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mGoingToDispatchAllMessages = true;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2014-05-23 19:53:17 +00:00
|
|
|
return NS_DispatchToMainThread(event);
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::DispatchAllMessageEvents()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED || mFrozen) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mGoingToDispatchAllMessages = false;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-19 07:21:14 +00:00
|
|
|
AutoJSAPI jsapi;
|
2014-06-25 10:17:17 +00:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(GetOwner()))) {
|
2014-05-09 07:07:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2011-05-23 18:53:31 +00:00
|
|
|
|
2011-05-22 21:30:07 +00:00
|
|
|
while (mMessagesToDispatch.GetSize() > 0) {
|
|
|
|
nsAutoPtr<Message>
|
|
|
|
message(static_cast<Message*>(mMessagesToDispatch.PopFront()));
|
|
|
|
|
2011-05-23 18:53:31 +00:00
|
|
|
// Now we can turn our string into a jsval
|
2013-05-08 02:34:56 +00:00
|
|
|
JS::Rooted<JS::Value> jsData(cx);
|
2011-05-23 18:53:31 +00:00
|
|
|
{
|
|
|
|
JSString* jsString;
|
|
|
|
jsString = JS_NewUCStringCopyN(cx,
|
|
|
|
message->mData.get(),
|
|
|
|
message->mData.Length());
|
2012-09-14 10:00:31 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(jsString);
|
2011-05-23 18:53:31 +00:00
|
|
|
|
2015-06-30 18:20:56 +00:00
|
|
|
jsData.setString(jsString);
|
2011-05-23 18:53:31 +00:00
|
|
|
}
|
|
|
|
|
2011-05-22 21:30:07 +00:00
|
|
|
// create an event that uses the MessageEvent interface,
|
|
|
|
// which does not bubble, is not cancelable, and has no default action
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MessageEvent> event =
|
2015-08-12 11:39:31 +00:00
|
|
|
NS_NewDOMMessageEvent(this, nullptr, nullptr);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
event->InitMessageEvent(nullptr, message->mEventName, false, false, jsData,
|
|
|
|
mOrigin, message->mLastEventID, nullptr, nullptr);
|
2015-08-12 11:39:31 +00:00
|
|
|
event->SetTrusted(true);
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2015-08-12 11:39:31 +00:00
|
|
|
rv = DispatchDOMEvent(nullptr, static_cast<Event*>(event), nullptr,
|
|
|
|
nullptr);
|
2011-05-22 21:30:07 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the message event!!!");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-22 19:09:13 +00:00
|
|
|
|
|
|
|
mLastEventID.Assign(message->mLastEventID);
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::ClearFields()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
// mLastEventID and mReconnectionTime must be cached
|
|
|
|
|
|
|
|
mCurrentMessage.mEventName.Truncate();
|
|
|
|
mCurrentMessage.mLastEventID.Truncate();
|
|
|
|
mCurrentMessage.mData.Truncate();
|
|
|
|
|
|
|
|
mLastFieldName.Truncate();
|
|
|
|
mLastFieldValue.Truncate();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::SetFieldAndClear()
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
if (mLastFieldName.IsEmpty()) {
|
|
|
|
mLastFieldValue.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t first_char;
|
2011-05-22 21:30:07 +00:00
|
|
|
first_char = mLastFieldName.CharAt(0);
|
|
|
|
|
|
|
|
switch (first_char) // with no case folding performed
|
|
|
|
{
|
2014-01-04 15:02:17 +00:00
|
|
|
case char16_t('d'):
|
2011-05-22 21:30:07 +00:00
|
|
|
if (mLastFieldName.EqualsLiteral("data")) {
|
|
|
|
// If the field name is "data" append the field value to the data
|
|
|
|
// buffer, then append a single U+000A LINE FEED (LF) character
|
|
|
|
// to the data buffer.
|
|
|
|
mCurrentMessage.mData.Append(mLastFieldValue);
|
|
|
|
mCurrentMessage.mData.Append(LF_CHAR);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
case char16_t('e'):
|
2011-05-22 21:30:07 +00:00
|
|
|
if (mLastFieldName.EqualsLiteral("event")) {
|
|
|
|
mCurrentMessage.mEventName.Assign(mLastFieldValue);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
case char16_t('i'):
|
2011-05-22 21:30:07 +00:00
|
|
|
if (mLastFieldName.EqualsLiteral("id")) {
|
|
|
|
mCurrentMessage.mLastEventID.Assign(mLastFieldValue);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
case char16_t('r'):
|
2011-05-22 21:30:07 +00:00
|
|
|
if (mLastFieldName.EqualsLiteral("retry")) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t newValue=0;
|
|
|
|
uint32_t i = 0; // we must ensure that there are only digits
|
2011-09-29 06:19:26 +00:00
|
|
|
bool assign = true;
|
2011-05-22 21:30:07 +00:00
|
|
|
for (i = 0; i < mLastFieldValue.Length(); ++i) {
|
2014-01-04 15:02:17 +00:00
|
|
|
if (mLastFieldValue.CharAt(i) < (char16_t)'0' ||
|
|
|
|
mLastFieldValue.CharAt(i) > (char16_t)'9') {
|
2011-10-17 14:59:28 +00:00
|
|
|
assign = false;
|
2011-05-22 21:30:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
newValue = newValue*10 +
|
2012-08-22 15:56:38 +00:00
|
|
|
(((uint32_t)mLastFieldValue.CharAt(i))-
|
2014-01-04 15:02:17 +00:00
|
|
|
((uint32_t)((char16_t)'0')));
|
2011-05-22 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (assign) {
|
|
|
|
if (newValue < MIN_RECONNECTION_TIME_VALUE) {
|
|
|
|
mReconnectionTime = MIN_RECONNECTION_TIME_VALUE;
|
|
|
|
} else if (newValue > MAX_RECONNECTION_TIME_VALUE) {
|
|
|
|
mReconnectionTime = MAX_RECONNECTION_TIME_VALUE;
|
|
|
|
} else {
|
|
|
|
mReconnectionTime = newValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastFieldName.Truncate();
|
|
|
|
mLastFieldValue.Truncate();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource::CheckHealthOfRequestCallback(nsIRequest *aRequestCallback)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
// check if we have been closed or if the request has been canceled
|
|
|
|
// or if we have been frozen
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED || !mHttpChannel ||
|
2011-05-22 21:30:07 +00:00
|
|
|
mFrozen || mErrorLoadOnRedirect) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequestCallback);
|
|
|
|
NS_ENSURE_STATE(httpChannel);
|
|
|
|
|
|
|
|
if (httpChannel != mHttpChannel) {
|
|
|
|
NS_WARNING("wrong channel from request callback");
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 15:02:17 +00:00
|
|
|
EventSource::ParseCharacter(char16_t aChr)
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-22 21:30:07 +00:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mStatus)
|
|
|
|
{
|
|
|
|
case PARSE_STATE_OFF:
|
|
|
|
NS_ERROR("Invalid state");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARSE_STATE_BEGIN_OF_STREAM:
|
|
|
|
if (aChr == BOM_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_BOM_WAS_READ; // ignore it
|
|
|
|
} else if (aChr == CR_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_CR_CHAR;
|
|
|
|
} else if (aChr == LF_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_BEGIN_OF_LINE;
|
|
|
|
} else if (aChr == COLON_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_COMMENT;
|
|
|
|
} else {
|
|
|
|
mLastFieldName += aChr;
|
|
|
|
mStatus = PARSE_STATE_FIELD_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARSE_STATE_BOM_WAS_READ:
|
|
|
|
if (aChr == CR_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_CR_CHAR;
|
|
|
|
} else if (aChr == LF_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_BEGIN_OF_LINE;
|
|
|
|
} else if (aChr == COLON_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_COMMENT;
|
|
|
|
} else {
|
|
|
|
mLastFieldName += aChr;
|
|
|
|
mStatus = PARSE_STATE_FIELD_NAME;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARSE_STATE_CR_CHAR:
|
|
|
|
if (aChr == CR_CHAR) {
|
|
|
|
rv = DispatchCurrentMessageEvent(); // there is an empty line (CRCR)
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else if (aChr == LF_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_BEGIN_OF_LINE;
|
|
|
|
} else if (aChr == COLON_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_COMMENT;
|
|
|
|
} else {
|
|
|
|
mLastFieldName += aChr;
|
|
|
|
mStatus = PARSE_STATE_FIELD_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARSE_STATE_COMMENT:
|
|
|
|
if (aChr == CR_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_CR_CHAR;
|
|
|
|
} else if (aChr == LF_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_BEGIN_OF_LINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARSE_STATE_FIELD_NAME:
|
|
|
|
if (aChr == CR_CHAR) {
|
|
|
|
rv = SetFieldAndClear();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStatus = PARSE_STATE_CR_CHAR;
|
|
|
|
} else if (aChr == LF_CHAR) {
|
|
|
|
rv = SetFieldAndClear();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStatus = PARSE_STATE_BEGIN_OF_LINE;
|
|
|
|
} else if (aChr == COLON_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE;
|
|
|
|
} else {
|
|
|
|
mLastFieldName += aChr;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE:
|
|
|
|
if (aChr == CR_CHAR) {
|
|
|
|
rv = SetFieldAndClear();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStatus = PARSE_STATE_CR_CHAR;
|
|
|
|
} else if (aChr == LF_CHAR) {
|
|
|
|
rv = SetFieldAndClear();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStatus = PARSE_STATE_BEGIN_OF_LINE;
|
|
|
|
} else if (aChr == SPACE_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_FIELD_VALUE;
|
|
|
|
} else {
|
|
|
|
mLastFieldValue += aChr;
|
|
|
|
mStatus = PARSE_STATE_FIELD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARSE_STATE_FIELD_VALUE:
|
|
|
|
if (aChr == CR_CHAR) {
|
|
|
|
rv = SetFieldAndClear();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStatus = PARSE_STATE_CR_CHAR;
|
|
|
|
} else if (aChr == LF_CHAR) {
|
|
|
|
rv = SetFieldAndClear();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStatus = PARSE_STATE_BEGIN_OF_LINE;
|
|
|
|
} else {
|
|
|
|
mLastFieldValue += aChr;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARSE_STATE_BEGIN_OF_LINE:
|
|
|
|
if (aChr == CR_CHAR) {
|
|
|
|
rv = DispatchCurrentMessageEvent(); // there is an empty line
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStatus = PARSE_STATE_CR_CHAR;
|
|
|
|
} else if (aChr == LF_CHAR) {
|
|
|
|
rv = DispatchCurrentMessageEvent(); // there is an empty line
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStatus = PARSE_STATE_BEGIN_OF_LINE;
|
|
|
|
} else if (aChr == COLON_CHAR) {
|
|
|
|
mStatus = PARSE_STATE_COMMENT;
|
|
|
|
} else {
|
|
|
|
mLastFieldName += aChr;
|
|
|
|
mStatus = PARSE_STATE_FIELD_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-12-22 08:17:05 +00:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|