Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "TestCommon.h" |
michael@0 | 6 | #include "nsIComponentRegistrar.h" |
michael@0 | 7 | #include "nsIStreamTransportService.h" |
michael@0 | 8 | #include "nsIAsyncInputStream.h" |
michael@0 | 9 | #include "nsIProgressEventSink.h" |
michael@0 | 10 | #include "nsIInterfaceRequestor.h" |
michael@0 | 11 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 12 | #include "nsIRequest.h" |
michael@0 | 13 | #include "nsIServiceManager.h" |
michael@0 | 14 | #include "nsIComponentManager.h" |
michael@0 | 15 | #include "nsCOMPtr.h" |
michael@0 | 16 | #include "nsMemory.h" |
michael@0 | 17 | #include "nsStringAPI.h" |
michael@0 | 18 | #include "nsIFileStreams.h" |
michael@0 | 19 | #include "nsIStreamListener.h" |
michael@0 | 20 | #include "nsIFile.h" |
michael@0 | 21 | #include "nsNetUtil.h" |
michael@0 | 22 | #include "nsAutoLock.h" |
michael@0 | 23 | #include "prlog.h" |
michael@0 | 24 | #include <algorithm> |
michael@0 | 25 | |
michael@0 | 26 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 27 | |
michael@0 | 28 | #if defined(PR_LOGGING) |
michael@0 | 29 | // |
michael@0 | 30 | // set NSPR_LOG_MODULES=Test:5 |
michael@0 | 31 | // |
michael@0 | 32 | static PRLogModuleInfo *gTestLog = nullptr; |
michael@0 | 33 | #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) |
michael@0 | 34 | #else |
michael@0 | 35 | #define LOG(args) |
michael@0 | 36 | #endif |
michael@0 | 37 | |
michael@0 | 38 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 39 | |
michael@0 | 40 | static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID); |
michael@0 | 41 | |
michael@0 | 42 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 43 | |
michael@0 | 44 | class MyListener : public nsIStreamListener |
michael@0 | 45 | { |
michael@0 | 46 | public: |
michael@0 | 47 | NS_DECL_ISUPPORTS |
michael@0 | 48 | |
michael@0 | 49 | MyListener() {} |
michael@0 | 50 | virtual ~MyListener() {} |
michael@0 | 51 | |
michael@0 | 52 | NS_IMETHOD OnStartRequest(nsIRequest *req, nsISupports *ctx) |
michael@0 | 53 | { |
michael@0 | 54 | LOG(("MyListener::OnStartRequest\n")); |
michael@0 | 55 | return NS_OK; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | NS_IMETHOD OnDataAvailable(nsIRequest *req, nsISupports *ctx, |
michael@0 | 59 | nsIInputStream *stream, |
michael@0 | 60 | uint32_t offset, uint32_t count) |
michael@0 | 61 | { |
michael@0 | 62 | LOG(("MyListener::OnDataAvailable [offset=%u count=%u]\n", offset, count)); |
michael@0 | 63 | |
michael@0 | 64 | char buf[500]; |
michael@0 | 65 | nsresult rv; |
michael@0 | 66 | |
michael@0 | 67 | while (count) { |
michael@0 | 68 | uint32_t n, amt = std::min<uint32_t>(count, sizeof(buf)); |
michael@0 | 69 | |
michael@0 | 70 | rv = stream->Read(buf, amt, &n); |
michael@0 | 71 | if (NS_FAILED(rv)) { |
michael@0 | 72 | LOG((" read returned 0x%08x\n", rv)); |
michael@0 | 73 | return rv; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | LOG((" read %u bytes\n", n)); |
michael@0 | 77 | count -= n; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | return NS_OK; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | NS_IMETHOD OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status) |
michael@0 | 84 | { |
michael@0 | 85 | LOG(("MyListener::OnStopRequest [status=%x]\n", status)); |
michael@0 | 86 | QuitPumpingEvents(); |
michael@0 | 87 | return NS_OK; |
michael@0 | 88 | } |
michael@0 | 89 | }; |
michael@0 | 90 | |
michael@0 | 91 | NS_IMPL_ISUPPORTS(MyListener, |
michael@0 | 92 | nsIRequestObserver, |
michael@0 | 93 | nsIStreamListener) |
michael@0 | 94 | |
michael@0 | 95 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 96 | |
michael@0 | 97 | class MyCallbacks : public nsIInterfaceRequestor |
michael@0 | 98 | , public nsIProgressEventSink |
michael@0 | 99 | { |
michael@0 | 100 | public: |
michael@0 | 101 | NS_DECL_ISUPPORTS |
michael@0 | 102 | |
michael@0 | 103 | MyCallbacks() {} |
michael@0 | 104 | virtual ~MyCallbacks() {} |
michael@0 | 105 | |
michael@0 | 106 | NS_IMETHOD GetInterface(const nsID &iid, void **result) |
michael@0 | 107 | { |
michael@0 | 108 | return QueryInterface(iid, result); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | NS_IMETHOD OnStatus(nsIRequest *req, nsISupports *ctx, nsresult status, |
michael@0 | 112 | const char16_t *statusArg) |
michael@0 | 113 | { |
michael@0 | 114 | LOG(("MyCallbacks::OnStatus [status=%x]\n", status)); |
michael@0 | 115 | return NS_OK; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | NS_IMETHOD OnProgress(nsIRequest *req, nsISupports *ctx, |
michael@0 | 119 | uint64_t progress, uint64_t progressMax) |
michael@0 | 120 | { |
michael@0 | 121 | LOG(("MyCallbacks::OnProgress [progress=%llu/%llu]\n", progress, progressMax)); |
michael@0 | 122 | return NS_OK; |
michael@0 | 123 | } |
michael@0 | 124 | }; |
michael@0 | 125 | |
michael@0 | 126 | NS_IMPL_ISUPPORTS(MyCallbacks, |
michael@0 | 127 | nsIInterfaceRequestor, |
michael@0 | 128 | nsIProgressEventSink) |
michael@0 | 129 | |
michael@0 | 130 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 131 | |
michael@0 | 132 | /** |
michael@0 | 133 | * asynchronously copy file. |
michael@0 | 134 | */ |
michael@0 | 135 | static nsresult |
michael@0 | 136 | RunTest(nsIFile *file) |
michael@0 | 137 | { |
michael@0 | 138 | nsresult rv; |
michael@0 | 139 | |
michael@0 | 140 | LOG(("RunTest\n")); |
michael@0 | 141 | |
michael@0 | 142 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 143 | rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file); |
michael@0 | 144 | if (NS_FAILED(rv)) return rv; |
michael@0 | 145 | |
michael@0 | 146 | nsCOMPtr<nsIURI> uri = do_CreateInstance(kSimpleURICID); |
michael@0 | 147 | if (uri) |
michael@0 | 148 | uri->SetSpec(NS_LITERAL_CSTRING("foo://bar")); |
michael@0 | 149 | |
michael@0 | 150 | nsCOMPtr<nsIChannel> chan; |
michael@0 | 151 | rv = NS_NewInputStreamChannel(getter_AddRefs(chan), uri, stream); |
michael@0 | 152 | if (NS_FAILED(rv)) return rv; |
michael@0 | 153 | |
michael@0 | 154 | rv = chan->SetNotificationCallbacks(new MyCallbacks()); |
michael@0 | 155 | if (NS_FAILED(rv)) return rv; |
michael@0 | 156 | |
michael@0 | 157 | rv = chan->AsyncOpen(new MyListener(), nullptr); |
michael@0 | 158 | if (NS_FAILED(rv)) return rv; |
michael@0 | 159 | |
michael@0 | 160 | PumpEvents(); |
michael@0 | 161 | return NS_OK; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 165 | |
michael@0 | 166 | int |
michael@0 | 167 | main(int argc, char* argv[]) |
michael@0 | 168 | { |
michael@0 | 169 | if (test_common_init(&argc, &argv) != 0) |
michael@0 | 170 | return -1; |
michael@0 | 171 | |
michael@0 | 172 | nsresult rv; |
michael@0 | 173 | |
michael@0 | 174 | if (argc < 2) { |
michael@0 | 175 | printf("usage: %s <file-to-read>\n", argv[0]); |
michael@0 | 176 | return -1; |
michael@0 | 177 | } |
michael@0 | 178 | char* fileName = argv[1]; |
michael@0 | 179 | { |
michael@0 | 180 | nsCOMPtr<nsIServiceManager> servMan; |
michael@0 | 181 | NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); |
michael@0 | 182 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); |
michael@0 | 183 | NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); |
michael@0 | 184 | if (registrar) |
michael@0 | 185 | registrar->AutoRegister(nullptr); |
michael@0 | 186 | |
michael@0 | 187 | #if defined(PR_LOGGING) |
michael@0 | 188 | gTestLog = PR_NewLogModule("Test"); |
michael@0 | 189 | #endif |
michael@0 | 190 | |
michael@0 | 191 | nsCOMPtr<nsIFile> file; |
michael@0 | 192 | rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file)); |
michael@0 | 193 | if (NS_FAILED(rv)) return rv; |
michael@0 | 194 | |
michael@0 | 195 | rv = RunTest(file); |
michael@0 | 196 | NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed"); |
michael@0 | 197 | |
michael@0 | 198 | // give background threads a chance to finish whatever work they may |
michael@0 | 199 | // be doing. |
michael@0 | 200 | PR_Sleep(PR_SecondsToInterval(1)); |
michael@0 | 201 | } // this scopes the nsCOMPtrs |
michael@0 | 202 | // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM |
michael@0 | 203 | rv = NS_ShutdownXPCOM(nullptr); |
michael@0 | 204 | NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); |
michael@0 | 205 | return NS_OK; |
michael@0 | 206 | } |