Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
7 #include "nsCache.h"
8 #include "nsReadableUtils.h"
9 #include "nsDependentSubstring.h"
10 #include "nsString.h"
13 /**
14 * Cache Service Utility Functions
15 */
17 #if defined(PR_LOGGING)
18 PRLogModuleInfo * gCacheLog = nullptr;
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 }
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 }
42 #endif
45 uint32_t
46 SecondsFromPRTime(PRTime prTime)
47 {
48 int64_t microSecondsPerSecond = PR_USEC_PER_SEC;
49 return uint32_t(prTime / microSecondsPerSecond);
50 }
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 }
62 nsresult
63 ClientIDFromCacheKey(const nsACString& key, char ** result)
64 {
65 nsresult rv = NS_OK;
66 *result = nullptr;
68 nsReadingIterator<char> colon;
69 key.BeginReading(colon);
71 nsReadingIterator<char> start;
72 key.BeginReading(start);
74 nsReadingIterator<char> end;
75 key.EndReading(end);
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 }
88 nsresult
89 ClientKeyFromCacheKey(const nsCString& key, nsACString &result)
90 {
91 nsresult rv = NS_OK;
93 nsReadingIterator<char> start;
94 key.BeginReading(start);
96 nsReadingIterator<char> end;
97 key.EndReading(end);
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 }