|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "nsCache.h" |
|
8 #include "nsReadableUtils.h" |
|
9 #include "nsDependentSubstring.h" |
|
10 #include "nsString.h" |
|
11 |
|
12 |
|
13 /** |
|
14 * Cache Service Utility Functions |
|
15 */ |
|
16 |
|
17 #if defined(PR_LOGGING) |
|
18 PRLogModuleInfo * gCacheLog = nullptr; |
|
19 |
|
20 |
|
21 void |
|
22 CacheLogInit() |
|
23 { |
|
24 if (gCacheLog) return; |
|
25 gCacheLog = PR_NewLogModule("cache"); |
|
26 NS_ASSERTION(gCacheLog, "\nfailed to allocate cache log.\n"); |
|
27 } |
|
28 |
|
29 |
|
30 void |
|
31 CacheLogPrintPath(PRLogModuleLevel level, const char * format, nsIFile * item) |
|
32 { |
|
33 nsAutoCString path; |
|
34 nsresult rv = item->GetNativePath(path); |
|
35 if (NS_SUCCEEDED(rv)) { |
|
36 PR_LOG(gCacheLog, level, (format, path.get())); |
|
37 } else { |
|
38 PR_LOG(gCacheLog, level, ("GetNativePath failed: %x", rv)); |
|
39 } |
|
40 } |
|
41 |
|
42 #endif |
|
43 |
|
44 |
|
45 uint32_t |
|
46 SecondsFromPRTime(PRTime prTime) |
|
47 { |
|
48 int64_t microSecondsPerSecond = PR_USEC_PER_SEC; |
|
49 return uint32_t(prTime / microSecondsPerSecond); |
|
50 } |
|
51 |
|
52 |
|
53 PRTime |
|
54 PRTimeFromSeconds(uint32_t seconds) |
|
55 { |
|
56 int64_t intermediateResult = seconds; |
|
57 PRTime prTime = intermediateResult * PR_USEC_PER_SEC; |
|
58 return prTime; |
|
59 } |
|
60 |
|
61 |
|
62 nsresult |
|
63 ClientIDFromCacheKey(const nsACString& key, char ** result) |
|
64 { |
|
65 nsresult rv = NS_OK; |
|
66 *result = nullptr; |
|
67 |
|
68 nsReadingIterator<char> colon; |
|
69 key.BeginReading(colon); |
|
70 |
|
71 nsReadingIterator<char> start; |
|
72 key.BeginReading(start); |
|
73 |
|
74 nsReadingIterator<char> end; |
|
75 key.EndReading(end); |
|
76 |
|
77 if (FindCharInReadable(':', colon, end)) { |
|
78 *result = ToNewCString( Substring(start, colon)); |
|
79 if (!*result) rv = NS_ERROR_OUT_OF_MEMORY; |
|
80 } else { |
|
81 NS_ASSERTION(false, "FindCharInRead failed to find ':'"); |
|
82 rv = NS_ERROR_UNEXPECTED; |
|
83 } |
|
84 return rv; |
|
85 } |
|
86 |
|
87 |
|
88 nsresult |
|
89 ClientKeyFromCacheKey(const nsCString& key, nsACString &result) |
|
90 { |
|
91 nsresult rv = NS_OK; |
|
92 |
|
93 nsReadingIterator<char> start; |
|
94 key.BeginReading(start); |
|
95 |
|
96 nsReadingIterator<char> end; |
|
97 key.EndReading(end); |
|
98 |
|
99 if (FindCharInReadable(':', start, end)) { |
|
100 ++start; // advance past clientID ':' delimiter |
|
101 result.Assign(Substring(start, end)); |
|
102 } else { |
|
103 NS_ASSERTION(false, "FindCharInRead failed to find ':'"); |
|
104 rv = NS_ERROR_UNEXPECTED; |
|
105 result.Truncate(0); |
|
106 } |
|
107 return rv; |
|
108 } |