|
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/. */ |
|
4 |
|
5 #ifndef nsAboutProtocolUtils_h |
|
6 #define nsAboutProtocolUtils_h |
|
7 |
|
8 #include "nsIURI.h" |
|
9 #include "nsString.h" |
|
10 #include "nsReadableUtils.h" |
|
11 #include "nsIAboutModule.h" |
|
12 #include "nsServiceManagerUtils.h" |
|
13 #include "prtime.h" |
|
14 |
|
15 inline nsresult |
|
16 NS_GetAboutModuleName(nsIURI *aAboutURI, nsCString& aModule) |
|
17 { |
|
18 #ifdef DEBUG |
|
19 { |
|
20 bool isAbout; |
|
21 NS_ASSERTION(NS_SUCCEEDED(aAboutURI->SchemeIs("about", &isAbout)) && |
|
22 isAbout, |
|
23 "should be used only on about: URIs"); |
|
24 } |
|
25 #endif |
|
26 |
|
27 nsresult rv = aAboutURI->GetPath(aModule); |
|
28 NS_ENSURE_SUCCESS(rv, rv); |
|
29 |
|
30 int32_t f = aModule.FindCharInSet(NS_LITERAL_CSTRING("#?")); |
|
31 if (f != kNotFound) { |
|
32 aModule.Truncate(f); |
|
33 } |
|
34 |
|
35 // convert to lowercase, as all about: modules are lowercase |
|
36 ToLowerCase(aModule); |
|
37 return NS_OK; |
|
38 } |
|
39 |
|
40 inline nsresult |
|
41 NS_GetAboutModule(nsIURI *aAboutURI, nsIAboutModule** aModule) |
|
42 { |
|
43 NS_PRECONDITION(aAboutURI, "Must have URI"); |
|
44 |
|
45 nsAutoCString contractID; |
|
46 nsresult rv = NS_GetAboutModuleName(aAboutURI, contractID); |
|
47 if (NS_FAILED(rv)) return rv; |
|
48 |
|
49 // look up a handler to deal with "what" |
|
50 contractID.Insert(NS_LITERAL_CSTRING(NS_ABOUT_MODULE_CONTRACTID_PREFIX), 0); |
|
51 |
|
52 return CallGetService(contractID.get(), aModule); |
|
53 } |
|
54 |
|
55 inline PRTime SecondsToPRTime(uint32_t t_sec) |
|
56 { |
|
57 PRTime t_usec, usec_per_sec; |
|
58 t_usec = t_sec; |
|
59 usec_per_sec = PR_USEC_PER_SEC; |
|
60 return t_usec *= usec_per_sec; |
|
61 } |
|
62 inline void PrintTimeString(char *buf, uint32_t bufsize, uint32_t t_sec) |
|
63 { |
|
64 PRExplodedTime et; |
|
65 PRTime t_usec = SecondsToPRTime(t_sec); |
|
66 PR_ExplodeTime(t_usec, PR_LocalTimeParameters, &et); |
|
67 PR_FormatTime(buf, bufsize, "%Y-%m-%d %H:%M:%S", &et); |
|
68 } |
|
69 |
|
70 |
|
71 #endif |