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)
|
|
|
|
PRLogModuleInfo * gCacheLog = nsnull;
|
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
|
|
|
{
|
|
|
|
nsCAutoString path;
|
|
|
|
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
|
|
|
|
2001-03-20 22:42:03 +00:00
|
|
|
PRUint32
|
|
|
|
SecondsFromPRTime(PRTime prTime)
|
|
|
|
{
|
|
|
|
PRInt64 microSecondsPerSecond, intermediateResult;
|
|
|
|
PRUint32 seconds;
|
|
|
|
|
|
|
|
LL_I2L(microSecondsPerSecond, PR_USEC_PER_SEC);
|
|
|
|
LL_DIV(intermediateResult, prTime, microSecondsPerSecond);
|
|
|
|
LL_L2UI(seconds, intermediateResult);
|
|
|
|
return seconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRTime
|
|
|
|
PRTimeFromSeconds(PRUint32 seconds)
|
|
|
|
{
|
|
|
|
PRInt64 microSecondsPerSecond, intermediateResult;
|
|
|
|
PRTime prTime;
|
|
|
|
|
|
|
|
LL_I2L(microSecondsPerSecond, PR_USEC_PER_SEC);
|
|
|
|
LL_UI2L(intermediateResult, seconds);
|
|
|
|
LL_MUL(prTime, intermediateResult, microSecondsPerSecond);
|
|
|
|
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;
|
|
|
|
*result = nsnull;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|