2015-05-03 22:32:37 +03: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 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"
|
2016-01-30 20:05:36 +03:00
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
2014-05-09 11:07:30 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2016-10-24 05:11:04 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2018-03-16 01:05:17 +03:00
|
|
|
#include "mozilla/dom/WorkerRef.h"
|
2016-10-24 05:11:04 +03:00
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2016-06-07 23:10:18 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2017-10-26 12:40:12 +03:00
|
|
|
#include "nsIThreadRetargetableStreamListener.h"
|
2011-05-23 01:30:07 +04:00
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIAuthPrompt.h"
|
|
|
|
#include "nsIAuthPrompt2.h"
|
2015-09-29 07:31:56 +03:00
|
|
|
#include "nsIInputStream.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2011-05-23 01:30:07 +04: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"
|
2016-10-24 05:11:04 +03:00
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
2011-05-23 01:30:07 +04:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#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"
|
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"
|
2017-04-27 13:27:03 +03:00
|
|
|
#include "mozilla/Encoding.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
|
|
|
|
2017-10-17 11:02:58 +03:00
|
|
|
static LazyLogModule gEventSourceLog("EventSource");
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
#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
|
|
|
|
|
|
|
// 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)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
class EventSourceImpl final : public nsIObserver,
|
|
|
|
public nsIStreamListener,
|
|
|
|
public nsIChannelEventSink,
|
|
|
|
public nsIInterfaceRequestor,
|
|
|
|
public nsSupportsWeakReference,
|
|
|
|
public nsIEventTarget,
|
|
|
|
public nsIThreadRetargetableStreamListener {
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
2017-05-20 06:58:07 +03:00
|
|
|
NS_DECL_NSIEVENTTARGET_FULL
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
|
|
|
|
|
|
|
|
explicit EventSourceImpl(EventSource* aEventSource);
|
|
|
|
|
|
|
|
enum { CONNECTING = 0U, OPEN = 1U, CLOSED = 2U };
|
|
|
|
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
void Init(nsIPrincipal* aPrincipal, const nsAString& aURL, ErrorResult& aRv);
|
|
|
|
|
|
|
|
nsresult GetBaseURI(nsIURI** aBaseURI);
|
|
|
|
|
|
|
|
void SetupHttpChannel();
|
|
|
|
nsresult SetupReferrerPolicy();
|
|
|
|
nsresult InitChannelAndRequestEventSource();
|
|
|
|
nsresult ResetConnection();
|
|
|
|
void ResetDecoder();
|
|
|
|
nsresult SetReconnectionTimeout();
|
|
|
|
|
|
|
|
void AnnounceConnection();
|
|
|
|
void DispatchAllMessageEvents();
|
|
|
|
nsresult RestartConnection();
|
|
|
|
void ReestablishConnection();
|
|
|
|
void DispatchFailConnection();
|
|
|
|
void FailConnection();
|
|
|
|
|
|
|
|
nsresult Thaw();
|
|
|
|
nsresult Freeze();
|
|
|
|
|
|
|
|
static void TimerCallback(nsITimer* aTimer, void* aClosure);
|
|
|
|
|
|
|
|
nsresult PrintErrorOnConsole(const char* aBundleURI, const char* aError,
|
|
|
|
const char16_t** aFormatStrings,
|
|
|
|
uint32_t aFormatStringsLen);
|
|
|
|
nsresult ConsoleError();
|
|
|
|
|
|
|
|
static nsresult StreamReaderFunc(nsIInputStream* aInputStream, void* aClosure,
|
|
|
|
const char* aFromRawSegment,
|
|
|
|
uint32_t aToOffset, uint32_t aCount,
|
|
|
|
uint32_t* aWriteCount);
|
|
|
|
void ParseSegment(const char* aBuffer, uint32_t aLength);
|
|
|
|
nsresult SetFieldAndClear();
|
2017-01-17 09:25:44 +03:00
|
|
|
void ClearFields();
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult ResetEvent();
|
|
|
|
nsresult DispatchCurrentMessageEvent();
|
|
|
|
nsresult ParseCharacter(char16_t aChr);
|
|
|
|
nsresult CheckHealthOfRequestCallback(nsIRequest* aRequestCallback);
|
|
|
|
nsresult OnRedirectVerifyCallback(nsresult result);
|
|
|
|
nsresult ParseURL(const nsAString& aURL);
|
|
|
|
nsresult AddWindowObservers();
|
|
|
|
void RemoveWindowObservers();
|
|
|
|
|
|
|
|
void CloseInternal();
|
|
|
|
void CleanupOnMainThread();
|
|
|
|
void AddRefObject();
|
|
|
|
void ReleaseObject();
|
|
|
|
|
2018-03-16 01:05:17 +03:00
|
|
|
bool CreateWorkerRef(WorkerPrivate* aWorkerPrivate);
|
|
|
|
void ReleaseWorkerRef();
|
2016-10-24 05:11:04 +03:00
|
|
|
|
|
|
|
void AssertIsOnTargetThread() const {
|
2018-10-08 13:36:59 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(IsTargetThread());
|
2016-10-24 05:11:04 +03:00
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2018-08-24 13:12:54 +03:00
|
|
|
bool IsTargetThread() const { return NS_GetCurrentThread() == mTargetThread; }
|
2013-08-02 05:29:05 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
uint16_t ReadyState() {
|
2017-01-24 05:26:30 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2016-10-24 05:11:04 +03:00
|
|
|
if (mEventSource) {
|
|
|
|
return mEventSource->mReadyState;
|
2012-02-16 20:45:25 +04:00
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
// EventSourceImpl keeps EventSource alive. If mEventSource is null, it
|
|
|
|
// means that the EventSource has been closed.
|
|
|
|
return CLOSED;
|
2012-01-26 19:55:30 +04:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void SetReadyState(uint16_t aReadyState) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(mEventSource);
|
|
|
|
MOZ_ASSERT(!mIsShutDown);
|
2016-10-24 05:11:04 +03:00
|
|
|
mEventSource->mReadyState = aReadyState;
|
|
|
|
}
|
2012-01-26 19:55:30 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
bool IsFrozen() {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mFrozen;
|
|
|
|
}
|
2012-01-26 19:55:30 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void SetFrozen(bool aFrozen) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mFrozen = aFrozen;
|
|
|
|
}
|
2012-01-26 19:55:30 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
bool IsClosed() { return ReadyState() == CLOSED; }
|
|
|
|
|
2017-01-24 05:26:30 +03:00
|
|
|
void ShutDown() {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
MOZ_ASSERT(!mIsShutDown);
|
|
|
|
mIsShutDown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsShutDown() {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mIsShutDown;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
RefPtr<EventSource> mEventSource;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple state machine used to manage the event-source's line buffer
|
|
|
|
*
|
|
|
|
* PARSE_STATE_OFF -> PARSE_STATE_BEGIN_OF_STREAM
|
|
|
|
*
|
2017-04-27 13:27:03 +03:00
|
|
|
* PARSE_STATE_BEGIN_OF_STREAM -> PARSE_STATE_CR_CHAR |
|
2016-10-24 05:11:04 +03:00
|
|
|
* PARSE_STATE_BEGIN_OF_LINE |
|
|
|
|
* PARSE_STATE_COMMENT |
|
|
|
|
* PARSE_STATE_FIELD_NAME
|
|
|
|
*
|
|
|
|
* PARSE_STATE_CR_CHAR -> PARSE_STATE_CR_CHAR |
|
|
|
|
* PARSE_STATE_COMMENT |
|
|
|
|
* PARSE_STATE_FIELD_NAME |
|
|
|
|
* PARSE_STATE_BEGIN_OF_LINE
|
|
|
|
*
|
|
|
|
* PARSE_STATE_COMMENT -> PARSE_STATE_CR_CHAR |
|
|
|
|
* PARSE_STATE_BEGIN_OF_LINE
|
|
|
|
*
|
|
|
|
* PARSE_STATE_FIELD_NAME -> PARSE_STATE_CR_CHAR |
|
|
|
|
* PARSE_STATE_BEGIN_OF_LINE |
|
|
|
|
* PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE
|
|
|
|
*
|
|
|
|
* PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE -> PARSE_STATE_FIELD_VALUE |
|
|
|
|
* PARSE_STATE_CR_CHAR |
|
|
|
|
* PARSE_STATE_BEGIN_OF_LINE
|
|
|
|
*
|
|
|
|
* PARSE_STATE_FIELD_VALUE -> PARSE_STATE_CR_CHAR |
|
|
|
|
* PARSE_STATE_BEGIN_OF_LINE
|
|
|
|
*
|
|
|
|
* PARSE_STATE_BEGIN_OF_LINE -> PARSE_STATE_CR_CHAR |
|
|
|
|
* PARSE_STATE_COMMENT |
|
|
|
|
* PARSE_STATE_FIELD_NAME |
|
|
|
|
* PARSE_STATE_BEGIN_OF_LINE
|
|
|
|
*
|
|
|
|
* Whenever the parser find an empty line or the end-of-file
|
|
|
|
* it dispatches the stacked event.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
enum ParserStatus {
|
|
|
|
PARSE_STATE_OFF = 0,
|
|
|
|
PARSE_STATE_BEGIN_OF_STREAM,
|
|
|
|
PARSE_STATE_CR_CHAR,
|
|
|
|
PARSE_STATE_COMMENT,
|
|
|
|
PARSE_STATE_FIELD_NAME,
|
|
|
|
PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE,
|
|
|
|
PARSE_STATE_FIELD_VALUE,
|
|
|
|
PARSE_STATE_BEGIN_OF_LINE
|
|
|
|
};
|
|
|
|
|
|
|
|
// Connection related data members. Should only be accessed on main thread.
|
|
|
|
nsCOMPtr<nsIURI> mSrc;
|
|
|
|
uint32_t mReconnectionTime; // in ms
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
|
|
|
nsString mOrigin;
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
nsCOMPtr<nsIHttpChannel> mHttpChannel;
|
|
|
|
|
|
|
|
struct Message {
|
|
|
|
nsString mEventName;
|
|
|
|
nsString mLastEventID;
|
|
|
|
nsString mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Message related data members. May be set / initialized when initializing
|
|
|
|
// EventSourceImpl on target thread but should only be used on target thread.
|
|
|
|
nsString mLastEventID;
|
2017-01-17 09:25:44 +03:00
|
|
|
UniquePtr<Message> mCurrentMessage;
|
2016-10-24 05:11:04 +03:00
|
|
|
nsDeque mMessagesToDispatch;
|
|
|
|
ParserStatus mStatus;
|
2017-04-27 13:27:03 +03:00
|
|
|
mozilla::UniquePtr<mozilla::Decoder> mUnicodeDecoder;
|
2016-10-24 05:11:04 +03:00
|
|
|
nsString mLastFieldName;
|
|
|
|
nsString mLastFieldValue;
|
|
|
|
|
|
|
|
// EventSourceImpl internal states.
|
2018-03-16 01:05:17 +03:00
|
|
|
// WorkerRef to keep the worker alive. (accessed on worker thread only)
|
|
|
|
RefPtr<ThreadSafeWorkerRef> mWorkerRef;
|
2016-10-24 05:11:04 +03:00
|
|
|
// This mutex protects mFrozen and mEventSource->mReadyState that are used in
|
|
|
|
// different threads.
|
|
|
|
mozilla::Mutex mMutex;
|
|
|
|
// Whether the window is frozen. May be set on main thread and read on target
|
|
|
|
// thread. Use mMutex to protect it before accessing it.
|
|
|
|
bool mFrozen;
|
|
|
|
// There are some messages are going to be dispatched when thaw.
|
|
|
|
bool mGoingToDispatchAllMessages;
|
|
|
|
// Whether the EventSource is run on main thread.
|
|
|
|
bool mIsMainThread;
|
|
|
|
// Whether the EventSourceImpl is going to be destroyed.
|
2017-01-24 05:26:30 +03:00
|
|
|
bool mIsShutDown;
|
2016-10-24 05:11:04 +03:00
|
|
|
|
|
|
|
// Event Source owner information:
|
|
|
|
// - the script file name
|
|
|
|
// - source code line number and column number where the Event Source object
|
|
|
|
// was constructed.
|
|
|
|
// - the ID of the inner window where the script lives. Note that this may not
|
|
|
|
// be the same as the Event Source owner window.
|
|
|
|
// These attributes are used for error reporting. Should only be accessed on
|
|
|
|
// target thread
|
|
|
|
nsString mScriptFile;
|
|
|
|
uint32_t mScriptLine;
|
|
|
|
uint32_t mScriptColumn;
|
|
|
|
uint64_t mInnerWindowID;
|
|
|
|
|
|
|
|
private:
|
2018-08-24 13:12:54 +03:00
|
|
|
// Pointer to the target thread for checking whether we are
|
|
|
|
// on the target thread. This is intentionally a non-owning
|
|
|
|
// pointer in order not to affect the thread destruction
|
|
|
|
// sequence. This pointer must only be compared for equality
|
|
|
|
// and must not be dereferenced.
|
|
|
|
nsIThread* mTargetThread;
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
// prevent bad usage
|
|
|
|
EventSourceImpl(const EventSourceImpl& x) = delete;
|
|
|
|
EventSourceImpl& operator=(const EventSourceImpl& x) = delete;
|
|
|
|
~EventSourceImpl() {
|
|
|
|
if (IsClosed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If we threw during Init we never called Close
|
|
|
|
SetReadyState(CLOSED);
|
|
|
|
CloseInternal();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(EventSourceImpl, nsIObserver, nsIStreamListener,
|
|
|
|
nsIRequestObserver, nsIChannelEventSink,
|
|
|
|
nsIInterfaceRequestor, nsISupportsWeakReference,
|
|
|
|
nsIEventTarget, nsIThreadRetargetableStreamListener)
|
|
|
|
|
|
|
|
EventSourceImpl::EventSourceImpl(EventSource* aEventSource)
|
|
|
|
: mEventSource(aEventSource),
|
|
|
|
mReconnectionTime(0),
|
|
|
|
mStatus(PARSE_STATE_OFF),
|
|
|
|
mMutex("EventSourceImpl::mMutex"),
|
|
|
|
mFrozen(false),
|
|
|
|
mGoingToDispatchAllMessages(false),
|
|
|
|
mIsMainThread(NS_IsMainThread()),
|
2017-01-24 05:26:30 +03:00
|
|
|
mIsShutDown(false),
|
2016-10-24 05:11:04 +03:00
|
|
|
mScriptLine(0),
|
|
|
|
mScriptColumn(0),
|
|
|
|
mInnerWindowID(0),
|
2018-08-24 13:12:54 +03:00
|
|
|
mTargetThread(NS_GetCurrentThread()) {
|
2016-10-24 05:11:04 +03:00
|
|
|
MOZ_ASSERT(mEventSource);
|
|
|
|
if (!mIsMainThread) {
|
|
|
|
mEventSource->mIsMainThread = false;
|
|
|
|
}
|
|
|
|
SetReadyState(CONNECTING);
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
class CleanupRunnable final : public WorkerMainThreadRunnable {
|
|
|
|
public:
|
|
|
|
explicit CleanupRunnable(EventSourceImpl* aEventSourceImpl)
|
2018-03-16 01:05:17 +03:00
|
|
|
: WorkerMainThreadRunnable(GetCurrentThreadWorkerPrivate(),
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_LITERAL_CSTRING("EventSource :: Cleanup")),
|
|
|
|
mImpl(aEventSourceImpl) {
|
2018-03-16 01:05:17 +03:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-10-24 05:11:04 +03:00
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
bool MainThreadRun() override {
|
|
|
|
mImpl->CleanupOnMainThread();
|
|
|
|
return true;
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
protected:
|
|
|
|
// Raw pointer because this runnable is sync.
|
|
|
|
EventSourceImpl* mImpl;
|
|
|
|
};
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::Close() {
|
|
|
|
if (IsClosed()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-15 13:16:04 +03:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
SetReadyState(CLOSED);
|
2018-03-15 13:16:04 +03:00
|
|
|
CloseInternal();
|
2012-03-13 04:56:07 +04:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::CloseInternal() {
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
MOZ_ASSERT(IsClosed());
|
2017-01-24 05:26:30 +03:00
|
|
|
if (IsShutDown()) {
|
2012-12-22 12:17:05 +04:00
|
|
|
return;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
2017-01-24 05:26:30 +03:00
|
|
|
// Invoke CleanupOnMainThread before cleaning any members. It will call
|
|
|
|
// ShutDown, which is supposed to be called before cleaning any members.
|
2016-10-24 05:11:04 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
CleanupOnMainThread();
|
|
|
|
} else {
|
|
|
|
ErrorResult rv;
|
|
|
|
// run CleanupOnMainThread synchronously on main thread since it touches
|
|
|
|
// observers and members only can be accessed on main thread.
|
|
|
|
RefPtr<CleanupRunnable> runnable = new CleanupRunnable(this);
|
|
|
|
runnable->Dispatch(Killing, rv);
|
|
|
|
MOZ_ASSERT(!rv.Failed());
|
2018-03-16 01:05:17 +03:00
|
|
|
ReleaseWorkerRef();
|
2016-10-24 05:11:04 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 05:26:30 +03:00
|
|
|
while (mMessagesToDispatch.GetSize() != 0) {
|
|
|
|
delete static_cast<Message*>(mMessagesToDispatch.PopFront());
|
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
SetFrozen(false);
|
|
|
|
ResetDecoder();
|
|
|
|
mUnicodeDecoder = nullptr;
|
|
|
|
// UpdateDontKeepAlive() can release the object. Don't access to any members
|
|
|
|
// after it.
|
|
|
|
mEventSource->UpdateDontKeepAlive();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventSourceImpl::CleanupOnMainThread() {
|
|
|
|
AssertIsOnMainThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(IsClosed());
|
|
|
|
|
|
|
|
// Call ShutDown before cleaning any members.
|
|
|
|
ShutDown();
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
if (mIsMainThread) {
|
|
|
|
RemoveWindowObservers();
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
mTimer = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ResetConnection();
|
2016-10-24 05:11:04 +03:00
|
|
|
mPrincipal = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
mSrc = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
class InitRunnable final : public WorkerMainThreadRunnable {
|
|
|
|
public:
|
2018-03-16 01:05:17 +03:00
|
|
|
InitRunnable(WorkerPrivate* aWorkerPrivate, EventSourceImpl* aEventSourceImpl,
|
|
|
|
const nsAString& aURL)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate,
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_LITERAL_CSTRING("EventSource :: Init")),
|
|
|
|
mImpl(aEventSourceImpl),
|
|
|
|
mURL(aURL),
|
2018-06-16 17:21:46 +03:00
|
|
|
mRv(NS_ERROR_NOT_INITIALIZED) {
|
2018-03-16 01:05:17 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
bool MainThreadRun() override {
|
|
|
|
// Get principal from worker's owner document or from worker.
|
2018-03-16 01:05:17 +03:00
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
2016-10-24 05:11:04 +03:00
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
nsPIDOMWindowInner* window = wp->GetWindow();
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = window ? window->GetExtantDoc() : nullptr;
|
2016-10-24 05:11:04 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
doc ? doc->NodePrincipal() : wp->GetPrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
mRv = NS_ERROR_FAILURE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ErrorResult rv;
|
|
|
|
mImpl->Init(principal, mURL, rv);
|
|
|
|
mRv = rv.StealNSResult();
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-22 12:17:05 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult ErrorCode() const { return mRv; }
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2018-03-16 01:05:17 +03:00
|
|
|
private:
|
2016-10-24 05:11:04 +03:00
|
|
|
// Raw pointer because this runnable is sync.
|
|
|
|
EventSourceImpl* mImpl;
|
|
|
|
const nsAString& mURL;
|
|
|
|
nsresult mRv;
|
|
|
|
};
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2018-03-16 01:05:17 +03:00
|
|
|
class ConnectRunnable final : public WorkerMainThreadRunnable {
|
|
|
|
public:
|
|
|
|
explicit ConnectRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
EventSourceImpl* aEventSourceImpl)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("EventSource :: Connect")),
|
|
|
|
mImpl(aEventSourceImpl) {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainThreadRun() override {
|
|
|
|
mImpl->InitChannelAndRequestEventSource();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<EventSourceImpl> mImpl;
|
|
|
|
};
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::ParseURL(const nsAString& aURL) {
|
|
|
|
AssertIsOnMainThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!IsShutDown());
|
2011-05-23 01:30:07 +04:00
|
|
|
// get the src
|
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
2016-04-21 01:04:37 +03:00
|
|
|
nsresult 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);
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsAutoString origin;
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(srcURI, origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
rv = srcURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mEventSource->mOriginalURL = NS_ConvertUTF8toUTF16(spec);
|
|
|
|
mSrc = srcURI;
|
|
|
|
mOrigin = origin;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult EventSourceImpl::AddWindowObservers() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mIsMainThread);
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!IsShutDown());
|
2011-05-23 01:30:07 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
NS_ENSURE_STATE(os);
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult 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);
|
2016-10-24 05:11:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::RemoveWindowObservers() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mIsMainThread);
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(IsClosed());
|
2016-10-24 05:11:04 +03: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);
|
|
|
|
}
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::Init(nsIPrincipal* aPrincipal, const nsAString& aURL,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aPrincipal);
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(ReadyState() == CONNECTING);
|
2016-10-24 05:11:04 +03:00
|
|
|
mPrincipal = aPrincipal;
|
|
|
|
aRv = ParseURL(aURL);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The conditional here is historical and not necessarily sane.
|
|
|
|
if (JSContext* cx = nsContentUtils::GetCurrentJSContext()) {
|
|
|
|
nsJSUtils::GetCallingLocation(cx, mScriptFile, &mScriptLine,
|
|
|
|
&mScriptColumn);
|
|
|
|
mInnerWindowID = nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(cx);
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
if (mIsMainThread) {
|
|
|
|
// we observe when the window freezes and thaws
|
|
|
|
aRv = AddWindowObservers();
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
mUnicodeDecoder = UTF_8_ENCODING->NewDecoderWithBOMRemoval();
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-10-24 05:11:04 +03:00
|
|
|
// EventSourceImpl::nsIObserver
|
2011-05-23 01:30:07 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-10-24 05:11:04 +03:00
|
|
|
EventSourceImpl::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
if (IsClosed()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aSubject);
|
2016-10-24 05:11:04 +03:00
|
|
|
if (!mEventSource->GetOwner() || window != mEventSource->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();
|
2016-10-24 05:11:04 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv), "Freeze() failed");
|
2011-05-23 01:30:07 +04:00
|
|
|
} else if (strcmp(aTopic, DOM_WINDOW_THAWED_TOPIC) == 0) {
|
|
|
|
rv = Thaw();
|
2016-10-24 05:11:04 +03:00
|
|
|
MOZ_ASSERT(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-10-24 05:11:04 +03:00
|
|
|
// EventSourceImpl::nsIStreamListener
|
2011-05-23 01:30:07 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:04 +03:00
|
|
|
EventSourceImpl::OnStartRequest(nsIRequest* aRequest) {
|
2016-10-24 05:11:04 +03:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
if (IsClosed()) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
if (!mIsMainThread) {
|
|
|
|
// Try to retarget to worker thread, otherwise fall back to main thread.
|
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(httpChannel);
|
|
|
|
if (rr) {
|
|
|
|
rv = rr->RetargetDeliveryTo(this);
|
2017-03-20 11:53:41 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
NS_WARNING("Retargeting failed");
|
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
}
|
|
|
|
}
|
2017-06-12 22:34:10 +03:00
|
|
|
rv = Dispatch(NewRunnableMethod("dom::EventSourceImpl::AnnounceConnection",
|
|
|
|
this, &EventSourceImpl::AnnounceConnection),
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_DISPATCH_NORMAL);
|
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.
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::StreamReaderFunc(nsIInputStream* aInputStream,
|
|
|
|
void* aClosure,
|
|
|
|
const char* aFromRawSegment,
|
|
|
|
uint32_t aToOffset, uint32_t aCount,
|
|
|
|
uint32_t* aWriteCount) {
|
|
|
|
EventSourceImpl* thisObject = static_cast<EventSourceImpl*>(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;
|
|
|
|
}
|
2017-01-26 15:39:11 +03:00
|
|
|
thisObject->AssertIsOnTargetThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!thisObject->IsShutDown());
|
2016-10-24 05:11:04 +03:00
|
|
|
thisObject->ParseSegment((const char*)aFromRawSegment, aCount);
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::ParseSegment(const char* aBuffer, uint32_t aLength) {
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
if (IsClosed()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
char16_t buffer[1024];
|
|
|
|
auto dst = MakeSpan(buffer);
|
|
|
|
auto src = AsBytes(MakeSpan(aBuffer, aLength));
|
|
|
|
// XXX EOF handling is https://bugzilla.mozilla.org/show_bug.cgi?id=1369018
|
|
|
|
for (;;) {
|
|
|
|
uint32_t result;
|
|
|
|
size_t read;
|
|
|
|
size_t written;
|
|
|
|
bool hadErrors;
|
|
|
|
Tie(result, read, written, hadErrors) =
|
|
|
|
mUnicodeDecoder->DecodeToUTF16(src, dst, false);
|
|
|
|
Unused << hadErrors;
|
|
|
|
for (auto c : dst.To(written)) {
|
|
|
|
nsresult rv = ParseCharacter(c);
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
if (result == kInputEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
src = src.From(read);
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:42:27 +03:00
|
|
|
EventSourceImpl::OnDataAvailable(nsIRequest* aRequest,
|
2016-10-24 05:11:04 +03:00
|
|
|
nsIInputStream* aInputStream, uint64_t aOffset,
|
|
|
|
uint32_t aCount) {
|
2017-01-20 04:41:05 +03:00
|
|
|
AssertIsOnTargetThread();
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aInputStream);
|
2016-10-24 05:11:04 +03:00
|
|
|
if (IsClosed()) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
nsresult rv = CheckHealthOfRequestCallback(aRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t totalRead;
|
2017-01-20 04:41:05 +03:00
|
|
|
return aInputStream->ReadSegments(EventSourceImpl::StreamReaderFunc, this,
|
2011-05-23 01:30:07 +04:00
|
|
|
aCount, &totalRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:31 +03:00
|
|
|
EventSourceImpl::OnStopRequest(nsIRequest* aRequest,
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult aStatusCode) {
|
|
|
|
AssertIsOnMainThread();
|
2012-02-16 20:45:25 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
if (IsClosed()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
MOZ_ASSERT(mSrc);
|
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
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
rv =
|
|
|
|
Dispatch(NewRunnableMethod("dom::EventSourceImpl::ReestablishConnection",
|
|
|
|
this, &EventSourceImpl::ReestablishConnection),
|
|
|
|
NS_DISPATCH_NORMAL);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-10-24 05:11:04 +03:00
|
|
|
// EventSourceImpl::nsIChannelEventSink
|
2011-05-23 01:30:07 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-10-24 05:11:04 +03:00
|
|
|
EventSourceImpl::AsyncOnChannelRedirect(
|
|
|
|
nsIChannel* aOldChannel, nsIChannel* aNewChannel, uint32_t aFlags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* aCallback) {
|
|
|
|
AssertIsOnMainThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
if (IsClosed()) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2018-10-02 00:38:01 +03:00
|
|
|
nsCOMPtr<nsIRequest> aOldRequest = aOldChannel;
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aOldRequest, "Redirect from a null request?");
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
nsresult rv = CheckHealthOfRequestCallback(aOldRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aNewChannel, "Redirect without a channel?");
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> newURI;
|
|
|
|
rv = NS_GetFinalChannelURI(aNewChannel, getter_AddRefs(newURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-08-05 06:06:19 +03:00
|
|
|
bool isValidScheme =
|
|
|
|
(NS_SUCCEEDED(newURI->SchemeIs("http", &isValidScheme)) &&
|
|
|
|
isValidScheme) ||
|
|
|
|
(NS_SUCCEEDED(newURI->SchemeIs("https", &isValidScheme)) &&
|
|
|
|
isValidScheme);
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
rv = mEventSource->CheckInnerWindowCorrectness();
|
2015-08-05 06:06:19 +03:00
|
|
|
if (NS_FAILED(rv) || !isValidScheme) {
|
|
|
|
DispatchFailConnection();
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// update our channel
|
|
|
|
|
2015-10-08 23:41:38 +03:00
|
|
|
mHttpChannel = do_QueryInterface(aNewChannel);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_STATE(mHttpChannel);
|
|
|
|
|
2016-04-21 01:04:37 +03:00
|
|
|
SetupHttpChannel();
|
|
|
|
// The HTTP impl already copies over the referrer and referrer policy on
|
|
|
|
// redirects, so we don't need to SetupReferrerPolicy().
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2015-10-08 23:41:38 +03:00
|
|
|
if ((aFlags & nsIChannelEventSink::REDIRECT_PERMANENT) != 0) {
|
2011-05-23 01:30:07 +04:00
|
|
|
rv = NS_GetFinalChannelURI(mHttpChannel, getter_AddRefs(mSrc));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:41:38 +03:00
|
|
|
aCallback->OnRedirectVerifyCallback(NS_OK);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-10-24 05:11:04 +03:00
|
|
|
// EventSourceImpl::nsIInterfaceRequestor
|
2011-05-23 01:30:07 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-10-24 05:11:04 +03:00
|
|
|
EventSourceImpl::GetInterface(const nsIID& aIID, void** aResult) {
|
|
|
|
AssertIsOnMainThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
|
|
|
|
if (IsClosed()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-05-23 01:30:07 +04: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))) {
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult rv = mEventSource->CheckInnerWindowCorrectness();
|
2011-05-23 01:30:07 +04:00
|
|
|
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 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window;
|
2016-10-24 05:11:04 +03:00
|
|
|
if (mEventSource->GetOwner()) {
|
|
|
|
window = mEventSource->GetOwner()->GetOuterWindow();
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return wwatch->GetPrompt(window, aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
EventSourceImpl::IsOnCurrentThread(bool* aResult) {
|
|
|
|
*aResult = IsTargetThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-22 21:26:39 +03:00
|
|
|
NS_IMETHODIMP_(bool)
|
|
|
|
EventSourceImpl::IsOnCurrentThreadInfallible() { return IsTargetThread(); }
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::GetBaseURI(nsIURI** aBaseURI) {
|
|
|
|
AssertIsOnMainThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!IsShutDown());
|
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()
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = mEventSource->GetDocumentIfCurrent();
|
2011-05-23 01:30:07 +04:00
|
|
|
if (doc) {
|
|
|
|
baseURI = doc->GetBaseURI();
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise we get from the doc's principal
|
|
|
|
if (!baseURI) {
|
2016-04-21 01:04:37 +03:00
|
|
|
nsresult 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;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::SetupHttpChannel() {
|
|
|
|
AssertIsOnMainThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!IsShutDown());
|
2017-10-17 11:02:58 +03:00
|
|
|
nsresult rv = mHttpChannel->SetRequestMethod(NS_LITERAL_CSTRING("GET"));
|
2016-12-20 06:49:32 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
/* set the http request headers */
|
|
|
|
|
2016-12-20 06:49:32 +03:00
|
|
|
rv = mHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_LITERAL_CSTRING(TEXT_EVENT_STREAM),
|
|
|
|
false);
|
2016-12-20 06:49:32 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
// LOAD_BYPASS_CACHE already adds the Cache-Control: no-cache header
|
|
|
|
|
2017-10-17 11:02:58 +03:00
|
|
|
if (mLastEventID.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_ConvertUTF16toUTF8 eventId(mLastEventID);
|
|
|
|
rv = mHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Last-Event-ID"),
|
|
|
|
eventId, false);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_LOG(gEventSourceLog, LogLevel::Warning,
|
|
|
|
("SetupHttpChannel. rv=%x (%s)", uint32_t(rv), eventId.get()));
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
2017-10-17 11:02:58 +03:00
|
|
|
#endif
|
|
|
|
Unused << rv;
|
2016-04-21 01:04:37 +03:00
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::SetupReferrerPolicy() {
|
|
|
|
AssertIsOnMainThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!IsShutDown());
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = mEventSource->GetDocumentIfCurrent();
|
2016-04-21 01:04:37 +03:00
|
|
|
if (doc) {
|
|
|
|
nsresult rv = mHttpChannel->SetReferrerWithPolicy(doc->GetDocumentURI(),
|
|
|
|
doc->GetReferrerPolicy());
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::InitChannelAndRequestEventSource() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
if (IsClosed()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2015-08-05 06:06:19 +03:00
|
|
|
bool isValidScheme =
|
|
|
|
(NS_SUCCEEDED(mSrc->SchemeIs("http", &isValidScheme)) && isValidScheme) ||
|
|
|
|
(NS_SUCCEEDED(mSrc->SchemeIs("https", &isValidScheme)) && isValidScheme);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult rv = mEventSource->CheckInnerWindowCorrectness();
|
2015-08-05 06:06:19 +03:00
|
|
|
if (NS_FAILED(rv) || !isValidScheme) {
|
2011-05-23 01:30:07 +04:00
|
|
|
DispatchFailConnection();
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2018-02-09 23:18:58 +03:00
|
|
|
// The html spec requires we use fetch cache mode of "no-store". This
|
|
|
|
// maps to LOAD_BYPASS_CACHE and LOAD_INHIBIT_CACHING in necko.
|
2011-05-23 01:30:07 +04:00
|
|
|
nsLoadFlags loadFlags;
|
2018-02-09 23:18:58 +03:00
|
|
|
loadFlags = nsIRequest::LOAD_BACKGROUND | nsIRequest::LOAD_BYPASS_CACHE |
|
|
|
|
nsIRequest::INHIBIT_CACHING;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = mEventSource->GetDocumentIfCurrent();
|
2014-09-21 20:36:25 +04:00
|
|
|
|
2015-08-05 06:06:19 +03:00
|
|
|
nsSecurityFlags securityFlags = nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
if (mEventSource->mWithCredentials) {
|
2015-12-07 02:33:15 +03:00
|
|
|
securityFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
|
2015-08-05 06:06:19 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-10-24 05:11:04 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = doc->GetDocumentLoadGroup();
|
2014-09-21 20:36:25 +04:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel), mSrc, doc, securityFlags,
|
2015-08-06 21:17:24 +03:00
|
|
|
nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE,
|
2018-01-24 19:17:31 +03:00
|
|
|
nullptr, // aPerformanceStorage
|
2016-10-24 05:11:04 +03:00
|
|
|
loadGroup,
|
2014-09-21 20:36:25 +04:00
|
|
|
nullptr, // aCallbacks
|
|
|
|
loadFlags); // aLoadFlags
|
|
|
|
} else {
|
|
|
|
// otherwise use the principal
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel), mSrc, mPrincipal, securityFlags,
|
2015-08-06 21:17:24 +03:00
|
|
|
nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE,
|
2018-01-24 19:17:31 +03:00
|
|
|
nullptr, // aPerformanceStorage
|
2016-10-24 05:11:04 +03:00
|
|
|
nullptr, // loadGroup
|
2014-09-21 20:36:25 +04:00
|
|
|
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);
|
|
|
|
|
2016-04-21 01:04:37 +03:00
|
|
|
SetupHttpChannel();
|
|
|
|
rv = SetupReferrerPolicy();
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-08 23:41:38 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> notificationCallbacks;
|
|
|
|
mHttpChannel->GetNotificationCallbacks(
|
|
|
|
getter_AddRefs(notificationCallbacks));
|
|
|
|
MOZ_ASSERT(!notificationCallbacks);
|
2012-02-24 23:37:30 +04:00
|
|
|
}
|
2015-10-08 23:41:38 +03:00
|
|
|
#endif
|
|
|
|
mHttpChannel->SetNotificationCallbacks(this);
|
2012-02-24 23:37:30 +04:00
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
// Start reading from the channel
|
2019-02-12 19:08:25 +03:00
|
|
|
rv = mHttpChannel->AsyncOpen(this);
|
2015-08-05 06:06:19 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
DispatchFailConnection();
|
|
|
|
return rv;
|
2012-02-16 20:45:25 +04:00
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
// Create the connection. Ask EventSource to hold reference until Close is
|
|
|
|
// called or network error is received.
|
|
|
|
mEventSource->UpdateMustKeepAlive();
|
2012-02-16 20:45:25 +04:00
|
|
|
return rv;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::AnnounceConnection() {
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
if (ReadyState() != 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.
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
SetReadyState(OPEN);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult rv = mEventSource->CheckInnerWindowCorrectness();
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
rv = mEventSource->CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("open"));
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_WARNING("Failed to dispatch the error event!!!");
|
2011-05-23 01:30:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::ResetConnection() {
|
|
|
|
AssertIsOnMainThread();
|
2011-05-23 01:30:07 +04:00
|
|
|
if (mHttpChannel) {
|
|
|
|
mHttpChannel->Cancel(NS_ERROR_ABORT);
|
2016-10-24 05:11:04 +03:00
|
|
|
mHttpChannel = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::ResetDecoder() {
|
|
|
|
AssertIsOnTargetThread();
|
2011-05-23 01:30:07 +04:00
|
|
|
if (mUnicodeDecoder) {
|
2017-04-27 13:27:03 +03:00
|
|
|
UTF_8_ENCODING->NewDecoderWithBOMRemovalInto(*mUnicodeDecoder);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
mStatus = PARSE_STATE_OFF;
|
|
|
|
ClearFields();
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
class CallRestartConnection final : public WorkerMainThreadRunnable {
|
|
|
|
public:
|
|
|
|
explicit CallRestartConnection(EventSourceImpl* aEventSourceImpl)
|
|
|
|
: WorkerMainThreadRunnable(
|
2018-03-16 01:05:17 +03:00
|
|
|
aEventSourceImpl->mWorkerRef->Private(),
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_LITERAL_CSTRING("EventSource :: RestartConnection")),
|
|
|
|
mImpl(aEventSourceImpl) {
|
2018-03-16 01:05:17 +03:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-10-24 05:11:04 +03:00
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
bool MainThreadRun() override {
|
|
|
|
mImpl->RestartConnection();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Raw pointer because this runnable is sync.
|
|
|
|
EventSourceImpl* mImpl;
|
|
|
|
};
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::RestartConnection() {
|
|
|
|
AssertIsOnMainThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
if (IsClosed()) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult rv = ResetConnection();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = SetReconnectionTimeout();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::ReestablishConnection() {
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
if (IsClosed()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult rv;
|
|
|
|
if (mIsMainThread) {
|
|
|
|
rv = RestartConnection();
|
|
|
|
} else {
|
|
|
|
RefPtr<CallRestartConnection> runnable = new CallRestartConnection(this);
|
|
|
|
ErrorResult result;
|
2018-07-12 20:33:41 +03:00
|
|
|
runnable->Dispatch(Canceling, result);
|
2016-10-24 05:11:04 +03:00
|
|
|
MOZ_ASSERT(!result.Failed());
|
|
|
|
rv = result.StealNSResult();
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
rv = mEventSource->CheckInnerWindowCorrectness();
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
SetReadyState(CONNECTING);
|
|
|
|
ResetDecoder();
|
|
|
|
rv = mEventSource->CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("error"));
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_WARNING("Failed to dispatch the error event!!!");
|
2011-05-23 01:30:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::SetReconnectionTimeout() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
if (IsClosed()) {
|
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) {
|
2017-10-16 09:15:40 +03:00
|
|
|
mTimer = NS_NewTimer();
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_STATE(mTimer);
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
nsresult rv = mTimer->InitWithNamedFuncCallback(
|
|
|
|
TimerCallback, this, mReconnectionTime, nsITimer::TYPE_ONE_SHOT,
|
|
|
|
"dom::EventSourceImpl::SetReconnectionTimeout");
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::PrintErrorOnConsole(const char* aBundleURI,
|
2017-07-12 08:13:37 +03:00
|
|
|
const char* aError,
|
2016-10-24 05:11:04 +03:00
|
|
|
const char16_t** aFormatStrings,
|
|
|
|
uint32_t aFormatStringsLen) {
|
|
|
|
AssertIsOnMainThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!IsShutDown());
|
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
|
2017-08-04 07:40:52 +03:00
|
|
|
nsAutoString message;
|
2011-05-23 01:30:07 +04:00
|
|
|
if (aFormatStrings) {
|
|
|
|
rv = strBundle->FormatStringFromName(aError, aFormatStrings,
|
2017-08-04 07:40:52 +03:00
|
|
|
aFormatStringsLen, message);
|
2011-05-23 01:30:07 +04:00
|
|
|
} else {
|
2017-08-04 07:40:52 +03:00
|
|
|
rv = strBundle->GetStringFromName(aError, message);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-10 03:29:12 +04:00
|
|
|
rv = errObj->InitWithWindowID(
|
2015-09-02 03:56:03 +03:00
|
|
|
message, mScriptFile, EmptyString(), mScriptLine, mScriptColumn,
|
2011-12-22 01:51:29 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::ConsoleError() {
|
|
|
|
AssertIsOnMainThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!IsShutDown());
|
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);
|
2016-10-24 05:11:04 +03:00
|
|
|
const char16_t* formatStrings[] = {specUTF16.get()};
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
if (ReadyState() == CONNECTING) {
|
2011-05-23 01:30:07 +04:00
|
|
|
rv = PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
2017-07-12 08:13:37 +03:00
|
|
|
"connectionFailure", formatStrings,
|
2011-10-11 09:50:08 +04:00
|
|
|
ArrayLength(formatStrings));
|
2011-05-23 01:30:07 +04:00
|
|
|
} else {
|
|
|
|
rv = PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
2017-07-12 08:13:37 +03:00
|
|
|
"netInterrupt", formatStrings,
|
2011-10-11 09:50:08 +04:00
|
|
|
ArrayLength(formatStrings));
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::DispatchFailConnection() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
if (IsClosed()) {
|
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");
|
|
|
|
}
|
2017-06-12 22:34:10 +03:00
|
|
|
rv = Dispatch(NewRunnableMethod("dom::EventSourceImpl::FailConnection", this,
|
|
|
|
&EventSourceImpl::FailConnection),
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_DISPATCH_NORMAL);
|
2018-04-22 10:20:45 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// if the worker is shutting down, the dispatching of normal WorkerRunnables
|
|
|
|
// fails.
|
|
|
|
return;
|
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::FailConnection() {
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
if (IsClosed()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return;
|
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
// Must change state to closed before firing event to content.
|
|
|
|
SetReadyState(CLOSED);
|
|
|
|
// 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.
|
|
|
|
nsresult rv = mEventSource->CheckInnerWindowCorrectness();
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mEventSource->CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("error"));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the error event!!!");
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
// Call CloseInternal in the end of function because it may release
|
|
|
|
// EventSourceImpl.
|
|
|
|
CloseInternal();
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::TimerCallback(nsITimer* aTimer, void* aClosure) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
RefPtr<EventSourceImpl> thisObject = static_cast<EventSourceImpl*>(aClosure);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
if (thisObject->IsClosed()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(!thisObject->mHttpChannel, "the channel hasn't been cancelled!!");
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
if (!thisObject->IsFrozen()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult rv = thisObject->InitChannelAndRequestEventSource();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("thisObject->InitChannelAndRequestEventSource() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::Thaw() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
if (IsClosed() || !IsFrozen()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
MOZ_ASSERT(!mHttpChannel, "the connection hasn't been closed!!!");
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
SetFrozen(false);
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (!mGoingToDispatchAllMessages && mMessagesToDispatch.GetSize() > 0) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod("dom::EventSourceImpl::DispatchAllMessageEvents",
|
|
|
|
this, &EventSourceImpl::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
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
rv = Dispatch(event.forget(), NS_DISPATCH_NORMAL);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = InitChannelAndRequestEventSource();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::Freeze() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
if (IsClosed() || IsFrozen()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
MOZ_ASSERT(!mHttpChannel, "the connection hasn't been closed!!!");
|
|
|
|
SetFrozen(true);
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::DispatchCurrentMessageEvent() {
|
|
|
|
AssertIsOnTargetThread();
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!IsShutDown());
|
2018-05-30 22:15:35 +03:00
|
|
|
UniquePtr<Message> message(std::move(mCurrentMessage));
|
2011-05-23 01:30:07 +04:00
|
|
|
ClearFields();
|
|
|
|
|
2017-01-17 09:25:44 +03:00
|
|
|
if (!message || message->mData.IsEmpty()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// removes the trailing LF from mData
|
2016-10-24 05:11:04 +03:00
|
|
|
MOZ_ASSERT(message->mData.CharAt(message->mData.Length() - 1) == LF_CHAR,
|
|
|
|
"Invalid trailing character! LF was expected instead.");
|
2011-05-23 01:30:07 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-01-17 09:25:44 +03:00
|
|
|
mMessagesToDispatch.Push(message.release());
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
if (!mGoingToDispatchAllMessages) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod("dom::EventSourceImpl::DispatchAllMessageEvents",
|
|
|
|
this, &EventSourceImpl::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
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
return Dispatch(event.forget(), NS_DISPATCH_NORMAL);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::DispatchAllMessageEvents() {
|
|
|
|
AssertIsOnTargetThread();
|
2017-02-03 05:27:55 +03:00
|
|
|
mGoingToDispatchAllMessages = false;
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
if (IsClosed() || IsFrozen()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult rv = mEventSource->CheckInnerWindowCorrectness();
|
2011-05-23 01:30:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-19 11:21:14 +04:00
|
|
|
AutoJSAPI jsapi;
|
2016-10-24 05:11:04 +03:00
|
|
|
if (mIsMainThread) {
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mEventSource->GetOwner()))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-16 01:05:17 +03:00
|
|
|
MOZ_ASSERT(mWorkerRef);
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mWorkerRef->Private()->GlobalScope()))) {
|
2016-10-24 05:11:04 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-05-09 11:07:30 +04:00
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2011-05-23 22:53:31 +04:00
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
while (mMessagesToDispatch.GetSize() > 0) {
|
2017-01-17 09:25:44 +03:00
|
|
|
UniquePtr<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
|
|
|
|
2015-06-30 21:20:56 +03:00
|
|
|
jsData.setString(jsString);
|
2011-05-23 22:53:31 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
RefPtr<MessageEvent> event =
|
|
|
|
new MessageEvent(mEventSource, nullptr, nullptr);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2018-06-25 19:23:50 +03:00
|
|
|
event->InitMessageEvent(nullptr, message->mEventName, CanBubble::eNo,
|
|
|
|
Cancelable::eNo, jsData, mOrigin,
|
|
|
|
message->mLastEventID, nullptr,
|
2016-10-25 08:48:05 +03:00
|
|
|
Sequence<OwningNonNull<MessagePort>>());
|
2015-08-12 14:39:31 +03:00
|
|
|
event->SetTrusted(true);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
IgnoredErrorResult err;
|
|
|
|
mEventSource->DispatchEvent(*event, err);
|
|
|
|
if (err.Failed()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_WARNING("Failed to dispatch the message event!!!");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-22 23:09:13 +04:00
|
|
|
|
|
|
|
mLastEventID.Assign(message->mLastEventID);
|
2016-10-24 05:11:04 +03:00
|
|
|
if (IsClosed() || IsFrozen()) {
|
|
|
|
return;
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::ClearFields() {
|
|
|
|
AssertIsOnTargetThread();
|
2017-01-17 09:25:44 +03:00
|
|
|
mCurrentMessage = nullptr;
|
2011-05-23 01:30:07 +04:00
|
|
|
mLastFieldName.Truncate();
|
|
|
|
mLastFieldValue.Truncate();
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::SetFieldAndClear() {
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!IsShutDown());
|
2016-10-24 05:11:04 +03:00
|
|
|
AssertIsOnTargetThread();
|
2011-05-23 01:30:07 +04:00
|
|
|
if (mLastFieldName.IsEmpty()) {
|
|
|
|
mLastFieldValue.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-01-17 09:25:44 +03:00
|
|
|
if (!mCurrentMessage) {
|
|
|
|
mCurrentMessage = MakeUnique<Message>();
|
|
|
|
}
|
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);
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
// with no case folding performed
|
|
|
|
switch (first_char) {
|
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.
|
2017-01-17 09:25:44 +03:00
|
|
|
mCurrentMessage->mData.Append(mLastFieldValue);
|
|
|
|
mCurrentMessage->mData.Append(LF_CHAR);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
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")) {
|
2017-01-17 09:25:44 +03:00
|
|
|
mCurrentMessage->mEventName.Assign(mLastFieldValue);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
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")) {
|
2017-01-17 09:25:44 +03:00
|
|
|
mCurrentMessage->mLastEventID.Assign(mLastFieldValue);
|
2011-05-23 01:30:07 +04:00
|
|
|
}
|
|
|
|
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")) {
|
2016-10-24 05:11:04 +03:00
|
|
|
uint32_t newValue = 0;
|
2012-08-22 19:56:38 +04:00
|
|
|
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;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
newValue = newValue * 10 + (((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;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::CheckHealthOfRequestCallback(
|
|
|
|
nsIRequest* aRequestCallback) {
|
|
|
|
// This function could be run on target thread if http channel support
|
|
|
|
// nsIThreadRetargetableRequest. otherwise, it's run on main thread.
|
|
|
|
|
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
|
2016-10-24 05:11:04 +03:00
|
|
|
if (IsClosed() || IsFrozen() || !mHttpChannel) {
|
2011-05-23 01:30:07 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult EventSourceImpl::ParseCharacter(char16_t aChr) {
|
|
|
|
AssertIsOnTargetThread();
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
if (IsClosed()) {
|
2011-05-23 01:30:07 +04:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
switch (mStatus) {
|
2011-05-23 01:30:07 +04:00
|
|
|
case PARSE_STATE_OFF:
|
|
|
|
NS_ERROR("Invalid state");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARSE_STATE_BEGIN_OF_STREAM:
|
|
|
|
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;
|
2017-10-20 05:22:55 +03:00
|
|
|
} else if (aChr != 0) {
|
|
|
|
// Avoid appending the null char to the field value.
|
2011-05-23 01:30:07 +04:00
|
|
|
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;
|
2017-10-20 05:22:55 +03:00
|
|
|
} else if (aChr != 0) {
|
|
|
|
// Avoid appending the null char to the field name.
|
2011-05-23 01:30:07 +04:00
|
|
|
mLastFieldName += aChr;
|
|
|
|
mStatus = PARSE_STATE_FIELD_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-12-22 12:17:05 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void EventSourceImpl::AddRefObject() { AddRef(); }
|
|
|
|
|
|
|
|
void EventSourceImpl::ReleaseObject() { Release(); }
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class WorkerRunnableDispatcher final : public WorkerRunnable {
|
|
|
|
RefPtr<EventSourceImpl> mEventSourceImpl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
WorkerRunnableDispatcher(EventSourceImpl* aImpl,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
|
|
|
already_AddRefed<nsIRunnable> aEvent)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
|
|
|
mEventSourceImpl(aImpl),
|
2018-05-30 22:15:35 +03:00
|
|
|
mEvent(std::move(aEvent)) {}
|
2016-10-24 05:11:04 +03:00
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
return !NS_FAILED(mEvent->Run());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aRunResult) override {}
|
|
|
|
|
|
|
|
bool PreDispatch(WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
// We don't call WorkerRunnable::PreDispatch because it would assert the
|
|
|
|
// wrong thing about which thread we're on. We're on whichever thread the
|
|
|
|
// channel implementation is running on (probably the main thread or
|
|
|
|
// transport thread).
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {
|
|
|
|
// We don't call WorkerRunnable::PostDispatch because it would assert the
|
|
|
|
// wrong thing about which thread we're on. We're on whichever thread the
|
|
|
|
// channel implementation is running on (probably the main thread or
|
|
|
|
// transport thread).
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIRunnable> mEvent;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-03-16 01:05:17 +03:00
|
|
|
bool EventSourceImpl::CreateWorkerRef(WorkerPrivate* aWorkerPrivate) {
|
2017-01-24 05:26:30 +03:00
|
|
|
MOZ_ASSERT(!IsShutDown());
|
2018-03-16 01:05:17 +03:00
|
|
|
MOZ_ASSERT(!mWorkerRef);
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
RefPtr<EventSourceImpl> self = this;
|
|
|
|
RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
|
|
|
|
aWorkerPrivate, "EventSource", [self]() { self->Close(); });
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!workerRef)) {
|
2016-10-24 05:11:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-16 01:05:17 +03:00
|
|
|
|
|
|
|
mWorkerRef = new ThreadSafeWorkerRef(workerRef);
|
2016-10-24 05:11:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-16 01:05:17 +03:00
|
|
|
void EventSourceImpl::ReleaseWorkerRef() {
|
2016-10-24 05:11:04 +03:00
|
|
|
MOZ_ASSERT(IsClosed());
|
2018-03-16 01:05:17 +03:00
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
|
|
|
mWorkerRef = nullptr;
|
2016-10-24 05:11:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// EventSourceImpl::nsIEventTarget
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
EventSourceImpl::DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags) {
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
return Dispatch(event.forget(), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
EventSourceImpl::Dispatch(already_AddRefed<nsIRunnable> aEvent,
|
|
|
|
uint32_t aFlags) {
|
|
|
|
nsCOMPtr<nsIRunnable> event_ref(aEvent);
|
|
|
|
if (mIsMainThread) {
|
|
|
|
return NS_DispatchToMainThread(event_ref.forget());
|
|
|
|
}
|
2017-01-24 05:26:30 +03:00
|
|
|
|
|
|
|
if (IsShutDown()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-03-16 01:05:17 +03:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
// If the target is a worker, we have to use a custom WorkerRunnableDispatcher
|
|
|
|
// runnable.
|
|
|
|
RefPtr<WorkerRunnableDispatcher> event = new WorkerRunnableDispatcher(
|
2018-03-16 01:05:17 +03:00
|
|
|
this, mWorkerRef->Private(), event_ref.forget());
|
2016-10-24 05:11:04 +03:00
|
|
|
|
|
|
|
if (!event->Dispatch()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
EventSourceImpl::DelayedDispatch(already_AddRefed<nsIRunnable> aEvent,
|
|
|
|
uint32_t aDelayMs) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// EventSourceImpl::nsIThreadRetargetableStreamListener
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
EventSourceImpl::CheckListenerChain() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be on the main thread!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// EventSource
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
EventSource::EventSource(nsPIDOMWindowInner* aOwnerWindow,
|
|
|
|
bool aWithCredentials)
|
|
|
|
: DOMEventTargetHelper(aOwnerWindow),
|
|
|
|
mWithCredentials(aWithCredentials),
|
|
|
|
mIsMainThread(true),
|
|
|
|
mKeepingAlive(false) {
|
|
|
|
mImpl = new EventSourceImpl(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventSource::~EventSource() {}
|
|
|
|
|
|
|
|
nsresult EventSource::CreateAndDispatchSimpleEvent(const nsAString& aName) {
|
|
|
|
RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
|
|
|
event->InitEvent(aName, false, false);
|
|
|
|
event->SetTrusted(true);
|
2018-04-05 20:42:41 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
DispatchEvent(*event, rv);
|
|
|
|
return rv.StealNSResult();
|
2016-10-24 05:11:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<EventSource> EventSource::Constructor(
|
|
|
|
const GlobalObject& aGlobal, const nsAString& aURL,
|
|
|
|
const EventSourceInit& aEventSourceInitDict, ErrorResult& aRv) {
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> ownerWindow =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
|
2017-11-22 02:07:45 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread() || ownerWindow);
|
2016-10-24 05:11:04 +03:00
|
|
|
|
|
|
|
RefPtr<EventSource> eventSource =
|
|
|
|
new EventSource(ownerWindow, aEventSourceInitDict.mWithCredentials);
|
|
|
|
RefPtr<EventSourceImpl> eventSourceImp = eventSource->mImpl;
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
// Get principal from document and init EventSourceImpl
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!scriptPrincipal) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = scriptPrincipal->GetPrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
eventSourceImp->Init(principal, aURL, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-03-16 01:05:17 +03:00
|
|
|
|
|
|
|
eventSourceImp->InitChannelAndRequestEventSource();
|
|
|
|
return eventSource.forget();
|
2016-10-24 05:11:04 +03:00
|
|
|
}
|
2018-03-16 01:05:17 +03:00
|
|
|
|
|
|
|
// Worker side.
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
RefPtr<InitRunnable> initRunnable =
|
|
|
|
new InitRunnable(workerPrivate, eventSourceImp, aURL);
|
2018-07-12 20:33:41 +03:00
|
|
|
initRunnable->Dispatch(Canceling, aRv);
|
2016-10-24 05:11:04 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-03-16 01:05:17 +03:00
|
|
|
|
|
|
|
aRv = initRunnable->ErrorCode();
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In workers we have to keep the worker alive using a WorkerRef in order
|
|
|
|
// to dispatch messages correctly.
|
|
|
|
if (!eventSourceImp->CreateWorkerRef(workerPrivate)) {
|
|
|
|
// The worker is already shutting down. Let's return an already closed
|
|
|
|
// object, but marked as Connecting.
|
|
|
|
eventSource->Close();
|
|
|
|
|
|
|
|
// EventSourceImpl must be released before returning the object, otherwise
|
|
|
|
// it will set EventSource to a CLOSED state in its DTOR.
|
|
|
|
eventSourceImp = nullptr;
|
|
|
|
|
|
|
|
eventSource->mReadyState = EventSourceImpl::CONNECTING;
|
|
|
|
return eventSource.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's connect to the server.
|
|
|
|
RefPtr<ConnectRunnable> connectRunnable =
|
|
|
|
new ConnectRunnable(workerPrivate, eventSourceImp);
|
2018-07-12 20:33:41 +03:00
|
|
|
connectRunnable->Dispatch(Canceling, aRv);
|
2018-03-16 01:05:17 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
return eventSource.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsWrapperCache
|
|
|
|
JSObject* EventSource::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return EventSource_Binding::Wrap(aCx, this, aGivenProto);
|
2016-10-24 05:11:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventSource::Close() {
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
if (mImpl) {
|
|
|
|
mImpl->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventSource::UpdateMustKeepAlive() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
if (mKeepingAlive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mKeepingAlive = true;
|
|
|
|
mImpl->AddRefObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventSource::UpdateDontKeepAlive() {
|
|
|
|
// Here we could not have mImpl.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
|
|
|
if (mKeepingAlive) {
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
mKeepingAlive = false;
|
|
|
|
mImpl->mEventSource = nullptr;
|
|
|
|
mImpl->ReleaseObject();
|
|
|
|
}
|
|
|
|
mImpl = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// EventSource::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(EventSource)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(EventSource,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(EventSource,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
if (tmp->mImpl) {
|
|
|
|
tmp->mImpl->Close();
|
|
|
|
MOZ_ASSERT(!tmp->mImpl);
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2017-01-25 04:40:11 +03:00
|
|
|
bool EventSource::IsCertainlyAliveForCC() const { return mKeepingAlive; }
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(EventSource)
|
2016-10-24 05:11:04 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(EventSource, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(EventSource, DOMEventTargetHelper)
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|