|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
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/. */ |
|
5 |
|
6 #include "nspr.h" |
|
7 #include "nscore.h" |
|
8 #include "nsCOMPtr.h" |
|
9 #include "nsIIOService.h" |
|
10 #include "nsIServiceManager.h" |
|
11 #include "nsIComponentRegistrar.h" |
|
12 #include "nsIURI.h" |
|
13 |
|
14 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); |
|
15 |
|
16 nsresult ServiceMakeAbsolute(nsIURI *baseURI, char *relativeInfo, char **_retval) { |
|
17 nsresult rv; |
|
18 nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv)); |
|
19 if (NS_FAILED(rv)) return rv; |
|
20 |
|
21 return serv->MakeAbsolute(relativeInfo, baseURI, _retval); |
|
22 } |
|
23 |
|
24 nsresult URLMakeAbsolute(nsIURI *baseURI, char *relativeInfo, char **_retval) { |
|
25 return baseURI->MakeAbsolute(relativeInfo, _retval); |
|
26 } |
|
27 |
|
28 int |
|
29 main(int argc, char* argv[]) |
|
30 { |
|
31 nsresult rv = NS_OK; |
|
32 if (argc < 4) { |
|
33 printf("usage: %s int (loop count) baseURL relativeSpec\n", argv[0]); |
|
34 return -1; |
|
35 } |
|
36 |
|
37 uint32_t cycles = atoi(argv[1]); |
|
38 char *base = argv[2]; |
|
39 char *rel = argv[3]; |
|
40 { |
|
41 nsCOMPtr<nsIServiceManager> servMan; |
|
42 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); |
|
43 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); |
|
44 NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); |
|
45 if (registrar) |
|
46 registrar->AutoRegister(nullptr); |
|
47 |
|
48 nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv)); |
|
49 if (NS_FAILED(rv)) return rv; |
|
50 |
|
51 nsCOMPtr<nsIURI> uri; |
|
52 rv = serv->NewURI(base, nullptr, getter_AddRefs(uri)); |
|
53 if (NS_FAILED(rv)) return rv; |
|
54 |
|
55 char *absURLString; |
|
56 uint32_t i = 0; |
|
57 while (i++ < cycles) { |
|
58 rv = ServiceMakeAbsolute(uri, rel, &absURLString); |
|
59 if (NS_FAILED(rv)) return rv; |
|
60 nsMemory::Free(absURLString); |
|
61 |
|
62 rv = URLMakeAbsolute(uri, rel, &absURLString); |
|
63 nsMemory::Free(absURLString); |
|
64 } |
|
65 } // this scopes the nsCOMPtrs |
|
66 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM |
|
67 NS_ShutdownXPCOM(nullptr); |
|
68 return rv; |
|
69 } |