Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
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 */
13 #ifndef mozilla_dom_EventSource_h
14 #define mozilla_dom_EventSource_h
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"
28 class nsPIDOMWindow;
30 namespace mozilla {
32 class ErrorResult;
34 namespace dom {
36 class AsyncVerifyRedirectCallbackFwr;
37 struct EventSourceInit;
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;
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)
55 NS_DECL_NSIOBSERVER
56 NS_DECL_NSISTREAMLISTENER
57 NS_DECL_NSIREQUESTOBSERVER
58 NS_DECL_NSICHANNELEVENTSINK
59 NS_DECL_NSIINTERFACEREQUESTOR
61 // nsWrapperCache
62 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
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);
75 void GetUrl(nsAString& aURL) const
76 {
77 aURL = mOriginalURL;
78 }
79 bool WithCredentials() const
80 {
81 return mWithCredentials;
82 }
84 enum {
85 CONNECTING = 0U,
86 OPEN = 1U,
87 CLOSED = 2U
88 };
89 uint16_t ReadyState() const
90 {
91 return mReadyState;
92 }
94 IMPL_EVENT_HANDLER(open)
95 IMPL_EVENT_HANDLER(message)
96 IMPL_EVENT_HANDLER(error)
97 void Close();
99 // Determine if preferences allow EventSource
100 static bool PrefEnabled(JSContext* aCx = nullptr, JSObject* aGlobal = nullptr);
102 virtual void DisconnectFromOwner() MOZ_OVERRIDE;
104 protected:
105 nsresult Init(nsISupports* aOwner,
106 const nsAString& aURL,
107 bool aWithCredentials);
109 nsresult GetBaseURI(nsIURI **aBaseURI);
111 nsresult SetupHttpChannel();
112 nsresult InitChannelAndRequestEventSource();
113 nsresult ResetConnection();
114 nsresult DispatchFailConnection();
115 nsresult SetReconnectionTimeout();
117 void AnnounceConnection();
118 void DispatchAllMessageEvents();
119 void ReestablishConnection();
120 void FailConnection();
122 nsresult Thaw();
123 nsresult Freeze();
125 static void TimerCallback(nsITimer *aTimer, void *aClosure);
127 nsresult PrintErrorOnConsole(const char *aBundleURI,
128 const char16_t *aError,
129 const char16_t **aFormatStrings,
130 uint32_t aFormatStringsLen);
131 nsresult ConsoleError();
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);
148 nsCOMPtr<nsIURI> mSrc;
150 nsString mLastEventID;
151 uint32_t mReconnectionTime; // in ms
153 struct Message {
154 nsString mEventName;
155 nsString mLastEventID;
156 nsString mData;
157 };
158 nsDeque mMessagesToDispatch;
159 Message mCurrentMessage;
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;
218 bool mFrozen;
219 bool mErrorLoadOnRedirect;
220 bool mGoingToDispatchAllMessages;
221 bool mWithCredentials;
222 bool mWaitingForOnStopRequest;
223 bool mInterrupted;
225 // used while reading the input streams
226 nsCOMPtr<nsIUnicodeDecoder> mUnicodeDecoder;
227 nsresult mLastConvertionResult;
228 nsString mLastFieldName;
229 nsString mLastFieldValue;
231 nsCOMPtr<nsILoadGroup> mLoadGroup;
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;
240 nsCOMPtr<nsIHttpChannel> mHttpChannel;
242 nsCOMPtr<nsITimer> mTimer;
244 uint16_t mReadyState;
245 nsString mOriginalURL;
247 nsCOMPtr<nsIPrincipal> mPrincipal;
248 nsString mOrigin;
250 uint32_t mRedirectFlags;
251 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
252 nsCOMPtr<nsIChannel> mNewRedirectChannel;
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;
264 private:
265 EventSource(const EventSource& x); // prevent bad usage
266 EventSource& operator=(const EventSource& x);
267 };
269 } // namespace dom
270 } // namespace mozilla
272 #endif // mozilla_dom_EventSource_h