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
|
|
|
|
|
|
|
/*
|
|
|
|
* This implementation has support only for http requests. It is because the
|
|
|
|
* spec has defined event streams only for http. HTTP is required because
|
|
|
|
* this implementation uses some http headers: "Last-Event-ID", "Cache-Control"
|
|
|
|
* and "Accept".
|
|
|
|
*/
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
#ifndef mozilla_dom_EventSource_h
|
|
|
|
#define mozilla_dom_EventSource_h
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2013-05-30 00:43:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2011-05-23 01:30:07 +04:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIChannelEventSink.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsWeakReference.h"
|
|
|
|
#include "nsDeque.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
class nsPIDOMWindow;
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
namespace mozilla {
|
2012-12-22 12:17:05 +04:00
|
|
|
|
|
|
|
class ErrorResult;
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
class AsyncVerifyRedirectCallbackFwr;
|
2012-12-22 12:17:05 +04:00
|
|
|
struct EventSourceInit;
|
2012-12-22 12:17:05 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class EventSource final : public DOMEventTargetHelper
|
2015-03-27 21:52:19 +03:00
|
|
|
, public nsIObserver
|
|
|
|
, public nsIStreamListener
|
|
|
|
, public nsIChannelEventSink
|
|
|
|
, public nsIInterfaceRequestor
|
|
|
|
, public nsSupportsWeakReference
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
|
|
|
friend class AsyncVerifyRedirectCallbackFwr;
|
|
|
|
|
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit EventSource(nsPIDOMWindow* aOwnerWindow);
|
2011-05-23 01:30:07 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(
|
|
|
|
EventSource, DOMEventTargetHelper)
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
// nsWrapperCache
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2012-12-22 12:17:05 +04:00
|
|
|
|
|
|
|
// WebIDL
|
|
|
|
nsPIDOMWindow*
|
|
|
|
GetParentObject() const
|
|
|
|
{
|
|
|
|
return GetOwner();
|
|
|
|
}
|
|
|
|
static already_AddRefed<EventSource>
|
2012-12-03 20:07:49 +04:00
|
|
|
Constructor(const GlobalObject& aGlobal, const nsAString& aURL,
|
2012-12-22 12:17:05 +04:00
|
|
|
const EventSourceInit& aEventSourceInitDict,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
void GetUrl(nsAString& aURL) const
|
|
|
|
{
|
|
|
|
aURL = mOriginalURL;
|
|
|
|
}
|
|
|
|
bool WithCredentials() const
|
|
|
|
{
|
|
|
|
return mWithCredentials;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CONNECTING = 0U,
|
|
|
|
OPEN = 1U,
|
|
|
|
CLOSED = 2U
|
|
|
|
};
|
|
|
|
uint16_t ReadyState() const
|
|
|
|
{
|
|
|
|
return mReadyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(open)
|
|
|
|
IMPL_EVENT_HANDLER(message)
|
|
|
|
IMPL_EVENT_HANDLER(error)
|
|
|
|
void Close();
|
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
// Determine if preferences allow EventSource
|
2014-02-06 22:28:14 +04:00
|
|
|
static bool PrefEnabled(JSContext* aCx = nullptr, JSObject* aGlobal = nullptr);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void DisconnectFromOwner() override;
|
2012-12-22 12:17:05 +04:00
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
protected:
|
2014-07-09 01:23:16 +04:00
|
|
|
virtual ~EventSource();
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
nsresult Init(nsISupports* aOwner,
|
|
|
|
const nsAString& aURL,
|
|
|
|
bool aWithCredentials);
|
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult GetBaseURI(nsIURI **aBaseURI);
|
|
|
|
|
2014-11-18 16:47:03 +03:00
|
|
|
net::ReferrerPolicy GetReferrerPolicy();
|
|
|
|
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult SetupHttpChannel();
|
|
|
|
nsresult InitChannelAndRequestEventSource();
|
|
|
|
nsresult ResetConnection();
|
|
|
|
nsresult DispatchFailConnection();
|
|
|
|
nsresult SetReconnectionTimeout();
|
|
|
|
|
|
|
|
void AnnounceConnection();
|
|
|
|
void DispatchAllMessageEvents();
|
|
|
|
void ReestablishConnection();
|
|
|
|
void FailConnection();
|
|
|
|
|
|
|
|
nsresult Thaw();
|
|
|
|
nsresult Freeze();
|
|
|
|
|
|
|
|
static void TimerCallback(nsITimer *aTimer, void *aClosure);
|
|
|
|
|
|
|
|
nsresult PrintErrorOnConsole(const char *aBundleURI,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *aError,
|
|
|
|
const char16_t **aFormatStrings,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFormatStringsLen);
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult ConsoleError();
|
|
|
|
|
|
|
|
static NS_METHOD StreamReaderFunc(nsIInputStream *aInputStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char *aFromRawSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount);
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult SetFieldAndClear();
|
|
|
|
nsresult ClearFields();
|
|
|
|
nsresult ResetEvent();
|
|
|
|
nsresult DispatchCurrentMessageEvent();
|
2014-01-04 19:02:17 +04:00
|
|
|
nsresult ParseCharacter(char16_t aChr);
|
2012-07-30 18:20:58 +04:00
|
|
|
bool CheckCanRequestSrc(nsIURI* aSrc = nullptr); // if null, it tests mSrc
|
2011-05-23 01:30:07 +04:00
|
|
|
nsresult CheckHealthOfRequestCallback(nsIRequest *aRequestCallback);
|
|
|
|
nsresult OnRedirectVerifyCallback(nsresult result);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> mSrc;
|
|
|
|
|
|
|
|
nsString mLastEventID;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mReconnectionTime; // in ms
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
struct Message {
|
|
|
|
nsString mEventName;
|
|
|
|
nsString mLastEventID;
|
|
|
|
nsString mData;
|
|
|
|
};
|
|
|
|
nsDeque mMessagesToDispatch;
|
|
|
|
Message mCurrentMessage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple state machine used to manage the event-source's line buffer
|
|
|
|
*
|
|
|
|
* PARSE_STATE_OFF -> PARSE_STATE_BEGIN_OF_STREAM
|
|
|
|
*
|
|
|
|
* PARSE_STATE_BEGIN_OF_STREAM -> PARSE_STATE_BOM_WAS_READ |
|
|
|
|
* PARSE_STATE_CR_CHAR |
|
|
|
|
* PARSE_STATE_BEGIN_OF_LINE |
|
|
|
|
* PARSE_STATE_COMMENT |
|
|
|
|
* PARSE_STATE_FIELD_NAME
|
|
|
|
*
|
|
|
|
* PARSE_STATE_BOM_WAS_READ -> PARSE_STATE_CR_CHAR |
|
|
|
|
* 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,
|
|
|
|
PARSE_STATE_BEGIN_OF_STREAM,
|
|
|
|
PARSE_STATE_BOM_WAS_READ,
|
|
|
|
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
|
|
|
|
};
|
|
|
|
ParserStatus mStatus;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mFrozen;
|
|
|
|
bool mErrorLoadOnRedirect;
|
|
|
|
bool mGoingToDispatchAllMessages;
|
2011-12-06 03:02:42 +04:00
|
|
|
bool mWithCredentials;
|
2012-02-16 20:45:25 +04:00
|
|
|
bool mWaitingForOnStopRequest;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
// used while reading the input streams
|
|
|
|
nsCOMPtr<nsIUnicodeDecoder> mUnicodeDecoder;
|
|
|
|
nsresult mLastConvertionResult;
|
|
|
|
nsString mLastFieldName;
|
|
|
|
nsString mLastFieldValue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> mLoadGroup;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The notification callbacks the channel had initially.
|
|
|
|
* We want to forward things here as needed.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> mNotificationCallbacks;
|
|
|
|
nsCOMPtr<nsIChannelEventSink> mChannelEventSink;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> mHttpChannel;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
uint16_t mReadyState;
|
2011-05-23 01:30:07 +04:00
|
|
|
nsString mOriginalURL;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2011-12-06 03:02:42 +04:00
|
|
|
nsString mOrigin;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mRedirectFlags;
|
2011-05-23 01:30:07 +04:00
|
|
|
nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
|
|
|
|
nsCOMPtr<nsIChannel> mNewRedirectChannel;
|
|
|
|
|
|
|
|
// Event Source owner information:
|
|
|
|
// - the script file name
|
|
|
|
// - source code line number where the Event Source object was constructed.
|
2011-08-25 00:44:35 +04:00
|
|
|
// - the ID of the inner window where the script lives. Note that this may not
|
|
|
|
// be the same as the Event Source owner window.
|
2011-05-23 01:30:07 +04:00
|
|
|
// These attributes are used for error reporting.
|
|
|
|
nsString mScriptFile;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mScriptLine;
|
|
|
|
uint64_t mInnerWindowID;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
|
|
|
private:
|
2012-12-22 12:17:05 +04:00
|
|
|
EventSource(const EventSource& x); // prevent bad usage
|
|
|
|
EventSource& operator=(const EventSource& x);
|
2011-05-23 01:30:07 +04:00
|
|
|
};
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_EventSource_h
|