2011-09-08 13:15:08 -07:00
|
|
|
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07: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/. */
|
2011-09-08 13:15:08 -07:00
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2011-09-08 13:16:59 -07:00
|
|
|
#include "nsDebug.h"
|
2011-09-08 13:15:08 -07:00
|
|
|
#include "nsTArray.h"
|
2011-11-13 02:25:48 -08:00
|
|
|
#include "nsString.h"
|
2011-09-08 13:15:08 -07:00
|
|
|
#include "nsUrlClassifierPrefixSet.h"
|
|
|
|
#include "nsIUrlClassifierPrefixSet.h"
|
2011-09-08 13:17:25 -07:00
|
|
|
#include "nsIRandomGenerator.h"
|
2011-09-08 13:16:59 -07:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsILocalFile.h"
|
2011-09-08 13:15:08 -07:00
|
|
|
#include "nsToolkitCompsCID.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
2011-10-02 12:26:10 -07:00
|
|
|
#include "mozilla/Telemetry.h"
|
2011-09-08 13:16:59 -07:00
|
|
|
#include "mozilla/FileUtils.h"
|
2011-09-08 13:15:08 -07:00
|
|
|
#include "prlog.h"
|
|
|
|
|
2011-09-08 13:15:27 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2011-09-08 13:15:08 -07:00
|
|
|
// NSPR_LOG_MODULES=UrlClassifierPrefixSet:5
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
static const PRLogModuleInfo *gUrlClassifierPrefixSetLog = nsnull;
|
|
|
|
#define LOG(args) PR_LOG(gUrlClassifierPrefixSetLog, PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(gUrlClassifierPrefixSetLog, 4)
|
|
|
|
#else
|
|
|
|
#define LOG(args)
|
2011-10-17 07:59:28 -07:00
|
|
|
#define LOG_ENABLED() (false)
|
2011-09-08 13:15:08 -07:00
|
|
|
#endif
|
|
|
|
|
2011-11-13 02:25:48 -08:00
|
|
|
class nsPrefixSetReporter : public nsIMemoryReporter
|
|
|
|
{
|
|
|
|
public:
|
2012-04-19 22:46:47 -07:00
|
|
|
nsPrefixSetReporter(nsUrlClassifierPrefixSet * aParent, const nsACString & aName);
|
2011-11-13 02:25:48 -08:00
|
|
|
virtual ~nsPrefixSetReporter() {};
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIMEMORYREPORTER
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mPath;
|
2012-04-19 22:46:47 -07:00
|
|
|
nsUrlClassifierPrefixSet * mParent;
|
2011-11-13 02:25:48 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsPrefixSetReporter, nsIMemoryReporter)
|
|
|
|
|
2011-12-13 20:54:18 -08:00
|
|
|
NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(StoragePrefixSetMallocSizeOf,
|
|
|
|
"storage/prefixset")
|
|
|
|
|
2012-04-19 22:46:47 -07:00
|
|
|
nsPrefixSetReporter::nsPrefixSetReporter(nsUrlClassifierPrefixSet * aParent,
|
|
|
|
const nsACString & aName)
|
2011-11-13 02:25:48 -08:00
|
|
|
: mParent(aParent)
|
|
|
|
{
|
|
|
|
mPath.Assign(NS_LITERAL_CSTRING("explicit/storage/prefixset"));
|
|
|
|
if (!aName.IsEmpty()) {
|
|
|
|
mPath.Append("/");
|
|
|
|
mPath.Append(aName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-19 22:46:47 -07:00
|
|
|
nsPrefixSetReporter::GetProcess(nsACString & aProcess)
|
2011-11-13 02:25:48 -08:00
|
|
|
{
|
|
|
|
aProcess.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-19 22:46:47 -07:00
|
|
|
nsPrefixSetReporter::GetPath(nsACString & aPath)
|
2011-11-13 02:25:48 -08:00
|
|
|
{
|
|
|
|
aPath.Assign(mPath);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-19 22:46:47 -07:00
|
|
|
nsPrefixSetReporter::GetKind(PRInt32 * aKind)
|
2011-11-13 02:25:48 -08:00
|
|
|
{
|
|
|
|
*aKind = nsIMemoryReporter::KIND_HEAP;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-19 22:46:47 -07:00
|
|
|
nsPrefixSetReporter::GetUnits(PRInt32 * aUnits)
|
2011-11-13 02:25:48 -08:00
|
|
|
{
|
|
|
|
*aUnits = nsIMemoryReporter::UNITS_BYTES;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-19 22:46:47 -07:00
|
|
|
nsPrefixSetReporter::GetAmount(PRInt64 * aAmount)
|
2011-11-13 02:25:48 -08:00
|
|
|
{
|
2011-12-13 20:54:18 -08:00
|
|
|
*aAmount = mParent->SizeOfIncludingThis(StoragePrefixSetMallocSizeOf);
|
|
|
|
return NS_OK;
|
2011-11-13 02:25:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-19 22:46:47 -07:00
|
|
|
nsPrefixSetReporter::GetDescription(nsACString & aDescription)
|
2011-11-13 02:25:48 -08:00
|
|
|
{
|
|
|
|
aDescription.Assign(NS_LITERAL_CSTRING("Memory used by a PrefixSet for "
|
|
|
|
"UrlClassifier, in bytes."));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-08 13:16:59 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsUrlClassifierPrefixSet, nsIUrlClassifierPrefixSet)
|
2011-09-08 13:15:08 -07:00
|
|
|
|
|
|
|
nsUrlClassifierPrefixSet::nsUrlClassifierPrefixSet()
|
2011-09-08 13:16:59 -07:00
|
|
|
: mPrefixSetLock("mPrefixSetLock"),
|
|
|
|
mSetIsReady(mPrefixSetLock, "mSetIsReady"),
|
2012-04-19 22:46:47 -07:00
|
|
|
mHasPrefixes(false),
|
|
|
|
mRandomKey(0)
|
2011-09-08 13:15:08 -07:00
|
|
|
{
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!gUrlClassifierPrefixSetLog)
|
|
|
|
gUrlClassifierPrefixSetLog = PR_NewLogModule("UrlClassifierPrefixSet");
|
|
|
|
#endif
|
2011-09-08 13:17:25 -07:00
|
|
|
|
2012-04-19 22:46:47 -07:00
|
|
|
nsresult rv = InitKey();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(("Failed to initialize PrefixSet"));
|
|
|
|
}
|
2011-12-06 10:03:05 -08:00
|
|
|
|
2012-04-19 22:46:47 -07:00
|
|
|
mReporter = new nsPrefixSetReporter(this, NS_LITERAL_CSTRING("all"));
|
|
|
|
NS_RegisterMemoryReporter(mReporter);
|
2011-11-13 02:25:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsUrlClassifierPrefixSet::~nsUrlClassifierPrefixSet()
|
|
|
|
{
|
|
|
|
NS_UnregisterMemoryReporter(mReporter);
|
2011-09-08 13:17:25 -07:00
|
|
|
}
|
|
|
|
|
2012-04-19 22:46:47 -07:00
|
|
|
nsresult
|
|
|
|
nsUrlClassifierPrefixSet::InitKey()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRandomGenerator> rg =
|
|
|
|
do_GetService("@mozilla.org/security/random-generator;1");
|
|
|
|
NS_ENSURE_STATE(rg);
|
|
|
|
|
|
|
|
PRUint8 *temp;
|
|
|
|
nsresult rv = rg->GenerateRandomBytes(sizeof(mRandomKey), &temp);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
memcpy(&mRandomKey, temp, sizeof(mRandomKey));
|
|
|
|
NS_Free(temp);
|
|
|
|
|
|
|
|
LOG(("Initialized PrefixSet, key = %X", mRandomKey));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-08 13:15:27 -07:00
|
|
|
NS_IMETHODIMP
|
2012-04-19 22:46:47 -07:00
|
|
|
nsUrlClassifierPrefixSet::SetPrefixes(const PRUint32 * aArray, PRUint32 aLength)
|
2011-09-08 13:15:08 -07:00
|
|
|
{
|
2012-01-10 08:09:32 -08:00
|
|
|
if (aLength <= 0) {
|
2011-09-08 13:16:59 -07:00
|
|
|
MutexAutoLock lock(mPrefixSetLock);
|
2011-09-08 13:15:27 -07:00
|
|
|
if (mHasPrefixes) {
|
|
|
|
LOG(("Clearing PrefixSet"));
|
|
|
|
mDeltas.Clear();
|
|
|
|
mIndexPrefixes.Clear();
|
|
|
|
mIndexStarts.Clear();
|
2011-10-17 07:59:28 -07:00
|
|
|
mHasPrefixes = false;
|
2011-09-08 13:15:27 -07:00
|
|
|
}
|
2012-01-10 08:09:32 -08:00
|
|
|
} else {
|
|
|
|
return MakePrefixSet(aArray, aLength);
|
2011-09-08 13:15:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-10 08:09:32 -08:00
|
|
|
nsresult
|
2012-04-19 22:46:47 -07:00
|
|
|
nsUrlClassifierPrefixSet::MakePrefixSet(const PRUint32 * prefixes, PRUint32 aLength)
|
2011-09-08 13:15:08 -07:00
|
|
|
{
|
|
|
|
if (aLength == 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-10 08:09:32 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
for (PRUint32 i = 1; i < aLength; i++) {
|
2012-04-19 22:46:47 -07:00
|
|
|
MOZ_ASSERT(prefixes[i] >= prefixes[i-1]);
|
2012-01-10 08:09:32 -08:00
|
|
|
}
|
|
|
|
#endif
|
2011-09-08 13:15:27 -07:00
|
|
|
|
2012-01-10 08:09:32 -08:00
|
|
|
FallibleTArray<PRUint32> newIndexPrefixes;
|
|
|
|
FallibleTArray<PRUint32> newIndexStarts;
|
|
|
|
FallibleTArray<PRUint16> newDeltas;
|
|
|
|
|
2012-04-19 22:46:47 -07:00
|
|
|
if (!newIndexPrefixes.AppendElement(prefixes[0])) {
|
2012-01-10 08:09:32 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
if (!newIndexStarts.AppendElement(newDeltas.Length())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2011-09-08 13:15:08 -07:00
|
|
|
|
|
|
|
PRUint32 numOfDeltas = 0;
|
2012-04-19 22:46:47 -07:00
|
|
|
PRUint32 currentItem = prefixes[0];
|
2011-09-08 13:15:08 -07:00
|
|
|
for (PRUint32 i = 1; i < aLength; i++) {
|
|
|
|
if ((numOfDeltas >= DELTAS_LIMIT) ||
|
2012-04-19 22:46:47 -07:00
|
|
|
(prefixes[i] - currentItem >= MAX_INDEX_DIFF)) {
|
2012-01-10 08:09:32 -08:00
|
|
|
if (!newIndexStarts.AppendElement(newDeltas.Length())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-04-19 22:46:47 -07:00
|
|
|
if (!newIndexPrefixes.AppendElement(prefixes[i])) {
|
2012-01-10 08:09:32 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2011-09-08 13:15:08 -07:00
|
|
|
numOfDeltas = 0;
|
|
|
|
} else {
|
2012-04-19 22:46:47 -07:00
|
|
|
PRUint16 delta = prefixes[i] - currentItem;
|
2012-01-10 08:09:32 -08:00
|
|
|
if (!newDeltas.AppendElement(delta)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2011-09-08 13:15:08 -07:00
|
|
|
numOfDeltas++;
|
|
|
|
}
|
2012-04-19 22:46:47 -07:00
|
|
|
currentItem = prefixes[i];
|
2011-09-08 13:15:08 -07:00
|
|
|
}
|
|
|
|
|
2012-01-10 08:09:32 -08:00
|
|
|
newIndexPrefixes.Compact();
|
|
|
|
newIndexStarts.Compact();
|
|
|
|
newDeltas.Compact();
|
2011-09-08 13:15:37 -07:00
|
|
|
|
2012-01-10 08:09:32 -08:00
|
|
|
LOG(("Total number of indices: %d", newIndexPrefixes.Length()));
|
|
|
|
LOG(("Total number of deltas: %d", newDeltas.Length()));
|
2011-09-08 13:15:37 -07:00
|
|
|
|
2011-09-08 13:16:59 -07:00
|
|
|
MutexAutoLock lock(mPrefixSetLock);
|
2011-09-08 13:15:08 -07:00
|
|
|
|
2011-09-08 13:15:37 -07:00
|
|
|
// This just swaps some pointers
|
2012-01-10 08:09:32 -08:00
|
|
|
mIndexPrefixes.SwapElements(newIndexPrefixes);
|
|
|
|
mIndexStarts.SwapElements(newIndexStarts);
|
|
|
|
mDeltas.SwapElements(newDeltas);
|
2011-09-08 13:15:08 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mHasPrefixes = true;
|
2011-09-08 13:16:59 -07:00
|
|
|
mSetIsReady.NotifyAll();
|
2011-09-08 13:15:08 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 nsUrlClassifierPrefixSet::BinSearch(PRUint32 start,
|
2011-09-08 13:15:37 -07:00
|
|
|
PRUint32 end,
|
|
|
|
PRUint32 target)
|
2011-09-08 13:15:08 -07:00
|
|
|
{
|
|
|
|
while (start != end && end >= start) {
|
2011-09-08 13:15:37 -07:00
|
|
|
PRUint32 i = start + ((end - start) >> 1);
|
|
|
|
PRUint32 value = mIndexPrefixes[i];
|
2011-09-08 13:15:08 -07:00
|
|
|
if (value < target) {
|
|
|
|
start = i + 1;
|
|
|
|
} else if (value > target) {
|
|
|
|
end = i - 1;
|
|
|
|
} else {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2012-01-10 08:09:32 -08:00
|
|
|
nsresult
|
2012-04-19 22:46:47 -07:00
|
|
|
nsUrlClassifierPrefixSet::Contains(PRUint32 aPrefix, bool * aFound)
|
2011-09-08 13:15:08 -07:00
|
|
|
{
|
2011-11-13 02:25:48 -08:00
|
|
|
mPrefixSetLock.AssertCurrentThreadOwns();
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aFound = false;
|
2011-09-08 13:15:08 -07:00
|
|
|
|
|
|
|
if (!mHasPrefixes) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 target = aPrefix;
|
|
|
|
|
|
|
|
// We want to do a "Price is Right" binary search, that is, we want to find
|
|
|
|
// the index of the value either equal to the target or the closest value
|
|
|
|
// that is less than the target.
|
|
|
|
//
|
|
|
|
if (target < mIndexPrefixes[0]) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// |binsearch| does not necessarily return the correct index (when the
|
|
|
|
// target is not found) but rather it returns an index at least one away
|
|
|
|
// from the correct index.
|
|
|
|
// Because of this, we need to check if the target lies before the beginning
|
|
|
|
// of the indices.
|
|
|
|
|
2011-09-08 13:17:25 -07:00
|
|
|
PRUint32 i = BinSearch(0, mIndexPrefixes.Length() - 1, target);
|
2011-09-08 13:15:08 -07:00
|
|
|
if (mIndexPrefixes[i] > target && i > 0) {
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now search through the deltas for the target.
|
|
|
|
PRUint32 diff = target - mIndexPrefixes[i];
|
|
|
|
PRUint32 deltaIndex = mIndexStarts[i];
|
2011-12-02 01:46:58 -08:00
|
|
|
PRUint32 deltaSize = mDeltas.Length();
|
2011-09-08 13:15:08 -07:00
|
|
|
PRUint32 end = (i + 1 < mIndexStarts.Length()) ? mIndexStarts[i+1]
|
2011-12-02 01:46:58 -08:00
|
|
|
: deltaSize;
|
|
|
|
|
|
|
|
// Sanity check the read values
|
|
|
|
if (end > deltaSize) {
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
|
2011-09-08 13:15:08 -07:00
|
|
|
while (diff > 0 && deltaIndex < end) {
|
|
|
|
diff -= mDeltas[deltaIndex];
|
|
|
|
deltaIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diff == 0) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aFound = true;
|
2011-09-08 13:15:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-13 20:54:18 -08:00
|
|
|
size_t
|
2011-12-15 14:59:53 -08:00
|
|
|
nsUrlClassifierPrefixSet::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf)
|
2011-09-08 13:15:08 -07:00
|
|
|
{
|
2011-09-08 13:16:59 -07:00
|
|
|
MutexAutoLock lock(mPrefixSetLock);
|
2011-12-13 20:54:18 -08:00
|
|
|
size_t n = 0;
|
2012-01-25 00:52:51 -08:00
|
|
|
n += aMallocSizeOf(this);
|
2011-12-15 14:59:53 -08:00
|
|
|
n += mDeltas.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
n += mIndexPrefixes.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
n += mIndexStarts.SizeOfExcludingThis(aMallocSizeOf);
|
2011-12-13 20:54:18 -08:00
|
|
|
return n;
|
2011-09-08 13:15:08 -07:00
|
|
|
}
|
2011-09-08 13:15:27 -07:00
|
|
|
|
2011-09-08 13:16:59 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsUrlClassifierPrefixSet::IsEmpty(bool * aEmpty)
|
2011-09-08 13:16:59 -07:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mPrefixSetLock);
|
|
|
|
*aEmpty = !mHasPrefixes;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-08 13:15:27 -07:00
|
|
|
NS_IMETHODIMP
|
2012-04-19 22:46:47 -07:00
|
|
|
nsUrlClassifierPrefixSet::GetKey(PRUint32 * aKey)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mPrefixSetLock);
|
|
|
|
*aKey = mRandomKey;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierPrefixSet::Probe(PRUint32 aPrefix, PRUint32 aKey,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aReady, bool* aFound)
|
2011-09-08 13:15:27 -07:00
|
|
|
{
|
2011-09-08 13:16:59 -07:00
|
|
|
MutexAutoLock lock(mPrefixSetLock);
|
2011-09-08 13:15:27 -07:00
|
|
|
|
2011-10-20 07:48:11 -07:00
|
|
|
*aFound = false;
|
|
|
|
|
2012-04-19 22:46:47 -07:00
|
|
|
// We might have raced here with a LoadPrefixSet call,
|
|
|
|
// loading a saved PrefixSet with another key than the one used to probe us.
|
|
|
|
// This must occur exactly between the GetKey call and the Probe call.
|
|
|
|
// This could cause a false negative immediately after browser start.
|
|
|
|
// Claim we are still busy loading instead.
|
|
|
|
if (aKey != mRandomKey) {
|
|
|
|
LOG(("Potential race condition detected, avoiding"));
|
|
|
|
*aReady = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-08 13:15:27 -07:00
|
|
|
// check whether we are opportunistically probing or should wait
|
|
|
|
if (*aReady) {
|
|
|
|
// we should block until we are ready
|
|
|
|
while (!mHasPrefixes) {
|
2011-09-08 13:16:59 -07:00
|
|
|
LOG(("Set is empty, probe must wait"));
|
|
|
|
mSetIsReady.Wait();
|
2011-09-08 13:15:27 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// opportunistic probe -> check if set is loaded
|
|
|
|
if (mHasPrefixes) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aReady = true;
|
2011-09-08 13:15:27 -07:00
|
|
|
} else {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = Contains(aPrefix, aFound);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-08 13:16:59 -07:00
|
|
|
|
|
|
|
nsresult
|
2012-04-19 22:46:47 -07:00
|
|
|
nsUrlClassifierPrefixSet::LoadFromFd(AutoFDClose & fileFd)
|
2011-09-08 13:16:59 -07:00
|
|
|
{
|
|
|
|
PRUint32 magic;
|
|
|
|
PRInt32 read;
|
|
|
|
|
|
|
|
read = PR_Read(fileFd, &magic, sizeof(PRUint32));
|
2011-12-02 01:46:58 -08:00
|
|
|
NS_ENSURE_TRUE(read == sizeof(PRUint32), NS_ERROR_FAILURE);
|
2011-09-08 13:16:59 -07:00
|
|
|
|
|
|
|
if (magic == PREFIXSET_VERSION_MAGIC) {
|
|
|
|
PRUint32 indexSize;
|
|
|
|
PRUint32 deltaSize;
|
|
|
|
|
2012-04-19 22:46:47 -07:00
|
|
|
read = PR_Read(fileFd, &mRandomKey, sizeof(PRUint32));
|
|
|
|
NS_ENSURE_TRUE(read == sizeof(PRUint32), NS_ERROR_FILE_CORRUPTED);
|
2011-09-08 13:16:59 -07:00
|
|
|
read = PR_Read(fileFd, &indexSize, sizeof(PRUint32));
|
2011-12-02 01:46:58 -08:00
|
|
|
NS_ENSURE_TRUE(read == sizeof(PRUint32), NS_ERROR_FILE_CORRUPTED);
|
2011-09-08 13:16:59 -07:00
|
|
|
read = PR_Read(fileFd, &deltaSize, sizeof(PRUint32));
|
2011-12-02 01:46:58 -08:00
|
|
|
NS_ENSURE_TRUE(read == sizeof(PRUint32), NS_ERROR_FILE_CORRUPTED);
|
2011-09-08 13:16:59 -07:00
|
|
|
|
|
|
|
if (indexSize == 0) {
|
|
|
|
LOG(("stored PrefixSet is empty!"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-12-02 01:46:58 -08:00
|
|
|
if (deltaSize > (indexSize * DELTAS_LIMIT)) {
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
|
2011-09-08 13:16:59 -07:00
|
|
|
nsTArray<PRUint32> mNewIndexPrefixes;
|
|
|
|
nsTArray<PRUint32> mNewIndexStarts;
|
|
|
|
nsTArray<PRUint16> mNewDeltas;
|
|
|
|
|
|
|
|
mNewIndexStarts.SetLength(indexSize);
|
|
|
|
mNewIndexPrefixes.SetLength(indexSize);
|
|
|
|
mNewDeltas.SetLength(deltaSize);
|
|
|
|
|
2011-12-02 01:46:58 -08:00
|
|
|
PRInt32 toRead = indexSize*sizeof(PRUint32);
|
|
|
|
read = PR_Read(fileFd, mNewIndexPrefixes.Elements(), toRead);
|
|
|
|
NS_ENSURE_TRUE(read == toRead, NS_ERROR_FILE_CORRUPTED);
|
|
|
|
read = PR_Read(fileFd, mNewIndexStarts.Elements(), toRead);
|
|
|
|
NS_ENSURE_TRUE(read == toRead, NS_ERROR_FILE_CORRUPTED);
|
2011-09-08 13:16:59 -07:00
|
|
|
if (deltaSize > 0) {
|
2011-12-02 01:46:58 -08:00
|
|
|
toRead = deltaSize*sizeof(PRUint16);
|
|
|
|
read = PR_Read(fileFd, mNewDeltas.Elements(), toRead);
|
|
|
|
NS_ENSURE_TRUE(read == toRead, NS_ERROR_FILE_CORRUPTED);
|
2011-09-08 13:16:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
MutexAutoLock lock(mPrefixSetLock);
|
|
|
|
|
|
|
|
mIndexPrefixes.SwapElements(mNewIndexPrefixes);
|
|
|
|
mIndexStarts.SwapElements(mNewIndexStarts);
|
|
|
|
mDeltas.SwapElements(mNewDeltas);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mHasPrefixes = true;
|
2011-09-08 13:16:59 -07:00
|
|
|
mSetIsReady.NotifyAll();
|
|
|
|
} else {
|
|
|
|
LOG(("Version magic mismatch, not loading"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("Loading PrefixSet successful"));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-19 22:46:47 -07:00
|
|
|
nsUrlClassifierPrefixSet::LoadFromFile(nsIFile * aFile)
|
2011-09-08 13:16:59 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsILocalFile> file(do_QueryInterface(aFile, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
AutoFDClose fileFd;
|
2012-04-12 03:21:24 -07:00
|
|
|
rv = file->OpenNSPRFileDesc(PR_RDONLY | nsILocalFile::OS_READAHEAD,
|
|
|
|
0, &fileFd.rwget());
|
2011-09-08 13:16:59 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return LoadFromFd(fileFd);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-04-19 22:46:47 -07:00
|
|
|
nsUrlClassifierPrefixSet::StoreToFd(AutoFDClose & fileFd)
|
2011-09-08 13:16:59 -07:00
|
|
|
{
|
2011-10-02 12:26:10 -07:00
|
|
|
{
|
|
|
|
Telemetry::AutoTimer<Telemetry::URLCLASSIFIER_PS_FALLOCATE_TIME> timer;
|
|
|
|
PRInt64 size = 4 * sizeof(PRUint32);
|
|
|
|
size += 2 * mIndexStarts.Length() * sizeof(PRUint32);
|
|
|
|
size += mDeltas.Length() * sizeof(PRUint16);
|
|
|
|
|
|
|
|
mozilla::fallocate(fileFd, size);
|
|
|
|
}
|
|
|
|
|
2011-09-08 13:16:59 -07:00
|
|
|
PRInt32 written;
|
|
|
|
PRUint32 magic = PREFIXSET_VERSION_MAGIC;
|
|
|
|
written = PR_Write(fileFd, &magic, sizeof(PRUint32));
|
|
|
|
NS_ENSURE_TRUE(written > 0, NS_ERROR_FAILURE);
|
|
|
|
|
2012-04-19 22:46:47 -07:00
|
|
|
written = PR_Write(fileFd, &mRandomKey, sizeof(PRUint32));
|
|
|
|
NS_ENSURE_TRUE(written > 0, NS_ERROR_FAILURE);
|
|
|
|
|
2011-09-08 13:16:59 -07:00
|
|
|
PRUint32 indexSize = mIndexStarts.Length();
|
|
|
|
PRUint32 deltaSize = mDeltas.Length();
|
|
|
|
written = PR_Write(fileFd, &indexSize, sizeof(PRUint32));
|
|
|
|
NS_ENSURE_TRUE(written > 0, NS_ERROR_FAILURE);
|
|
|
|
written = PR_Write(fileFd, &deltaSize, sizeof(PRUint32));
|
|
|
|
NS_ENSURE_TRUE(written > 0, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
written = PR_Write(fileFd, mIndexPrefixes.Elements(), indexSize * sizeof(PRUint32));
|
|
|
|
NS_ENSURE_TRUE(written > 0, NS_ERROR_FAILURE);
|
|
|
|
written = PR_Write(fileFd, mIndexStarts.Elements(), indexSize * sizeof(PRUint32));
|
|
|
|
NS_ENSURE_TRUE(written > 0, NS_ERROR_FAILURE);
|
|
|
|
if (deltaSize > 0) {
|
|
|
|
written = PR_Write(fileFd, mDeltas.Elements(), deltaSize * sizeof(PRUint16));
|
|
|
|
NS_ENSURE_TRUE(written > 0, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("Saving PrefixSet successful\n"));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-19 22:46:47 -07:00
|
|
|
nsUrlClassifierPrefixSet::StoreToFile(nsIFile * aFile)
|
2011-09-08 13:16:59 -07:00
|
|
|
{
|
|
|
|
if (!mHasPrefixes) {
|
|
|
|
LOG(("Attempt to serialize empty PrefixSet"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsILocalFile> file(do_QueryInterface(aFile, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
AutoFDClose fileFd;
|
|
|
|
rv = file->OpenNSPRFileDesc(PR_RDWR | PR_TRUNCATE | PR_CREATE_FILE,
|
2012-04-12 03:21:24 -07:00
|
|
|
0644, &fileFd.rwget());
|
2011-09-08 13:16:59 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
MutexAutoLock lock(mPrefixSetLock);
|
|
|
|
|
|
|
|
return StoreToFd(fileFd);
|
|
|
|
}
|