1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * This implementation has support only for http requests. It is because the
9 * spec has defined event streams only for http. HTTP is required because
10 * this implementation uses some http headers: "Last-Event-ID", "Cache-Control"
14 #ifndef mozilla_dom_EventSource_h
15 #define mozilla_dom_EventSource_h
17 #include "mozilla/Attributes.h"
18 #include "mozilla/DOMEventTargetHelper.h"
19 #include "nsIObserver.h"
20 #include "nsIStreamListener.h"
21 #include "nsIChannelEventSink.h"
22 #include "nsIInterfaceRequestor.h"
24 #include "nsIHttpChannel.h"
27 class nsIGlobalObject
;
28 class nsICookieSettings
;
36 struct EventSourceInit
;
38 class EventSourceImpl
;
40 class EventSource final
: public DOMEventTargetHelper
{
41 friend class EventSourceImpl
;
44 NS_DECL_ISUPPORTS_INHERITED
45 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(EventSource
, DOMEventTargetHelper
)
46 virtual bool IsCertainlyAliveForCC() const override
;
49 void DisconnectFromOwner() override
{
50 DOMEventTargetHelper::DisconnectFromOwner();
54 JSObject
* WrapObject(JSContext
* aCx
,
55 JS::Handle
<JSObject
*> aGivenProto
) override
;
58 static already_AddRefed
<EventSource
> Constructor(
59 const GlobalObject
& aGlobal
, const nsAString
& aURL
,
60 const EventSourceInit
& aEventSourceInitDict
, ErrorResult
& aRv
);
62 void GetUrl(nsAString
& aURL
) const {
63 AssertIsOnTargetThread();
67 bool WithCredentials() const {
68 AssertIsOnTargetThread();
69 return mWithCredentials
;
72 uint16_t ReadyState() const {
73 AssertIsOnTargetThread();
77 IMPL_EVENT_HANDLER(open
)
78 IMPL_EVENT_HANDLER(message
)
79 IMPL_EVENT_HANDLER(error
)
84 EventSource(nsIGlobalObject
* aGlobal
, nsICookieSettings
* aCookieSettings
,
85 bool aWithCredentials
);
86 virtual ~EventSource();
88 EventSource(const EventSource
& x
) = delete;
89 EventSource
& operator=(const EventSource
& x
) = delete;
91 void AssertIsOnTargetThread() const {
92 MOZ_ASSERT(NS_IsMainThread() == mIsMainThread
);
95 nsresult
CreateAndDispatchSimpleEvent(const nsAString
& aName
);
97 // Raw pointer because this EventSourceImpl is created, managed and destroyed
99 EventSourceImpl
* mImpl
;
100 nsString mOriginalURL
;
101 uint16_t mReadyState
;
102 bool mWithCredentials
;
104 // This is used to keep EventSourceImpl instance when there is a connection.
107 void UpdateMustKeepAlive();
108 void UpdateDontKeepAlive();
112 } // namespace mozilla
114 #endif // mozilla_dom_EventSource_h