2001-03-20 22:42:03 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-03-20 22:42:03 +00:00
|
|
|
|
|
|
|
#include "nsCache.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2002-03-23 21:35:01 +00:00
|
|
|
#include "nsDependentSubstring.h"
|
2003-03-10 23:19:05 +00:00
|
|
|
#include "nsString.h"
|
2001-03-20 22:42:03 +00:00
|
|
|
|
2001-04-04 03:30:45 +00:00
|
|
|
|
2001-03-20 22:42:03 +00:00
|
|
|
/**
|
|
|
|
* Cache Service Utility Functions
|
|
|
|
*/
|
|
|
|
|
2001-04-04 03:30:45 +00:00
|
|
|
#if defined(PR_LOGGING)
|
2012-07-30 14:20:58 +00:00
|
|
|
PRLogModuleInfo * gCacheLog = nullptr;
|
2001-03-20 22:42:03 +00:00
|
|
|
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2001-04-04 03:30:45 +00:00
|
|
|
void
|
|
|
|
CacheLogInit()
|
2001-03-20 22:42:03 +00:00
|
|
|
{
|
2001-04-04 03:30:45 +00:00
|
|
|
if (gCacheLog) return;
|
|
|
|
gCacheLog = PR_NewLogModule("cache");
|
2003-03-10 23:19:05 +00:00
|
|
|
NS_ASSERTION(gCacheLog, "\nfailed to allocate cache log.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-05-26 08:53:15 +00:00
|
|
|
CacheLogPrintPath(PRLogModuleLevel level, const char * format, nsIFile * item)
|
2003-03-10 23:19:05 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString path;
|
2003-03-10 23:19:05 +00:00
|
|
|
nsresult rv = item->GetNativePath(path);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PR_LOG(gCacheLog, level, (format, path.get()));
|
2003-03-17 07:33:45 +00:00
|
|
|
} else {
|
|
|
|
PR_LOG(gCacheLog, level, ("GetNativePath failed: %x", rv));
|
2003-03-10 23:19:05 +00:00
|
|
|
}
|
2001-03-20 22:42:03 +00:00
|
|
|
}
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2001-03-20 22:42:03 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
2001-03-20 22:42:03 +00:00
|
|
|
SecondsFromPRTime(PRTime prTime)
|
|
|
|
{
|
2012-10-12 17:29:11 +00:00
|
|
|
int64_t microSecondsPerSecond = PR_USEC_PER_SEC;
|
2012-10-12 17:29:11 +00:00
|
|
|
return uint32_t(prTime / microSecondsPerSecond);
|
2001-03-20 22:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRTime
|
2012-08-22 15:56:38 +00:00
|
|
|
PRTimeFromSeconds(uint32_t seconds)
|
2001-03-20 22:42:03 +00:00
|
|
|
{
|
2012-10-25 23:25:57 +00:00
|
|
|
int64_t intermediateResult = seconds;
|
2012-10-12 17:29:11 +00:00
|
|
|
PRTime prTime = intermediateResult * PR_USEC_PER_SEC;
|
2001-03-20 22:42:03 +00:00
|
|
|
return prTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2002-03-23 21:35:01 +00:00
|
|
|
ClientIDFromCacheKey(const nsACString& key, char ** result)
|
2001-03-20 22:42:03 +00:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2012-07-30 14:20:58 +00:00
|
|
|
*result = nullptr;
|
2001-03-20 22:42:03 +00:00
|
|
|
|
|
|
|
nsReadingIterator<char> colon;
|
|
|
|
key.BeginReading(colon);
|
|
|
|
|
|
|
|
nsReadingIterator<char> start;
|
|
|
|
key.BeginReading(start);
|
|
|
|
|
|
|
|
nsReadingIterator<char> end;
|
|
|
|
key.EndReading(end);
|
|
|
|
|
|
|
|
if (FindCharInReadable(':', colon, end)) {
|
|
|
|
*result = ToNewCString( Substring(start, colon));
|
|
|
|
if (!*result) rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ASSERTION(false, "FindCharInRead failed to find ':'");
|
2001-03-20 22:42:03 +00:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2005-01-13 03:25:28 +00:00
|
|
|
ClientKeyFromCacheKey(const nsCString& key, nsACString &result)
|
2001-03-20 22:42:03 +00:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
nsReadingIterator<char> start;
|
|
|
|
key.BeginReading(start);
|
|
|
|
|
|
|
|
nsReadingIterator<char> end;
|
|
|
|
key.EndReading(end);
|
|
|
|
|
|
|
|
if (FindCharInReadable(':', start, end)) {
|
|
|
|
++start; // advance past clientID ':' delimiter
|
2005-01-13 03:25:28 +00:00
|
|
|
result.Assign(Substring(start, end));
|
2001-03-20 22:42:03 +00:00
|
|
|
} else {
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ASSERTION(false, "FindCharInRead failed to find ':'");
|
2001-03-20 22:42:03 +00:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
2005-01-13 03:25:28 +00:00
|
|
|
result.Truncate(0);
|
2001-03-20 22:42:03 +00:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|