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"
21 class nsIGlobalObject
;
22 class nsICookieJarSettings
;
30 struct EventSourceInit
;
32 class EventSourceImpl
;
34 class EventSource final
: public DOMEventTargetHelper
{
35 friend class EventSourceImpl
;
38 NS_DECL_ISUPPORTS_INHERITED
39 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(EventSource
, DOMEventTargetHelper
)
40 virtual bool IsCertainlyAliveForCC() const override
;
43 void DisconnectFromOwner() override
{
44 DOMEventTargetHelper::DisconnectFromOwner();
48 JSObject
* WrapObject(JSContext
* aCx
,
49 JS::Handle
<JSObject
*> aGivenProto
) override
;
52 static already_AddRefed
<EventSource
> Constructor(
53 const GlobalObject
& aGlobal
, const nsAString
& aURL
,
54 const EventSourceInit
& aEventSourceInitDict
, ErrorResult
& aRv
);
56 void GetUrl(nsAString
& aURL
) const {
57 AssertIsOnTargetThread();
61 bool WithCredentials() const {
62 AssertIsOnTargetThread();
63 return mWithCredentials
;
66 uint16_t ReadyState() const {
67 AssertIsOnTargetThread();
71 IMPL_EVENT_HANDLER(open
)
72 IMPL_EVENT_HANDLER(message
)
73 IMPL_EVENT_HANDLER(error
)
78 EventSource(nsIGlobalObject
* aGlobal
,
79 nsICookieJarSettings
* aCookieJarSettings
, bool aWithCredentials
);
80 virtual ~EventSource();
82 EventSource(const EventSource
& x
) = delete;
83 EventSource
& operator=(const EventSource
& x
) = delete;
85 void AssertIsOnTargetThread() const {
86 MOZ_ASSERT(NS_IsMainThread() == mIsMainThread
);
89 nsresult
CreateAndDispatchSimpleEvent(const nsAString
& aName
);
91 // Raw pointer because this EventSourceImpl is created, managed and destroyed
93 EventSourceImpl
* mImpl
;
94 nsString mOriginalURL
;
96 bool mWithCredentials
;
98 // This is used to keep EventSourceImpl instance when there is a connection.
101 void UpdateMustKeepAlive();
102 void UpdateDontKeepAlive();
106 } // namespace mozilla
108 #endif // mozilla_dom_EventSource_h