1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set sw=2 ts=8 et tw=80 : */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Wellington Fernando de Macedo.
20 : * Portions created by the Initial Developer are Copyright (C) 2009
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Wellington Fernando de Macedo <wfernandom2004@gmail.com> (original author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef nsWebSocket_h__
41 : #define nsWebSocket_h__
42 :
43 : #include "nsISupportsUtils.h"
44 : #include "nsIWebSocket.h"
45 : #include "nsCOMPtr.h"
46 : #include "nsString.h"
47 : #include "nsIJSNativeInitializer.h"
48 : #include "nsIPrincipal.h"
49 : #include "nsCycleCollectionParticipant.h"
50 : #include "nsIDOMEventListener.h"
51 : #include "nsDOMEventTargetHelper.h"
52 : #include "nsAutoPtr.h"
53 : #include "nsIDOMDOMStringList.h"
54 : #include "nsIInterfaceRequestor.h"
55 : #include "nsIWebSocketChannel.h"
56 : #include "nsIWebSocketListener.h"
57 : #include "nsIObserver.h"
58 : #include "nsIRequest.h"
59 : #include "nsWeakReference.h"
60 :
61 : #define DEFAULT_WS_SCHEME_PORT 80
62 : #define DEFAULT_WSS_SCHEME_PORT 443
63 :
64 : #define NS_WEBSOCKET_CID \
65 : { /* 7ca25214-98dc-40a6-bc1f-41ddbe41f46c */ \
66 : 0x7ca25214, 0x98dc, 0x40a6, \
67 : {0xbc, 0x1f, 0x41, 0xdd, 0xbe, 0x41, 0xf4, 0x6c} }
68 :
69 : #define NS_WEBSOCKET_CONTRACTID "@mozilla.org/websocket;1"
70 :
71 : class nsWSCloseEvent;
72 : class nsAutoCloseWS;
73 :
74 : class nsWebSocket: public nsDOMEventTargetHelper,
75 : public nsIWebSocket,
76 : public nsIJSNativeInitializer,
77 : public nsIInterfaceRequestor,
78 : public nsIWebSocketListener,
79 : public nsIObserver,
80 : public nsSupportsWeakReference,
81 : public nsIRequest
82 : {
83 : friend class nsWSCloseEvent;
84 : friend class nsAutoCloseWS;
85 :
86 : public:
87 : nsWebSocket();
88 : virtual ~nsWebSocket();
89 : NS_DECL_ISUPPORTS_INHERITED
90 1464 : NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(nsWebSocket,
91 : nsDOMEventTargetHelper)
92 : NS_DECL_NSIWEBSOCKET
93 : NS_DECL_NSIINTERFACEREQUESTOR
94 : NS_DECL_NSIWEBSOCKETLISTENER
95 : NS_DECL_NSIOBSERVER
96 : NS_DECL_NSIREQUEST
97 :
98 : // nsIJSNativeInitializer
99 : NS_IMETHOD Initialize(nsISupports* aOwner, JSContext* aContext,
100 : JSObject* aObject, PRUint32 aArgc, jsval* aArgv);
101 :
102 : // nsIDOMEventTarget
103 : NS_IMETHOD AddEventListener(const nsAString& aType,
104 : nsIDOMEventListener *aListener,
105 : bool aUseCapture,
106 : bool aWantsUntrusted,
107 : PRUint8 optional_argc);
108 : NS_IMETHOD RemoveEventListener(const nsAString& aType,
109 : nsIDOMEventListener* aListener,
110 : bool aUseCapture);
111 :
112 : // Determine if preferences allow WebSocket
113 : static bool PrefEnabled();
114 :
115 : virtual void DisconnectFromOwner();
116 : protected:
117 : nsresult ParseURL(const nsString& aURL);
118 : nsresult EstablishConnection();
119 :
120 : // These methods when called can release the WebSocket object
121 : nsresult FailConnection(PRUint16 reasonCode,
122 : const nsACString& aReasonString = EmptyCString());
123 : void FailConnectionQuietly();
124 : nsresult CloseConnection(PRUint16 reasonCode,
125 0 : const nsACString& aReasonString = EmptyCString());
126 : nsresult Disconnect();
127 :
128 : nsresult ConsoleError();
129 : nsresult PrintErrorOnConsole(const char *aBundleURI,
130 : const PRUnichar *aError,
131 : const PRUnichar **aFormatStrings,
132 : PRUint32 aFormatStringsLen);
133 :
134 : nsresult ConvertTextToUTF8(const nsString& aMessage, nsCString& buf);
135 :
136 : // Get msg info out of JS variable being sent (string, arraybuffer, blob)
137 : nsresult GetSendParams(nsIVariant *aData, nsCString &aStringOut,
138 : nsCOMPtr<nsIInputStream> &aStreamOut,
139 : bool &aIsBinary, PRUint32 &aOutgoingLength);
140 :
141 : nsresult DoOnMessageAvailable(const nsACString & aMsg, bool isBinary);
142 : nsresult CreateAndDispatchSimpleEvent(const nsString& aName);
143 : nsresult CreateAndDispatchMessageEvent(const nsACString& aData,
144 : bool isBinary);
145 : nsresult CreateAndDispatchCloseEvent(bool aWasClean, PRUint16 aCode,
146 : const nsString &aReason);
147 : nsresult CreateResponseBlob(const nsACString& aData, JSContext *aCx,
148 : jsval &jsData);
149 :
150 : void SetReadyState(PRUint16 aNewReadyState);
151 :
152 : // if there are "strong event listeners" (see comment in nsWebSocket.cpp) or
153 : // outgoing not sent messages then this method keeps the object alive
154 : // when js doesn't have strong references to it.
155 : void UpdateMustKeepAlive();
156 : // ATTENTION, when calling this method the object can be released
157 : // (and possibly collected).
158 : void DontKeepAliveAnyMore();
159 :
160 : nsresult UpdateURI();
161 :
162 : nsCOMPtr<nsIWebSocketChannel> mChannel;
163 :
164 : nsRefPtr<nsDOMEventListenerWrapper> mOnOpenListener;
165 : nsRefPtr<nsDOMEventListenerWrapper> mOnErrorListener;
166 : nsRefPtr<nsDOMEventListenerWrapper> mOnMessageListener;
167 : nsRefPtr<nsDOMEventListenerWrapper> mOnCloseListener;
168 :
169 : // related to the WebSocket constructor steps
170 : nsString mOriginalURL;
171 : nsString mEffectiveURL; // after redirects
172 : bool mSecure; // if true it is using SSL and the wss scheme,
173 : // otherwise it is using the ws scheme with no SSL
174 :
175 : bool mKeepingAlive;
176 : bool mCheckMustKeepAlive;
177 : bool mTriggeredCloseEvent;
178 : bool mDisconnected;
179 :
180 : // Set attributes of DOM 'onclose' message
181 : bool mCloseEventWasClean;
182 : nsString mCloseEventReason;
183 : PRUint16 mCloseEventCode;
184 :
185 : nsCString mAsciiHost; // hostname
186 : PRUint32 mPort;
187 : nsCString mResource; // [filepath[?query]]
188 : nsString mUTF16Origin;
189 :
190 : nsCOMPtr<nsIURI> mURI;
191 : nsCString mRequestedProtocolList;
192 : nsCString mEstablishedProtocol;
193 : nsCString mEstablishedExtensions;
194 :
195 : PRUint16 mReadyState;
196 :
197 : nsCOMPtr<nsIPrincipal> mPrincipal;
198 :
199 : PRUint32 mOutgoingBufferedAmount;
200 :
201 : enum
202 : {
203 : WS_BINARY_TYPE_ARRAYBUFFER,
204 : WS_BINARY_TYPE_BLOB,
205 : } mBinaryType;
206 :
207 : // Web Socket owner information:
208 : // - the script file name, UTF8 encoded.
209 : // - source code line number where the Web Socket object was constructed.
210 : // - the ID of the inner window where the script lives. Note that this may not
211 : // be the same as the Web Socket owner window.
212 : // These attributes are used for error reporting.
213 : nsCString mScriptFile;
214 : PRUint32 mScriptLine;
215 : PRUint64 mInnerWindowID;
216 :
217 : private:
218 : nsWebSocket(const nsWebSocket& x); // prevent bad usage
219 : nsWebSocket& operator=(const nsWebSocket& x);
220 : };
221 :
222 : #endif
|