Bug 346849: Add a "Save Image as..." entry to the context menu for <canvas>, Original...
[mozilla-central.git] / netwerk / test / TestStreamTransport.cpp
blobad2b9463f891341aa1a9809eef85c23ce8449ebd
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
14 * The Original Code is Mozilla.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2002
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Darin Fisher <darin@netscape.com>
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.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestCommon.h"
39 #include "nsIComponentRegistrar.h"
40 #include "nsIStreamTransportService.h"
41 #include "nsIAsyncInputStream.h"
42 #include "nsIProgressEventSink.h"
43 #include "nsIInterfaceRequestor.h"
44 #include "nsIInterfaceRequestorUtils.h"
45 #include "nsIProxyObjectManager.h"
46 #include "nsIRequest.h"
47 #include "nsIServiceManager.h"
48 #include "nsIComponentManager.h"
49 #include "nsCOMPtr.h"
50 #include "nsMemory.h"
51 #include "nsStringAPI.h"
52 #include "nsIFileStreams.h"
53 #include "nsIStreamListener.h"
54 #include "nsILocalFile.h"
55 #include "nsNetUtil.h"
56 #include "nsAutoLock.h"
57 #include "prlog.h"
58 #include "prenv.h"
60 ////////////////////////////////////////////////////////////////////////////////
62 #if defined(PR_LOGGING)
64 // set NSPR_LOG_MODULES=Test:5
66 static PRLogModuleInfo *gTestLog = nsnull;
67 #endif
68 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
70 ////////////////////////////////////////////////////////////////////////////////
72 static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
74 ////////////////////////////////////////////////////////////////////////////////
76 #define CHUNK_SIZE 500
78 class MyCopier : public nsIInputStreamCallback
79 , public nsIOutputStreamCallback
81 public:
82 NS_DECL_ISUPPORTS
84 MyCopier()
85 : mLock(nsnull)
86 , mInputCondition(NS_OK)
90 virtual ~MyCopier()
92 if (mLock)
93 PR_DestroyLock(mLock);
94 if (mInput)
95 mInput->Close();
96 if (mOutput)
97 mOutput->Close();
100 // called on any thread
101 NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream *inStr)
103 LOG(("OnInputStreamReady\n"));
104 nsAutoLock lock(mLock);
105 NS_ASSERTION(inStr == mInput, "unexpected stream");
106 Process_Locked();
107 return NS_OK;
110 // called on any thread
111 NS_IMETHOD OnOutputStreamReady(nsIAsyncOutputStream *outStr)
113 LOG(("OnOutputStreamReady\n"));
114 nsAutoLock lock(mLock);
115 NS_ASSERTION(outStr == mOutput, "unexpected stream");
116 Process_Locked();
117 return NS_OK;
120 void Close_Locked()
122 LOG(("Close_Locked\n"));
124 mOutput->Close();
125 mOutput = 0;
126 mInput->Close();
127 mInput = 0;
129 // post done copying event
130 QuitPumpingEvents();
133 void Process_Locked()
135 while (1) {
136 mInputCondition = NS_OK; // reset
138 PRUint32 n;
139 nsresult rv = mOutput->WriteSegments(FillOutputBuffer, this, CHUNK_SIZE, &n);
140 if (NS_FAILED(rv) || (n == 0)) {
141 if (rv == NS_BASE_STREAM_WOULD_BLOCK)
142 mOutput->AsyncWait(this, 0, 0, nsnull);
143 else if (mInputCondition == NS_BASE_STREAM_WOULD_BLOCK)
144 mInput->AsyncWait(this, 0, 0, nsnull);
145 else
146 Close_Locked();
147 break;
152 nsresult AsyncCopy(nsITransport *srcTrans, nsITransport *destTrans)
154 mLock = PR_NewLock();
155 if (!mLock)
156 return NS_ERROR_OUT_OF_MEMORY;
158 nsresult rv;
160 nsCOMPtr<nsIInputStream> inStr;
161 rv = srcTrans->OpenInputStream(0, 0, 0, getter_AddRefs(inStr));
162 if (NS_FAILED(rv)) return rv;
164 nsCOMPtr<nsIOutputStream> outStr;
165 rv = destTrans->OpenOutputStream(0, 0, 0, getter_AddRefs(outStr));
166 if (NS_FAILED(rv)) return rv;
168 mInput = do_QueryInterface(inStr);
169 mOutput = do_QueryInterface(outStr);
171 return mInput->AsyncWait(this, 0, 0, nsnull);
174 static NS_METHOD FillOutputBuffer(nsIOutputStream *outStr,
175 void *closure,
176 char *buffer,
177 PRUint32 offset,
178 PRUint32 count,
179 PRUint32 *countRead)
181 MyCopier *self = (MyCopier *) closure;
183 nsresult rv = self->mInput->Read(buffer, count, countRead);
184 if (NS_FAILED(rv))
185 self->mInputCondition = rv;
186 else if (*countRead == 0)
187 self->mInputCondition = NS_BASE_STREAM_CLOSED;
189 return self->mInputCondition;
192 protected:
193 PRLock *mLock;
194 nsCOMPtr<nsIAsyncInputStream> mInput;
195 nsCOMPtr<nsIAsyncOutputStream> mOutput;
196 nsresult mInputCondition;
199 NS_IMPL_THREADSAFE_ISUPPORTS2(MyCopier,
200 nsIInputStreamCallback,
201 nsIOutputStreamCallback)
203 ////////////////////////////////////////////////////////////////////////////////
206 * asynchronously copy file.
208 static nsresult
209 RunTest(nsIFile *srcFile, nsIFile *destFile)
211 nsresult rv;
213 LOG(("RunTest\n"));
215 nsCOMPtr<nsIStreamTransportService> sts =
216 do_GetService(kStreamTransportServiceCID, &rv);
217 if (NS_FAILED(rv)) return rv;
219 nsCOMPtr<nsIInputStream> srcStr;
220 rv = NS_NewLocalFileInputStream(getter_AddRefs(srcStr), srcFile);
221 if (NS_FAILED(rv)) return rv;
223 nsCOMPtr<nsIOutputStream> destStr;
224 rv = NS_NewLocalFileOutputStream(getter_AddRefs(destStr), destFile);
225 if (NS_FAILED(rv)) return rv;
227 nsCOMPtr<nsITransport> srcTransport;
228 rv = sts->CreateInputTransport(srcStr, nsInt64(-1), nsInt64(-1), PR_TRUE,
229 getter_AddRefs(srcTransport));
230 if (NS_FAILED(rv)) return rv;
232 nsCOMPtr<nsITransport> destTransport;
233 rv = sts->CreateOutputTransport(destStr, nsInt64(-1), nsInt64(-1), PR_TRUE,
234 getter_AddRefs(destTransport));
235 if (NS_FAILED(rv)) return rv;
237 MyCopier *copier = new MyCopier();
238 if (copier == nsnull)
239 return NS_ERROR_OUT_OF_MEMORY;
240 NS_ADDREF(copier);
242 rv = copier->AsyncCopy(srcTransport, destTransport);
243 if (NS_FAILED(rv)) return rv;
245 PumpEvents();
247 NS_RELEASE(copier);
248 return NS_OK;
251 ////////////////////////////////////////////////////////////////////////////////
253 static nsresult
254 RunBlockingTest(nsIFile *srcFile, nsIFile *destFile)
256 nsresult rv;
258 LOG(("RunBlockingTest\n"));
260 nsCOMPtr<nsIStreamTransportService> sts =
261 do_GetService(kStreamTransportServiceCID, &rv);
262 if (NS_FAILED(rv)) return rv;
264 nsCOMPtr<nsIInputStream> srcIn;
265 rv = NS_NewLocalFileInputStream(getter_AddRefs(srcIn), srcFile);
266 if (NS_FAILED(rv)) return rv;
268 nsCOMPtr<nsIOutputStream> fileOut;
269 rv = NS_NewLocalFileOutputStream(getter_AddRefs(fileOut), destFile);
270 if (NS_FAILED(rv)) return rv;
272 nsCOMPtr<nsITransport> destTransport;
273 rv = sts->CreateOutputTransport(fileOut, nsInt64(-1), nsInt64(-1),
274 PR_TRUE, getter_AddRefs(destTransport));
275 if (NS_FAILED(rv)) return rv;
277 nsCOMPtr<nsIOutputStream> destOut;
278 rv = destTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(destOut));
279 if (NS_FAILED(rv)) return rv;
281 char buf[120];
282 PRUint32 n;
283 for (;;) {
284 rv = srcIn->Read(buf, sizeof(buf), &n);
285 if (NS_FAILED(rv) || (n == 0)) return rv;
287 rv = destOut->Write(buf, n, &n);
288 if (NS_FAILED(rv)) return rv;
291 return NS_OK;
294 ////////////////////////////////////////////////////////////////////////////////
297 main(int argc, char* argv[])
299 if (test_common_init(&argc, &argv) != 0)
300 return -1;
302 nsresult rv;
304 if (argc < 2) {
305 printf("usage: %s <file-to-read>\n", argv[0]);
306 return -1;
308 char* fileName = argv[1];
310 nsCOMPtr<nsIServiceManager> servMan;
311 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
312 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
313 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
314 if (registrar)
315 registrar->AutoRegister(nsnull);
317 #if defined(PR_LOGGING)
318 gTestLog = PR_NewLogModule("Test");
319 #endif
321 nsCOMPtr<nsILocalFile> srcFile;
322 rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(srcFile));
323 if (NS_FAILED(rv)) return rv;
325 nsCOMPtr<nsIFile> destFile;
326 rv = srcFile->Clone(getter_AddRefs(destFile));
327 if (NS_FAILED(rv)) return rv;
329 nsCAutoString leafName;
330 rv = destFile->GetNativeLeafName(leafName);
331 if (NS_FAILED(rv)) return rv;
333 nsCAutoString newName(leafName);
334 newName.Append(NS_LITERAL_CSTRING(".1"));
335 rv = destFile->SetNativeLeafName(newName);
336 if (NS_FAILED(rv)) return rv;
338 rv = RunTest(srcFile, destFile);
339 NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
341 newName = leafName;
342 newName.Append(NS_LITERAL_CSTRING(".2"));
343 rv = destFile->SetNativeLeafName(newName);
344 if (NS_FAILED(rv)) return rv;
346 rv = RunBlockingTest(srcFile, destFile);
347 NS_ASSERTION(NS_SUCCEEDED(rv), "RunBlockingTest failed");
349 // give background threads a chance to finish whatever work they may
350 // be doing.
351 PR_Sleep(PR_SecondsToInterval(1));
352 } // this scopes the nsCOMPtrs
353 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
354 rv = NS_ShutdownXPCOM(nsnull);
355 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
356 return NS_OK;