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.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2011
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Josh Matthews <josh@joshmatthews.net>
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 : #include "WebSocketLog.h"
41 : #include "WebSocketChannelParent.h"
42 : #include "nsIAuthPromptProvider.h"
43 :
44 : namespace mozilla {
45 : namespace net {
46 :
47 0 : NS_IMPL_THREADSAFE_ISUPPORTS2(WebSocketChannelParent,
48 : nsIWebSocketListener,
49 : nsIInterfaceRequestor)
50 :
51 0 : WebSocketChannelParent::WebSocketChannelParent(nsIAuthPromptProvider* aAuthProvider)
52 : : mAuthProvider(aAuthProvider)
53 0 : , mIPCOpen(true)
54 : {
55 : #if defined(PR_LOGGING)
56 0 : if (!webSocketLog)
57 0 : webSocketLog = PR_NewLogModule("nsWebSocket");
58 : #endif
59 0 : }
60 :
61 : bool
62 0 : WebSocketChannelParent::RecvDeleteSelf()
63 : {
64 0 : LOG(("WebSocketChannelParent::RecvDeleteSelf() %p\n", this));
65 0 : mChannel = nsnull;
66 0 : mAuthProvider = nsnull;
67 0 : return mIPCOpen ? Send__delete__(this) : true;
68 : }
69 :
70 : bool
71 0 : WebSocketChannelParent::RecvAsyncOpen(const IPC::URI& aURI,
72 : const nsCString& aOrigin,
73 : const nsCString& aProtocol,
74 : const bool& aSecure)
75 : {
76 0 : LOG(("WebSocketChannelParent::RecvAsyncOpen() %p\n", this));
77 : nsresult rv;
78 0 : if (aSecure) {
79 : mChannel =
80 0 : do_CreateInstance("@mozilla.org/network/protocol;1?name=wss", &rv);
81 : } else {
82 : mChannel =
83 0 : do_CreateInstance("@mozilla.org/network/protocol;1?name=ws", &rv);
84 : }
85 0 : if (NS_FAILED(rv))
86 0 : goto fail;
87 :
88 0 : rv = mChannel->SetNotificationCallbacks(this);
89 0 : if (NS_FAILED(rv))
90 0 : goto fail;
91 :
92 0 : rv = mChannel->SetProtocol(aProtocol);
93 0 : if (NS_FAILED(rv))
94 0 : goto fail;
95 :
96 0 : rv = mChannel->AsyncOpen(aURI, aOrigin, this, nsnull);
97 0 : if (NS_FAILED(rv))
98 0 : goto fail;
99 :
100 0 : return true;
101 :
102 : fail:
103 0 : mChannel = nsnull;
104 0 : return SendOnStop(rv);
105 : }
106 :
107 : bool
108 0 : WebSocketChannelParent::RecvClose(const PRUint16& code, const nsCString& reason)
109 : {
110 0 : LOG(("WebSocketChannelParent::RecvClose() %p\n", this));
111 0 : if (mChannel) {
112 0 : nsresult rv = mChannel->Close(code, reason);
113 0 : NS_ENSURE_SUCCESS(rv, true);
114 : }
115 0 : return true;
116 : }
117 :
118 : bool
119 0 : WebSocketChannelParent::RecvSendMsg(const nsCString& aMsg)
120 : {
121 0 : LOG(("WebSocketChannelParent::RecvSendMsg() %p\n", this));
122 0 : if (mChannel) {
123 0 : nsresult rv = mChannel->SendMsg(aMsg);
124 0 : NS_ENSURE_SUCCESS(rv, true);
125 : }
126 0 : return true;
127 : }
128 :
129 : bool
130 0 : WebSocketChannelParent::RecvSendBinaryMsg(const nsCString& aMsg)
131 : {
132 0 : LOG(("WebSocketChannelParent::RecvSendBinaryMsg() %p\n", this));
133 0 : if (mChannel) {
134 0 : nsresult rv = mChannel->SendBinaryMsg(aMsg);
135 0 : NS_ENSURE_SUCCESS(rv, true);
136 : }
137 0 : return true;
138 : }
139 :
140 : bool
141 0 : WebSocketChannelParent::RecvSendBinaryStream(const InputStream& aStream,
142 : const PRUint32& aLength)
143 : {
144 0 : LOG(("WebSocketChannelParent::RecvSendBinaryStream() %p\n", this));
145 0 : if (mChannel) {
146 0 : nsresult rv = mChannel->SendBinaryStream(aStream, aLength);
147 0 : NS_ENSURE_SUCCESS(rv, true);
148 : }
149 0 : return true;
150 : }
151 :
152 : NS_IMETHODIMP
153 0 : WebSocketChannelParent::GetInterface(const nsIID & iid, void **result NS_OUTPARAM)
154 : {
155 0 : LOG(("WebSocketChannelParent::GetInterface() %p\n", this));
156 0 : if (mAuthProvider && iid.Equals(NS_GET_IID(nsIAuthPromptProvider)))
157 0 : return mAuthProvider->GetAuthPrompt(nsIAuthPromptProvider::PROMPT_NORMAL,
158 0 : iid, result);
159 :
160 0 : return NS_ERROR_FAILURE;
161 : }
162 :
163 : NS_IMETHODIMP
164 0 : WebSocketChannelParent::OnStart(nsISupports *aContext)
165 : {
166 0 : LOG(("WebSocketChannelParent::OnStart() %p\n", this));
167 0 : nsCAutoString protocol, extensions;
168 0 : if (mChannel) {
169 0 : mChannel->GetProtocol(protocol);
170 0 : mChannel->GetExtensions(extensions);
171 : }
172 0 : if (!mIPCOpen || !SendOnStart(protocol, extensions)) {
173 0 : return NS_ERROR_FAILURE;
174 : }
175 0 : return NS_OK;
176 : }
177 :
178 : NS_IMETHODIMP
179 0 : WebSocketChannelParent::OnStop(nsISupports *aContext, nsresult aStatusCode)
180 : {
181 0 : LOG(("WebSocketChannelParent::OnStop() %p\n", this));
182 0 : if (!mIPCOpen || !SendOnStop(aStatusCode)) {
183 0 : return NS_ERROR_FAILURE;
184 : }
185 0 : return NS_OK;
186 : }
187 :
188 : NS_IMETHODIMP
189 0 : WebSocketChannelParent::OnMessageAvailable(nsISupports *aContext, const nsACString& aMsg)
190 : {
191 0 : LOG(("WebSocketChannelParent::OnMessageAvailable() %p\n", this));
192 0 : if (!mIPCOpen || !SendOnMessageAvailable(nsCString(aMsg))) {
193 0 : return NS_ERROR_FAILURE;
194 : }
195 0 : return NS_OK;
196 : }
197 :
198 : NS_IMETHODIMP
199 0 : WebSocketChannelParent::OnBinaryMessageAvailable(nsISupports *aContext, const nsACString& aMsg)
200 : {
201 0 : LOG(("WebSocketChannelParent::OnBinaryMessageAvailable() %p\n", this));
202 0 : if (!mIPCOpen || !SendOnBinaryMessageAvailable(nsCString(aMsg))) {
203 0 : return NS_ERROR_FAILURE;
204 : }
205 0 : return NS_OK;
206 : }
207 :
208 : NS_IMETHODIMP
209 0 : WebSocketChannelParent::OnAcknowledge(nsISupports *aContext, PRUint32 aSize)
210 : {
211 0 : LOG(("WebSocketChannelParent::OnAcknowledge() %p\n", this));
212 0 : if (!mIPCOpen || !SendOnAcknowledge(aSize)) {
213 0 : return NS_ERROR_FAILURE;
214 : }
215 0 : return NS_OK;
216 : }
217 :
218 : NS_IMETHODIMP
219 0 : WebSocketChannelParent::OnServerClose(nsISupports *aContext,
220 : PRUint16 code, const nsACString & reason)
221 : {
222 0 : LOG(("WebSocketChannelParent::OnServerClose() %p\n", this));
223 0 : if (!mIPCOpen || !SendOnServerClose(code, nsCString(reason))) {
224 0 : return NS_ERROR_FAILURE;
225 : }
226 0 : return NS_OK;
227 : }
228 :
229 : void
230 0 : WebSocketChannelParent::ActorDestroy(ActorDestroyReason why)
231 : {
232 0 : LOG(("WebSocketChannelParent::ActorDestroy() %p\n", this));
233 0 : mIPCOpen = false;
234 0 : }
235 :
236 : } // namespace net
237 : } // namespace mozilla
|