2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 08:17:05 +00:00
|
|
|
#ifndef mozilla_dom_EventSource_h
|
|
|
|
#define mozilla_dom_EventSource_h
|
2011-05-22 21:30:07 +00:00
|
|
|
|
2013-05-29 20:43:41 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-04-01 06:13:50 +00:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2011-05-22 21:30:07 +00: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"
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
class nsPIDOMWindowInner;
|
2012-12-22 08:17:05 +00:00
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
namespace mozilla {
|
2012-12-22 08:17:05 +00:00
|
|
|
|
|
|
|
class ErrorResult;
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
namespace dom {
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
struct EventSourceInit;
|
2012-12-22 08:17:05 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class EventSource final : public DOMEventTargetHelper
|
2015-03-27 18:52:19 +00:00
|
|
|
, public nsIObserver
|
|
|
|
, public nsIStreamListener
|
|
|
|
, public nsIChannelEventSink
|
|
|
|
, public nsIInterfaceRequestor
|
|
|
|
, public nsSupportsWeakReference
|
2011-05-22 21:30:07 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-01-30 17:05:36 +00:00
|
|
|
explicit EventSource(nsPIDOMWindowInner* aOwnerWindow);
|
2011-05-22 21:30:07 +00:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(
|
|
|
|
EventSource, DOMEventTargetHelper)
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
// nsWrapperCache
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2012-12-22 08:17:05 +00:00
|
|
|
|
|
|
|
// WebIDL
|
2016-01-30 17:05:36 +00:00
|
|
|
nsPIDOMWindowInner*
|
2012-12-22 08:17:05 +00:00
|
|
|
GetParentObject() const
|
|
|
|
{
|
|
|
|
return GetOwner();
|
|
|
|
}
|
|
|
|
static already_AddRefed<EventSource>
|
2012-12-03 16:07:49 +00:00
|
|
|
Constructor(const GlobalObject& aGlobal, const nsAString& aURL,
|
2012-12-22 08:17:05 +00: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();
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual void DisconnectFromOwner() override;
|
2012-12-22 08:17:05 +00:00
|
|
|
|
2011-05-22 21:30:07 +00:00
|
|
|
protected:
|
2014-07-08 21:23:16 +00:00
|
|
|
virtual ~EventSource();
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
nsresult Init(nsISupports* aOwner,
|
|
|
|
const nsAString& aURL,
|
|
|
|
bool aWithCredentials);
|
|
|
|
|
2011-05-22 21:30:07 +00:00
|
|
|
nsresult GetBaseURI(nsIURI **aBaseURI);
|
|
|
|
|
2016-04-20 22:04:37 +00:00
|
|
|
void SetupHttpChannel();
|
|
|
|
nsresult SetupReferrerPolicy();
|
2011-05-22 21:30:07 +00:00
|
|
|
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 15:02:17 +00:00
|
|
|
const char16_t *aError,
|
|
|
|
const char16_t **aFormatStrings,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aFormatStringsLen);
|
2011-05-22 21:30:07 +00:00
|
|
|
nsresult ConsoleError();
|
|
|
|
|
|
|
|
static NS_METHOD StreamReaderFunc(nsIInputStream *aInputStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char *aFromRawSegment,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount);
|
2011-05-22 21:30:07 +00:00
|
|
|
nsresult SetFieldAndClear();
|
|
|
|
nsresult ClearFields();
|
|
|
|
nsresult ResetEvent();
|
|
|
|
nsresult DispatchCurrentMessageEvent();
|
2014-01-04 15:02:17 +00:00
|
|
|
nsresult ParseCharacter(char16_t aChr);
|
2011-05-22 21:30:07 +00:00
|
|
|
nsresult CheckHealthOfRequestCallback(nsIRequest *aRequestCallback);
|
|
|
|
nsresult OnRedirectVerifyCallback(nsresult result);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> mSrc;
|
|
|
|
|
|
|
|
nsString mLastEventID;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mReconnectionTime; // in ms
|
2011-05-22 21:30:07 +00: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 06:19:26 +00:00
|
|
|
bool mFrozen;
|
|
|
|
bool mErrorLoadOnRedirect;
|
|
|
|
bool mGoingToDispatchAllMessages;
|
2011-12-05 23:02:42 +00:00
|
|
|
bool mWithCredentials;
|
2012-02-16 16:45:25 +00:00
|
|
|
bool mWaitingForOnStopRequest;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
// used while reading the input streams
|
|
|
|
nsCOMPtr<nsIUnicodeDecoder> mUnicodeDecoder;
|
|
|
|
nsresult mLastConvertionResult;
|
|
|
|
nsString mLastFieldName;
|
|
|
|
nsString mLastFieldValue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> mLoadGroup;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> mHttpChannel;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
uint16_t mReadyState;
|
2011-05-22 21:30:07 +00:00
|
|
|
nsString mOriginalURL;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2011-12-05 23:02:42 +00:00
|
|
|
nsString mOrigin;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
// Event Source owner information:
|
|
|
|
// - the script file name
|
2015-09-02 00:56:03 +00:00
|
|
|
// - source code line number and column number where the Event Source object
|
|
|
|
// was constructed.
|
2011-08-24 20:44:35 +00: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-22 21:30:07 +00:00
|
|
|
// These attributes are used for error reporting.
|
|
|
|
nsString mScriptFile;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mScriptLine;
|
2015-09-02 00:56:03 +00:00
|
|
|
uint32_t mScriptColumn;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t mInnerWindowID;
|
2011-05-22 21:30:07 +00:00
|
|
|
|
|
|
|
private:
|
2012-12-22 08:17:05 +00:00
|
|
|
EventSource(const EventSource& x); // prevent bad usage
|
|
|
|
EventSource& operator=(const EventSource& x);
|
2011-05-22 21:30:07 +00:00
|
|
|
};
|
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_EventSource_h
|