Line data Source code
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/. */
6 :
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".
12 : */
13 :
14 : #ifndef mozilla_dom_EventSource_h
15 : #define mozilla_dom_EventSource_h
16 :
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"
23 : #include "nsITimer.h"
24 : #include "nsIHttpChannel.h"
25 : #include "nsWeakReference.h"
26 : #include "nsDeque.h"
27 :
28 : class nsPIDOMWindowInner;
29 :
30 : namespace mozilla {
31 :
32 : class ErrorResult;
33 :
34 : namespace dom {
35 :
36 : struct EventSourceInit;
37 :
38 : class EventSourceImpl;
39 :
40 : class EventSource final : public DOMEventTargetHelper
41 : {
42 : friend class EventSourceImpl;
43 : public:
44 : NS_DECL_ISUPPORTS_INHERITED
45 0 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(EventSource, DOMEventTargetHelper)
46 : virtual bool IsCertainlyAliveForCC() const override;
47 :
48 : // EventTarget
49 0 : void DisconnectFromOwner() override
50 : {
51 0 : DOMEventTargetHelper::DisconnectFromOwner();
52 0 : Close();
53 0 : }
54 :
55 : JSObject* WrapObject(JSContext* aCx,
56 : JS::Handle<JSObject*> aGivenProto) override;
57 :
58 : // WebIDL
59 : static already_AddRefed<EventSource>
60 : Constructor(const GlobalObject& aGlobal, const nsAString& aURL,
61 : const EventSourceInit& aEventSourceInitDict, ErrorResult& aRv);
62 :
63 0 : void GetUrl(nsAString& aURL) const
64 : {
65 0 : AssertIsOnTargetThread();
66 0 : aURL = mOriginalURL;
67 0 : }
68 :
69 0 : bool WithCredentials() const
70 : {
71 0 : AssertIsOnTargetThread();
72 0 : return mWithCredentials;
73 : }
74 :
75 0 : uint16_t ReadyState() const
76 : {
77 0 : AssertIsOnTargetThread();
78 0 : return mReadyState;
79 : }
80 :
81 0 : IMPL_EVENT_HANDLER(open)
82 0 : IMPL_EVENT_HANDLER(message)
83 0 : IMPL_EVENT_HANDLER(error)
84 :
85 : void Close();
86 :
87 : private:
88 : EventSource(nsPIDOMWindowInner* aOwnerWindow, bool aWithCredentials);
89 : virtual ~EventSource();
90 : // prevent bad usage
91 : EventSource(const EventSource& x) = delete;
92 : EventSource& operator=(const EventSource& x) = delete;
93 :
94 0 : void AssertIsOnTargetThread() const
95 : {
96 0 : MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
97 0 : }
98 :
99 : nsresult CreateAndDispatchSimpleEvent(const nsAString& aName);
100 :
101 : // Raw pointer because this EventSourceImpl is created, managed and destroyed
102 : // by EventSource.
103 : EventSourceImpl* mImpl;
104 : nsString mOriginalURL;
105 : uint16_t mReadyState;
106 : bool mWithCredentials;
107 : bool mIsMainThread;
108 : // This is used to keep EventSourceImpl instance when there is a connection.
109 : bool mKeepingAlive;
110 :
111 : void UpdateMustKeepAlive();
112 : void UpdateDontKeepAlive();
113 : };
114 :
115 : } // namespace dom
116 : } // namespace mozilla
117 :
118 : #endif // mozilla_dom_EventSource_h
|