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/Atomics.h"
18 #include "mozilla/Attributes.h"
19 #include "mozilla/DOMEventTargetHelper.h"
21 #include "nsICookieJarSettings.h"
23 class nsIGlobalObject
;
31 struct EventSourceInit
;
33 class EventSourceImpl
;
35 class EventSource final
: public DOMEventTargetHelper
{
36 friend class EventSourceImpl
;
39 NS_DECL_ISUPPORTS_INHERITED
40 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(EventSource
, DOMEventTargetHelper
)
41 virtual bool IsCertainlyAliveForCC() const override
;
44 void DisconnectFromOwner() override
{
45 DOMEventTargetHelper::DisconnectFromOwner();
49 JSObject
* WrapObject(JSContext
* aCx
,
50 JS::Handle
<JSObject
*> aGivenProto
) override
;
53 static already_AddRefed
<EventSource
> Constructor(
54 const GlobalObject
& aGlobal
, const nsAString
& aURL
,
55 const EventSourceInit
& aEventSourceInitDict
, ErrorResult
& aRv
);
57 void GetUrl(nsAString
& aURL
) const {
58 AssertIsOnTargetThread();
62 bool WithCredentials() const {
63 AssertIsOnTargetThread();
64 return mWithCredentials
;
67 uint16_t ReadyState() const {
68 AssertIsOnTargetThread();
72 IMPL_EVENT_HANDLER(open
)
73 IMPL_EVENT_HANDLER(message
)
74 IMPL_EVENT_HANDLER(error
)
79 EventSource(nsIGlobalObject
* aGlobal
,
80 nsICookieJarSettings
* aCookieJarSettings
, bool aWithCredentials
);
81 virtual ~EventSource();
83 EventSource(const EventSource
& x
) = delete;
84 EventSource
& operator=(const EventSource
& x
) = delete;
86 void AssertIsOnTargetThread() const {
87 MOZ_ASSERT(NS_IsMainThread() == mIsMainThread
);
90 nsresult
CreateAndDispatchSimpleEvent(const nsAString
& aName
);
92 // This EventSourceImpl is created, managed and destroyed
94 RefPtr
<EventSourceImpl
> mESImpl
;
95 nsString mOriginalURL
;
96 Atomic
<uint32_t> mReadyState
;
97 const bool mWithCredentials
;
98 const bool mIsMainThread
;
102 } // namespace mozilla
104 #endif // mozilla_dom_EventSource_h