|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 * This implementation has support only for http requests. It is because the |
|
8 * spec has defined event streams only for http. HTTP is required because |
|
9 * this implementation uses some http headers: "Last-Event-ID", "Cache-Control" |
|
10 * and "Accept". |
|
11 */ |
|
12 |
|
13 #ifndef mozilla_dom_EventSource_h |
|
14 #define mozilla_dom_EventSource_h |
|
15 |
|
16 #include "mozilla/Attributes.h" |
|
17 #include "mozilla/DOMEventTargetHelper.h" |
|
18 #include "nsIObserver.h" |
|
19 #include "nsIStreamListener.h" |
|
20 #include "nsIChannelEventSink.h" |
|
21 #include "nsIInterfaceRequestor.h" |
|
22 #include "nsITimer.h" |
|
23 #include "nsIHttpChannel.h" |
|
24 #include "nsWeakReference.h" |
|
25 #include "nsDeque.h" |
|
26 #include "nsIUnicodeDecoder.h" |
|
27 |
|
28 class nsPIDOMWindow; |
|
29 |
|
30 namespace mozilla { |
|
31 |
|
32 class ErrorResult; |
|
33 |
|
34 namespace dom { |
|
35 |
|
36 class AsyncVerifyRedirectCallbackFwr; |
|
37 struct EventSourceInit; |
|
38 |
|
39 class EventSource : public DOMEventTargetHelper |
|
40 , public nsIObserver |
|
41 , public nsIStreamListener |
|
42 , public nsIChannelEventSink |
|
43 , public nsIInterfaceRequestor |
|
44 , public nsSupportsWeakReference |
|
45 { |
|
46 friend class AsyncVerifyRedirectCallbackFwr; |
|
47 |
|
48 public: |
|
49 EventSource(nsPIDOMWindow* aOwnerWindow); |
|
50 virtual ~EventSource(); |
|
51 NS_DECL_ISUPPORTS_INHERITED |
|
52 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED( |
|
53 EventSource, DOMEventTargetHelper) |
|
54 |
|
55 NS_DECL_NSIOBSERVER |
|
56 NS_DECL_NSISTREAMLISTENER |
|
57 NS_DECL_NSIREQUESTOBSERVER |
|
58 NS_DECL_NSICHANNELEVENTSINK |
|
59 NS_DECL_NSIINTERFACEREQUESTOR |
|
60 |
|
61 // nsWrapperCache |
|
62 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
63 |
|
64 // WebIDL |
|
65 nsPIDOMWindow* |
|
66 GetParentObject() const |
|
67 { |
|
68 return GetOwner(); |
|
69 } |
|
70 static already_AddRefed<EventSource> |
|
71 Constructor(const GlobalObject& aGlobal, const nsAString& aURL, |
|
72 const EventSourceInit& aEventSourceInitDict, |
|
73 ErrorResult& aRv); |
|
74 |
|
75 void GetUrl(nsAString& aURL) const |
|
76 { |
|
77 aURL = mOriginalURL; |
|
78 } |
|
79 bool WithCredentials() const |
|
80 { |
|
81 return mWithCredentials; |
|
82 } |
|
83 |
|
84 enum { |
|
85 CONNECTING = 0U, |
|
86 OPEN = 1U, |
|
87 CLOSED = 2U |
|
88 }; |
|
89 uint16_t ReadyState() const |
|
90 { |
|
91 return mReadyState; |
|
92 } |
|
93 |
|
94 IMPL_EVENT_HANDLER(open) |
|
95 IMPL_EVENT_HANDLER(message) |
|
96 IMPL_EVENT_HANDLER(error) |
|
97 void Close(); |
|
98 |
|
99 // Determine if preferences allow EventSource |
|
100 static bool PrefEnabled(JSContext* aCx = nullptr, JSObject* aGlobal = nullptr); |
|
101 |
|
102 virtual void DisconnectFromOwner() MOZ_OVERRIDE; |
|
103 |
|
104 protected: |
|
105 nsresult Init(nsISupports* aOwner, |
|
106 const nsAString& aURL, |
|
107 bool aWithCredentials); |
|
108 |
|
109 nsresult GetBaseURI(nsIURI **aBaseURI); |
|
110 |
|
111 nsresult SetupHttpChannel(); |
|
112 nsresult InitChannelAndRequestEventSource(); |
|
113 nsresult ResetConnection(); |
|
114 nsresult DispatchFailConnection(); |
|
115 nsresult SetReconnectionTimeout(); |
|
116 |
|
117 void AnnounceConnection(); |
|
118 void DispatchAllMessageEvents(); |
|
119 void ReestablishConnection(); |
|
120 void FailConnection(); |
|
121 |
|
122 nsresult Thaw(); |
|
123 nsresult Freeze(); |
|
124 |
|
125 static void TimerCallback(nsITimer *aTimer, void *aClosure); |
|
126 |
|
127 nsresult PrintErrorOnConsole(const char *aBundleURI, |
|
128 const char16_t *aError, |
|
129 const char16_t **aFormatStrings, |
|
130 uint32_t aFormatStringsLen); |
|
131 nsresult ConsoleError(); |
|
132 |
|
133 static NS_METHOD StreamReaderFunc(nsIInputStream *aInputStream, |
|
134 void *aClosure, |
|
135 const char *aFromRawSegment, |
|
136 uint32_t aToOffset, |
|
137 uint32_t aCount, |
|
138 uint32_t *aWriteCount); |
|
139 nsresult SetFieldAndClear(); |
|
140 nsresult ClearFields(); |
|
141 nsresult ResetEvent(); |
|
142 nsresult DispatchCurrentMessageEvent(); |
|
143 nsresult ParseCharacter(char16_t aChr); |
|
144 bool CheckCanRequestSrc(nsIURI* aSrc = nullptr); // if null, it tests mSrc |
|
145 nsresult CheckHealthOfRequestCallback(nsIRequest *aRequestCallback); |
|
146 nsresult OnRedirectVerifyCallback(nsresult result); |
|
147 |
|
148 nsCOMPtr<nsIURI> mSrc; |
|
149 |
|
150 nsString mLastEventID; |
|
151 uint32_t mReconnectionTime; // in ms |
|
152 |
|
153 struct Message { |
|
154 nsString mEventName; |
|
155 nsString mLastEventID; |
|
156 nsString mData; |
|
157 }; |
|
158 nsDeque mMessagesToDispatch; |
|
159 Message mCurrentMessage; |
|
160 |
|
161 /** |
|
162 * A simple state machine used to manage the event-source's line buffer |
|
163 * |
|
164 * PARSE_STATE_OFF -> PARSE_STATE_BEGIN_OF_STREAM |
|
165 * |
|
166 * PARSE_STATE_BEGIN_OF_STREAM -> PARSE_STATE_BOM_WAS_READ | |
|
167 * PARSE_STATE_CR_CHAR | |
|
168 * PARSE_STATE_BEGIN_OF_LINE | |
|
169 * PARSE_STATE_COMMENT | |
|
170 * PARSE_STATE_FIELD_NAME |
|
171 * |
|
172 * PARSE_STATE_BOM_WAS_READ -> PARSE_STATE_CR_CHAR | |
|
173 * PARSE_STATE_BEGIN_OF_LINE | |
|
174 * PARSE_STATE_COMMENT | |
|
175 * PARSE_STATE_FIELD_NAME |
|
176 * |
|
177 * PARSE_STATE_CR_CHAR -> PARSE_STATE_CR_CHAR | |
|
178 * PARSE_STATE_COMMENT | |
|
179 * PARSE_STATE_FIELD_NAME | |
|
180 * PARSE_STATE_BEGIN_OF_LINE |
|
181 * |
|
182 * PARSE_STATE_COMMENT -> PARSE_STATE_CR_CHAR | |
|
183 * PARSE_STATE_BEGIN_OF_LINE |
|
184 * |
|
185 * PARSE_STATE_FIELD_NAME -> PARSE_STATE_CR_CHAR | |
|
186 * PARSE_STATE_BEGIN_OF_LINE | |
|
187 * PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE |
|
188 * |
|
189 * PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE -> PARSE_STATE_FIELD_VALUE | |
|
190 * PARSE_STATE_CR_CHAR | |
|
191 * PARSE_STATE_BEGIN_OF_LINE |
|
192 * |
|
193 * PARSE_STATE_FIELD_VALUE -> PARSE_STATE_CR_CHAR | |
|
194 * PARSE_STATE_BEGIN_OF_LINE |
|
195 * |
|
196 * PARSE_STATE_BEGIN_OF_LINE -> PARSE_STATE_CR_CHAR | |
|
197 * PARSE_STATE_COMMENT | |
|
198 * PARSE_STATE_FIELD_NAME | |
|
199 * PARSE_STATE_BEGIN_OF_LINE |
|
200 * |
|
201 * Whenever the parser find an empty line or the end-of-file |
|
202 * it dispatches the stacked event. |
|
203 * |
|
204 */ |
|
205 enum ParserStatus { |
|
206 PARSE_STATE_OFF, |
|
207 PARSE_STATE_BEGIN_OF_STREAM, |
|
208 PARSE_STATE_BOM_WAS_READ, |
|
209 PARSE_STATE_CR_CHAR, |
|
210 PARSE_STATE_COMMENT, |
|
211 PARSE_STATE_FIELD_NAME, |
|
212 PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE, |
|
213 PARSE_STATE_FIELD_VALUE, |
|
214 PARSE_STATE_BEGIN_OF_LINE |
|
215 }; |
|
216 ParserStatus mStatus; |
|
217 |
|
218 bool mFrozen; |
|
219 bool mErrorLoadOnRedirect; |
|
220 bool mGoingToDispatchAllMessages; |
|
221 bool mWithCredentials; |
|
222 bool mWaitingForOnStopRequest; |
|
223 bool mInterrupted; |
|
224 |
|
225 // used while reading the input streams |
|
226 nsCOMPtr<nsIUnicodeDecoder> mUnicodeDecoder; |
|
227 nsresult mLastConvertionResult; |
|
228 nsString mLastFieldName; |
|
229 nsString mLastFieldValue; |
|
230 |
|
231 nsCOMPtr<nsILoadGroup> mLoadGroup; |
|
232 |
|
233 /** |
|
234 * The notification callbacks the channel had initially. |
|
235 * We want to forward things here as needed. |
|
236 */ |
|
237 nsCOMPtr<nsIInterfaceRequestor> mNotificationCallbacks; |
|
238 nsCOMPtr<nsIChannelEventSink> mChannelEventSink; |
|
239 |
|
240 nsCOMPtr<nsIHttpChannel> mHttpChannel; |
|
241 |
|
242 nsCOMPtr<nsITimer> mTimer; |
|
243 |
|
244 uint16_t mReadyState; |
|
245 nsString mOriginalURL; |
|
246 |
|
247 nsCOMPtr<nsIPrincipal> mPrincipal; |
|
248 nsString mOrigin; |
|
249 |
|
250 uint32_t mRedirectFlags; |
|
251 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback; |
|
252 nsCOMPtr<nsIChannel> mNewRedirectChannel; |
|
253 |
|
254 // Event Source owner information: |
|
255 // - the script file name |
|
256 // - source code line number where the Event Source object was constructed. |
|
257 // - the ID of the inner window where the script lives. Note that this may not |
|
258 // be the same as the Event Source owner window. |
|
259 // These attributes are used for error reporting. |
|
260 nsString mScriptFile; |
|
261 uint32_t mScriptLine; |
|
262 uint64_t mInnerWindowID; |
|
263 |
|
264 private: |
|
265 EventSource(const EventSource& x); // prevent bad usage |
|
266 EventSource& operator=(const EventSource& x); |
|
267 }; |
|
268 |
|
269 } // namespace dom |
|
270 } // namespace mozilla |
|
271 |
|
272 #endif // mozilla_dom_EventSource_h |