Line data Source code
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 : #include "mozilla/IntegerPrintfMacros.h"
12 :
13 :
14 : /**
15 : * Cache Service Utility Functions
16 : */
17 :
18 : mozilla::LazyLogModule gCacheLog("cache");
19 :
20 : void
21 0 : CacheLogPrintPath(mozilla::LogLevel level, const char * format, nsIFile * item)
22 : {
23 0 : nsAutoCString path;
24 0 : nsresult rv = item->GetNativePath(path);
25 0 : if (NS_SUCCEEDED(rv)) {
26 0 : MOZ_LOG(gCacheLog, level, (format, path.get()));
27 : } else {
28 0 : MOZ_LOG(gCacheLog, level, ("GetNativePath failed: %" PRIx32,
29 : static_cast<uint32_t>(rv)));
30 : }
31 0 : }
32 :
33 :
34 : uint32_t
35 0 : SecondsFromPRTime(PRTime prTime)
36 : {
37 0 : int64_t microSecondsPerSecond = PR_USEC_PER_SEC;
38 0 : return uint32_t(prTime / microSecondsPerSecond);
39 : }
40 :
41 :
42 : PRTime
43 0 : PRTimeFromSeconds(uint32_t seconds)
44 : {
45 0 : int64_t intermediateResult = seconds;
46 0 : PRTime prTime = intermediateResult * PR_USEC_PER_SEC;
47 0 : return prTime;
48 : }
49 :
50 :
51 : nsresult
52 0 : ClientIDFromCacheKey(const nsACString& key, char ** result)
53 : {
54 0 : nsresult rv = NS_OK;
55 0 : *result = nullptr;
56 :
57 0 : nsReadingIterator<char> colon;
58 0 : key.BeginReading(colon);
59 :
60 0 : nsReadingIterator<char> start;
61 0 : key.BeginReading(start);
62 :
63 0 : nsReadingIterator<char> end;
64 0 : key.EndReading(end);
65 :
66 0 : if (FindCharInReadable(':', colon, end)) {
67 0 : *result = ToNewCString( Substring(start, colon));
68 0 : if (!*result) rv = NS_ERROR_OUT_OF_MEMORY;
69 : } else {
70 0 : NS_ASSERTION(false, "FindCharInRead failed to find ':'");
71 0 : rv = NS_ERROR_UNEXPECTED;
72 : }
73 0 : return rv;
74 : }
75 :
76 :
77 : nsresult
78 0 : ClientKeyFromCacheKey(const nsCString& key, nsACString &result)
79 : {
80 0 : nsresult rv = NS_OK;
81 :
82 0 : nsReadingIterator<char> start;
83 0 : key.BeginReading(start);
84 :
85 0 : nsReadingIterator<char> end;
86 0 : key.EndReading(end);
87 :
88 0 : if (FindCharInReadable(':', start, end)) {
89 0 : ++start; // advance past clientID ':' delimiter
90 0 : result.Assign(Substring(start, end));
91 : } else {
92 0 : NS_ASSERTION(false, "FindCharInRead failed to find ':'");
93 0 : rv = NS_ERROR_UNEXPECTED;
94 0 : result.Truncate(0);
95 : }
96 0 : return rv;
97 : }
|