Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsIResProtocolHandler.h" |
michael@0 | 7 | #include "nsIServiceManager.h" |
michael@0 | 8 | #include "nsIIOService.h" |
michael@0 | 9 | #include "nsIInputStream.h" |
michael@0 | 10 | #include "nsIComponentManager.h" |
michael@0 | 11 | #include "nsIComponentRegistrar.h" |
michael@0 | 12 | #include "nsIStreamListener.h" |
michael@0 | 13 | #include "nsIEventQueueService.h" |
michael@0 | 14 | #include "nsIURI.h" |
michael@0 | 15 | #include "nsCRT.h" |
michael@0 | 16 | #include "nsNetCID.h" |
michael@0 | 17 | |
michael@0 | 18 | static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); |
michael@0 | 19 | static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); |
michael@0 | 20 | |
michael@0 | 21 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 22 | |
michael@0 | 23 | nsresult |
michael@0 | 24 | SetupMapping() |
michael@0 | 25 | { |
michael@0 | 26 | nsresult rv; |
michael@0 | 27 | |
michael@0 | 28 | nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv)); |
michael@0 | 29 | if (NS_FAILED(rv)) return rv; |
michael@0 | 30 | |
michael@0 | 31 | nsCOMPtr<nsIProtocolHandler> ph; |
michael@0 | 32 | rv = serv->GetProtocolHandler("res", getter_AddRefs(ph)); |
michael@0 | 33 | if (NS_FAILED(rv)) return rv; |
michael@0 | 34 | |
michael@0 | 35 | nsCOMPtr<nsIResProtocolHandler> resPH = do_QueryInterface(ph, &rv); |
michael@0 | 36 | if (NS_FAILED(rv)) return rv; |
michael@0 | 37 | |
michael@0 | 38 | rv = resPH->AppendSubstitution("foo", "file://y|/"); |
michael@0 | 39 | if (NS_FAILED(rv)) return rv; |
michael@0 | 40 | |
michael@0 | 41 | rv = resPH->AppendSubstitution("foo", "file://y|/mozilla/dist/win32_D.OBJ/bin/"); |
michael@0 | 42 | if (NS_FAILED(rv)) return rv; |
michael@0 | 43 | |
michael@0 | 44 | return rv; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 48 | |
michael@0 | 49 | nsresult |
michael@0 | 50 | TestOpenInputStream(const char* url) |
michael@0 | 51 | { |
michael@0 | 52 | nsresult rv; |
michael@0 | 53 | |
michael@0 | 54 | nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv)); |
michael@0 | 55 | if (NS_FAILED(rv)) return rv; |
michael@0 | 56 | |
michael@0 | 57 | nsCOMPtr<nsIChannel> channel; |
michael@0 | 58 | rv = serv->NewChannel(url, |
michael@0 | 59 | nullptr, // base uri |
michael@0 | 60 | getter_AddRefs(channel)); |
michael@0 | 61 | if (NS_FAILED(rv)) return rv; |
michael@0 | 62 | |
michael@0 | 63 | nsCOMPtr<nsIInputStream> in; |
michael@0 | 64 | rv = channel->Open(getter_AddRefs(in)); |
michael@0 | 65 | if (NS_FAILED(rv)) { |
michael@0 | 66 | fprintf(stdout, "failed to OpenInputStream for %s\n", url); |
michael@0 | 67 | return NS_OK; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | char buf[1024]; |
michael@0 | 71 | while (1) { |
michael@0 | 72 | uint32_t amt; |
michael@0 | 73 | rv = in->Read(buf, sizeof(buf), &amt); |
michael@0 | 74 | if (NS_FAILED(rv)) return rv; |
michael@0 | 75 | if (amt == 0) break; // eof |
michael@0 | 76 | |
michael@0 | 77 | char* str = buf; |
michael@0 | 78 | while (amt-- > 0) { |
michael@0 | 79 | fputc(*str++, stdout); |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | nsCOMPtr<nsIURI> uri; |
michael@0 | 83 | char* str; |
michael@0 | 84 | |
michael@0 | 85 | rv = channel->GetOriginalURI(getter_AddRefs(uri)); |
michael@0 | 86 | if (NS_FAILED(rv)) return rv; |
michael@0 | 87 | rv = uri->GetSpec(&str); |
michael@0 | 88 | if (NS_FAILED(rv)) return rv; |
michael@0 | 89 | fprintf(stdout, "%s resolved to ", str); |
michael@0 | 90 | free(str); |
michael@0 | 91 | |
michael@0 | 92 | rv = channel->GetURI(getter_AddRefs(uri)); |
michael@0 | 93 | if (NS_FAILED(rv)) return rv; |
michael@0 | 94 | rv = uri->GetSpec(&str); |
michael@0 | 95 | if (NS_FAILED(rv)) return rv; |
michael@0 | 96 | fprintf(stdout, "%s\n", str); |
michael@0 | 97 | free(str); |
michael@0 | 98 | |
michael@0 | 99 | return NS_OK; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 103 | |
michael@0 | 104 | bool gDone = false; |
michael@0 | 105 | nsIEventQueue* gEventQ = nullptr; |
michael@0 | 106 | |
michael@0 | 107 | class Listener : public nsIStreamListener |
michael@0 | 108 | { |
michael@0 | 109 | public: |
michael@0 | 110 | NS_DECL_ISUPPORTS |
michael@0 | 111 | |
michael@0 | 112 | Listener() {} |
michael@0 | 113 | virtual ~Listener() {} |
michael@0 | 114 | |
michael@0 | 115 | NS_IMETHOD OnStartRequest(nsIRequest *request, nsISupports *ctxt) { |
michael@0 | 116 | nsresult rv; |
michael@0 | 117 | nsCOMPtr<nsIURI> uri; |
michael@0 | 118 | nsCOMPtr<nsIChannel> channel = do_QueryInterface(request); |
michael@0 | 119 | |
michael@0 | 120 | rv = channel->GetURI(getter_AddRefs(uri)); |
michael@0 | 121 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 122 | char* str; |
michael@0 | 123 | rv = uri->GetSpec(&str); |
michael@0 | 124 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 125 | fprintf(stdout, "Starting to load %s\n", str); |
michael@0 | 126 | free(str); |
michael@0 | 127 | } |
michael@0 | 128 | } |
michael@0 | 129 | return NS_OK; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | NS_IMETHOD OnStopRequest(nsIRequest *request, nsISupports *ctxt, |
michael@0 | 133 | nsresult aStatus) { |
michael@0 | 134 | nsresult rv; |
michael@0 | 135 | nsCOMPtr<nsIURI> uri; |
michael@0 | 136 | nsCOMPtr<nsIChannel> channel = do_QueryInterface(request); |
michael@0 | 137 | |
michael@0 | 138 | rv = channel->GetURI(getter_AddRefs(uri)); |
michael@0 | 139 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 140 | char* str; |
michael@0 | 141 | rv = uri->GetSpec(&str); |
michael@0 | 142 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 143 | fprintf(stdout, "Ending load %s, status=%x\n", str, aStatus); |
michael@0 | 144 | free(str); |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | gDone = true; |
michael@0 | 148 | return NS_OK; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | NS_IMETHOD OnDataAvailable(nsIRequest *request, nsISupports *ctxt, |
michael@0 | 152 | nsIInputStream *inStr, |
michael@0 | 153 | uint64_t sourceOffset, uint32_t count) { |
michael@0 | 154 | nsresult rv; |
michael@0 | 155 | char buf[1024]; |
michael@0 | 156 | while (count > 0) { |
michael@0 | 157 | uint32_t amt; |
michael@0 | 158 | rv = inStr->Read(buf, sizeof(buf), &amt); |
michael@0 | 159 | count -= amt; |
michael@0 | 160 | char* c = buf; |
michael@0 | 161 | while (amt-- > 0) { |
michael@0 | 162 | fputc(*c++, stdout); |
michael@0 | 163 | } |
michael@0 | 164 | } |
michael@0 | 165 | return NS_OK; |
michael@0 | 166 | } |
michael@0 | 167 | }; |
michael@0 | 168 | |
michael@0 | 169 | NS_IMPL_ISUPPORTS(Listener, nsIStreamListener, nsIRequestObserver) |
michael@0 | 170 | |
michael@0 | 171 | nsresult |
michael@0 | 172 | TestAsyncRead(const char* url) |
michael@0 | 173 | { |
michael@0 | 174 | nsresult rv; |
michael@0 | 175 | |
michael@0 | 176 | nsCOMPtr<nsIEventQueueService> eventQService = |
michael@0 | 177 | do_GetService(kEventQueueServiceCID, &rv); |
michael@0 | 178 | if (NS_FAILED(rv)) return rv; |
michael@0 | 179 | |
michael@0 | 180 | rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); |
michael@0 | 181 | if (NS_FAILED(rv)) return rv; |
michael@0 | 182 | |
michael@0 | 183 | nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv)); |
michael@0 | 184 | if (NS_FAILED(rv)) return rv; |
michael@0 | 185 | |
michael@0 | 186 | nsCOMPtr<nsIChannel> channel; |
michael@0 | 187 | rv = serv->NewChannel(url, |
michael@0 | 188 | nullptr, // base uri |
michael@0 | 189 | getter_AddRefs(channel)); |
michael@0 | 190 | if (NS_FAILED(rv)) return rv; |
michael@0 | 191 | |
michael@0 | 192 | nsCOMPtr<nsIStreamListener> listener = new Listener(); |
michael@0 | 193 | if (listener == nullptr) |
michael@0 | 194 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 195 | rv = channel->AsyncOpen(nullptr, listener); |
michael@0 | 196 | if (NS_FAILED(rv)) return rv; |
michael@0 | 197 | |
michael@0 | 198 | while (!gDone) { |
michael@0 | 199 | PLEvent* event; |
michael@0 | 200 | rv = gEventQ->GetEvent(&event); |
michael@0 | 201 | if (NS_FAILED(rv)) return rv; |
michael@0 | 202 | rv = gEventQ->HandleEvent(event); |
michael@0 | 203 | if (NS_FAILED(rv)) return rv; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | return rv; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | int |
michael@0 | 210 | main(int argc, char* argv[]) |
michael@0 | 211 | { |
michael@0 | 212 | nsresult rv; |
michael@0 | 213 | { |
michael@0 | 214 | nsCOMPtr<nsIServiceManager> servMan; |
michael@0 | 215 | NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); |
michael@0 | 216 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); |
michael@0 | 217 | NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); |
michael@0 | 218 | if (registrar) |
michael@0 | 219 | registrar->AutoRegister(nullptr); |
michael@0 | 220 | |
michael@0 | 221 | NS_ASSERTION(NS_SUCCEEDED(rv), "AutoregisterComponents failed"); |
michael@0 | 222 | |
michael@0 | 223 | if (argc < 2) { |
michael@0 | 224 | printf("usage: %s resource://foo/<path-to-resolve>\n", argv[0]); |
michael@0 | 225 | return -1; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | rv = SetupMapping(); |
michael@0 | 229 | NS_ASSERTION(NS_SUCCEEDED(rv), "SetupMapping failed"); |
michael@0 | 230 | if (NS_FAILED(rv)) return rv; |
michael@0 | 231 | |
michael@0 | 232 | rv = TestOpenInputStream(argv[1]); |
michael@0 | 233 | NS_ASSERTION(NS_SUCCEEDED(rv), "TestOpenInputStream failed"); |
michael@0 | 234 | |
michael@0 | 235 | rv = TestAsyncRead(argv[1]); |
michael@0 | 236 | NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed"); |
michael@0 | 237 | } // this scopes the nsCOMPtrs |
michael@0 | 238 | // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM |
michael@0 | 239 | rv = NS_ShutdownXPCOM(nullptr); |
michael@0 | 240 | NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); |
michael@0 | 241 | return rv; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | //////////////////////////////////////////////////////////////////////////////// |