1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "TestCommon.h"
6 #include "nsIComponentRegistrar.h"
7 #include "nsIStreamTransportService.h"
8 #include "nsIAsyncInputStream.h"
9 #include "nsIProgressEventSink.h"
10 #include "nsIInterfaceRequestor.h"
11 #include "nsIInterfaceRequestorUtils.h"
12 #include "nsIRequest.h"
13 #include "nsIServiceManager.h"
14 #include "nsIComponentManager.h"
17 #include "nsStringAPI.h"
18 #include "nsIFileStreams.h"
19 #include "nsIStreamListener.h"
21 #include "nsNetUtil.h"
22 #include "nsAutoLock.h"
26 ////////////////////////////////////////////////////////////////////////////////
28 #if defined(PR_LOGGING)
30 // set NSPR_LOG_MODULES=Test:5
32 static PRLogModuleInfo
*gTestLog
= nullptr;
33 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
38 ////////////////////////////////////////////////////////////////////////////////
40 static NS_DEFINE_CID(kSimpleURICID
, NS_SIMPLEURI_CID
);
42 ////////////////////////////////////////////////////////////////////////////////
44 class MyListener
: public nsIStreamListener
50 virtual ~MyListener() {}
52 NS_IMETHOD
OnStartRequest(nsIRequest
*req
, nsISupports
*ctx
)
54 LOG(("MyListener::OnStartRequest\n"));
58 NS_IMETHOD
OnDataAvailable(nsIRequest
*req
, nsISupports
*ctx
,
59 nsIInputStream
*stream
,
60 uint32_t offset
, uint32_t count
)
62 LOG(("MyListener::OnDataAvailable [offset=%u count=%u]\n", offset
, count
));
68 uint32_t n
, amt
= std::min
<uint32_t>(count
, sizeof(buf
));
70 rv
= stream
->Read(buf
, amt
, &n
);
72 LOG((" read returned 0x%08x\n", rv
));
76 LOG((" read %u bytes\n", n
));
83 NS_IMETHOD
OnStopRequest(nsIRequest
*req
, nsISupports
*ctx
, nsresult status
)
85 LOG(("MyListener::OnStopRequest [status=%x]\n", status
));
91 NS_IMPL_ISUPPORTS(MyListener
,
95 ////////////////////////////////////////////////////////////////////////////////
97 class MyCallbacks
: public nsIInterfaceRequestor
98 , public nsIProgressEventSink
104 virtual ~MyCallbacks() {}
106 NS_IMETHOD
GetInterface(const nsID
&iid
, void **result
)
108 return QueryInterface(iid
, result
);
111 NS_IMETHOD
OnStatus(nsIRequest
*req
, nsISupports
*ctx
, nsresult status
,
112 const char16_t
*statusArg
)
114 LOG(("MyCallbacks::OnStatus [status=%x]\n", status
));
118 NS_IMETHOD
OnProgress(nsIRequest
*req
, nsISupports
*ctx
,
119 uint64_t progress
, uint64_t progressMax
)
121 LOG(("MyCallbacks::OnProgress [progress=%llu/%llu]\n", progress
, progressMax
));
126 NS_IMPL_ISUPPORTS(MyCallbacks
,
127 nsIInterfaceRequestor
,
128 nsIProgressEventSink
)
130 ////////////////////////////////////////////////////////////////////////////////
133 * asynchronously copy file.
136 RunTest(nsIFile
*file
)
142 nsCOMPtr
<nsIInputStream
> stream
;
143 rv
= NS_NewLocalFileInputStream(getter_AddRefs(stream
), file
);
144 if (NS_FAILED(rv
)) return rv
;
146 nsCOMPtr
<nsIURI
> uri
= do_CreateInstance(kSimpleURICID
);
148 uri
->SetSpec(NS_LITERAL_CSTRING("foo://bar"));
150 nsCOMPtr
<nsIChannel
> chan
;
151 rv
= NS_NewInputStreamChannel(getter_AddRefs(chan
), uri
, stream
);
152 if (NS_FAILED(rv
)) return rv
;
154 rv
= chan
->SetNotificationCallbacks(new MyCallbacks());
155 if (NS_FAILED(rv
)) return rv
;
157 rv
= chan
->AsyncOpen(new MyListener(), nullptr);
158 if (NS_FAILED(rv
)) return rv
;
164 ////////////////////////////////////////////////////////////////////////////////
167 main(int argc
, char* argv
[])
169 if (test_common_init(&argc
, &argv
) != 0)
175 printf("usage: %s <file-to-read>\n", argv
[0]);
178 char* fileName
= argv
[1];
180 nsCOMPtr
<nsIServiceManager
> servMan
;
181 NS_InitXPCOM2(getter_AddRefs(servMan
), nullptr, nullptr);
182 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
183 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
185 registrar
->AutoRegister(nullptr);
187 #if defined(PR_LOGGING)
188 gTestLog
= PR_NewLogModule("Test");
191 nsCOMPtr
<nsIFile
> file
;
192 rv
= NS_NewNativeLocalFile(nsDependentCString(fileName
), false, getter_AddRefs(file
));
193 if (NS_FAILED(rv
)) return rv
;
196 NS_ASSERTION(NS_SUCCEEDED(rv
), "RunTest failed");
198 // give background threads a chance to finish whatever work they may
200 PR_Sleep(PR_SecondsToInterval(1));
201 } // this scopes the nsCOMPtrs
202 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
203 rv
= NS_ShutdownXPCOM(nullptr);
204 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");