Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / EventSource.h
blob22c3f78a3791896e53a7ff5583a8f7a50cabf0b3
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/Atomics.h"
18 #include "mozilla/Attributes.h"
19 #include "mozilla/DOMEventTargetHelper.h"
20 #include "nsDeque.h"
21 #include "nsICookieJarSettings.h"
23 class nsIGlobalObject;
25 namespace mozilla {
27 class ErrorResult;
29 namespace dom {
31 struct EventSourceInit;
33 class EventSourceImpl;
35 class EventSource final : public DOMEventTargetHelper {
36 friend class EventSourceImpl;
38 public:
39 NS_DECL_ISUPPORTS_INHERITED
40 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(EventSource, DOMEventTargetHelper)
41 virtual bool IsCertainlyAliveForCC() const override;
43 // EventTarget
44 void DisconnectFromOwner() override {
45 DOMEventTargetHelper::DisconnectFromOwner();
46 Close();
49 JSObject* WrapObject(JSContext* aCx,
50 JS::Handle<JSObject*> aGivenProto) override;
52 // WebIDL
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();
59 aURL = mOriginalURL;
62 bool WithCredentials() const {
63 AssertIsOnTargetThread();
64 return mWithCredentials;
67 uint16_t ReadyState() const {
68 AssertIsOnTargetThread();
69 return mReadyState;
72 IMPL_EVENT_HANDLER(open)
73 IMPL_EVENT_HANDLER(message)
74 IMPL_EVENT_HANDLER(error)
76 void Close();
78 private:
79 EventSource(nsIGlobalObject* aGlobal,
80 nsICookieJarSettings* aCookieJarSettings, bool aWithCredentials);
81 virtual ~EventSource();
82 // prevent bad usage
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
93 // by EventSource.
94 RefPtr<EventSourceImpl> mESImpl;
95 nsString mOriginalURL;
96 Atomic<uint32_t> mReadyState;
97 const bool mWithCredentials;
98 const bool mIsMainThread;
101 } // namespace dom
102 } // namespace mozilla
104 #endif // mozilla_dom_EventSource_h