michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsCache.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsDependentSubstring.h" michael@0: #include "nsString.h" michael@0: michael@0: michael@0: /** michael@0: * Cache Service Utility Functions michael@0: */ michael@0: michael@0: #if defined(PR_LOGGING) michael@0: PRLogModuleInfo * gCacheLog = nullptr; michael@0: michael@0: michael@0: void michael@0: CacheLogInit() michael@0: { michael@0: if (gCacheLog) return; michael@0: gCacheLog = PR_NewLogModule("cache"); michael@0: NS_ASSERTION(gCacheLog, "\nfailed to allocate cache log.\n"); michael@0: } michael@0: michael@0: michael@0: void michael@0: CacheLogPrintPath(PRLogModuleLevel level, const char * format, nsIFile * item) michael@0: { michael@0: nsAutoCString path; michael@0: nsresult rv = item->GetNativePath(path); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: PR_LOG(gCacheLog, level, (format, path.get())); michael@0: } else { michael@0: PR_LOG(gCacheLog, level, ("GetNativePath failed: %x", rv)); michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: michael@0: michael@0: uint32_t michael@0: SecondsFromPRTime(PRTime prTime) michael@0: { michael@0: int64_t microSecondsPerSecond = PR_USEC_PER_SEC; michael@0: return uint32_t(prTime / microSecondsPerSecond); michael@0: } michael@0: michael@0: michael@0: PRTime michael@0: PRTimeFromSeconds(uint32_t seconds) michael@0: { michael@0: int64_t intermediateResult = seconds; michael@0: PRTime prTime = intermediateResult * PR_USEC_PER_SEC; michael@0: return prTime; michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: ClientIDFromCacheKey(const nsACString& key, char ** result) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: *result = nullptr; michael@0: michael@0: nsReadingIterator colon; michael@0: key.BeginReading(colon); michael@0: michael@0: nsReadingIterator start; michael@0: key.BeginReading(start); michael@0: michael@0: nsReadingIterator end; michael@0: key.EndReading(end); michael@0: michael@0: if (FindCharInReadable(':', colon, end)) { michael@0: *result = ToNewCString( Substring(start, colon)); michael@0: if (!*result) rv = NS_ERROR_OUT_OF_MEMORY; michael@0: } else { michael@0: NS_ASSERTION(false, "FindCharInRead failed to find ':'"); michael@0: rv = NS_ERROR_UNEXPECTED; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: ClientKeyFromCacheKey(const nsCString& key, nsACString &result) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: michael@0: nsReadingIterator start; michael@0: key.BeginReading(start); michael@0: michael@0: nsReadingIterator end; michael@0: key.EndReading(end); michael@0: michael@0: if (FindCharInReadable(':', start, end)) { michael@0: ++start; // advance past clientID ':' delimiter michael@0: result.Assign(Substring(start, end)); michael@0: } else { michael@0: NS_ASSERTION(false, "FindCharInRead failed to find ':'"); michael@0: rv = NS_ERROR_UNEXPECTED; michael@0: result.Truncate(0); michael@0: } michael@0: return rv; michael@0: }