1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsIResProtocolHandler.h"
7 #include "nsIServiceManager.h"
8 #include "nsIIOService.h"
9 #include "nsIInputStream.h"
10 #include "nsIComponentManager.h"
11 #include "nsIComponentRegistrar.h"
12 #include "nsIStreamListener.h"
13 #include "nsIEventQueueService.h"
18 static NS_DEFINE_CID(kIOServiceCID
, NS_IOSERVICE_CID
);
19 static NS_DEFINE_CID(kEventQueueServiceCID
, NS_EVENTQUEUESERVICE_CID
);
21 ////////////////////////////////////////////////////////////////////////////////
28 nsCOMPtr
<nsIIOService
> serv(do_GetService(kIOServiceCID
, &rv
));
29 if (NS_FAILED(rv
)) return rv
;
31 nsCOMPtr
<nsIProtocolHandler
> ph
;
32 rv
= serv
->GetProtocolHandler("res", getter_AddRefs(ph
));
33 if (NS_FAILED(rv
)) return rv
;
35 nsCOMPtr
<nsIResProtocolHandler
> resPH
= do_QueryInterface(ph
, &rv
);
36 if (NS_FAILED(rv
)) return rv
;
38 rv
= resPH
->AppendSubstitution("foo", "file://y|/");
39 if (NS_FAILED(rv
)) return rv
;
41 rv
= resPH
->AppendSubstitution("foo", "file://y|/mozilla/dist/win32_D.OBJ/bin/");
42 if (NS_FAILED(rv
)) return rv
;
47 ////////////////////////////////////////////////////////////////////////////////
50 TestOpenInputStream(const char* url
)
54 nsCOMPtr
<nsIIOService
> serv(do_GetService(kIOServiceCID
, &rv
));
55 if (NS_FAILED(rv
)) return rv
;
57 nsCOMPtr
<nsIChannel
> channel
;
58 rv
= serv
->NewChannel(url
,
60 getter_AddRefs(channel
));
61 if (NS_FAILED(rv
)) return rv
;
63 nsCOMPtr
<nsIInputStream
> in
;
64 rv
= channel
->Open(getter_AddRefs(in
));
66 fprintf(stdout
, "failed to OpenInputStream for %s\n", url
);
73 rv
= in
->Read(buf
, sizeof(buf
), &amt
);
74 if (NS_FAILED(rv
)) return rv
;
75 if (amt
== 0) break; // eof
79 fputc(*str
++, stdout
);
85 rv
= channel
->GetOriginalURI(getter_AddRefs(uri
));
86 if (NS_FAILED(rv
)) return rv
;
87 rv
= uri
->GetSpec(&str
);
88 if (NS_FAILED(rv
)) return rv
;
89 fprintf(stdout
, "%s resolved to ", str
);
92 rv
= channel
->GetURI(getter_AddRefs(uri
));
93 if (NS_FAILED(rv
)) return rv
;
94 rv
= uri
->GetSpec(&str
);
95 if (NS_FAILED(rv
)) return rv
;
96 fprintf(stdout
, "%s\n", str
);
102 ////////////////////////////////////////////////////////////////////////////////
105 nsIEventQueue
* gEventQ
= nullptr;
107 class Listener
: public nsIStreamListener
113 virtual ~Listener() {}
115 NS_IMETHOD
OnStartRequest(nsIRequest
*request
, nsISupports
*ctxt
) {
117 nsCOMPtr
<nsIURI
> uri
;
118 nsCOMPtr
<nsIChannel
> channel
= do_QueryInterface(request
);
120 rv
= channel
->GetURI(getter_AddRefs(uri
));
121 if (NS_SUCCEEDED(rv
)) {
123 rv
= uri
->GetSpec(&str
);
124 if (NS_SUCCEEDED(rv
)) {
125 fprintf(stdout
, "Starting to load %s\n", str
);
132 NS_IMETHOD
OnStopRequest(nsIRequest
*request
, nsISupports
*ctxt
,
135 nsCOMPtr
<nsIURI
> uri
;
136 nsCOMPtr
<nsIChannel
> channel
= do_QueryInterface(request
);
138 rv
= channel
->GetURI(getter_AddRefs(uri
));
139 if (NS_SUCCEEDED(rv
)) {
141 rv
= uri
->GetSpec(&str
);
142 if (NS_SUCCEEDED(rv
)) {
143 fprintf(stdout
, "Ending load %s, status=%x\n", str
, aStatus
);
151 NS_IMETHOD
OnDataAvailable(nsIRequest
*request
, nsISupports
*ctxt
,
152 nsIInputStream
*inStr
,
153 uint64_t sourceOffset
, uint32_t count
) {
158 rv
= inStr
->Read(buf
, sizeof(buf
), &amt
);
169 NS_IMPL_ISUPPORTS(Listener
, nsIStreamListener
, nsIRequestObserver
)
172 TestAsyncRead(const char* url
)
176 nsCOMPtr
<nsIEventQueueService
> eventQService
=
177 do_GetService(kEventQueueServiceCID
, &rv
);
178 if (NS_FAILED(rv
)) return rv
;
180 rv
= eventQService
->GetThreadEventQueue(NS_CURRENT_THREAD
, &gEventQ
);
181 if (NS_FAILED(rv
)) return rv
;
183 nsCOMPtr
<nsIIOService
> serv(do_GetService(kIOServiceCID
, &rv
));
184 if (NS_FAILED(rv
)) return rv
;
186 nsCOMPtr
<nsIChannel
> channel
;
187 rv
= serv
->NewChannel(url
,
189 getter_AddRefs(channel
));
190 if (NS_FAILED(rv
)) return rv
;
192 nsCOMPtr
<nsIStreamListener
> listener
= new Listener();
193 if (listener
== nullptr)
194 return NS_ERROR_OUT_OF_MEMORY
;
195 rv
= channel
->AsyncOpen(nullptr, listener
);
196 if (NS_FAILED(rv
)) return rv
;
200 rv
= gEventQ
->GetEvent(&event
);
201 if (NS_FAILED(rv
)) return rv
;
202 rv
= gEventQ
->HandleEvent(event
);
203 if (NS_FAILED(rv
)) return rv
;
210 main(int argc
, char* argv
[])
214 nsCOMPtr
<nsIServiceManager
> servMan
;
215 NS_InitXPCOM2(getter_AddRefs(servMan
), nullptr, nullptr);
216 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
217 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
219 registrar
->AutoRegister(nullptr);
221 NS_ASSERTION(NS_SUCCEEDED(rv
), "AutoregisterComponents failed");
224 printf("usage: %s resource://foo/<path-to-resolve>\n", argv
[0]);
229 NS_ASSERTION(NS_SUCCEEDED(rv
), "SetupMapping failed");
230 if (NS_FAILED(rv
)) return rv
;
232 rv
= TestOpenInputStream(argv
[1]);
233 NS_ASSERTION(NS_SUCCEEDED(rv
), "TestOpenInputStream failed");
235 rv
= TestAsyncRead(argv
[1]);
236 NS_ASSERTION(NS_SUCCEEDED(rv
), "TestAsyncRead failed");
237 } // this scopes the nsCOMPtrs
238 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
239 rv
= NS_ShutdownXPCOM(nullptr);
240 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");
244 ////////////////////////////////////////////////////////////////////////////////