1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * The Mozilla Foundation
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Michal Novotny <michal.novotny@gmail.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsWyciwyg.h"
39 :
40 : #include "mozilla/net/WyciwygChannelParent.h"
41 : #include "nsWyciwygChannel.h"
42 : #include "nsNetUtil.h"
43 : #include "nsISupportsPriority.h"
44 : #include "nsIParser.h"
45 : #include "nsISerializable.h"
46 : #include "nsSerializationHelper.h"
47 :
48 : namespace mozilla {
49 : namespace net {
50 :
51 0 : WyciwygChannelParent::WyciwygChannelParent()
52 0 : : mIPCClosed(false)
53 : {
54 : #if defined(PR_LOGGING)
55 0 : if (!gWyciwygLog)
56 0 : gWyciwygLog = PR_NewLogModule("nsWyciwygChannel");
57 : #endif
58 0 : }
59 :
60 0 : WyciwygChannelParent::~WyciwygChannelParent()
61 : {
62 0 : }
63 :
64 : void
65 0 : WyciwygChannelParent::ActorDestroy(ActorDestroyReason why)
66 : {
67 : // We may still have refcount>0 if the channel hasn't called OnStopRequest
68 : // yet, but we must not send any more msgs to child.
69 0 : mIPCClosed = true;
70 0 : }
71 :
72 : //-----------------------------------------------------------------------------
73 : // WyciwygChannelParent::nsISupports
74 : //-----------------------------------------------------------------------------
75 :
76 0 : NS_IMPL_ISUPPORTS2(WyciwygChannelParent,
77 : nsIStreamListener,
78 : nsIRequestObserver);
79 :
80 : //-----------------------------------------------------------------------------
81 : // WyciwygChannelParent::PWyciwygChannelParent
82 : //-----------------------------------------------------------------------------
83 :
84 : bool
85 0 : WyciwygChannelParent::RecvInit(const IPC::URI& aURI)
86 : {
87 : nsresult rv;
88 :
89 0 : nsCOMPtr<nsIURI> uri(aURI);
90 :
91 0 : nsCString uriSpec;
92 0 : uri->GetSpec(uriSpec);
93 0 : LOG(("WyciwygChannelParent RecvInit [this=%x uri=%s]\n",
94 : this, uriSpec.get()));
95 :
96 0 : nsCOMPtr<nsIIOService> ios(do_GetIOService(&rv));
97 0 : if (NS_FAILED(rv))
98 0 : return SendCancelEarly(rv);
99 :
100 0 : nsCOMPtr<nsIChannel> chan;
101 0 : rv = NS_NewChannel(getter_AddRefs(chan), uri, ios);
102 0 : if (NS_FAILED(rv))
103 0 : return SendCancelEarly(rv);
104 :
105 0 : mChannel = do_QueryInterface(chan, &rv);
106 0 : if (NS_FAILED(rv))
107 0 : return SendCancelEarly(rv);
108 :
109 0 : return true;
110 : }
111 :
112 : bool
113 0 : WyciwygChannelParent::RecvAsyncOpen(const IPC::URI& aOriginal,
114 : const PRUint32& aLoadFlags)
115 : {
116 0 : nsCOMPtr<nsIURI> original(aOriginal);
117 :
118 0 : LOG(("WyciwygChannelParent RecvAsyncOpen [this=%x]\n", this));
119 :
120 0 : if (!mChannel)
121 0 : return true;
122 :
123 : nsresult rv;
124 :
125 0 : rv = mChannel->SetOriginalURI(original);
126 0 : if (NS_FAILED(rv))
127 0 : return SendCancelEarly(rv);
128 :
129 0 : rv = mChannel->SetLoadFlags(aLoadFlags);
130 0 : if (NS_FAILED(rv))
131 0 : return SendCancelEarly(rv);
132 :
133 0 : rv = mChannel->AsyncOpen(this, nsnull);
134 0 : if (NS_FAILED(rv))
135 0 : return SendCancelEarly(rv);
136 :
137 0 : return true;
138 : }
139 :
140 : bool
141 0 : WyciwygChannelParent::RecvWriteToCacheEntry(const nsString& data)
142 : {
143 0 : if (mChannel)
144 0 : mChannel->WriteToCacheEntry(data);
145 :
146 0 : return true;
147 : }
148 :
149 : bool
150 0 : WyciwygChannelParent::RecvCloseCacheEntry(const nsresult& reason)
151 : {
152 0 : if (mChannel)
153 0 : mChannel->CloseCacheEntry(reason);
154 :
155 0 : return true;
156 : }
157 :
158 : bool
159 0 : WyciwygChannelParent::RecvSetCharsetAndSource(const PRInt32& aCharsetSource,
160 : const nsCString& aCharset)
161 : {
162 0 : if (mChannel)
163 0 : mChannel->SetCharsetAndSource(aCharsetSource, aCharset);
164 :
165 0 : return true;
166 : }
167 :
168 : bool
169 0 : WyciwygChannelParent::RecvSetSecurityInfo(const nsCString& aSecurityInfo)
170 : {
171 0 : if (mChannel) {
172 0 : nsCOMPtr<nsISupports> securityInfo;
173 0 : NS_DeserializeObject(aSecurityInfo, getter_AddRefs(securityInfo));
174 0 : mChannel->SetSecurityInfo(securityInfo);
175 : }
176 :
177 0 : return true;
178 : }
179 :
180 : bool
181 0 : WyciwygChannelParent::RecvCancel(const nsresult& aStatusCode)
182 : {
183 0 : if (mChannel)
184 0 : mChannel->Cancel(aStatusCode);
185 0 : return true;
186 : }
187 :
188 : //-----------------------------------------------------------------------------
189 : // WyciwygChannelParent::nsIRequestObserver
190 : //-----------------------------------------------------------------------------
191 :
192 : NS_IMETHODIMP
193 0 : WyciwygChannelParent::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
194 : {
195 0 : LOG(("WyciwygChannelParent::OnStartRequest [this=%x]\n", this));
196 :
197 : nsresult rv;
198 :
199 0 : nsCOMPtr<nsIWyciwygChannel> chan = do_QueryInterface(aRequest, &rv);
200 0 : NS_ENSURE_SUCCESS(rv, rv);
201 :
202 : nsresult status;
203 0 : chan->GetStatus(&status);
204 :
205 0 : PRInt32 contentLength = -1;
206 0 : chan->GetContentLength(&contentLength);
207 :
208 0 : PRInt32 charsetSource = kCharsetUninitialized;
209 0 : nsCAutoString charset;
210 0 : chan->GetCharsetAndSource(&charsetSource, charset);
211 :
212 0 : nsCOMPtr<nsISupports> securityInfo;
213 0 : chan->GetSecurityInfo(getter_AddRefs(securityInfo));
214 0 : nsCString secInfoStr;
215 0 : if (securityInfo) {
216 0 : nsCOMPtr<nsISerializable> serializable = do_QueryInterface(securityInfo);
217 0 : if (serializable)
218 0 : NS_SerializeToString(serializable, secInfoStr);
219 : else {
220 0 : NS_ERROR("Can't serialize security info");
221 0 : return NS_ERROR_UNEXPECTED;
222 : }
223 : }
224 :
225 0 : if (mIPCClosed ||
226 0 : !SendOnStartRequest(status, contentLength, charsetSource, charset, secInfoStr)) {
227 0 : return NS_ERROR_UNEXPECTED;
228 : }
229 :
230 0 : return NS_OK;
231 : }
232 :
233 : NS_IMETHODIMP
234 0 : WyciwygChannelParent::OnStopRequest(nsIRequest *aRequest,
235 : nsISupports *aContext,
236 : nsresult aStatusCode)
237 : {
238 0 : LOG(("WyciwygChannelParent::OnStopRequest: [this=%x status=%ul]\n",
239 : this, aStatusCode));
240 :
241 0 : if (mIPCClosed || !SendOnStopRequest(aStatusCode)) {
242 0 : return NS_ERROR_UNEXPECTED;
243 : }
244 :
245 0 : return NS_OK;
246 : }
247 :
248 : //-----------------------------------------------------------------------------
249 : // WyciwygChannelParent::nsIStreamListener
250 : //-----------------------------------------------------------------------------
251 :
252 : NS_IMETHODIMP
253 0 : WyciwygChannelParent::OnDataAvailable(nsIRequest *aRequest,
254 : nsISupports *aContext,
255 : nsIInputStream *aInputStream,
256 : PRUint32 aOffset,
257 : PRUint32 aCount)
258 : {
259 0 : LOG(("WyciwygChannelParent::OnDataAvailable [this=%x]\n", this));
260 :
261 0 : nsCString data;
262 0 : nsresult rv = NS_ReadInputStreamToString(aInputStream, data, aCount);
263 0 : if (NS_FAILED(rv))
264 0 : return rv;
265 :
266 0 : if (mIPCClosed || !SendOnDataAvailable(data, aOffset)) {
267 0 : return NS_ERROR_UNEXPECTED;
268 : }
269 :
270 0 : return NS_OK;
271 : }
272 :
273 : }} // mozilla::net
|