Bug 1665252 - remove allowpaymentrequest attribute from HTMLIFrameElement r=dom-worke...
[gecko.git] / dom / base / EventSource.h
blob36dbb8f85a5bfaaddb6cd471310892f51e638021
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/. */
7 /*
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"
11 * and "Accept".
14 #ifndef mozilla_dom_EventSource_h
15 #define mozilla_dom_EventSource_h
17 #include "mozilla/Attributes.h"
18 #include "mozilla/DOMEventTargetHelper.h"
19 #include "nsDeque.h"
21 class nsIGlobalObject;
22 class nsICookieJarSettings;
24 namespace mozilla {
26 class ErrorResult;
28 namespace dom {
30 struct EventSourceInit;
32 class EventSourceImpl;
34 class EventSource final : public DOMEventTargetHelper {
35 friend class EventSourceImpl;
37 public:
38 NS_DECL_ISUPPORTS_INHERITED
39 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(EventSource, DOMEventTargetHelper)
40 virtual bool IsCertainlyAliveForCC() const override;
42 // EventTarget
43 void DisconnectFromOwner() override {
44 DOMEventTargetHelper::DisconnectFromOwner();
45 Close();
48 JSObject* WrapObject(JSContext* aCx,
49 JS::Handle<JSObject*> aGivenProto) override;
51 // WebIDL
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();
58 aURL = mOriginalURL;
61 bool WithCredentials() const {
62 AssertIsOnTargetThread();
63 return mWithCredentials;
66 uint16_t ReadyState() const {
67 AssertIsOnTargetThread();
68 return mReadyState;
71 IMPL_EVENT_HANDLER(open)
72 IMPL_EVENT_HANDLER(message)
73 IMPL_EVENT_HANDLER(error)
75 void Close();
77 private:
78 EventSource(nsIGlobalObject* aGlobal,
79 nsICookieJarSettings* aCookieJarSettings, bool aWithCredentials);
80 virtual ~EventSource();
81 // prevent bad usage
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
92 // by EventSource.
93 EventSourceImpl* mImpl;
94 nsString mOriginalURL;
95 uint16_t mReadyState;
96 bool mWithCredentials;
97 bool mIsMainThread;
98 // This is used to keep EventSourceImpl instance when there is a connection.
99 bool mKeepingAlive;
101 void UpdateMustKeepAlive();
102 void UpdateDontKeepAlive();
105 } // namespace dom
106 } // namespace mozilla
108 #endif // mozilla_dom_EventSource_h