michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/EventSource.h" michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/DebugOnly.h" michael@0: #include "mozilla/DOMEventTargetHelper.h" michael@0: #include "mozilla/dom/EventSourceBinding.h" michael@0: #include "mozilla/dom/MessageEvent.h" michael@0: michael@0: #include "js/OldDebugAPI.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsMimeTypes.h" michael@0: #include "nsIPromptFactory.h" michael@0: #include "nsIWindowWatcher.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsContentPolicyUtils.h" michael@0: #include "nsIStringBundle.h" michael@0: #include "nsIConsoleService.h" michael@0: #include "nsIObserverService.h" michael@0: #include "nsIScriptObjectPrincipal.h" michael@0: #include "nsJSUtils.h" michael@0: #include "nsIAsyncVerifyRedirectCallback.h" michael@0: #include "nsIScriptError.h" michael@0: #include "mozilla/dom/EncodingUtils.h" michael@0: #include "nsIChannelPolicy.h" michael@0: #include "nsIContentSecurityPolicy.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsCxPusher.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include "xpcpublic.h" michael@0: #include "nsCrossSiteListenerProxy.h" michael@0: #include "nsWrapperCacheInlines.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsError.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: #define REPLACEMENT_CHAR (char16_t)0xFFFD michael@0: #define BOM_CHAR (char16_t)0xFEFF michael@0: #define SPACE_CHAR (char16_t)0x0020 michael@0: #define CR_CHAR (char16_t)0x000D michael@0: #define LF_CHAR (char16_t)0x000A michael@0: #define COLON_CHAR (char16_t)0x003A michael@0: michael@0: #define DEFAULT_BUFFER_SIZE 4096 michael@0: michael@0: // Reconnection time related values in milliseconds. The default one is equal michael@0: // to the default value of the pref dom.server-events.default-reconnection-time michael@0: #define MIN_RECONNECTION_TIME_VALUE 500 michael@0: #define DEFAULT_RECONNECTION_TIME_VALUE 5000 michael@0: #define MAX_RECONNECTION_TIME_VALUE PR_IntervalToMilliseconds(DELAY_INTERVAL_LIMIT) michael@0: michael@0: EventSource::EventSource(nsPIDOMWindow* aOwnerWindow) : michael@0: DOMEventTargetHelper(aOwnerWindow), michael@0: mStatus(PARSE_STATE_OFF), michael@0: mFrozen(false), michael@0: mErrorLoadOnRedirect(false), michael@0: mGoingToDispatchAllMessages(false), michael@0: mWithCredentials(false), michael@0: mWaitingForOnStopRequest(false), michael@0: mInterrupted(false), michael@0: mLastConvertionResult(NS_OK), michael@0: mReadyState(CONNECTING), michael@0: mScriptLine(0), michael@0: mInnerWindowID(0) michael@0: { michael@0: } michael@0: michael@0: EventSource::~EventSource() michael@0: { michael@0: Close(); michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // EventSource::nsISupports michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(EventSource) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(EventSource) michael@0: bool isBlack = tmp->IsBlack(); michael@0: if (isBlack || tmp->mWaitingForOnStopRequest) { michael@0: if (tmp->mListenerManager) { michael@0: tmp->mListenerManager->MarkForCC(); michael@0: } michael@0: if (!isBlack && tmp->PreservingWrapper()) { michael@0: // This marks the wrapper black. michael@0: tmp->GetWrapper(); michael@0: } michael@0: return true; michael@0: } michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(EventSource) michael@0: return tmp->IsBlack(); michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(EventSource) michael@0: return tmp->IsBlack(); michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(EventSource, michael@0: DOMEventTargetHelper) michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(EventSource, michael@0: DOMEventTargetHelper) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSrc) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNotificationCallbacks) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoadGroup) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mChannelEventSink) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mHttpChannel) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTimer) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mUnicodeDecoder) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(EventSource, michael@0: DOMEventTargetHelper) michael@0: tmp->Close(); michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(EventSource) michael@0: NS_INTERFACE_MAP_ENTRY(nsIObserver) michael@0: NS_INTERFACE_MAP_ENTRY(nsIRequestObserver) michael@0: NS_INTERFACE_MAP_ENTRY(nsIStreamListener) michael@0: NS_INTERFACE_MAP_ENTRY(nsIChannelEventSink) michael@0: NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) michael@0: NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(EventSource, DOMEventTargetHelper) michael@0: NS_IMPL_RELEASE_INHERITED(EventSource, DOMEventTargetHelper) michael@0: michael@0: void michael@0: EventSource::DisconnectFromOwner() michael@0: { michael@0: DOMEventTargetHelper::DisconnectFromOwner(); michael@0: Close(); michael@0: } michael@0: michael@0: void michael@0: EventSource::Close() michael@0: { michael@0: if (mReadyState == CLOSED) { michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr os = mozilla::services::GetObserverService(); michael@0: if (os) { michael@0: os->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC); michael@0: os->RemoveObserver(this, DOM_WINDOW_FROZEN_TOPIC); michael@0: os->RemoveObserver(this, DOM_WINDOW_THAWED_TOPIC); michael@0: } michael@0: michael@0: if (mTimer) { michael@0: mTimer->Cancel(); michael@0: mTimer = nullptr; michael@0: } michael@0: michael@0: ResetConnection(); michael@0: michael@0: ClearFields(); michael@0: michael@0: while (mMessagesToDispatch.GetSize() != 0) { michael@0: delete static_cast(mMessagesToDispatch.PopFront()); michael@0: } michael@0: michael@0: mSrc = nullptr; michael@0: mFrozen = false; michael@0: michael@0: mUnicodeDecoder = nullptr; michael@0: michael@0: mReadyState = CLOSED; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::Init(nsISupports* aOwner, michael@0: const nsAString& aURL, michael@0: bool aWithCredentials) michael@0: { michael@0: if (mReadyState != CONNECTING || !PrefEnabled()) { michael@0: return NS_ERROR_DOM_SECURITY_ERR; michael@0: } michael@0: michael@0: nsCOMPtr sgo = do_QueryInterface(aOwner); michael@0: NS_ENSURE_STATE(sgo); michael@0: nsCOMPtr scriptContext = sgo->GetContext(); michael@0: NS_ENSURE_STATE(scriptContext); michael@0: michael@0: nsCOMPtr scriptPrincipal = michael@0: do_QueryInterface(aOwner); michael@0: NS_ENSURE_STATE(scriptPrincipal); michael@0: nsCOMPtr principal = scriptPrincipal->GetPrincipal(); michael@0: NS_ENSURE_STATE(principal); michael@0: michael@0: mPrincipal = principal; michael@0: mWithCredentials = aWithCredentials; michael@0: michael@0: // The conditional here is historical and not necessarily sane. michael@0: if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) { michael@0: const char *filename; michael@0: if (nsJSUtils::GetCallingLocation(cx, &filename, &mScriptLine)) { michael@0: mScriptFile.AssignASCII(filename); michael@0: } michael@0: michael@0: mInnerWindowID = nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(cx); michael@0: } michael@0: michael@0: // Get the load group for the page. When requesting we'll add ourselves to it. michael@0: // This way any pending requests will be automatically aborted if the user michael@0: // leaves the page. michael@0: nsresult rv; michael@0: nsIScriptContext* sc = GetContextForEventHandlers(&rv); michael@0: if (sc) { michael@0: nsCOMPtr doc = michael@0: nsContentUtils::GetDocumentFromScriptContext(sc); michael@0: if (doc) { michael@0: mLoadGroup = doc->GetDocumentLoadGroup(); michael@0: } michael@0: } michael@0: michael@0: // get the src michael@0: nsCOMPtr baseURI; michael@0: rv = GetBaseURI(getter_AddRefs(baseURI)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr srcURI; michael@0: rv = NS_NewURI(getter_AddRefs(srcURI), aURL, nullptr, baseURI); michael@0: NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR); michael@0: michael@0: // we observe when the window freezes and thaws michael@0: nsCOMPtr os = mozilla::services::GetObserverService(); michael@0: NS_ENSURE_STATE(os); michael@0: michael@0: rv = os->AddObserver(this, DOM_WINDOW_DESTROYED_TOPIC, true); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = os->AddObserver(this, DOM_WINDOW_FROZEN_TOPIC, true); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = os->AddObserver(this, DOM_WINDOW_THAWED_TOPIC, true); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsAutoString origin; michael@0: rv = nsContentUtils::GetUTFOrigin(srcURI, origin); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsAutoCString spec; michael@0: rv = srcURI->GetSpec(spec); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mOriginalURL = NS_ConvertUTF8toUTF16(spec); michael@0: mSrc = srcURI; michael@0: mOrigin = origin; michael@0: michael@0: mReconnectionTime = michael@0: Preferences::GetInt("dom.server-events.default-reconnection-time", michael@0: DEFAULT_RECONNECTION_TIME_VALUE); michael@0: michael@0: mUnicodeDecoder = EncodingUtils::DecoderForEncoding("UTF-8"); michael@0: michael@0: // the constructor should throw a SYNTAX_ERROR only if it fails resolving the michael@0: // url parameter, so we don't care about the InitChannelAndRequestEventSource michael@0: // result. michael@0: InitChannelAndRequestEventSource(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* virtual */ JSObject* michael@0: EventSource::WrapObject(JSContext* aCx) michael@0: { michael@0: return EventSourceBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: /* static */ already_AddRefed michael@0: EventSource::Constructor(const GlobalObject& aGlobal, michael@0: const nsAString& aURL, michael@0: const EventSourceInit& aEventSourceInitDict, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr ownerWindow = michael@0: do_QueryInterface(aGlobal.GetAsSupports()); michael@0: if (!ownerWindow) { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: return nullptr; michael@0: } michael@0: MOZ_ASSERT(ownerWindow->IsInnerWindow()); michael@0: michael@0: nsRefPtr eventSource = new EventSource(ownerWindow); michael@0: aRv = eventSource->Init(aGlobal.GetAsSupports(), aURL, michael@0: aEventSourceInitDict.mWithCredentials); michael@0: return eventSource.forget(); michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // EventSource::nsIObserver michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMETHODIMP michael@0: EventSource::Observe(nsISupports* aSubject, michael@0: const char* aTopic, michael@0: const char16_t* aData) michael@0: { michael@0: if (mReadyState == CLOSED) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsCOMPtr window = do_QueryInterface(aSubject); michael@0: if (!GetOwner() || window != GetOwner()) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: DebugOnly rv; michael@0: if (strcmp(aTopic, DOM_WINDOW_FROZEN_TOPIC) == 0) { michael@0: rv = Freeze(); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "Freeze() failed"); michael@0: } else if (strcmp(aTopic, DOM_WINDOW_THAWED_TOPIC) == 0) { michael@0: rv = Thaw(); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "Thaw() failed"); michael@0: } else if (strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC) == 0) { michael@0: Close(); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // EventSource::nsIStreamListener michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMETHODIMP michael@0: EventSource::OnStartRequest(nsIRequest *aRequest, michael@0: nsISupports *ctxt) michael@0: { michael@0: nsresult rv = CheckHealthOfRequestCallback(aRequest); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr httpChannel = do_QueryInterface(aRequest, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool requestSucceeded; michael@0: rv = httpChannel->GetRequestSucceeded(&requestSucceeded); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsAutoCString contentType; michael@0: rv = httpChannel->GetContentType(contentType); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsresult status; michael@0: aRequest->GetStatus(&status); michael@0: michael@0: if (NS_FAILED(status) || !requestSucceeded || michael@0: !contentType.EqualsLiteral(TEXT_EVENT_STREAM)) { michael@0: DispatchFailConnection(); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: uint32_t httpStatus; michael@0: rv = httpChannel->GetResponseStatus(&httpStatus); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (httpStatus != 200) { michael@0: mInterrupted = true; michael@0: DispatchFailConnection(); michael@0: return NS_ERROR_ABORT; michael@0: } michael@0: michael@0: nsCOMPtr principal = mPrincipal; michael@0: if (nsContentUtils::IsSystemPrincipal(principal)) { michael@0: // Don't give this channel the system principal. michael@0: principal = do_CreateInstance("@mozilla.org/nullprincipal;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: rv = httpChannel->SetOwner(principal); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr event = michael@0: NS_NewRunnableMethod(this, &EventSource::AnnounceConnection); michael@0: NS_ENSURE_STATE(event); michael@0: michael@0: rv = NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mStatus = PARSE_STATE_BEGIN_OF_STREAM; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // this method parses the characters as they become available instead of michael@0: // buffering them. michael@0: NS_METHOD michael@0: EventSource::StreamReaderFunc(nsIInputStream *aInputStream, michael@0: void *aClosure, michael@0: const char *aFromRawSegment, michael@0: uint32_t aToOffset, michael@0: uint32_t aCount, michael@0: uint32_t *aWriteCount) michael@0: { michael@0: EventSource* thisObject = static_cast(aClosure); michael@0: if (!thisObject || !aWriteCount) { michael@0: NS_WARNING("EventSource cannot read from stream: no aClosure or aWriteCount"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: *aWriteCount = 0; michael@0: michael@0: int32_t srcCount, outCount; michael@0: char16_t out[2]; michael@0: nsresult rv; michael@0: michael@0: const char *p = aFromRawSegment, michael@0: *end = aFromRawSegment + aCount; michael@0: michael@0: do { michael@0: srcCount = aCount - (p - aFromRawSegment); michael@0: outCount = 2; michael@0: michael@0: thisObject->mLastConvertionResult = michael@0: thisObject->mUnicodeDecoder->Convert(p, &srcCount, out, &outCount); michael@0: MOZ_ASSERT(thisObject->mLastConvertionResult != NS_ERROR_ILLEGAL_INPUT); michael@0: michael@0: for (int32_t i = 0; i < outCount; ++i) { michael@0: rv = thisObject->ParseCharacter(out[i]); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: p = p + srcCount; michael@0: } while (p < end && michael@0: thisObject->mLastConvertionResult != NS_PARTIAL_MORE_INPUT && michael@0: thisObject->mLastConvertionResult != NS_OK); michael@0: michael@0: *aWriteCount = aCount; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: EventSource::OnDataAvailable(nsIRequest *aRequest, michael@0: nsISupports *aContext, michael@0: nsIInputStream *aInputStream, michael@0: uint64_t aOffset, michael@0: uint32_t aCount) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aInputStream); michael@0: michael@0: nsresult rv = CheckHealthOfRequestCallback(aRequest); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: uint32_t totalRead; michael@0: return aInputStream->ReadSegments(EventSource::StreamReaderFunc, this, michael@0: aCount, &totalRead); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: EventSource::OnStopRequest(nsIRequest *aRequest, michael@0: nsISupports *aContext, michael@0: nsresult aStatusCode) michael@0: { michael@0: mWaitingForOnStopRequest = false; michael@0: michael@0: if (mReadyState == CLOSED) { michael@0: return NS_ERROR_ABORT; michael@0: } michael@0: michael@0: if (NS_FAILED(aStatusCode)) { michael@0: DispatchFailConnection(); michael@0: return aStatusCode; michael@0: } michael@0: michael@0: nsresult rv; michael@0: nsresult healthOfRequestResult = CheckHealthOfRequestCallback(aRequest); michael@0: if (NS_SUCCEEDED(healthOfRequestResult) && michael@0: mLastConvertionResult == NS_PARTIAL_MORE_INPUT) { michael@0: // we had an incomplete UTF8 char at the end of the stream michael@0: rv = ParseCharacter(REPLACEMENT_CHAR); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: ClearFields(); michael@0: michael@0: nsCOMPtr event = michael@0: NS_NewRunnableMethod(this, &EventSource::ReestablishConnection); michael@0: NS_ENSURE_STATE(event); michael@0: michael@0: rv = NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return healthOfRequestResult; michael@0: } michael@0: michael@0: /** michael@0: * Simple helper class that just forwards the redirect callback back michael@0: * to the EventSource. michael@0: */ michael@0: class AsyncVerifyRedirectCallbackFwr MOZ_FINAL : public nsIAsyncVerifyRedirectCallback michael@0: { michael@0: public: michael@0: AsyncVerifyRedirectCallbackFwr(EventSource* aEventsource) michael@0: : mEventSource(aEventsource) michael@0: { michael@0: } michael@0: michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: NS_DECL_CYCLE_COLLECTION_CLASS(AsyncVerifyRedirectCallbackFwr) michael@0: michael@0: // nsIAsyncVerifyRedirectCallback implementation michael@0: NS_IMETHOD OnRedirectVerifyCallback(nsresult aResult) michael@0: { michael@0: nsresult rv = mEventSource->OnRedirectVerifyCallback(aResult); michael@0: if (NS_FAILED(rv)) { michael@0: mEventSource->mErrorLoadOnRedirect = true; michael@0: mEventSource->DispatchFailConnection(); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: nsRefPtr mEventSource; michael@0: }; michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION(AsyncVerifyRedirectCallbackFwr, mEventSource) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AsyncVerifyRedirectCallbackFwr) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_ENTRY(nsIAsyncVerifyRedirectCallback) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(AsyncVerifyRedirectCallbackFwr) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(AsyncVerifyRedirectCallbackFwr) michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // EventSource::nsIChannelEventSink michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMETHODIMP michael@0: EventSource::AsyncOnChannelRedirect(nsIChannel *aOldChannel, michael@0: nsIChannel *aNewChannel, michael@0: uint32_t aFlags, michael@0: nsIAsyncVerifyRedirectCallback *aCallback) michael@0: { michael@0: nsCOMPtr aOldRequest = do_QueryInterface(aOldChannel); michael@0: NS_PRECONDITION(aOldRequest, "Redirect from a null request?"); michael@0: michael@0: nsresult rv = CheckHealthOfRequestCallback(aOldRequest); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: NS_PRECONDITION(aNewChannel, "Redirect without a channel?"); michael@0: michael@0: nsCOMPtr newURI; michael@0: rv = NS_GetFinalChannelURI(aNewChannel, getter_AddRefs(newURI)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (!CheckCanRequestSrc(newURI)) { michael@0: DispatchFailConnection(); michael@0: return NS_ERROR_DOM_SECURITY_ERR; michael@0: } michael@0: michael@0: // Prepare to receive callback michael@0: mRedirectFlags = aFlags; michael@0: mRedirectCallback = aCallback; michael@0: mNewRedirectChannel = aNewChannel; michael@0: michael@0: if (mChannelEventSink) { michael@0: nsRefPtr fwd = michael@0: new AsyncVerifyRedirectCallbackFwr(this); michael@0: michael@0: rv = mChannelEventSink->AsyncOnChannelRedirect(aOldChannel, michael@0: aNewChannel, michael@0: aFlags, fwd); michael@0: if (NS_FAILED(rv)) { michael@0: mRedirectCallback = nullptr; michael@0: mNewRedirectChannel = nullptr; michael@0: mErrorLoadOnRedirect = true; michael@0: DispatchFailConnection(); michael@0: } michael@0: return rv; michael@0: } michael@0: OnRedirectVerifyCallback(NS_OK); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::OnRedirectVerifyCallback(nsresult aResult) michael@0: { michael@0: NS_ABORT_IF_FALSE(mRedirectCallback, "mRedirectCallback not set in callback"); michael@0: NS_ABORT_IF_FALSE(mNewRedirectChannel, michael@0: "mNewRedirectChannel not set in callback"); michael@0: michael@0: NS_ENSURE_SUCCESS(aResult, aResult); michael@0: michael@0: // update our channel michael@0: michael@0: mHttpChannel = do_QueryInterface(mNewRedirectChannel); michael@0: NS_ENSURE_STATE(mHttpChannel); michael@0: michael@0: nsresult rv = SetupHttpChannel(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if ((mRedirectFlags & nsIChannelEventSink::REDIRECT_PERMANENT) != 0) { michael@0: rv = NS_GetFinalChannelURI(mHttpChannel, getter_AddRefs(mSrc)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: mNewRedirectChannel = nullptr; michael@0: michael@0: mRedirectCallback->OnRedirectVerifyCallback(aResult); michael@0: mRedirectCallback = nullptr; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // EventSource::nsIInterfaceRequestor michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMETHODIMP michael@0: EventSource::GetInterface(const nsIID & aIID, michael@0: void **aResult) michael@0: { michael@0: // Make sure to return ourselves for the channel event sink interface, michael@0: // no matter what. We can forward these to mNotificationCallbacks michael@0: // if it wants to get notifications for them. But we michael@0: // need to see these notifications for proper functioning. michael@0: if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) { michael@0: mChannelEventSink = do_GetInterface(mNotificationCallbacks); michael@0: *aResult = static_cast(this); michael@0: NS_ADDREF_THIS(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Now give mNotificationCallbacks (if non-null) a chance to return the michael@0: // desired interface. michael@0: if (mNotificationCallbacks) { michael@0: nsresult rv = mNotificationCallbacks->GetInterface(aIID, aResult); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: NS_ASSERTION(*aResult, "Lying nsIInterfaceRequestor implementation!"); michael@0: return rv; michael@0: } michael@0: } michael@0: michael@0: if (aIID.Equals(NS_GET_IID(nsIAuthPrompt)) || michael@0: aIID.Equals(NS_GET_IID(nsIAuthPrompt2))) { michael@0: nsresult rv = CheckInnerWindowCorrectness(); michael@0: NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED); michael@0: michael@0: nsCOMPtr wwatch = michael@0: do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Get the an auth prompter for our window so that the parenting michael@0: // of the dialogs works as it should when using tabs. michael@0: michael@0: nsCOMPtr window; michael@0: if (GetOwner()) { michael@0: window = GetOwner()->GetOuterWindow(); michael@0: } michael@0: michael@0: return wwatch->GetPrompt(window, aIID, aResult); michael@0: } michael@0: michael@0: return QueryInterface(aIID, aResult); michael@0: } michael@0: michael@0: // static michael@0: bool michael@0: EventSource::PrefEnabled(JSContext* aCx, JSObject* aGlobal) michael@0: { michael@0: return Preferences::GetBool("dom.server-events.enabled", false); michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::GetBaseURI(nsIURI **aBaseURI) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aBaseURI); michael@0: michael@0: *aBaseURI = nullptr; michael@0: michael@0: nsCOMPtr baseURI; michael@0: michael@0: // first we try from document->GetBaseURI() michael@0: nsresult rv; michael@0: nsIScriptContext* sc = GetContextForEventHandlers(&rv); michael@0: nsCOMPtr doc = michael@0: nsContentUtils::GetDocumentFromScriptContext(sc); michael@0: if (doc) { michael@0: baseURI = doc->GetBaseURI(); michael@0: } michael@0: michael@0: // otherwise we get from the doc's principal michael@0: if (!baseURI) { michael@0: rv = mPrincipal->GetURI(getter_AddRefs(baseURI)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: NS_ENSURE_STATE(baseURI); michael@0: michael@0: baseURI.forget(aBaseURI); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::SetupHttpChannel() michael@0: { michael@0: mHttpChannel->SetRequestMethod(NS_LITERAL_CSTRING("GET")); michael@0: michael@0: /* set the http request headers */ michael@0: michael@0: mHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"), michael@0: NS_LITERAL_CSTRING(TEXT_EVENT_STREAM), false); michael@0: michael@0: // LOAD_BYPASS_CACHE already adds the Cache-Control: no-cache header michael@0: michael@0: if (!mLastEventID.IsEmpty()) { michael@0: mHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Last-Event-ID"), michael@0: NS_ConvertUTF16toUTF8(mLastEventID), false); michael@0: } michael@0: michael@0: nsCOMPtr codebase; michael@0: nsresult rv = GetBaseURI(getter_AddRefs(codebase)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = mHttpChannel->SetReferrer(codebase); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::InitChannelAndRequestEventSource() michael@0: { michael@0: if (mReadyState == CLOSED) { michael@0: return NS_ERROR_ABORT; michael@0: } michael@0: michael@0: // eventsource validation michael@0: michael@0: if (!CheckCanRequestSrc()) { michael@0: DispatchFailConnection(); michael@0: return NS_ERROR_DOM_SECURITY_ERR; michael@0: } michael@0: michael@0: nsLoadFlags loadFlags; michael@0: loadFlags = nsIRequest::LOAD_BACKGROUND | nsIRequest::LOAD_BYPASS_CACHE; michael@0: michael@0: // get Content Security Policy from principal to pass into channel michael@0: nsCOMPtr channelPolicy; michael@0: nsCOMPtr csp; michael@0: nsresult rv = mPrincipal->GetCsp(getter_AddRefs(csp)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (csp) { michael@0: channelPolicy = do_CreateInstance("@mozilla.org/nschannelpolicy;1"); michael@0: channelPolicy->SetContentSecurityPolicy(csp); michael@0: channelPolicy->SetLoadType(nsIContentPolicy::TYPE_DATAREQUEST); michael@0: } michael@0: michael@0: nsCOMPtr channel; michael@0: rv = NS_NewChannel(getter_AddRefs(channel), mSrc, nullptr, mLoadGroup, michael@0: nullptr, loadFlags, channelPolicy); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mHttpChannel = do_QueryInterface(channel); michael@0: NS_ENSURE_TRUE(mHttpChannel, NS_ERROR_NO_INTERFACE); michael@0: michael@0: rv = SetupHttpChannel(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr notificationCallbacks; michael@0: mHttpChannel->GetNotificationCallbacks(getter_AddRefs(notificationCallbacks)); michael@0: if (notificationCallbacks != this) { michael@0: mNotificationCallbacks = notificationCallbacks; michael@0: mHttpChannel->SetNotificationCallbacks(this); michael@0: } michael@0: michael@0: nsRefPtr listener = michael@0: new nsCORSListenerProxy(this, mPrincipal, mWithCredentials); michael@0: rv = listener->Init(mHttpChannel); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Start reading from the channel michael@0: rv = mHttpChannel->AsyncOpen(listener, nullptr); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: mWaitingForOnStopRequest = true; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: void michael@0: EventSource::AnnounceConnection() michael@0: { michael@0: if (mReadyState == CLOSED) { michael@0: return; michael@0: } michael@0: michael@0: if (mReadyState != CONNECTING) { michael@0: NS_WARNING("Unexpected mReadyState!!!"); michael@0: return; michael@0: } michael@0: michael@0: // When a user agent is to announce the connection, the user agent must set michael@0: // the readyState attribute to OPEN and queue a task to fire a simple event michael@0: // named open at the EventSource object. michael@0: michael@0: mReadyState = OPEN; michael@0: michael@0: nsresult rv = CheckInnerWindowCorrectness(); michael@0: if (NS_FAILED(rv)) { michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr event; michael@0: rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to create the open event!!!"); michael@0: return; michael@0: } michael@0: michael@0: // it doesn't bubble, and it isn't cancelable michael@0: rv = event->InitEvent(NS_LITERAL_STRING("open"), false, false); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to init the open event!!!"); michael@0: return; michael@0: } michael@0: michael@0: event->SetTrusted(true); michael@0: michael@0: rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to dispatch the open event!!!"); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::ResetConnection() michael@0: { michael@0: if (mHttpChannel) { michael@0: mHttpChannel->Cancel(NS_ERROR_ABORT); michael@0: } michael@0: michael@0: if (mUnicodeDecoder) { michael@0: mUnicodeDecoder->Reset(); michael@0: } michael@0: mLastConvertionResult = NS_OK; michael@0: michael@0: mHttpChannel = nullptr; michael@0: mNotificationCallbacks = nullptr; michael@0: mChannelEventSink = nullptr; michael@0: mStatus = PARSE_STATE_OFF; michael@0: mRedirectCallback = nullptr; michael@0: mNewRedirectChannel = nullptr; michael@0: michael@0: mReadyState = CONNECTING; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: EventSource::ReestablishConnection() michael@0: { michael@0: if (mReadyState == CLOSED) { michael@0: return; michael@0: } michael@0: michael@0: if (mReadyState != OPEN) { michael@0: NS_WARNING("Unexpected mReadyState!!!"); michael@0: return; michael@0: } michael@0: michael@0: nsresult rv = ResetConnection(); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to reset the connection!!!"); michael@0: return; michael@0: } michael@0: michael@0: rv = CheckInnerWindowCorrectness(); michael@0: if (NS_FAILED(rv)) { michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr event; michael@0: rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to create the error event!!!"); michael@0: return; michael@0: } michael@0: michael@0: // it doesn't bubble, and it isn't cancelable michael@0: rv = event->InitEvent(NS_LITERAL_STRING("error"), false, false); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to init the error event!!!"); michael@0: return; michael@0: } michael@0: michael@0: event->SetTrusted(true); michael@0: michael@0: rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to dispatch the error event!!!"); michael@0: return; michael@0: } michael@0: michael@0: rv = SetReconnectionTimeout(); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to set the timeout for reestablishing the connection!!!"); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::SetReconnectionTimeout() michael@0: { michael@0: if (mReadyState == CLOSED) { michael@0: return NS_ERROR_ABORT; michael@0: } michael@0: michael@0: // the timer will be used whenever the requests are going finished. michael@0: if (!mTimer) { michael@0: mTimer = do_CreateInstance("@mozilla.org/timer;1"); michael@0: NS_ENSURE_STATE(mTimer); michael@0: } michael@0: michael@0: nsresult rv = mTimer->InitWithFuncCallback(TimerCallback, this, michael@0: mReconnectionTime, michael@0: nsITimer::TYPE_ONE_SHOT); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::PrintErrorOnConsole(const char *aBundleURI, michael@0: const char16_t *aError, michael@0: const char16_t **aFormatStrings, michael@0: uint32_t aFormatStringsLen) michael@0: { michael@0: nsCOMPtr bundleService = michael@0: mozilla::services::GetStringBundleService(); michael@0: NS_ENSURE_STATE(bundleService); michael@0: michael@0: nsCOMPtr strBundle; michael@0: nsresult rv = michael@0: bundleService->CreateBundle(aBundleURI, getter_AddRefs(strBundle)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr console( michael@0: do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr errObj( michael@0: do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Localize the error message michael@0: nsXPIDLString message; michael@0: if (aFormatStrings) { michael@0: rv = strBundle->FormatStringFromName(aError, aFormatStrings, michael@0: aFormatStringsLen, michael@0: getter_Copies(message)); michael@0: } else { michael@0: rv = strBundle->GetStringFromName(aError, getter_Copies(message)); michael@0: } michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = errObj->InitWithWindowID(message, michael@0: mScriptFile, michael@0: EmptyString(), michael@0: mScriptLine, 0, michael@0: nsIScriptError::errorFlag, michael@0: "Event Source", mInnerWindowID); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // print the error message directly to the JS console michael@0: rv = console->LogMessage(errObj); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::ConsoleError() michael@0: { michael@0: nsAutoCString targetSpec; michael@0: nsresult rv = mSrc->GetSpec(targetSpec); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: NS_ConvertUTF8toUTF16 specUTF16(targetSpec); michael@0: const char16_t *formatStrings[] = { specUTF16.get() }; michael@0: michael@0: if (mReadyState == CONNECTING && !mInterrupted) { michael@0: rv = PrintErrorOnConsole("chrome://global/locale/appstrings.properties", michael@0: MOZ_UTF16("connectionFailure"), michael@0: formatStrings, ArrayLength(formatStrings)); michael@0: } else { michael@0: rv = PrintErrorOnConsole("chrome://global/locale/appstrings.properties", michael@0: MOZ_UTF16("netInterrupt"), michael@0: formatStrings, ArrayLength(formatStrings)); michael@0: } michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::DispatchFailConnection() michael@0: { michael@0: nsCOMPtr event = michael@0: NS_NewRunnableMethod(this, &EventSource::FailConnection); michael@0: NS_ENSURE_STATE(event); michael@0: michael@0: return NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); michael@0: } michael@0: michael@0: void michael@0: EventSource::FailConnection() michael@0: { michael@0: if (mReadyState == CLOSED) { michael@0: return; michael@0: } michael@0: michael@0: nsresult rv = ConsoleError(); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to print to the console error"); michael@0: } michael@0: michael@0: // When a user agent is to fail the connection, the user agent must set the michael@0: // readyState attribute to CLOSED and queue a task to fire a simple event michael@0: // named error at the EventSource object. michael@0: michael@0: Close(); // it sets mReadyState to CLOSED michael@0: michael@0: rv = CheckInnerWindowCorrectness(); michael@0: if (NS_FAILED(rv)) { michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr event; michael@0: rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to create the error event!!!"); michael@0: return; michael@0: } michael@0: michael@0: // it doesn't bubble, and it isn't cancelable michael@0: rv = event->InitEvent(NS_LITERAL_STRING("error"), false, false); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to init the error event!!!"); michael@0: return; michael@0: } michael@0: michael@0: event->SetTrusted(true); michael@0: michael@0: rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to dispatch the error event!!!"); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: bool michael@0: EventSource::CheckCanRequestSrc(nsIURI* aSrc) michael@0: { michael@0: if (mReadyState == CLOSED) { michael@0: return false; michael@0: } michael@0: michael@0: bool isValidURI = false; michael@0: bool isValidContentLoadPolicy = false; michael@0: bool isValidProtocol = false; michael@0: michael@0: nsCOMPtr srcToTest = aSrc ? aSrc : mSrc.get(); michael@0: NS_ENSURE_TRUE(srcToTest, false); michael@0: michael@0: uint32_t aCheckURIFlags = michael@0: nsIScriptSecurityManager::DISALLOW_INHERIT_PRINCIPAL | michael@0: nsIScriptSecurityManager::DISALLOW_SCRIPT; michael@0: michael@0: nsresult rv = nsContentUtils::GetSecurityManager()-> michael@0: CheckLoadURIWithPrincipal(mPrincipal, michael@0: srcToTest, michael@0: aCheckURIFlags); michael@0: isValidURI = NS_SUCCEEDED(rv); michael@0: michael@0: // After the security manager, the content-policy check michael@0: michael@0: nsIScriptContext* sc = GetContextForEventHandlers(&rv); michael@0: nsCOMPtr doc = michael@0: nsContentUtils::GetDocumentFromScriptContext(sc); michael@0: michael@0: // mScriptContext should be initialized because of GetBaseURI() above. michael@0: // Still need to consider the case that doc is nullptr however. michael@0: rv = CheckInnerWindowCorrectness(); michael@0: NS_ENSURE_SUCCESS(rv, false); michael@0: int16_t shouldLoad = nsIContentPolicy::ACCEPT; michael@0: rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_DATAREQUEST, michael@0: srcToTest, michael@0: mPrincipal, michael@0: doc, michael@0: NS_LITERAL_CSTRING(TEXT_EVENT_STREAM), michael@0: nullptr, // extra michael@0: &shouldLoad, michael@0: nsContentUtils::GetContentPolicy(), michael@0: nsContentUtils::GetSecurityManager()); michael@0: isValidContentLoadPolicy = NS_SUCCEEDED(rv) && NS_CP_ACCEPTED(shouldLoad); michael@0: michael@0: nsAutoCString targetURIScheme; michael@0: rv = srcToTest->GetScheme(targetURIScheme); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // We only have the http support for now michael@0: isValidProtocol = targetURIScheme.EqualsLiteral("http") || michael@0: targetURIScheme.EqualsLiteral("https"); michael@0: } michael@0: michael@0: return isValidURI && isValidContentLoadPolicy && isValidProtocol; michael@0: } michael@0: michael@0: // static michael@0: void michael@0: EventSource::TimerCallback(nsITimer* aTimer, void* aClosure) michael@0: { michael@0: nsRefPtr thisObject = static_cast(aClosure); michael@0: michael@0: if (thisObject->mReadyState == CLOSED) { michael@0: return; michael@0: } michael@0: michael@0: NS_PRECONDITION(!thisObject->mHttpChannel, michael@0: "the channel hasn't been cancelled!!"); michael@0: michael@0: if (!thisObject->mFrozen) { michael@0: nsresult rv = thisObject->InitChannelAndRequestEventSource(); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("thisObject->InitChannelAndRequestEventSource() failed"); michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::Thaw() michael@0: { michael@0: if (mReadyState == CLOSED || !mFrozen) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_ASSERTION(!mHttpChannel, "the connection hasn't been closed!!!"); michael@0: michael@0: mFrozen = false; michael@0: nsresult rv; michael@0: if (!mGoingToDispatchAllMessages && mMessagesToDispatch.GetSize() > 0) { michael@0: nsCOMPtr event = michael@0: NS_NewRunnableMethod(this, &EventSource::DispatchAllMessageEvents); michael@0: NS_ENSURE_STATE(event); michael@0: michael@0: mGoingToDispatchAllMessages = true; michael@0: michael@0: rv = NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: rv = InitChannelAndRequestEventSource(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::Freeze() michael@0: { michael@0: if (mReadyState == CLOSED || mFrozen) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_ASSERTION(!mHttpChannel, "the connection hasn't been closed!!!"); michael@0: mFrozen = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::DispatchCurrentMessageEvent() michael@0: { michael@0: nsAutoPtr message(new Message()); michael@0: *message = mCurrentMessage; michael@0: michael@0: ClearFields(); michael@0: michael@0: if (message->mData.IsEmpty()) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // removes the trailing LF from mData michael@0: NS_ASSERTION(message->mData.CharAt(message->mData.Length() - 1) == LF_CHAR, michael@0: "Invalid trailing character! LF was expected instead."); michael@0: message->mData.SetLength(message->mData.Length() - 1); michael@0: michael@0: if (message->mEventName.IsEmpty()) { michael@0: message->mEventName.AssignLiteral("message"); michael@0: } michael@0: michael@0: if (message->mLastEventID.IsEmpty() && !mLastEventID.IsEmpty()) { michael@0: message->mLastEventID.Assign(mLastEventID); michael@0: } michael@0: michael@0: int32_t sizeBefore = mMessagesToDispatch.GetSize(); michael@0: mMessagesToDispatch.Push(message.forget()); michael@0: NS_ENSURE_TRUE(mMessagesToDispatch.GetSize() == sizeBefore + 1, michael@0: NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: michael@0: if (!mGoingToDispatchAllMessages) { michael@0: nsCOMPtr event = michael@0: NS_NewRunnableMethod(this, &EventSource::DispatchAllMessageEvents); michael@0: NS_ENSURE_STATE(event); michael@0: michael@0: mGoingToDispatchAllMessages = true; michael@0: michael@0: return NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: EventSource::DispatchAllMessageEvents() michael@0: { michael@0: if (mReadyState == CLOSED || mFrozen) { michael@0: return; michael@0: } michael@0: michael@0: mGoingToDispatchAllMessages = false; michael@0: michael@0: nsresult rv = CheckInnerWindowCorrectness(); michael@0: if (NS_FAILED(rv)) { michael@0: return; michael@0: } michael@0: michael@0: // Let's play get the JSContext michael@0: nsCOMPtr sgo = do_QueryInterface(GetOwner()); michael@0: NS_ENSURE_TRUE_VOID(sgo); michael@0: michael@0: nsIScriptContext* scriptContext = sgo->GetContext(); michael@0: NS_ENSURE_TRUE_VOID(scriptContext); michael@0: michael@0: AutoPushJSContext cx(scriptContext->GetNativeContext()); michael@0: NS_ENSURE_TRUE_VOID(cx); michael@0: michael@0: while (mMessagesToDispatch.GetSize() > 0) { michael@0: nsAutoPtr michael@0: message(static_cast(mMessagesToDispatch.PopFront())); michael@0: michael@0: // Now we can turn our string into a jsval michael@0: JS::Rooted jsData(cx); michael@0: { michael@0: JSString* jsString; michael@0: jsString = JS_NewUCStringCopyN(cx, michael@0: message->mData.get(), michael@0: message->mData.Length()); michael@0: NS_ENSURE_TRUE_VOID(jsString); michael@0: michael@0: jsData = STRING_TO_JSVAL(jsString); michael@0: } michael@0: michael@0: // create an event that uses the MessageEvent interface, michael@0: // which does not bubble, is not cancelable, and has no default action michael@0: michael@0: nsCOMPtr event; michael@0: rv = NS_NewDOMMessageEvent(getter_AddRefs(event), this, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to create the message event!!!"); michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr messageEvent = do_QueryInterface(event); michael@0: rv = messageEvent->InitMessageEvent(message->mEventName, michael@0: false, false, michael@0: jsData, michael@0: mOrigin, michael@0: message->mLastEventID, nullptr); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to init the message event!!!"); michael@0: return; michael@0: } michael@0: michael@0: messageEvent->SetTrusted(true); michael@0: michael@0: rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to dispatch the message event!!!"); michael@0: return; michael@0: } michael@0: michael@0: mLastEventID.Assign(message->mLastEventID); michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::ClearFields() michael@0: { michael@0: // mLastEventID and mReconnectionTime must be cached michael@0: michael@0: mCurrentMessage.mEventName.Truncate(); michael@0: mCurrentMessage.mLastEventID.Truncate(); michael@0: mCurrentMessage.mData.Truncate(); michael@0: michael@0: mLastFieldName.Truncate(); michael@0: mLastFieldValue.Truncate(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::SetFieldAndClear() michael@0: { michael@0: if (mLastFieldName.IsEmpty()) { michael@0: mLastFieldValue.Truncate(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: char16_t first_char; michael@0: first_char = mLastFieldName.CharAt(0); michael@0: michael@0: switch (first_char) // with no case folding performed michael@0: { michael@0: case char16_t('d'): michael@0: if (mLastFieldName.EqualsLiteral("data")) { michael@0: // If the field name is "data" append the field value to the data michael@0: // buffer, then append a single U+000A LINE FEED (LF) character michael@0: // to the data buffer. michael@0: mCurrentMessage.mData.Append(mLastFieldValue); michael@0: mCurrentMessage.mData.Append(LF_CHAR); michael@0: } michael@0: break; michael@0: michael@0: case char16_t('e'): michael@0: if (mLastFieldName.EqualsLiteral("event")) { michael@0: mCurrentMessage.mEventName.Assign(mLastFieldValue); michael@0: } michael@0: break; michael@0: michael@0: case char16_t('i'): michael@0: if (mLastFieldName.EqualsLiteral("id")) { michael@0: mCurrentMessage.mLastEventID.Assign(mLastFieldValue); michael@0: } michael@0: break; michael@0: michael@0: case char16_t('r'): michael@0: if (mLastFieldName.EqualsLiteral("retry")) { michael@0: uint32_t newValue=0; michael@0: uint32_t i = 0; // we must ensure that there are only digits michael@0: bool assign = true; michael@0: for (i = 0; i < mLastFieldValue.Length(); ++i) { michael@0: if (mLastFieldValue.CharAt(i) < (char16_t)'0' || michael@0: mLastFieldValue.CharAt(i) > (char16_t)'9') { michael@0: assign = false; michael@0: break; michael@0: } michael@0: newValue = newValue*10 + michael@0: (((uint32_t)mLastFieldValue.CharAt(i))- michael@0: ((uint32_t)((char16_t)'0'))); michael@0: } michael@0: michael@0: if (assign) { michael@0: if (newValue < MIN_RECONNECTION_TIME_VALUE) { michael@0: mReconnectionTime = MIN_RECONNECTION_TIME_VALUE; michael@0: } else if (newValue > MAX_RECONNECTION_TIME_VALUE) { michael@0: mReconnectionTime = MAX_RECONNECTION_TIME_VALUE; michael@0: } else { michael@0: mReconnectionTime = newValue; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: mLastFieldName.Truncate(); michael@0: mLastFieldValue.Truncate(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::CheckHealthOfRequestCallback(nsIRequest *aRequestCallback) michael@0: { michael@0: // check if we have been closed or if the request has been canceled michael@0: // or if we have been frozen michael@0: if (mReadyState == CLOSED || !mHttpChannel || michael@0: mFrozen || mErrorLoadOnRedirect) { michael@0: return NS_ERROR_ABORT; michael@0: } michael@0: michael@0: nsCOMPtr httpChannel = do_QueryInterface(aRequestCallback); michael@0: NS_ENSURE_STATE(httpChannel); michael@0: michael@0: if (httpChannel != mHttpChannel) { michael@0: NS_WARNING("wrong channel from request callback"); michael@0: return NS_ERROR_ABORT; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventSource::ParseCharacter(char16_t aChr) michael@0: { michael@0: nsresult rv; michael@0: michael@0: if (mReadyState == CLOSED) { michael@0: return NS_ERROR_ABORT; michael@0: } michael@0: michael@0: switch (mStatus) michael@0: { michael@0: case PARSE_STATE_OFF: michael@0: NS_ERROR("Invalid state"); michael@0: return NS_ERROR_FAILURE; michael@0: break; michael@0: michael@0: case PARSE_STATE_BEGIN_OF_STREAM: michael@0: if (aChr == BOM_CHAR) { michael@0: mStatus = PARSE_STATE_BOM_WAS_READ; // ignore it michael@0: } else if (aChr == CR_CHAR) { michael@0: mStatus = PARSE_STATE_CR_CHAR; michael@0: } else if (aChr == LF_CHAR) { michael@0: mStatus = PARSE_STATE_BEGIN_OF_LINE; michael@0: } else if (aChr == COLON_CHAR) { michael@0: mStatus = PARSE_STATE_COMMENT; michael@0: } else { michael@0: mLastFieldName += aChr; michael@0: mStatus = PARSE_STATE_FIELD_NAME; michael@0: } michael@0: michael@0: break; michael@0: michael@0: case PARSE_STATE_BOM_WAS_READ: michael@0: if (aChr == CR_CHAR) { michael@0: mStatus = PARSE_STATE_CR_CHAR; michael@0: } else if (aChr == LF_CHAR) { michael@0: mStatus = PARSE_STATE_BEGIN_OF_LINE; michael@0: } else if (aChr == COLON_CHAR) { michael@0: mStatus = PARSE_STATE_COMMENT; michael@0: } else { michael@0: mLastFieldName += aChr; michael@0: mStatus = PARSE_STATE_FIELD_NAME; michael@0: } michael@0: break; michael@0: michael@0: case PARSE_STATE_CR_CHAR: michael@0: if (aChr == CR_CHAR) { michael@0: rv = DispatchCurrentMessageEvent(); // there is an empty line (CRCR) michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } else if (aChr == LF_CHAR) { michael@0: mStatus = PARSE_STATE_BEGIN_OF_LINE; michael@0: } else if (aChr == COLON_CHAR) { michael@0: mStatus = PARSE_STATE_COMMENT; michael@0: } else { michael@0: mLastFieldName += aChr; michael@0: mStatus = PARSE_STATE_FIELD_NAME; michael@0: } michael@0: michael@0: break; michael@0: michael@0: case PARSE_STATE_COMMENT: michael@0: if (aChr == CR_CHAR) { michael@0: mStatus = PARSE_STATE_CR_CHAR; michael@0: } else if (aChr == LF_CHAR) { michael@0: mStatus = PARSE_STATE_BEGIN_OF_LINE; michael@0: } michael@0: michael@0: break; michael@0: michael@0: case PARSE_STATE_FIELD_NAME: michael@0: if (aChr == CR_CHAR) { michael@0: rv = SetFieldAndClear(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mStatus = PARSE_STATE_CR_CHAR; michael@0: } else if (aChr == LF_CHAR) { michael@0: rv = SetFieldAndClear(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mStatus = PARSE_STATE_BEGIN_OF_LINE; michael@0: } else if (aChr == COLON_CHAR) { michael@0: mStatus = PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE; michael@0: } else { michael@0: mLastFieldName += aChr; michael@0: } michael@0: michael@0: break; michael@0: michael@0: case PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE: michael@0: if (aChr == CR_CHAR) { michael@0: rv = SetFieldAndClear(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mStatus = PARSE_STATE_CR_CHAR; michael@0: } else if (aChr == LF_CHAR) { michael@0: rv = SetFieldAndClear(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mStatus = PARSE_STATE_BEGIN_OF_LINE; michael@0: } else if (aChr == SPACE_CHAR) { michael@0: mStatus = PARSE_STATE_FIELD_VALUE; michael@0: } else { michael@0: mLastFieldValue += aChr; michael@0: mStatus = PARSE_STATE_FIELD_VALUE; michael@0: } michael@0: michael@0: break; michael@0: michael@0: case PARSE_STATE_FIELD_VALUE: michael@0: if (aChr == CR_CHAR) { michael@0: rv = SetFieldAndClear(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mStatus = PARSE_STATE_CR_CHAR; michael@0: } else if (aChr == LF_CHAR) { michael@0: rv = SetFieldAndClear(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mStatus = PARSE_STATE_BEGIN_OF_LINE; michael@0: } else { michael@0: mLastFieldValue += aChr; michael@0: } michael@0: michael@0: break; michael@0: michael@0: case PARSE_STATE_BEGIN_OF_LINE: michael@0: if (aChr == CR_CHAR) { michael@0: rv = DispatchCurrentMessageEvent(); // there is an empty line michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mStatus = PARSE_STATE_CR_CHAR; michael@0: } else if (aChr == LF_CHAR) { michael@0: rv = DispatchCurrentMessageEvent(); // there is an empty line michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mStatus = PARSE_STATE_BEGIN_OF_LINE; michael@0: } else if (aChr == COLON_CHAR) { michael@0: mStatus = PARSE_STATE_COMMENT; michael@0: } else { michael@0: mLastFieldName += aChr; michael@0: mStatus = PARSE_STATE_FIELD_NAME; michael@0: } michael@0: michael@0: break; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla