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