2011-05-23 01:30:07 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04: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-23 01:30:07 +04:00
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
#include "mozilla/dom/EventSource.h"
|
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2014-07-10 10:56:37 +04:00
|
|
|
#include "mozilla/LoadInfo.h"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2012-12-22 12:17:05 +04:00
|
|
|
#include "mozilla/dom/EventSourceBinding.h"
|
2014-02-27 14:51:14 +04:00
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
2014-05-09 11:07:30 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#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 11:31:52 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2011-05-23 01:30:07 +04:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2011-08-11 17:29:50 +04:00
|
|
|
#include "nsContentUtils.h"
|
2011-05-25 10:31:59 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-09-29 20:06:36 +04:00
|
|
|
#include "xpcpublic.h"
|
2014-12-31 02:54:59 +03:00
|
|
|
#include "nsCORSListenerProxy.h"
|
2012-01-26 19:55:30 +04:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-06-19 06:30:09 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2011-05-25 10:31:59 +04:00
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2014-01-04 19:02:17 +04: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-23 01:30:07 +04: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)
|
|
|
|
|
2014-01-07 06:53:23 +04:00
|
|
|
EventSource::EventSource(nsPIDOMWindow* aOwnerWindow) :
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper(aOwnerWindow),
|
2011-05-23 01:30:07 +04:00
|
|
|
mStatus(PARSE_STATE_OFF),
|
2011-10-17 18:59:28 +04:00
|
|
|
mFrozen(false),
|
|
|
|
mErrorLoadOnRedirect(false),
|
|
|
|
mGoingToDispatchAllMessages(false),
|
2011-12-06 03:02:42 +04:00
|
|
|
mWithCredentials(false),
|
2012-02-16 20:45:25 +04:00
|
|
|
mWaitingForOnStopRequest(false),
|
2011-05-23 01:30:07 +04:00
|
|
|
mLastConvertionResult(NS_OK),
|
2012-12-22 12:17:05 +04:00
|
|
|
mReadyState(CONNECTING),
|
2011-05-23 01:30:07 +04:00
|
|
|
mScriptLine(0),
|
2011-08-25 00:44:35 +04:00
|
|
|
mInnerWindowID(0)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::~EventSource()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-12-22 12:17:05 +04:00
|
|
|
// EventSource::nsISupports
|
2011-05-23 01:30:07 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(EventSource)
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(EventSource)
|
2012-02-16 20:45:25 +04:00
|
|
|
bool isBlack = tmp->IsBlack();
|
|
|
|
if (isBlack || tmp->mWaitingForOnStopRequest) {
|
2012-01-26 19:55:30 +04:00
|
|
|
if (tmp->mListenerManager) {
|
2012-10-17 05:22:02 +04:00
|
|
|
tmp->mListenerManager->MarkForCC();
|
2012-01-26 19:55:30 +04:00
|
|
|
}
|
2012-03-14 18:22:10 +04:00
|
|
|
if (!isBlack && tmp->PreservingWrapper()) {
|
2013-09-09 07:28:48 +04:00
|
|
|
// This marks the wrapper black.
|
|
|
|
tmp->GetWrapper();
|
2012-02-16 20:45:25 +04:00
|
|
|
}
|
2012-01-26 19:55:30 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(EventSource)
|
2012-01-26 19:55:30 +04:00
|
|
|
return tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(EventSource)
|
2012-01-26 19:55:30 +04:00
|
|
|
return tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(EventSource,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2012-01-26 19:55:30 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(EventSource,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSrc)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNotificationCallbacks)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoadGroup)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mChannelEventSink)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mHttpChannel)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTimer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mUnicodeDecoder)
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(EventSource,
|
|
|
|
DOMEventTargetHelper)
|
2011-05-23 01:30:07 +04:00
|
|
|
tmp->Close();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(EventSource)
|
2011-05-23 01:30:07 +04: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 10:13:50 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(EventSource, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(EventSource, DOMEventTargetHelper)
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-03-13 04:56:07 +04:00
|
|
|
void
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::DisconnectFromOwner()
|
2012-03-13 04:56:07 +04:00
|
|
|
{
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2012-03-13 04:56:20 +04:00
|
|
|
Close();
|
2012-03-13 04:56:07 +04:00
|
|
|
}
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
void
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::Close()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED) {
|
|
|
|
return;
|
2011-05-23 01:30:07 +04: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 18:20:58 +04:00
|
|
|
mTimer = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ResetConnection();
|
|
|
|
|
|
|
|
ClearFields();
|
|
|
|
|
|
|
|
while (mMessagesToDispatch.GetSize() != 0) {
|
|
|
|
delete static_cast<Message*>(mMessagesToDispatch.PopFront());
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mSrc = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mFrozen = false;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mUnicodeDecoder = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
mReadyState = CLOSED;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
nsresult
|
|
|
|
EventSource::Init(nsISupports* aOwner,
|
2012-12-22 12:17:05 +04:00
|
|
|
const nsAString& aURL,
|
|
|
|
bool aWithCredentials)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState != CONNECTING || !PrefEnabled()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
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);
|
|
|
|
|
|
|
|
mPrincipal = principal;
|
2011-12-06 03:02:42 +04:00
|
|
|
mWithCredentials = aWithCredentials;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2013-04-29 22:16:17 +04:00
|
|
|
// The conditional here is historical and not necessarily sane.
|
|
|
|
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
|
2015-01-06 23:50:29 +03:00
|
|
|
nsJSUtils::GetCallingLocation(cx, mScriptFile, &mScriptLine);
|
2011-08-25 00:44:35 +04:00
|
|
|
mInnerWindowID = nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(cx);
|
2011-05-23 01:30:07 +04: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.
|
2012-03-13 04:56:07 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
|
|
|
if (sc) {
|
2011-05-23 01:30:07 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc =
|
2012-03-13 04:56:07 +04:00
|
|
|
nsContentUtils::GetDocumentFromScriptContext(sc);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (doc) {
|
|
|
|
mLoadGroup = doc->GetDocumentLoadGroup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the src
|
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
2012-03-13 04:56:07 +04:00
|
|
|
rv = GetBaseURI(getter_AddRefs(baseURI));
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> srcURI;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_NewURI(getter_AddRefs(srcURI), aURL, nullptr, baseURI);
|
2011-05-23 01:30:07 +04: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 18:59:28 +04:00
|
|
|
rv = os->AddObserver(this, DOM_WINDOW_DESTROYED_TOPIC, true);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = os->AddObserver(this, DOM_WINDOW_FROZEN_TOPIC, true);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = os->AddObserver(this, DOM_WINDOW_THAWED_TOPIC, true);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-06 03:02:42 +04:00
|
|
|
nsAutoString origin;
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(srcURI, origin);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2011-05-23 01:30:07 +04:00
|
|
|
rv = srcURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mOriginalURL = NS_ConvertUTF8toUTF16(spec);
|
|
|
|
mSrc = srcURI;
|
|
|
|
mOrigin = origin;
|
|
|
|
|
|
|
|
mReconnectionTime =
|
2011-05-25 10:32:00 +04:00
|
|
|
Preferences::GetInt("dom.server-events.default-reconnection-time",
|
|
|
|
DEFAULT_RECONNECTION_TIME_VALUE);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2013-11-26 11:31:52 +04:00
|
|
|
mUnicodeDecoder = EncodingUtils::DecoderForEncoding("UTF-8");
|
2011-05-23 01:30:07 +04: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 12:17:05 +04: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 17:13:33 +03:00
|
|
|
EventSource::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2011-05-23 01:30:07 +04: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 17:13:33 +03:00
|
|
|
return EventSourceBinding::Wrap(aCx, this, aGivenProto);
|
2012-12-22 12:17:05 +04:00
|
|
|
}
|
2011-12-06 03:02:42 +04:00
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
/* static */ already_AddRefed<EventSource>
|
2013-08-23 09:17:08 +04:00
|
|
|
EventSource::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aURL,
|
2012-12-22 12:17:05 +04:00
|
|
|
const EventSourceInit& aEventSourceInitDict,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2014-01-07 06:53:23 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> ownerWindow =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!ownerWindow) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(ownerWindow->IsInnerWindow());
|
|
|
|
|
|
|
|
nsRefPtr<EventSource> eventSource = new EventSource(ownerWindow);
|
2013-08-23 09:17:08 +04:00
|
|
|
aRv = eventSource->Init(aGlobal.GetAsSupports(), aURL,
|
2012-12-03 20:07:49 +04:00
|
|
|
aEventSourceInitDict.mWithCredentials);
|
2012-12-22 12:17:05 +04:00
|
|
|
return eventSource.forget();
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-12-22 12:17:05 +04:00
|
|
|
// EventSource::nsIObserver
|
2011-05-23 01:30:07 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aSubject);
|
2012-03-13 04:56:07 +04:00
|
|
|
if (!GetOwner() || window != GetOwner()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-28 17:14:33 +04:00
|
|
|
DebugOnly<nsresult> rv;
|
2011-05-23 01:30:07 +04:00
|
|
|
if (strcmp(aTopic, DOM_WINDOW_FROZEN_TOPIC) == 0) {
|
|
|
|
rv = Freeze();
|
2012-09-02 01:24:42 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Freeze() failed");
|
2011-05-23 01:30:07 +04:00
|
|
|
} else if (strcmp(aTopic, DOM_WINDOW_THAWED_TOPIC) == 0) {
|
|
|
|
rv = Thaw();
|
2012-09-02 01:24:42 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Thaw() failed");
|
2011-05-23 01:30:07 +04:00
|
|
|
} else if (strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC) == 0) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-12-22 12:17:05 +04:00
|
|
|
// EventSource::nsIStreamListener
|
2011-05-23 01:30:07 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::OnStartRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *ctxt)
|
2011-05-23 01:30:07 +04: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 13:34:15 +04:00
|
|
|
nsresult status;
|
2014-11-08 17:51:38 +03:00
|
|
|
rv = aRequest->GetStatus(&status);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-06-04 13:34:15 +04:00
|
|
|
|
2014-11-08 17:51:38 +03: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 16:29:31 +04:00
|
|
|
}
|
|
|
|
|
2013-06-04 13:34:15 +04:00
|
|
|
uint32_t httpStatus;
|
|
|
|
rv = httpChannel->GetResponseStatus(&httpStatus);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-06-04 13:34:15 +04:00
|
|
|
if (httpStatus != 200) {
|
2014-11-08 17:51:38 +03: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-23 01:30:07 +04:00
|
|
|
DispatchFailConnection();
|
2013-06-04 13:34:15 +04:00
|
|
|
return NS_ERROR_ABORT;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_NewRunnableMethod(this, &EventSource::AnnounceConnection);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_STATE(event);
|
|
|
|
|
2014-05-23 23:53:17 +04:00
|
|
|
rv = NS_DispatchToMainThread(event);
|
2011-05-23 01:30:07 +04: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 12:17:05 +04:00
|
|
|
EventSource::StreamReaderFunc(nsIInputStream *aInputStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char *aFromRawSegment,
|
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource* thisObject = static_cast<EventSource*>(aClosure);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (!thisObject || !aWriteCount) {
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_WARNING("EventSource cannot read from stream: no aClosure or aWriteCount");
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aWriteCount = 0;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t srcCount, outCount;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t out[2];
|
2011-05-23 01:30:07 +04: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 18:11:15 +04:00
|
|
|
MOZ_ASSERT(thisObject->mLastConvertionResult != NS_ERROR_ILLEGAL_INPUT);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-12-10 18:11:15 +04:00
|
|
|
for (int32_t i = 0; i < outCount; ++i) {
|
|
|
|
rv = thisObject->ParseCharacter(out[i]);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-12-10 18:11:15 +04:00
|
|
|
p = p + srcCount;
|
2011-05-23 01:30:07 +04:00
|
|
|
} while (p < end &&
|
|
|
|
thisObject->mLastConvertionResult != NS_PARTIAL_MORE_INPUT &&
|
|
|
|
thisObject->mLastConvertionResult != NS_OK);
|
|
|
|
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::OnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aInputStream,
|
|
|
|
uint64_t aOffset,
|
|
|
|
uint32_t aCount)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aInputStream);
|
|
|
|
|
|
|
|
nsresult rv = CheckHealthOfRequestCallback(aRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t totalRead;
|
2012-12-22 12:17:05 +04:00
|
|
|
return aInputStream->ReadSegments(EventSource::StreamReaderFunc, this,
|
2011-05-23 01:30:07 +04:00
|
|
|
aCount, &totalRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::OnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatusCode)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-02-16 20:45:25 +04:00
|
|
|
mWaitingForOnStopRequest = false;
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2014-11-08 17:51:38 +03: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-23 01:30:07 +04:00
|
|
|
DispatchFailConnection();
|
2014-11-08 17:51:38 +03:00
|
|
|
return NS_ERROR_ABORT;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
2014-11-08 17:51:38 +03:00
|
|
|
nsresult rv = CheckHealthOfRequestCallback(aRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2013-01-22 23:09:13 +04:00
|
|
|
ClearFields();
|
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_NewRunnableMethod(this, &EventSource::ReestablishConnection);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_STATE(event);
|
|
|
|
|
2014-05-23 23:53:17 +04:00
|
|
|
rv = NS_DispatchToMainThread(event);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-11-08 17:51:38 +03:00
|
|
|
return NS_OK;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple helper class that just forwards the redirect callback back
|
2012-12-22 12:17:05 +04:00
|
|
|
* to the EventSource.
|
2011-05-23 01:30:07 +04:00
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class AsyncVerifyRedirectCallbackFwr final : public nsIAsyncVerifyRedirectCallback
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit AsyncVerifyRedirectCallbackFwr(EventSource* aEventsource)
|
2011-05-23 01:30:07 +04:00
|
|
|
: mEventSource(aEventsource)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(AsyncVerifyRedirectCallbackFwr)
|
|
|
|
|
|
|
|
// nsIAsyncVerifyRedirectCallback implementation
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnRedirectVerifyCallback(nsresult aResult) override
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
nsresult rv = mEventSource->OnRedirectVerifyCallback(aResult);
|
|
|
|
if (NS_FAILED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mEventSource->mErrorLoadOnRedirect = true;
|
2011-05-23 01:30:07 +04:00
|
|
|
mEventSource->DispatchFailConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-25 06:09:15 +04:00
|
|
|
~AsyncVerifyRedirectCallbackFwr() {}
|
2012-12-22 12:17:05 +04:00
|
|
|
nsRefPtr<EventSource> mEventSource;
|
2011-05-23 01:30:07 +04:00
|
|
|
};
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(AsyncVerifyRedirectCallbackFwr, mEventSource)
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AsyncVerifyRedirectCallbackFwr)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIAsyncVerifyRedirectCallback)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(AsyncVerifyRedirectCallbackFwr)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(AsyncVerifyRedirectCallbackFwr)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-12-22 12:17:05 +04:00
|
|
|
// EventSource::nsIChannelEventSink
|
2011-05-23 01:30:07 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::AsyncOnChannelRedirect(nsIChannel *aOldChannel,
|
|
|
|
nsIChannel *aNewChannel,
|
|
|
|
uint32_t aFlags,
|
|
|
|
nsIAsyncVerifyRedirectCallback *aCallback)
|
2011-05-23 01:30:07 +04: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);
|
|
|
|
|
|
|
|
if (!CheckCanRequestSrc(newURI)) {
|
|
|
|
DispatchFailConnection();
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare to receive callback
|
|
|
|
mRedirectFlags = aFlags;
|
|
|
|
mRedirectCallback = aCallback;
|
|
|
|
mNewRedirectChannel = aNewChannel;
|
|
|
|
|
|
|
|
if (mChannelEventSink) {
|
|
|
|
nsRefPtr<AsyncVerifyRedirectCallbackFwr> fwd =
|
|
|
|
new AsyncVerifyRedirectCallbackFwr(this);
|
|
|
|
|
|
|
|
rv = mChannelEventSink->AsyncOnChannelRedirect(aOldChannel,
|
|
|
|
aNewChannel,
|
|
|
|
aFlags, fwd);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mRedirectCallback = nullptr;
|
|
|
|
mNewRedirectChannel = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mErrorLoadOnRedirect = true;
|
2011-05-23 01:30:07 +04:00
|
|
|
DispatchFailConnection();
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::OnRedirectVerifyCallback(nsresult aResult)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(mRedirectCallback, "mRedirectCallback not set in callback");
|
|
|
|
MOZ_ASSERT(mNewRedirectChannel,
|
|
|
|
"mNewRedirectChannel not set in callback");
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(aResult, aResult);
|
|
|
|
|
|
|
|
// update our channel
|
|
|
|
|
|
|
|
mHttpChannel = do_QueryInterface(mNewRedirectChannel);
|
|
|
|
NS_ENSURE_STATE(mHttpChannel);
|
|
|
|
|
|
|
|
nsresult rv = SetupHttpChannel();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if ((mRedirectFlags & nsIChannelEventSink::REDIRECT_PERMANENT) != 0) {
|
|
|
|
rv = NS_GetFinalChannelURI(mHttpChannel, getter_AddRefs(mSrc));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mNewRedirectChannel = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(aResult);
|
2012-07-30 18:20:58 +04:00
|
|
|
mRedirectCallback = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-12-22 12:17:05 +04:00
|
|
|
// EventSource::nsIInterfaceRequestor
|
2011-05-23 01:30:07 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::GetInterface(const nsIID & aIID,
|
|
|
|
void **aResult)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
// Make sure to return ourselves for the channel event sink interface,
|
|
|
|
// no matter what. We can forward these to mNotificationCallbacks
|
|
|
|
// if it wants to get notifications for them. But we
|
|
|
|
// need to see these notifications for proper functioning.
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
|
|
|
mChannelEventSink = do_GetInterface(mNotificationCallbacks);
|
|
|
|
*aResult = static_cast<nsIChannelEventSink*>(this);
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now give mNotificationCallbacks (if non-null) a chance to return the
|
|
|
|
// desired interface.
|
|
|
|
if (mNotificationCallbacks) {
|
|
|
|
nsresult rv = mNotificationCallbacks->GetInterface(aIID, aResult);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ASSERTION(*aResult, "Lying nsIInterfaceRequestor implementation!");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> window;
|
2012-03-13 04:56:07 +04:00
|
|
|
if (GetOwner()) {
|
|
|
|
window = GetOwner()->GetOuterWindow();
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return wwatch->GetPrompt(window, aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-02-06 22:28:14 +04:00
|
|
|
EventSource::PrefEnabled(JSContext* aCx, JSObject* aGlobal)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
return Preferences::GetBool("dom.server-events.enabled", false);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::GetBaseURI(nsIURI **aBaseURI)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aBaseURI);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aBaseURI = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
|
|
|
|
|
|
|
// first we try from document->GetBaseURI()
|
2012-03-13 04:56:07 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
2011-05-23 01:30:07 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc =
|
2012-03-13 04:56:07 +04:00
|
|
|
nsContentUtils::GetDocumentFromScriptContext(sc);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (doc) {
|
|
|
|
baseURI = doc->GetBaseURI();
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise we get from the doc's principal
|
|
|
|
if (!baseURI) {
|
2012-03-13 04:56:07 +04:00
|
|
|
rv = mPrincipal->GetURI(getter_AddRefs(baseURI));
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_STATE(baseURI);
|
|
|
|
|
|
|
|
baseURI.forget(aBaseURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:47:03 +03:00
|
|
|
net::ReferrerPolicy
|
|
|
|
EventSource::GetReferrerPolicy()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, mozilla::net::RP_Default);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromScriptContext(sc);
|
|
|
|
return doc ? doc->GetReferrerPolicy() : mozilla::net::RP_Default;
|
|
|
|
}
|
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::SetupHttpChannel()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
mHttpChannel->SetRequestMethod(NS_LITERAL_CSTRING("GET"));
|
|
|
|
|
|
|
|
/* set the http request headers */
|
|
|
|
|
|
|
|
mHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_LITERAL_CSTRING(TEXT_EVENT_STREAM), false);
|
2011-05-23 01:30:07 +04: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 18:59:28 +04:00
|
|
|
NS_ConvertUTF16toUTF8(mLastEventID), false);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> codebase;
|
|
|
|
nsresult rv = GetBaseURI(getter_AddRefs(codebase));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-11-18 16:47:03 +03:00
|
|
|
rv = mHttpChannel->SetReferrerWithPolicy(codebase, this->GetReferrerPolicy());
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::InitChannelAndRequestEventSource()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eventsource validation
|
|
|
|
|
|
|
|
if (!CheckCanRequestSrc()) {
|
|
|
|
DispatchFailConnection();
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsLoadFlags loadFlags;
|
|
|
|
loadFlags = nsIRequest::LOAD_BACKGROUND | nsIRequest::LOAD_BYPASS_CACHE;
|
|
|
|
|
2014-10-16 06:11:45 +04:00
|
|
|
nsresult rv;
|
2014-09-21 20:36:25 +04:00
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
|
|
|
nsCOMPtr<nsIDocument> doc =
|
|
|
|
nsContentUtils::GetDocumentFromScriptContext(sc);
|
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2014-09-21 20:36:25 +04:00
|
|
|
// If we have the document, use it
|
|
|
|
if (doc) {
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
mSrc,
|
|
|
|
doc,
|
|
|
|
nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL,
|
|
|
|
nsIContentPolicy::TYPE_DATAREQUEST,
|
|
|
|
mLoadGroup, // loadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
loadFlags); // aLoadFlags
|
|
|
|
} else {
|
|
|
|
// otherwise use the principal
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
mSrc,
|
2014-09-23 02:34:26 +04:00
|
|
|
mPrincipal,
|
2014-09-21 20:36:25 +04:00
|
|
|
nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL,
|
|
|
|
nsIContentPolicy::TYPE_DATAREQUEST,
|
|
|
|
mLoadGroup, // loadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
loadFlags); // aLoadFlags
|
|
|
|
}
|
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mHttpChannel = do_QueryInterface(channel);
|
|
|
|
NS_ENSURE_TRUE(mHttpChannel, NS_ERROR_NO_INTERFACE);
|
|
|
|
|
|
|
|
rv = SetupHttpChannel();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-02-24 23:37:30 +04:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> notificationCallbacks;
|
|
|
|
mHttpChannel->GetNotificationCallbacks(getter_AddRefs(notificationCallbacks));
|
|
|
|
if (notificationCallbacks != this) {
|
|
|
|
mNotificationCallbacks = notificationCallbacks;
|
|
|
|
mHttpChannel->SetNotificationCallbacks(this);
|
|
|
|
}
|
|
|
|
|
2012-09-19 06:16:23 +04:00
|
|
|
nsRefPtr<nsCORSListenerProxy> listener =
|
|
|
|
new nsCORSListenerProxy(this, mPrincipal, mWithCredentials);
|
|
|
|
rv = listener->Init(mHttpChannel);
|
2011-12-06 03:02:42 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
// Start reading from the channel
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = mHttpChannel->AsyncOpen(listener, nullptr);
|
2012-02-16 20:45:25 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mWaitingForOnStopRequest = true;
|
|
|
|
}
|
|
|
|
return rv;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::AnnounceConnection()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState != CONNECTING) {
|
2011-05-23 01:30:07 +04: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 12:17:05 +04:00
|
|
|
mReadyState = OPEN;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 15:34:29 +04:00
|
|
|
rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create the open event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = event->InitEvent(NS_LITERAL_STRING("open"), false, false);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to init the open event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
event->SetTrusted(true);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the open event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::ResetConnection()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
if (mHttpChannel) {
|
|
|
|
mHttpChannel->Cancel(NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mUnicodeDecoder) {
|
|
|
|
mUnicodeDecoder->Reset();
|
|
|
|
}
|
|
|
|
mLastConvertionResult = NS_OK;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mHttpChannel = nullptr;
|
|
|
|
mNotificationCallbacks = nullptr;
|
|
|
|
mChannelEventSink = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
mStatus = PARSE_STATE_OFF;
|
2012-07-30 18:20:58 +04:00
|
|
|
mRedirectCallback = nullptr;
|
|
|
|
mNewRedirectChannel = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
mReadyState = CONNECTING;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::ReestablishConnection()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = ResetConnection();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to reset the connection!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 15:34:29 +04:00
|
|
|
rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create the error event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = event->InitEvent(NS_LITERAL_STRING("error"), false, false);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to init the error event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
event->SetTrusted(true);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2011-05-23 01:30:07 +04: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 12:17:05 +04:00
|
|
|
EventSource::SetReconnectionTimeout()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-23 01:30:07 +04: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 12:17:05 +04:00
|
|
|
EventSource::PrintErrorOnConsole(const char *aBundleURI,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *aError,
|
|
|
|
const char16_t **aFormatStrings,
|
2012-12-22 12:17:05 +04:00
|
|
|
uint32_t aFormatStringsLen)
|
2011-05-23 01:30:07 +04: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-22 01:51:29 +04:00
|
|
|
nsCOMPtr<nsIScriptError> errObj(
|
2011-05-23 01:30:07 +04: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-10 03:29:12 +04:00
|
|
|
rv = errObj->InitWithWindowID(message,
|
|
|
|
mScriptFile,
|
|
|
|
EmptyString(),
|
2011-12-22 01:51:29 +04:00
|
|
|
mScriptLine, 0,
|
|
|
|
nsIScriptError::errorFlag,
|
|
|
|
"Event Source", mInnerWindowID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
// print the error message directly to the JS console
|
2011-12-22 01:51:29 +04:00
|
|
|
rv = console->LogMessage(errObj);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::ConsoleError()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString targetSpec;
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult rv = mSrc->GetSpec(targetSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 specUTF16(targetSpec);
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *formatStrings[] = { specUTF16.get() };
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2014-11-08 17:51:38 +03:00
|
|
|
if (mReadyState == CONNECTING) {
|
2011-05-23 01:30:07 +04:00
|
|
|
rv = PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
2013-12-13 05:50:01 +04:00
|
|
|
MOZ_UTF16("connectionFailure"),
|
2011-10-11 09:50:08 +04:00
|
|
|
formatStrings, ArrayLength(formatStrings));
|
2011-05-23 01:30:07 +04:00
|
|
|
} else {
|
|
|
|
rv = PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
2013-12-13 05:50:01 +04:00
|
|
|
MOZ_UTF16("netInterrupt"),
|
2011-10-11 09:50:08 +04:00
|
|
|
formatStrings, ArrayLength(formatStrings));
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::DispatchFailConnection()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_NewRunnableMethod(this, &EventSource::FailConnection);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_STATE(event);
|
|
|
|
|
2014-05-23 23:53:17 +04:00
|
|
|
return NS_DispatchToMainThread(event);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::FailConnection()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-23 01:30:07 +04: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 15:34:29 +04:00
|
|
|
rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create the error event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = event->InitEvent(NS_LITERAL_STRING("error"), false, false);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to init the error event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
event->SetTrusted(true);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the error event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::CheckCanRequestSrc(nsIURI* aSrc)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isValidURI = false;
|
|
|
|
bool isValidContentLoadPolicy = false;
|
|
|
|
bool isValidProtocol = false;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> srcToTest = aSrc ? aSrc : mSrc.get();
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(srcToTest, false);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCheckURIFlags =
|
2011-05-23 01:30:07 +04:00
|
|
|
nsIScriptSecurityManager::DISALLOW_INHERIT_PRINCIPAL |
|
|
|
|
nsIScriptSecurityManager::DISALLOW_SCRIPT;
|
|
|
|
|
|
|
|
nsresult rv = nsContentUtils::GetSecurityManager()->
|
|
|
|
CheckLoadURIWithPrincipal(mPrincipal,
|
|
|
|
srcToTest,
|
|
|
|
aCheckURIFlags);
|
|
|
|
isValidURI = NS_SUCCEEDED(rv);
|
|
|
|
|
|
|
|
// After the security manager, the content-policy check
|
|
|
|
|
2012-03-13 04:56:07 +04:00
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
2011-05-23 01:30:07 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc =
|
2012-03-13 04:56:07 +04:00
|
|
|
nsContentUtils::GetDocumentFromScriptContext(sc);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
// mScriptContext should be initialized because of GetBaseURI() above.
|
2012-07-30 18:20:58 +04:00
|
|
|
// Still need to consider the case that doc is nullptr however.
|
2011-05-23 01:30:07 +04:00
|
|
|
rv = CheckInnerWindowCorrectness();
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t shouldLoad = nsIContentPolicy::ACCEPT;
|
2011-10-31 16:48:50 +04:00
|
|
|
rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_DATAREQUEST,
|
2011-05-23 01:30:07 +04:00
|
|
|
srcToTest,
|
|
|
|
mPrincipal,
|
|
|
|
doc,
|
|
|
|
NS_LITERAL_CSTRING(TEXT_EVENT_STREAM),
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, // extra
|
2011-05-23 01:30:07 +04:00
|
|
|
&shouldLoad,
|
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
|
|
|
isValidContentLoadPolicy = NS_SUCCEEDED(rv) && NS_CP_ACCEPTED(shouldLoad);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString targetURIScheme;
|
2011-05-23 01:30:07 +04:00
|
|
|
rv = srcToTest->GetScheme(targetURIScheme);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// We only have the http support for now
|
|
|
|
isValidProtocol = targetURIScheme.EqualsLiteral("http") ||
|
|
|
|
targetURIScheme.EqualsLiteral("https");
|
|
|
|
}
|
|
|
|
|
2011-12-06 03:02:42 +04:00
|
|
|
return isValidURI && isValidContentLoadPolicy && isValidProtocol;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::TimerCallback(nsITimer* aTimer, void* aClosure)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
nsRefPtr<EventSource> thisObject = static_cast<EventSource*>(aClosure);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
if (thisObject->mReadyState == CLOSED) {
|
2011-05-23 01:30:07 +04: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 12:17:05 +04:00
|
|
|
EventSource::Thaw()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED || !mFrozen) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mHttpChannel, "the connection hasn't been closed!!!");
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mFrozen = false;
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (!mGoingToDispatchAllMessages && mMessagesToDispatch.GetSize() > 0) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_NewRunnableMethod(this, &EventSource::DispatchAllMessageEvents);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_STATE(event);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mGoingToDispatchAllMessages = true;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2014-05-23 23:53:17 +04:00
|
|
|
rv = NS_DispatchToMainThread(event);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = InitChannelAndRequestEventSource();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::Freeze()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED || mFrozen) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mHttpChannel, "the connection hasn't been closed!!!");
|
2011-10-17 18:59:28 +04:00
|
|
|
mFrozen = true;
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::DispatchCurrentMessageEvent()
|
2011-05-23 01:30:07 +04: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);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t sizeBefore = mMessagesToDispatch.GetSize();
|
2011-05-23 01:30:07 +04:00
|
|
|
mMessagesToDispatch.Push(message.forget());
|
|
|
|
NS_ENSURE_TRUE(mMessagesToDispatch.GetSize() == sizeBefore + 1,
|
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
|
|
|
|
if (!mGoingToDispatchAllMessages) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2012-12-22 12:17:05 +04:00
|
|
|
NS_NewRunnableMethod(this, &EventSource::DispatchAllMessageEvents);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_STATE(event);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mGoingToDispatchAllMessages = true;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2014-05-23 23:53:17 +04:00
|
|
|
return NS_DispatchToMainThread(event);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::DispatchAllMessageEvents()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED || mFrozen) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mGoingToDispatchAllMessages = false;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-19 11:21:14 +04:00
|
|
|
AutoJSAPI jsapi;
|
2014-06-25 14:17:17 +04:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(GetOwner()))) {
|
2014-05-09 11:07:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2011-05-23 22:53:31 +04:00
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
while (mMessagesToDispatch.GetSize() > 0) {
|
|
|
|
nsAutoPtr<Message>
|
|
|
|
message(static_cast<Message*>(mMessagesToDispatch.PopFront()));
|
|
|
|
|
2011-05-23 22:53:31 +04:00
|
|
|
// Now we can turn our string into a jsval
|
2013-05-08 06:34:56 +04:00
|
|
|
JS::Rooted<JS::Value> jsData(cx);
|
2011-05-23 22:53:31 +04:00
|
|
|
{
|
|
|
|
JSString* jsString;
|
|
|
|
jsString = JS_NewUCStringCopyN(cx,
|
|
|
|
message->mData.get(),
|
|
|
|
message->mData.Length());
|
2012-09-14 14:00:31 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(jsString);
|
2011-05-23 22:53:31 +04:00
|
|
|
|
|
|
|
jsData = STRING_TO_JSVAL(jsString);
|
|
|
|
}
|
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
// create an event that uses the MessageEvent interface,
|
|
|
|
// which does not bubble, is not cancelable, and has no default action
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 15:34:29 +04:00
|
|
|
rv = NS_NewDOMMessageEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create the message event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMMessageEvent> messageEvent = do_QueryInterface(event);
|
|
|
|
rv = messageEvent->InitMessageEvent(message->mEventName,
|
2011-10-17 18:59:28 +04:00
|
|
|
false, false,
|
2011-05-23 22:53:31 +04:00
|
|
|
jsData,
|
2011-12-06 03:02:42 +04:00
|
|
|
mOrigin,
|
2012-07-30 18:20:58 +04:00
|
|
|
message->mLastEventID, nullptr);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to init the message event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
messageEvent->SetTrusted(true);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the message event!!!");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-22 23:09:13 +04:00
|
|
|
|
|
|
|
mLastEventID.Assign(message->mLastEventID);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource::ClearFields()
|
2011-05-23 01:30:07 +04: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 12:17:05 +04:00
|
|
|
EventSource::SetFieldAndClear()
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
if (mLastFieldName.IsEmpty()) {
|
|
|
|
mLastFieldValue.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t first_char;
|
2011-05-23 01:30:07 +04:00
|
|
|
first_char = mLastFieldName.CharAt(0);
|
|
|
|
|
|
|
|
switch (first_char) // with no case folding performed
|
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
case char16_t('d'):
|
2011-05-23 01:30:07 +04: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 19:02:17 +04:00
|
|
|
case char16_t('e'):
|
2011-05-23 01:30:07 +04:00
|
|
|
if (mLastFieldName.EqualsLiteral("event")) {
|
|
|
|
mCurrentMessage.mEventName.Assign(mLastFieldValue);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
case char16_t('i'):
|
2011-05-23 01:30:07 +04:00
|
|
|
if (mLastFieldName.EqualsLiteral("id")) {
|
|
|
|
mCurrentMessage.mLastEventID.Assign(mLastFieldValue);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
case char16_t('r'):
|
2011-05-23 01:30:07 +04:00
|
|
|
if (mLastFieldName.EqualsLiteral("retry")) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t newValue=0;
|
|
|
|
uint32_t i = 0; // we must ensure that there are only digits
|
2011-09-29 10:19:26 +04:00
|
|
|
bool assign = true;
|
2011-05-23 01:30:07 +04:00
|
|
|
for (i = 0; i < mLastFieldValue.Length(); ++i) {
|
2014-01-04 19:02:17 +04:00
|
|
|
if (mLastFieldValue.CharAt(i) < (char16_t)'0' ||
|
|
|
|
mLastFieldValue.CharAt(i) > (char16_t)'9') {
|
2011-10-17 18:59:28 +04:00
|
|
|
assign = false;
|
2011-05-23 01:30:07 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
newValue = newValue*10 +
|
2012-08-22 19:56:38 +04:00
|
|
|
(((uint32_t)mLastFieldValue.CharAt(i))-
|
2014-01-04 19:02:17 +04:00
|
|
|
((uint32_t)((char16_t)'0')));
|
2011-05-23 01:30:07 +04: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 12:17:05 +04:00
|
|
|
EventSource::CheckHealthOfRequestCallback(nsIRequest *aRequestCallback)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
// check if we have been closed or if the request has been canceled
|
|
|
|
// or if we have been frozen
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED || !mHttpChannel ||
|
2011-05-23 01:30:07 +04: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 19:02:17 +04:00
|
|
|
EventSource::ParseCharacter(char16_t aChr)
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
if (mReadyState == CLOSED) {
|
2011-05-23 01:30:07 +04: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 12:17:05 +04:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|