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: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Url Classifier code
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* the Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Gian-Carlo Pascutto <gpascutto@mozilla.com>
|
|
|
|
* Mehdi Mulani <mars.martian+bugmail@gmail.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#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:
|
2011-12-06 10:03:05 -08: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;
|
2011-12-06 10:03:05 -08: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")
|
|
|
|
|
2011-12-06 10:03:05 -08: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
|
2011-12-06 10:03:05 -08:00
|
|
|
nsPrefixSetReporter::GetProcess(nsACString& aProcess)
|
2011-11-13 02:25:48 -08:00
|
|
|
{
|
|
|
|
aProcess.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-12-06 10:03:05 -08:00
|
|
|
nsPrefixSetReporter::GetPath(nsACString& aPath)
|
2011-11-13 02:25:48 -08:00
|
|
|
{
|
|
|
|
aPath.Assign(mPath);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-12-06 10:03:05 -08:00
|
|
|
nsPrefixSetReporter::GetKind(PRInt32* aKind)
|
2011-11-13 02:25:48 -08:00
|
|
|
{
|
|
|
|
*aKind = nsIMemoryReporter::KIND_HEAP;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-12-06 10:03:05 -08:00
|
|
|
nsPrefixSetReporter::GetUnits(PRInt32* aUnits)
|
2011-11-13 02:25:48 -08:00
|
|
|
{
|
|
|
|
*aUnits = nsIMemoryReporter::UNITS_BYTES;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-12-06 10:03:05 -08: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
|
2011-12-06 10:03:05 -08: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"),
|
2011-12-06 10:03:05 -08:00
|
|
|
mHasPrefixes(false)
|
2011-09-08 13:15:08 -07:00
|
|
|
{
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!gUrlClassifierPrefixSetLog)
|
|
|
|
gUrlClassifierPrefixSetLog = PR_NewLogModule("UrlClassifierPrefixSet");
|
|
|
|
#endif
|
2011-12-06 10:03:05 -08:00
|
|
|
}
|
2011-09-08 13:17:25 -07:00
|
|
|
|
2011-12-06 10:03:05 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierPrefixSet::Init(const nsACString& aName)
|
|
|
|
{
|
|
|
|
mReporter = new nsPrefixSetReporter(this, aName);
|
2012-02-01 11:10:02 -08:00
|
|
|
NS_RegisterMemoryReporter(mReporter);
|
2011-12-06 10:03:05 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
2011-11-13 02:25:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsUrlClassifierPrefixSet::~nsUrlClassifierPrefixSet()
|
|
|
|
{
|
|
|
|
NS_UnregisterMemoryReporter(mReporter);
|
2011-09-08 13:17:25 -07:00
|
|
|
}
|
|
|
|
|
2011-09-08 13:15:27 -07:00
|
|
|
NS_IMETHODIMP
|
2011-12-06 10:03:05 -08: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
|
2011-12-06 10:03:05 -08:00
|
|
|
nsUrlClassifierPrefixSet::MakePrefixSet(const PRUint32* aPrefixes, 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++) {
|
2011-12-06 10:03:05 -08:00
|
|
|
MOZ_ASSERT(aPrefixes[i] >= aPrefixes[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;
|
|
|
|
|
2011-12-06 10:03:05 -08:00
|
|
|
if (!newIndexPrefixes.AppendElement(aPrefixes[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;
|
2011-12-06 10:03:05 -08:00
|
|
|
PRUint32 currentItem = aPrefixes[0];
|
2011-09-08 13:15:08 -07:00
|
|
|
for (PRUint32 i = 1; i < aLength; i++) {
|
|
|
|
if ((numOfDeltas >= DELTAS_LIMIT) ||
|
2011-12-06 10:03:05 -08:00
|
|
|
(aPrefixes[i] - currentItem >= MAX_INDEX_DIFF)) {
|
2012-01-10 08:09:32 -08:00
|
|
|
if (!newIndexStarts.AppendElement(newDeltas.Length())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2011-12-06 10:03:05 -08:00
|
|
|
if (!newIndexPrefixes.AppendElement(aPrefixes[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 {
|
2011-12-06 10:03:05 -08:00
|
|
|
PRUint16 delta = aPrefixes[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++;
|
|
|
|
}
|
2011-12-06 10:03:05 -08:00
|
|
|
currentItem = aPrefixes[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;
|
|
|
|
}
|
|
|
|
|
2011-12-06 10:03:05 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierPrefixSet::GetPrefixes(PRUint32* aCount,
|
|
|
|
PRUint32** aPrefixes)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCount);
|
|
|
|
*aCount = 0;
|
|
|
|
NS_ENSURE_ARG_POINTER(aPrefixes);
|
|
|
|
*aPrefixes = nsnull;
|
|
|
|
|
|
|
|
nsTArray<PRUint32> aArray;
|
|
|
|
PRUint32 prefixLength = mIndexPrefixes.Length();
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < prefixLength; i++) {
|
|
|
|
PRUint32 prefix = mIndexPrefixes[i];
|
|
|
|
PRUint32 start = mIndexStarts[i];
|
|
|
|
PRUint32 end = (i == (prefixLength - 1)) ? mDeltas.Length()
|
|
|
|
: mIndexStarts[i + 1];
|
|
|
|
aArray.AppendElement(prefix);
|
|
|
|
for (PRUint32 j = start; j < end; j++) {
|
|
|
|
prefix += mDeltas[j];
|
|
|
|
aArray.AppendElement(prefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mIndexStarts.Length() + mDeltas.Length() == aArray.Length(),
|
|
|
|
"Lengths are inconsistent");
|
|
|
|
|
|
|
|
PRUint32 itemCount = aArray.Length();
|
|
|
|
|
|
|
|
if (itemCount == 1 && aArray[0] == 0) {
|
|
|
|
/* sentinel for empty set */
|
|
|
|
aArray.Clear();
|
|
|
|
itemCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32* retval = static_cast<PRUint32*>(nsMemory::Alloc(itemCount * sizeof(PRUint32)));
|
|
|
|
NS_ENSURE_TRUE(retval, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
for (PRUint32 i = 0; i < itemCount; i++) {
|
|
|
|
retval[i] = aArray[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
*aCount = itemCount;
|
|
|
|
*aPrefixes = retval;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-08 13:15:08 -07:00
|
|
|
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
|
2011-12-06 10:03:05 -08: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
|
2011-12-06 10:03:05 -08:00
|
|
|
nsUrlClassifierPrefixSet::Probe(PRUint32 aPrefix,
|
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;
|
|
|
|
|
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
|
2011-12-06 10:03:05 -08: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;
|
|
|
|
|
|
|
|
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
|
2011-12-06 10:03:05 -08:00
|
|
|
nsUrlClassifierPrefixSet::LoadFromFile(nsIFile* aFile)
|
2011-09-08 13:16:59 -07:00
|
|
|
{
|
2011-12-06 10:03:05 -08:00
|
|
|
Telemetry::AutoTimer<Telemetry::URLCLASSIFIER_PS_FILELOAD_TIME> timer;
|
|
|
|
|
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-11 15:33:37 -07:00
|
|
|
rv = file->OpenNSPRFileDesc(PR_RDONLY | nsILocalFile::OS_READAHEAD, 0, &fileFd);
|
2011-09-08 13:16:59 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return LoadFromFd(fileFd);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-12-06 10:03:05 -08: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);
|
|
|
|
|
|
|
|
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
|
2011-12-06 10:03:05 -08: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-11 15:33:37 -07:00
|
|
|
0644, &fileFd);
|
2011-09-08 13:16:59 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
MutexAutoLock lock(mPrefixSetLock);
|
|
|
|
|
|
|
|
return StoreToFd(fileFd);
|
|
|
|
}
|