2001-03-21 01:42:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04: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-21 01:42:03 +03:00
|
|
|
|
|
|
|
#include "nsCache.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2002-03-24 00:35:01 +03:00
|
|
|
#include "nsDependentSubstring.h"
|
2003-03-11 02:19:05 +03:00
|
|
|
#include "nsString.h"
|
2001-03-21 01:42:03 +03:00
|
|
|
|
2001-04-04 07:30:45 +04:00
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
/**
|
|
|
|
* Cache Service Utility Functions
|
|
|
|
*/
|
|
|
|
|
2015-11-03 07:35:29 +03:00
|
|
|
mozilla::LazyLogModule gCacheLog("cache");
|
2003-03-11 02:19:05 +03:00
|
|
|
|
|
|
|
void
|
2015-06-04 01:25:57 +03:00
|
|
|
CacheLogPrintPath(mozilla::LogLevel level, const char * format, nsIFile * item)
|
2003-03-11 02:19:05 +03:00
|
|
|
{
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString path;
|
2003-03-11 02:19:05 +03:00
|
|
|
nsresult rv = item->GetNativePath(path);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2015-05-21 23:22:04 +03:00
|
|
|
MOZ_LOG(gCacheLog, level, (format, path.get()));
|
2003-03-17 10:33:45 +03:00
|
|
|
} else {
|
2015-05-21 23:22:04 +03:00
|
|
|
MOZ_LOG(gCacheLog, level, ("GetNativePath failed: %x", rv));
|
2003-03-11 02:19:05 +03:00
|
|
|
}
|
2001-03-21 01:42:03 +03:00
|
|
|
}
|
2003-03-11 02:19:05 +03:00
|
|
|
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2001-03-21 01:42:03 +03:00
|
|
|
SecondsFromPRTime(PRTime prTime)
|
|
|
|
{
|
2012-10-12 21:29:11 +04:00
|
|
|
int64_t microSecondsPerSecond = PR_USEC_PER_SEC;
|
2012-10-12 21:29:11 +04:00
|
|
|
return uint32_t(prTime / microSecondsPerSecond);
|
2001-03-21 01:42:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRTime
|
2012-08-22 19:56:38 +04:00
|
|
|
PRTimeFromSeconds(uint32_t seconds)
|
2001-03-21 01:42:03 +03:00
|
|
|
{
|
2012-10-26 03:25:57 +04:00
|
|
|
int64_t intermediateResult = seconds;
|
2012-10-12 21:29:11 +04:00
|
|
|
PRTime prTime = intermediateResult * PR_USEC_PER_SEC;
|
2001-03-21 01:42:03 +03:00
|
|
|
return prTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2002-03-24 00:35:01 +03:00
|
|
|
ClientIDFromCacheKey(const nsACString& key, char ** result)
|
2001-03-21 01:42:03 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2001-03-21 01:42:03 +03: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 18:59:28 +04:00
|
|
|
NS_ASSERTION(false, "FindCharInRead failed to find ':'");
|
2001-03-21 01:42:03 +03:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2005-01-13 06:25:28 +03:00
|
|
|
ClientKeyFromCacheKey(const nsCString& key, nsACString &result)
|
2001-03-21 01:42:03 +03: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 06:25:28 +03:00
|
|
|
result.Assign(Substring(start, end));
|
2001-03-21 01:42:03 +03:00
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ASSERTION(false, "FindCharInRead failed to find ':'");
|
2001-03-21 01:42:03 +03:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
2005-01-13 06:25:28 +03:00
|
|
|
result.Truncate(0);
|
2001-03-21 01:42:03 +03:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|