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"
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
class nsPIDOMWindowInner;
|
2012-12-22 12:17:05 +04:00
|
|
|
|
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 {
|
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
struct EventSourceInit;
|
2012-12-22 12:17:05 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
class EventSourceImpl;
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class EventSource final : public DOMEventTargetHelper
|
2011-05-23 01:30:07 +04:00
|
|
|
{
|
2016-10-24 05:11:04 +03:00
|
|
|
friend class EventSourceImpl;
|
2011-05-23 01:30:07 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2017-01-25 04:40:11 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(EventSource, DOMEventTargetHelper)
|
|
|
|
virtual bool IsCertainlyAliveForCC() const override;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
// EventTarget
|
|
|
|
void DisconnectFromOwner() override
|
|
|
|
{
|
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
|
|
|
Close();
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
2012-12-22 12:17:05 +04:00
|
|
|
|
|
|
|
// WebIDL
|
|
|
|
static already_AddRefed<EventSource>
|
2012-12-03 20:07:49 +04:00
|
|
|
Constructor(const GlobalObject& aGlobal, const nsAString& aURL,
|
2016-10-24 05:11:04 +03:00
|
|
|
const EventSourceInit& aEventSourceInitDict, ErrorResult& aRv);
|
2012-12-22 12:17:05 +04:00
|
|
|
|
|
|
|
void GetUrl(nsAString& aURL) const
|
|
|
|
{
|
2016-10-24 05:11:04 +03:00
|
|
|
AssertIsOnTargetThread();
|
2012-12-22 12:17:05 +04:00
|
|
|
aURL = mOriginalURL;
|
|
|
|
}
|
2016-10-24 05:11:04 +03:00
|
|
|
|
2012-12-22 12:17:05 +04:00
|
|
|
bool WithCredentials() const
|
|
|
|
{
|
2016-10-24 05:11:04 +03:00
|
|
|
AssertIsOnTargetThread();
|
2012-12-22 12:17:05 +04:00
|
|
|
return mWithCredentials;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t ReadyState() const
|
|
|
|
{
|
2016-10-24 05:11:04 +03:00
|
|
|
AssertIsOnTargetThread();
|
2012-12-22 12:17:05 +04:00
|
|
|
return mReadyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(open)
|
|
|
|
IMPL_EVENT_HANDLER(message)
|
|
|
|
IMPL_EVENT_HANDLER(error)
|
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void Close();
|
2012-12-22 12:17:05 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
private:
|
|
|
|
EventSource(nsPIDOMWindowInner* aOwnerWindow, bool aWithCredentials);
|
2014-07-09 01:23:16 +04:00
|
|
|
virtual ~EventSource();
|
2016-10-24 05:11:04 +03:00
|
|
|
// prevent bad usage
|
|
|
|
EventSource(const EventSource& x) = delete;
|
|
|
|
EventSource& operator=(const EventSource& x) = delete;
|
2014-07-09 01:23:16 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void AssertIsOnTargetThread() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
|
|
|
}
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
nsresult CreateAndDispatchSimpleEvent(const nsAString& aName);
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
// Raw pointer because this EventSourceImpl is created, managed and destroyed
|
|
|
|
// by EventSource.
|
|
|
|
EventSourceImpl* mImpl;
|
2011-05-23 01:30:07 +04:00
|
|
|
nsString mOriginalURL;
|
2016-10-24 05:11:04 +03:00
|
|
|
uint16_t mReadyState;
|
|
|
|
bool mWithCredentials;
|
|
|
|
bool mIsMainThread;
|
|
|
|
// This is used to keep EventSourceImpl instance when there is a connection.
|
|
|
|
bool mKeepingAlive;
|
2011-05-23 01:30:07 +04:00
|
|
|
|
2016-10-24 05:11:04 +03:00
|
|
|
void UpdateMustKeepAlive();
|
|
|
|
void UpdateDontKeepAlive();
|
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
|