2007-03-22 10:30:00 -07:00
|
|
|
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-31 16:18:46 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2007-07-25 23:38:43 -07:00
|
|
|
#include "nsCOMPtr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
|
|
|
#include "nsCRT.h"
|
2007-07-25 23:38:43 -07:00
|
|
|
#include "nsICryptoHash.h"
|
2008-02-27 00:51:02 -08:00
|
|
|
#include "nsICryptoHMAC.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDirectoryService.h"
|
2008-03-18 12:45:40 -07:00
|
|
|
#include "nsIKeyModule.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIObserverService.h"
|
2011-05-03 10:39:28 -07:00
|
|
|
#include "nsIPermissionManager.h"
|
2007-08-31 16:18:46 -07:00
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsIPrefService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIProperties.h"
|
|
|
|
#include "nsToolkitCompsCID.h"
|
2007-07-25 23:38:43 -07:00
|
|
|
#include "nsIUrlClassifierUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsUrlClassifierDBService.h"
|
2008-02-27 00:51:02 -08:00
|
|
|
#include "nsUrlClassifierUtils.h"
|
2011-08-19 08:50:04 -07:00
|
|
|
#include "nsUrlClassifierProxies.h"
|
2007-08-31 16:18:46 -07:00
|
|
|
#include "nsURILoader.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsString.h"
|
2009-01-21 20:15:34 -08:00
|
|
|
#include "nsReadableUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsTArray.h"
|
2007-07-25 23:38:43 -07:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsNetCID.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOMStrings.h"
|
2012-08-15 00:04:19 -07:00
|
|
|
#include "nsProxyRelease.h"
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
#include "mozilla/Mutex.h"
|
2012-08-15 00:04:19 -07:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2011-09-08 13:17:34 -07:00
|
|
|
#include "mozilla/Telemetry.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "prlog.h"
|
|
|
|
#include "prprf.h"
|
2008-01-29 12:57:18 -08:00
|
|
|
#include "prnetdb.h"
|
2012-08-15 00:04:19 -07:00
|
|
|
#include "Entries.h"
|
2012-06-19 21:01:02 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-07-20 09:46:59 -07:00
|
|
|
#include "nsIPrincipal.h"
|
2012-08-15 00:07:10 -07:00
|
|
|
#include "Classifier.h"
|
2012-08-15 00:04:19 -07:00
|
|
|
#include "ProtocolParser.h"
|
2008-08-14 22:46:41 -07:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
using namespace mozilla;
|
2012-08-15 00:04:19 -07:00
|
|
|
using namespace mozilla::safebrowsing;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// NSPR_LOG_MODULES=UrlClassifierDbService:5
|
|
|
|
#if defined(PR_LOGGING)
|
2012-08-15 00:04:19 -07:00
|
|
|
PRLogModuleInfo *gUrlClassifierDbServiceLog = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
#define LOG(args) PR_LOG(gUrlClassifierDbServiceLog, PR_LOG_DEBUG, args)
|
2007-07-25 23:38:43 -07:00
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(gUrlClassifierDbServiceLog, 4)
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
|
|
|
#define LOG(args)
|
2011-10-17 07:59:28 -07:00
|
|
|
#define LOG_ENABLED() (false)
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2007-08-31 16:18:46 -07:00
|
|
|
// Prefs for implementing nsIURIClassifier to block page loads
|
|
|
|
#define CHECK_MALWARE_PREF "browser.safebrowsing.malware.enabled"
|
2011-10-17 07:59:28 -07:00
|
|
|
#define CHECK_MALWARE_DEFAULT false
|
2007-08-31 16:18:46 -07:00
|
|
|
|
2007-12-03 21:10:22 -08:00
|
|
|
#define CHECK_PHISHING_PREF "browser.safebrowsing.enabled"
|
2011-10-17 07:59:28 -07:00
|
|
|
#define CHECK_PHISHING_DEFAULT false
|
2007-12-03 21:10:22 -08:00
|
|
|
|
2012-08-15 00:07:10 -07:00
|
|
|
#define RANDOMIZE_CLIENT_PREF "urlclassifier.randomizeclient"
|
2012-11-02 17:53:10 -07:00
|
|
|
#define RANDOMIZE_CLIENT_DEFAULT false
|
2012-08-15 00:07:10 -07:00
|
|
|
|
2008-02-26 21:51:28 -08:00
|
|
|
#define GETHASH_NOISE_PREF "urlclassifier.gethashnoise"
|
|
|
|
#define GETHASH_NOISE_DEFAULT 4
|
|
|
|
|
2008-04-15 15:39:44 -07:00
|
|
|
#define GETHASH_TABLES_PREF "urlclassifier.gethashtables"
|
|
|
|
|
|
|
|
#define CONFIRM_AGE_PREF "urlclassifier.confirm-age"
|
|
|
|
#define CONFIRM_AGE_DEFAULT_SEC (45 * 60)
|
|
|
|
|
2008-01-12 13:32:01 -08:00
|
|
|
class nsUrlClassifierDBServiceWorker;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Singleton instance.
|
|
|
|
static nsUrlClassifierDBService* sUrlClassifierDBService;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIThread* nsUrlClassifierDBService::gDbBackgroundThread = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-24 10:41:44 -07:00
|
|
|
// Once we've committed to shutting down, don't do work in the background
|
|
|
|
// thread.
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gShuttingDownThread = false;
|
2007-04-24 10:41:44 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t gFreshnessGuarantee = CONFIRM_AGE_DEFAULT_SEC;
|
2008-04-15 15:39:44 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
SplitTables(const nsACString& str, nsTArray<nsCString>& tables)
|
|
|
|
{
|
|
|
|
tables.Clear();
|
|
|
|
|
|
|
|
nsACString::const_iterator begin, iter, end;
|
|
|
|
str.BeginReading(begin);
|
|
|
|
str.EndReading(end);
|
|
|
|
while (begin != end) {
|
|
|
|
iter = begin;
|
|
|
|
FindCharInReadable(',', iter, end);
|
|
|
|
tables.AppendElement(Substring(begin, iter));
|
|
|
|
begin = iter;
|
|
|
|
if (begin != end)
|
|
|
|
begin++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-25 23:38:43 -07:00
|
|
|
// -------------------------------------------------------------------------
|
2012-08-15 00:04:19 -07:00
|
|
|
// Actual worker implemenatation
|
|
|
|
class nsUrlClassifierDBServiceWorker MOZ_FINAL :
|
|
|
|
public nsIUrlClassifierDBServiceWorker
|
2007-07-25 23:38:43 -07:00
|
|
|
{
|
|
|
|
public:
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierDBServiceWorker();
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIURLCLASSIFIERDBSERVICE
|
|
|
|
NS_DECL_NSIURLCLASSIFIERDBSERVICEWORKER
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult Init(int32_t gethashNoise, nsCOMPtr<nsIFile> aCacheDir,
|
2012-08-15 00:07:10 -07:00
|
|
|
bool aPerClientRandomize);
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Queue a lookup for the worker to perform, called in the main thread.
|
|
|
|
nsresult QueueLookup(const nsACString& lookupKey,
|
|
|
|
nsIUrlClassifierLookupCallback* callback);
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Handle any queued-up lookups. We call this function during long-running
|
|
|
|
// update operations to prevent lookups from blocking for too long.
|
|
|
|
nsresult HandlePendingLookups();
|
2008-01-28 15:04:43 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
private:
|
|
|
|
// No subclassing
|
|
|
|
~nsUrlClassifierDBServiceWorker();
|
2008-01-29 12:57:18 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Disallow copy constructor
|
|
|
|
nsUrlClassifierDBServiceWorker(nsUrlClassifierDBServiceWorker&);
|
2008-01-12 13:32:01 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult OpenDb();
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Applies the current transaction and resets the update/working times.
|
|
|
|
nsresult ApplyUpdate();
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Reset the in-progress update stream
|
|
|
|
void ResetStream();
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Reset the in-progress update
|
|
|
|
void ResetUpdate();
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Perform a classifier lookup for a given url.
|
|
|
|
nsresult DoLookup(const nsACString& spec, nsIUrlClassifierLookupCallback* c);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult AddNoise(const Prefix aPrefix,
|
|
|
|
const nsCString tableName,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aCount,
|
2012-08-15 00:04:19 -07:00
|
|
|
LookupResultArray& results);
|
2008-02-26 21:51:28 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsCOMPtr<nsICryptoHash> mCryptoHash;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-15 00:04:31 -07:00
|
|
|
nsAutoPtr<Classifier> mClassifier;
|
|
|
|
nsAutoPtr<ProtocolParser> mProtocolParser;
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Directory where to store the SB databases.
|
|
|
|
nsCOMPtr<nsIFile> mCacheDir;
|
2008-01-12 13:32:01 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// XXX: maybe an array of autoptrs. Or maybe a class specifically
|
|
|
|
// storing a series of updates.
|
|
|
|
nsTArray<TableUpdate*> mTableUpdates;
|
2012-02-01 11:10:02 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mUpdateWait;
|
2012-02-01 11:10:02 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Entries that cannot be completed. We expect them to die at
|
|
|
|
// the next update
|
|
|
|
PrefixArray mMissCache;
|
2012-02-01 11:10:02 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult mUpdateStatus;
|
|
|
|
nsTArray<nsCString> mUpdateTables;
|
2008-01-12 13:32:01 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsCOMPtr<nsIUrlClassifierUpdateObserver> mUpdateObserver;
|
|
|
|
bool mInStream;
|
2008-01-29 12:57:18 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// The client key with which the data from the server will be MAC'ed.
|
|
|
|
nsCString mUpdateClientKey;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// The client-specific hash key to rehash
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mHashKey;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// The number of noise entries to add to the set of lookup results.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mGethashNoise;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:07:10 -07:00
|
|
|
// Randomize clients with a key or not.
|
|
|
|
bool mPerClientRandomize;
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Pending lookups are stored in a queue for processing. The queue
|
|
|
|
// is protected by mPendingLookupLock.
|
|
|
|
Mutex mPendingLookupLock;
|
2012-02-01 11:10:02 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
class PendingLookup {
|
|
|
|
public:
|
|
|
|
TimeStamp mStartTime;
|
|
|
|
nsCString mKey;
|
|
|
|
nsCOMPtr<nsIUrlClassifierLookupCallback> mCallback;
|
|
|
|
};
|
2012-02-01 11:10:02 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// list of pending lookups
|
|
|
|
nsTArray<PendingLookup> mPendingLookups;
|
2012-04-19 22:46:47 -07:00
|
|
|
};
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(nsUrlClassifierDBServiceWorker,
|
|
|
|
nsIUrlClassifierDBServiceWorker,
|
|
|
|
nsIUrlClassifierDBService)
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierDBServiceWorker::nsUrlClassifierDBServiceWorker()
|
|
|
|
: mInStream(false)
|
|
|
|
, mGethashNoise(0)
|
2012-08-15 00:07:10 -07:00
|
|
|
, mPerClientRandomize(true)
|
2012-08-15 00:04:19 -07:00
|
|
|
, mPendingLookupLock("nsUrlClassifierDBServerWorker.mPendingLookupLock")
|
2012-04-19 22:46:47 -07:00
|
|
|
{
|
2012-02-01 11:10:02 -08:00
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierDBServiceWorker::~nsUrlClassifierDBServiceWorker()
|
2012-02-01 11:10:02 -08:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ASSERTION(!mClassifier,
|
|
|
|
"Db connection not closed, leaking memory! Call CloseDb "
|
|
|
|
"to close the connection.");
|
2012-04-19 22:46:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsUrlClassifierDBServiceWorker::Init(int32_t gethashNoise,
|
2012-08-15 00:07:10 -07:00
|
|
|
nsCOMPtr<nsIFile> aCacheDir,
|
|
|
|
bool aPerClientRandomize)
|
2012-04-19 22:46:47 -07:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
mGethashNoise = gethashNoise;
|
|
|
|
mCacheDir = aCacheDir;
|
2012-08-15 00:07:10 -07:00
|
|
|
mPerClientRandomize = aPerClientRandomize;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
ResetUpdate();
|
2008-01-29 12:57:18 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierDBServiceWorker::QueueLookup(const nsACString& spec,
|
|
|
|
nsIUrlClassifierLookupCallback* callback)
|
2008-01-29 12:57:18 -08:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
MutexAutoLock lock(mPendingLookupLock);
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
PendingLookup* lookup = mPendingLookups.AppendElement();
|
|
|
|
if (!lookup) return NS_ERROR_OUT_OF_MEMORY;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
lookup->mStartTime = TimeStamp::Now();
|
|
|
|
lookup->mKey = spec;
|
|
|
|
lookup->mCallback = callback;
|
2008-01-28 15:04:43 -08:00
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
/**
|
|
|
|
* Lookup up a key in the database is a two step process:
|
|
|
|
*
|
|
|
|
* a) First we look for any Entries in the database that might apply to this
|
|
|
|
* url. For each URL there are one or two possible domain names to check:
|
|
|
|
* the two-part domain name (example.com) and the three-part name
|
|
|
|
* (www.example.com). We check the database for both of these.
|
|
|
|
* b) If we find any entries, we check the list of fragments for that entry
|
|
|
|
* against the possible subfragments of the URL as described in the
|
|
|
|
* "Simplified Regular Expression Lookup" section of the protocol doc.
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsUrlClassifierDBServiceWorker::DoLookup(const nsACString& spec,
|
|
|
|
nsIUrlClassifierLookupCallback* c)
|
2012-04-19 22:46:47 -07:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
if (gShuttingDownThread) {
|
|
|
|
c->LookupComplete(nullptr);
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2011-12-06 10:03:05 -08:00
|
|
|
}
|
2012-02-01 11:10:02 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult rv = OpenDb();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
c->LookupComplete(nullptr);
|
2012-10-23 00:57:18 -07:00
|
|
|
NS_ERROR("Unable to open SafeBrowsing database.");
|
2012-08-15 00:04:19 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2007-07-25 23:38:43 -07:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
PRIntervalTime clockStart = 0;
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
clockStart = PR_IntervalNow();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsAutoPtr<LookupResultArray> results(new LookupResultArray());
|
|
|
|
if (!results) {
|
|
|
|
c->LookupComplete(nullptr);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2008-05-06 18:51:11 -07:00
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// we ignore failures from Check because we'd rather return the
|
|
|
|
// results that were found than fail.
|
|
|
|
mClassifier->SetFreshTime(gFreshnessGuarantee);
|
|
|
|
mClassifier->Check(spec, *results);
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("Found %d results.", results->Length()));
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2008-10-19 20:48:25 -07:00
|
|
|
|
2007-07-25 23:38:43 -07:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
PRIntervalTime clockEnd = PR_IntervalNow();
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("query took %dms\n",
|
2008-01-29 12:57:18 -08:00
|
|
|
PR_IntervalToMilliseconds(clockEnd - clockStart)));
|
2007-07-25 23:38:43 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsAutoPtr<LookupResultArray> completes(new LookupResultArray());
|
2012-02-01 11:10:02 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < results->Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
if (!mMissCache.Contains(results->ElementAt(i).hash.prefix)) {
|
|
|
|
completes->AppendElement(results->ElementAt(i));
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < completes->Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
if (!completes->ElementAt(i).Confirmed()) {
|
|
|
|
// We're going to be doing a gethash request, add some extra entries.
|
|
|
|
// Note that we cannot pass the first two by reference, because we
|
|
|
|
// add to completes, whicah can cause completes to reallocate and move.
|
|
|
|
AddNoise(completes->ElementAt(i).mCodedPrefix,
|
|
|
|
completes->ElementAt(i).mTableName,
|
|
|
|
mGethashNoise, *completes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// At this point ownership of 'results' is handed to the callback.
|
|
|
|
c->LookupComplete(completes.forget());
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2007-12-03 20:35:25 -08:00
|
|
|
return NS_OK;
|
2007-07-25 23:38:43 -07:00
|
|
|
}
|
|
|
|
|
2008-01-12 13:32:01 -08:00
|
|
|
nsresult
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierDBServiceWorker::HandlePendingLookups()
|
2008-01-12 13:32:01 -08:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
MutexAutoLock lock(mPendingLookupLock);
|
|
|
|
while (mPendingLookups.Length() > 0) {
|
|
|
|
PendingLookup lookup = mPendingLookups[0];
|
|
|
|
mPendingLookups.RemoveElementAt(0);
|
|
|
|
{
|
|
|
|
MutexAutoUnlock unlock(mPendingLookupLock);
|
|
|
|
DoLookup(lookup.mKey, lookup.mCallback);
|
|
|
|
}
|
|
|
|
double lookupTime = (TimeStamp::Now() - lookup.mStartTime).ToMilliseconds();
|
|
|
|
Telemetry::Accumulate(Telemetry::URLCLASSIFIER_LOOKUP_TIME,
|
2012-08-22 08:56:38 -07:00
|
|
|
static_cast<uint32_t>(lookupTime));
|
2012-08-15 00:04:19 -07:00
|
|
|
}
|
2008-01-12 13:32:01 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
return NS_OK;
|
2008-01-12 13:32:01 -08:00
|
|
|
}
|
|
|
|
|
2007-07-25 23:38:43 -07:00
|
|
|
nsresult
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierDBServiceWorker::AddNoise(const Prefix aPrefix,
|
|
|
|
const nsCString tableName,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aCount,
|
2012-08-15 00:04:19 -07:00
|
|
|
LookupResultArray& results)
|
2007-07-25 23:38:43 -07:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
if (aCount < 1) {
|
2008-05-06 18:51:11 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
PrefixArray noiseEntries;
|
|
|
|
nsresult rv = mClassifier->ReadNoiseEntries(aPrefix, tableName,
|
|
|
|
aCount, &noiseEntries);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < noiseEntries.Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
LookupResult *result = results.AppendElement();
|
|
|
|
if (!result)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
result->hash.prefix = noiseEntries[i];
|
|
|
|
result->mNoise = true;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
result->mTableName.Assign(tableName);
|
2008-01-12 13:32:01 -08:00
|
|
|
}
|
|
|
|
|
2007-12-03 20:35:25 -08:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Lookup a key in the db.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBServiceWorker::Lookup(nsIPrincipal* aPrincipal,
|
|
|
|
nsIUrlClassifierCallback* c)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
return HandlePendingLookups();
|
2007-07-25 18:49:20 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBServiceWorker::GetTables(nsIUrlClassifierCallback* c)
|
2007-07-25 18:49:20 -07:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
if (gShuttingDownThread)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult rv = OpenDb();
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-10-23 00:57:18 -07:00
|
|
|
NS_ERROR("Unable to open SafeBrowsing database");
|
2012-08-15 00:04:19 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2007-12-03 21:49:11 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString response;
|
2012-08-15 00:04:19 -07:00
|
|
|
mClassifier->TableRequest(response);
|
|
|
|
c->HandleEvent(response);
|
2007-07-25 23:38:43 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2007-07-25 18:49:20 -07:00
|
|
|
|
2007-07-25 23:38:43 -07:00
|
|
|
void
|
2008-01-12 14:22:03 -08:00
|
|
|
nsUrlClassifierDBServiceWorker::ResetStream()
|
2007-07-25 23:38:43 -07:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("ResetStream"));
|
2011-10-17 07:59:28 -07:00
|
|
|
mInStream = false;
|
2012-08-15 00:04:19 -07:00
|
|
|
mProtocolParser = nullptr;
|
2007-07-25 23:38:43 -07:00
|
|
|
}
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
void
|
|
|
|
nsUrlClassifierDBServiceWorker::ResetUpdate()
|
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("ResetUpdate"));
|
2008-01-12 14:22:03 -08:00
|
|
|
mUpdateWait = 0;
|
|
|
|
mUpdateStatus = NS_OK;
|
2012-07-30 07:20:58 -07:00
|
|
|
mUpdateObserver = nullptr;
|
2008-02-27 00:51:02 -08:00
|
|
|
mUpdateClientKey.Truncate();
|
2008-01-12 14:22:03 -08:00
|
|
|
}
|
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBServiceWorker::SetHashCompleter(const nsACString &tableName,
|
|
|
|
nsIUrlClassifierHashCompleter *completer)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_IMETHODIMP
|
2008-02-27 00:51:02 -08:00
|
|
|
nsUrlClassifierDBServiceWorker::BeginUpdate(nsIUrlClassifierUpdateObserver *observer,
|
2008-04-15 15:39:44 -07:00
|
|
|
const nsACString &tables,
|
2008-02-27 00:51:02 -08:00
|
|
|
const nsACString &clientKey)
|
2008-01-12 14:22:03 -08:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("nsUrlClassifierDBServiceWorker::BeginUpdate [%s]", PromiseFlatCString(tables).get()));
|
|
|
|
|
2008-01-16 11:20:45 -08:00
|
|
|
if (gShuttingDownThread)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_ENSURE_STATE(!mUpdateObserver);
|
|
|
|
|
|
|
|
nsresult rv = OpenDb();
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-10-23 00:57:18 -07:00
|
|
|
NS_ERROR("Unable to open SafeBrowsing database");
|
2008-01-12 14:22:03 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
mUpdateStatus = NS_OK;
|
2008-01-12 14:22:03 -08:00
|
|
|
mUpdateObserver = observer;
|
2012-08-15 00:04:19 -07:00
|
|
|
SplitTables(tables, mUpdateTables);
|
2008-01-12 14:22:03 -08:00
|
|
|
|
2008-02-27 00:51:02 -08:00
|
|
|
if (!clientKey.IsEmpty()) {
|
|
|
|
rv = nsUrlClassifierUtils::DecodeClientKey(clientKey, mUpdateClientKey);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("clientKey present, marking update key"));
|
2008-02-27 00:51:02 -08:00
|
|
|
}
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-02-27 00:51:02 -08:00
|
|
|
nsUrlClassifierDBServiceWorker::BeginStream(const nsACString &table,
|
|
|
|
const nsACString &serverMAC)
|
2008-01-12 14:22:03 -08:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("nsUrlClassifierDBServiceWorker::BeginStream"));
|
|
|
|
|
2008-01-16 11:20:45 -08:00
|
|
|
if (gShuttingDownThread)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_ENSURE_STATE(mUpdateObserver);
|
|
|
|
NS_ENSURE_STATE(!mInStream);
|
|
|
|
|
2012-04-19 22:46:47 -07:00
|
|
|
mInStream = true;
|
2008-03-18 12:45:40 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_ASSERTION(!mProtocolParser, "Should not have a protocol parser.");
|
2008-02-27 00:51:02 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
mProtocolParser = new ProtocolParser(mHashKey);
|
|
|
|
if (!mProtocolParser)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2008-02-27 00:51:02 -08:00
|
|
|
|
2012-08-15 00:07:10 -07:00
|
|
|
mProtocolParser->Init(mCryptoHash, mPerClientRandomize);
|
2008-02-27 00:51:02 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult rv;
|
2012-02-01 11:10:02 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// If we're expecting a MAC, create the nsICryptoHMAC component now.
|
|
|
|
if (!mUpdateClientKey.IsEmpty()) {
|
|
|
|
LOG(("Expecting MAC in this stream"));
|
|
|
|
rv = mProtocolParser->InitHMAC(mUpdateClientKey, serverMAC);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
LOG(("No MAC in this stream"));
|
|
|
|
}
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
if (!table.IsEmpty()) {
|
2012-08-15 00:04:19 -07:00
|
|
|
mProtocolParser->SetCurrentTable(table);
|
2008-01-29 12:57:18 -08:00
|
|
|
}
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-07-25 23:38:43 -07:00
|
|
|
/**
|
|
|
|
* Updating the database:
|
|
|
|
*
|
2010-05-13 05:19:50 -07:00
|
|
|
* The Update() method takes a series of chunks separated with control data,
|
2007-07-25 23:38:43 -07:00
|
|
|
* as described in
|
|
|
|
* http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec
|
|
|
|
*
|
|
|
|
* It will iterate through the control data until it reaches a chunk. By
|
|
|
|
* the time it reaches a chunk, it should have received
|
|
|
|
* a) the table to which this chunk applies
|
|
|
|
* b) the type of chunk (add, delete, expire add, expire delete).
|
|
|
|
* c) the chunk ID
|
|
|
|
* d) the length of the chunk.
|
|
|
|
*
|
|
|
|
* For add and subtract chunks, it needs to read the chunk data (expires
|
|
|
|
* don't have any data). Chunk data is a list of URI fragments whose
|
|
|
|
* encoding depends on the type of table (which is indicated by the end
|
|
|
|
* of the table name):
|
|
|
|
* a) tables ending with -exp are a zlib-compressed list of URI fragments
|
|
|
|
* separated by newlines.
|
|
|
|
* b) tables ending with -sha128 have the form
|
|
|
|
* [domain][N][frag0]...[fragN]
|
|
|
|
* 16 1 16 16
|
|
|
|
* If N is 0, the domain is reused as a fragment.
|
|
|
|
* c) any other tables are assumed to be a plaintext list of URI fragments
|
|
|
|
* separated by newlines.
|
|
|
|
*
|
|
|
|
* Update() can be fed partial data; It will accumulate data until there is
|
|
|
|
* enough to act on. Finish() should be called when there will be no more
|
|
|
|
* data.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2008-01-12 14:22:03 -08:00
|
|
|
nsUrlClassifierDBServiceWorker::UpdateStream(const nsACString& chunk)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-25 23:38:43 -07:00
|
|
|
if (gShuttingDownThread)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_ENSURE_STATE(mInStream);
|
|
|
|
|
2007-07-25 23:38:43 -07:00
|
|
|
HandlePendingLookups();
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
return mProtocolParser->AppendStream(chunk);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-01-12 14:22:03 -08:00
|
|
|
nsUrlClassifierDBServiceWorker::FinishStream()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-16 11:20:45 -08:00
|
|
|
if (gShuttingDownThread)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_ENSURE_STATE(mInStream);
|
|
|
|
NS_ENSURE_STATE(mUpdateObserver);
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
mInStream = false;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
mProtocolParser->FinishHMAC();
|
|
|
|
if (NS_SUCCEEDED(mProtocolParser->Status())) {
|
|
|
|
if (mProtocolParser->UpdateWait()) {
|
|
|
|
mUpdateWait = mProtocolParser->UpdateWait();
|
2012-04-19 22:46:47 -07:00
|
|
|
}
|
2012-08-15 00:04:19 -07:00
|
|
|
// XXX: Only allow forwards from the initial update?
|
|
|
|
const nsTArray<ProtocolParser::ForwardedUpdate> &forwards =
|
|
|
|
mProtocolParser->Forwards();
|
2012-10-11 16:38:04 -07:00
|
|
|
for (uint32_t i = 0; i < forwards.Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
const ProtocolParser::ForwardedUpdate &forward = forwards[i];
|
|
|
|
mUpdateObserver->UpdateUrlRequested(forward.url, forward.table, forward.mac);
|
2011-12-06 10:03:05 -08:00
|
|
|
}
|
2012-08-15 00:04:19 -07:00
|
|
|
// Hold on to any TableUpdate objects that were created by the
|
|
|
|
// parser.
|
|
|
|
mTableUpdates.AppendElements(mProtocolParser->GetTableUpdates());
|
|
|
|
mProtocolParser->ForgetTableUpdates();
|
|
|
|
} else {
|
|
|
|
mUpdateStatus = mProtocolParser->Status();
|
2012-02-01 11:10:02 -08:00
|
|
|
}
|
2012-08-15 00:04:19 -07:00
|
|
|
mUpdateObserver->StreamFinished(mProtocolParser->Status(), 0);
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Only reset if MAC was OK
|
2012-04-19 22:46:47 -07:00
|
|
|
if (NS_SUCCEEDED(mUpdateStatus)) {
|
2012-08-15 00:04:19 -07:00
|
|
|
if (mProtocolParser->ResetRequested()) {
|
|
|
|
mClassifier->Reset();
|
|
|
|
}
|
2012-04-19 22:46:47 -07:00
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Rekey will cause update to fail (can't check MACs)
|
|
|
|
if (mProtocolParser->RekeyRequested()) {
|
|
|
|
mUpdateObserver->RekeyRequested();
|
2008-05-07 13:33:45 -07:00
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
mProtocolParser = nullptr;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2008-05-07 13:33:45 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBServiceWorker::FinishUpdate()
|
|
|
|
{
|
2008-01-16 11:20:45 -08:00
|
|
|
if (gShuttingDownThread)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_ENSURE_STATE(mUpdateObserver);
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
if (NS_SUCCEEDED(mUpdateStatus)) {
|
|
|
|
mUpdateStatus = ApplyUpdate();
|
2011-12-02 01:45:56 -08:00
|
|
|
}
|
2007-12-03 20:35:25 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
mMissCache.Clear();
|
|
|
|
|
2007-07-25 23:38:43 -07:00
|
|
|
if (NS_SUCCEEDED(mUpdateStatus)) {
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("Notifying success: %d", mUpdateWait));
|
2008-01-12 14:22:03 -08:00
|
|
|
mUpdateObserver->UpdateSuccess(mUpdateWait);
|
2007-07-25 20:11:43 -07:00
|
|
|
} else {
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("Notifying error: %d", mUpdateStatus));
|
2008-01-12 14:22:03 -08:00
|
|
|
mUpdateObserver->UpdateError(mUpdateStatus);
|
2012-08-15 00:04:19 -07:00
|
|
|
/*
|
|
|
|
* mark the tables as spoiled, we don't want to block hosts
|
|
|
|
* longer than normal because our update failed
|
|
|
|
*/
|
|
|
|
mClassifier->MarkSpoiled(mUpdateTables);
|
2007-07-25 20:11:43 -07:00
|
|
|
}
|
2012-08-15 00:04:19 -07:00
|
|
|
mUpdateObserver = nullptr;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
2011-12-06 10:03:05 -08:00
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult
|
|
|
|
nsUrlClassifierDBServiceWorker::ApplyUpdate()
|
|
|
|
{
|
|
|
|
LOG(("nsUrlClassifierDBServiceWorker::ApplyUpdate()"));
|
|
|
|
return mClassifier->ApplyUpdates(&mTableUpdates);
|
|
|
|
}
|
|
|
|
|
2008-01-12 13:32:01 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBServiceWorker::ResetDatabase()
|
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult rv = OpenDb();
|
2012-02-01 11:10:02 -08:00
|
|
|
|
2012-10-23 00:57:18 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mClassifier->Reset();
|
|
|
|
}
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
rv = CloseDb();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2008-01-12 13:32:01 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2008-01-12 14:22:03 -08:00
|
|
|
nsUrlClassifierDBServiceWorker::CancelUpdate()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("nsUrlClassifierDBServiceWorker::CancelUpdate"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-14 13:13:43 -08:00
|
|
|
if (mUpdateObserver) {
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("UpdateObserver exists, cancelling"));
|
|
|
|
|
2008-01-14 13:13:43 -08:00
|
|
|
mUpdateStatus = NS_BINDING_ABORTED;
|
2008-01-12 14:22:03 -08:00
|
|
|
|
2008-01-14 13:13:43 -08:00
|
|
|
mUpdateObserver->UpdateError(mUpdateStatus);
|
2008-01-12 14:22:03 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
/*
|
|
|
|
* mark the tables as spoiled, we don't want to block hosts
|
|
|
|
* longer than normal because our update failed
|
|
|
|
*/
|
|
|
|
mClassifier->MarkSpoiled(mUpdateTables);
|
2008-04-15 15:39:44 -07:00
|
|
|
|
2008-01-14 13:13:43 -08:00
|
|
|
ResetStream();
|
|
|
|
ResetUpdate();
|
2012-08-15 00:04:19 -07:00
|
|
|
} else {
|
|
|
|
LOG(("No UpdateObserver, nothing to cancel"));
|
2008-01-14 13:13:43 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allows the main thread to delete the connection which may be in
|
|
|
|
// a background thread.
|
|
|
|
// XXX This could be turned into a single shutdown event so the logic
|
|
|
|
// is simpler in nsUrlClassifierDBService::Shutdown.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBServiceWorker::CloseDb()
|
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
if (mClassifier) {
|
|
|
|
mClassifier->Close();
|
|
|
|
mClassifier = nullptr;
|
2007-07-25 20:11:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mCryptoHash = nullptr;
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("urlclassifier db closed\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-25 23:38:43 -07:00
|
|
|
return NS_OK;
|
2007-07-25 20:11:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-29 18:26:44 -08:00
|
|
|
NS_IMETHODIMP
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierDBServiceWorker::CacheCompletions(CacheResultArray *results)
|
2008-01-29 18:26:44 -08:00
|
|
|
{
|
|
|
|
LOG(("nsUrlClassifierDBServiceWorker::CacheCompletions [%p]", this));
|
2012-08-15 00:04:19 -07:00
|
|
|
if (!mClassifier)
|
2007-07-25 20:11:43 -07:00
|
|
|
return NS_OK;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Ownership is transferred in to us
|
|
|
|
nsAutoPtr<CacheResultArray> resultsPtr(results);
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsAutoPtr<ProtocolParser> pParse(new ProtocolParser(mHashKey));
|
|
|
|
nsTArray<TableUpdate*> updates;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Only cache results for tables that we have, don't take
|
|
|
|
// in tables we might accidentally have hit during a completion.
|
|
|
|
// This happens due to goog vs googpub lists existing.
|
|
|
|
nsTArray<nsCString> tables;
|
|
|
|
nsresult rv = mClassifier->ActiveTables(tables);
|
2012-04-19 22:46:47 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < resultsPtr->Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
bool activeTable = false;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t table = 0; table < tables.Length(); table++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
if (tables[table].Equals(resultsPtr->ElementAt(i).table)) {
|
|
|
|
activeTable = true;
|
2012-04-19 22:46:47 -07:00
|
|
|
}
|
|
|
|
}
|
2012-08-15 00:04:19 -07:00
|
|
|
if (activeTable) {
|
|
|
|
TableUpdate * tu = pParse->GetTableUpdate(resultsPtr->ElementAt(i).table);
|
|
|
|
LOG(("CacheCompletion Addchunk %d hash %X", resultsPtr->ElementAt(i).entry.addChunk,
|
|
|
|
resultsPtr->ElementAt(i).entry.hash.prefix));
|
|
|
|
tu->NewAddComplete(resultsPtr->ElementAt(i).entry.addChunk,
|
|
|
|
resultsPtr->ElementAt(i).entry.hash.complete);
|
|
|
|
tu->NewAddChunk(resultsPtr->ElementAt(i).entry.addChunk);
|
|
|
|
tu->SetLocalUpdate();
|
|
|
|
updates.AppendElement(tu);
|
|
|
|
pParse->ForgetTableUpdates();
|
|
|
|
} else {
|
|
|
|
LOG(("Completion received, but table is not active, so not caching."));
|
2011-12-02 01:45:56 -08:00
|
|
|
}
|
2012-08-15 00:04:19 -07:00
|
|
|
}
|
2011-09-08 13:17:25 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
mClassifier->ApplyUpdates(&updates);
|
2011-09-08 13:15:18 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBServiceWorker::CacheMisses(PrefixArray *results)
|
2011-09-08 13:15:18 -07:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("nsUrlClassifierDBServiceWorker::CacheMisses [%p] %d",
|
|
|
|
this, results->Length()));
|
2011-09-08 13:16:59 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Ownership is transferred in to us
|
|
|
|
nsAutoPtr<PrefixArray> resultsPtr(results);
|
2012-01-10 08:09:38 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < resultsPtr->Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
mMissCache.AppendElement(resultsPtr->ElementAt(i));
|
|
|
|
}
|
2011-09-08 13:16:59 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierDBServiceWorker::OpenDb()
|
2011-09-08 13:16:59 -07:00
|
|
|
{
|
2012-08-15 00:04:19 -07:00
|
|
|
// Connection already open, don't do anything.
|
|
|
|
if (mClassifier) {
|
2011-09-08 13:16:59 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
LOG(("Opening db"));
|
2011-09-08 13:16:59 -07:00
|
|
|
|
2012-10-23 00:57:18 -07:00
|
|
|
nsresult rv;
|
|
|
|
mCryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsAutoPtr<Classifier> classifier(new Classifier());
|
|
|
|
if (!classifier) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2011-09-08 13:17:25 -07:00
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
classifier->SetFreshTime(gFreshnessGuarantee);
|
2012-08-15 00:07:10 -07:00
|
|
|
classifier->SetPerClientRandomize(mPerClientRandomize);
|
2011-09-08 13:16:59 -07:00
|
|
|
|
2012-10-23 00:57:18 -07:00
|
|
|
rv = classifier->Open(*mCacheDir);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
mHashKey = classifier->GetHashKey();
|
|
|
|
mClassifier = classifier;
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// nsUrlClassifierLookupCallback
|
|
|
|
//
|
|
|
|
// This class takes the results of a lookup found on the worker thread
|
|
|
|
// and handles any necessary partial hash expansions before calling
|
|
|
|
// the client callback.
|
|
|
|
|
2012-06-19 21:01:02 -07:00
|
|
|
class nsUrlClassifierLookupCallback MOZ_FINAL : public nsIUrlClassifierLookupCallback
|
|
|
|
, public nsIUrlClassifierHashCompleterCallback
|
2008-01-29 12:57:18 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIURLCLASSIFIERLOOKUPCALLBACK
|
|
|
|
NS_DECL_NSIURLCLASSIFIERHASHCOMPLETERCALLBACK
|
|
|
|
|
|
|
|
nsUrlClassifierLookupCallback(nsUrlClassifierDBService *dbservice,
|
|
|
|
nsIUrlClassifierCallback *c)
|
|
|
|
: mDBService(dbservice)
|
2012-07-30 07:20:58 -07:00
|
|
|
, mResults(nullptr)
|
2008-01-29 12:57:18 -08:00
|
|
|
, mPendingCompletions(0)
|
|
|
|
, mCallback(c)
|
|
|
|
{}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
~nsUrlClassifierLookupCallback();
|
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
private:
|
|
|
|
nsresult HandleResults();
|
|
|
|
|
|
|
|
nsRefPtr<nsUrlClassifierDBService> mDBService;
|
2012-08-15 00:04:19 -07:00
|
|
|
nsAutoPtr<LookupResultArray> mResults;
|
2008-01-29 18:26:44 -08:00
|
|
|
|
|
|
|
// Completed results to send back to the worker for caching.
|
2012-08-15 00:04:19 -07:00
|
|
|
nsAutoPtr<CacheResultArray> mCacheResults;
|
2008-01-29 18:26:44 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mPendingCompletions;
|
2008-01-29 12:57:18 -08:00
|
|
|
nsCOMPtr<nsIUrlClassifierCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(nsUrlClassifierLookupCallback,
|
|
|
|
nsIUrlClassifierLookupCallback,
|
|
|
|
nsIUrlClassifierHashCompleterCallback)
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierLookupCallback::~nsUrlClassifierLookupCallback()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
(void)NS_GetMainThread(getter_AddRefs(thread));
|
|
|
|
|
|
|
|
if (mCallback) {
|
|
|
|
(void)NS_ProxyRelease(thread, mCallback, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
NS_IMETHODIMP
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierLookupCallback::LookupComplete(nsTArray<LookupResult>* results)
|
2008-01-29 12:57:18 -08:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(mResults == nullptr,
|
2008-01-29 12:57:18 -08:00
|
|
|
"Should only get one set of results per nsUrlClassifierLookupCallback!");
|
|
|
|
|
|
|
|
if (!results) {
|
|
|
|
HandleResults();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mResults = results;
|
|
|
|
|
2008-04-15 15:39:44 -07:00
|
|
|
// Check the results entries that need to be completed.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < results->Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
LookupResult& result = results->ElementAt(i);
|
2008-04-15 15:39:44 -07:00
|
|
|
|
|
|
|
// We will complete partial matches and matches that are stale.
|
2012-08-15 00:04:19 -07:00
|
|
|
if (!result.Confirmed()) {
|
2008-01-29 12:57:18 -08:00
|
|
|
nsCOMPtr<nsIUrlClassifierHashCompleter> completer;
|
|
|
|
if (mDBService->GetCompleter(result.mTableName,
|
|
|
|
getter_AddRefs(completer))) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString partialHash;
|
2012-08-15 00:04:19 -07:00
|
|
|
partialHash.Assign(reinterpret_cast<char*>(&result.hash.prefix),
|
|
|
|
PREFIX_SIZE);
|
2008-01-29 12:57:18 -08:00
|
|
|
|
|
|
|
nsresult rv = completer->Complete(partialHash, this);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mPendingCompletions++;
|
|
|
|
}
|
|
|
|
} else {
|
2008-04-15 15:39:44 -07:00
|
|
|
// For tables with no hash completer, a complete hash match is
|
2012-08-15 00:04:19 -07:00
|
|
|
// good enough, we'll consider it fresh.
|
|
|
|
if (result.Complete()) {
|
|
|
|
result.mFresh = true;
|
2008-04-15 15:39:44 -07:00
|
|
|
} else {
|
|
|
|
NS_WARNING("Partial match in a table without a valid completer, ignoring partial match.");
|
|
|
|
}
|
2008-01-29 12:57:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPendingCompletions == 0) {
|
|
|
|
// All results were complete, we're ready!
|
|
|
|
HandleResults();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierLookupCallback::CompletionFinished(nsresult status)
|
|
|
|
{
|
|
|
|
LOG(("nsUrlClassifierLookupCallback::CompletionFinished [%p, %08x]",
|
|
|
|
this, status));
|
|
|
|
if (NS_FAILED(status)) {
|
|
|
|
NS_WARNING("gethash response failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
mPendingCompletions--;
|
|
|
|
if (mPendingCompletions == 0) {
|
|
|
|
HandleResults();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierLookupCallback::Completion(const nsACString& completeHash,
|
|
|
|
const nsACString& tableName,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t chunkId,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool verified)
|
2008-01-29 12:57:18 -08:00
|
|
|
{
|
2008-04-15 15:39:44 -07:00
|
|
|
LOG(("nsUrlClassifierLookupCallback::Completion [%p, %s, %d, %d]",
|
|
|
|
this, PromiseFlatCString(tableName).get(), chunkId, verified));
|
2012-08-15 00:04:19 -07:00
|
|
|
mozilla::safebrowsing::Completion hash;
|
2008-01-29 12:57:18 -08:00
|
|
|
hash.Assign(completeHash);
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Send this completion to the store for caching.
|
|
|
|
if (!mCacheResults) {
|
|
|
|
mCacheResults = new CacheResultArray();
|
|
|
|
if (!mCacheResults)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
if (verified) {
|
|
|
|
CacheResult result;
|
|
|
|
result.entry.addChunk = chunkId;
|
|
|
|
result.entry.hash.complete = hash;
|
|
|
|
result.table = tableName;
|
|
|
|
|
|
|
|
// OK if this fails, we just won't cache the item.
|
|
|
|
mCacheResults->AppendElement(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this matched any of our results.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mResults->Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
LookupResult& result = mResults->ElementAt(i);
|
2008-04-15 15:39:44 -07:00
|
|
|
|
|
|
|
// Now, see if it verifies a lookup
|
2012-08-15 00:04:19 -07:00
|
|
|
if (result.CompleteHash() == hash && result.mTableName.Equals(tableName)) {
|
|
|
|
result.mProtocolConfirmed = true;
|
2008-01-29 12:57:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsUrlClassifierLookupCallback::HandleResults()
|
|
|
|
{
|
|
|
|
if (!mResults) {
|
|
|
|
// No results, this URI is clean.
|
|
|
|
return mCallback->HandleEvent(NS_LITERAL_CSTRING(""));
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsTArray<nsCString> tables;
|
2008-01-29 12:57:18 -08:00
|
|
|
// Build a stringified list of result tables.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mResults->Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
LookupResult& result = mResults->ElementAt(i);
|
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
// Leave out results that weren't confirmed, as their existence on
|
2008-02-26 21:51:28 -08:00
|
|
|
// the list can't be verified. Also leave out randomly-generated
|
|
|
|
// noise.
|
2012-08-15 00:04:19 -07:00
|
|
|
if (!result.Confirmed() || result.mNoise) {
|
|
|
|
LOG(("Skipping result from table %s", result.mTableName.get()));
|
2008-01-29 12:57:18 -08:00
|
|
|
continue;
|
2012-08-15 00:04:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("Confirmed result from table %s", result.mTableName.get()));
|
|
|
|
|
|
|
|
if (tables.IndexOf(result.mTableName) == nsTArray<nsCString>::NoIndex) {
|
|
|
|
tables.AppendElement(result.mTableName);
|
|
|
|
}
|
|
|
|
}
|
2008-01-29 12:57:18 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Some parts of this gethash request generated no hits at all.
|
|
|
|
// Prefixes must have been removed from the database since our last update.
|
|
|
|
// Save the prefixes we checked to prevent repeated requests
|
|
|
|
// until the next update.
|
|
|
|
nsAutoPtr<PrefixArray> cacheMisses(new PrefixArray());
|
|
|
|
if (cacheMisses) {
|
2012-10-11 16:38:04 -07:00
|
|
|
for (uint32_t i = 0; i < mResults->Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
LookupResult &result = mResults->ElementAt(i);
|
|
|
|
if (!result.Confirmed()) {
|
|
|
|
cacheMisses->AppendElement(result.PrefixHash());
|
|
|
|
}
|
2008-01-29 12:57:18 -08:00
|
|
|
}
|
2012-08-15 00:04:19 -07:00
|
|
|
// Hands ownership of the miss array back to the worker thread.
|
|
|
|
mDBService->CacheMisses(cacheMisses.forget());
|
|
|
|
}
|
2008-01-29 12:57:18 -08:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
if (mCacheResults) {
|
|
|
|
// This hands ownership of the cache results array back to the worker
|
|
|
|
// thread.
|
|
|
|
mDBService->CacheCompletions(mCacheResults.forget());
|
2008-01-29 12:57:18 -08:00
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString tableStr;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < tables.Length(); i++) {
|
2012-08-15 00:04:19 -07:00
|
|
|
if (i != 0)
|
|
|
|
tableStr.Append(',');
|
|
|
|
tableStr.Append(tables[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mCallback->HandleEvent(tableStr);
|
2008-01-29 12:57:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 16:18:46 -07:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// Helper class for nsIURIClassifier implementation, translates table names
|
|
|
|
// to nsIURIClassifier enums.
|
|
|
|
|
2012-06-19 21:01:02 -07:00
|
|
|
class nsUrlClassifierClassifyCallback MOZ_FINAL : public nsIUrlClassifierCallback
|
2007-08-31 16:18:46 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIURLCLASSIFIERCALLBACK
|
|
|
|
|
2007-12-03 21:10:22 -08:00
|
|
|
nsUrlClassifierClassifyCallback(nsIURIClassifierCallback *c,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool checkMalware,
|
|
|
|
bool checkPhishing)
|
2007-08-31 16:18:46 -07:00
|
|
|
: mCallback(c)
|
2007-12-03 21:10:22 -08:00
|
|
|
, mCheckMalware(checkMalware)
|
|
|
|
, mCheckPhishing(checkPhishing)
|
2007-08-31 16:18:46 -07:00
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIURIClassifierCallback> mCallback;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mCheckMalware;
|
|
|
|
bool mCheckPhishing;
|
2007-08-31 16:18:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsUrlClassifierClassifyCallback,
|
2007-08-31 16:48:39 -07:00
|
|
|
nsIUrlClassifierCallback)
|
2007-08-31 16:18:46 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierClassifyCallback::HandleEvent(const nsACString& tables)
|
|
|
|
{
|
|
|
|
// XXX: we should probably have the wardens tell the service which table
|
|
|
|
// names match with which classification. For now the table names give
|
|
|
|
// enough information.
|
|
|
|
nsresult response = NS_OK;
|
|
|
|
|
|
|
|
nsACString::const_iterator begin, end;
|
|
|
|
|
|
|
|
tables.BeginReading(begin);
|
|
|
|
tables.EndReading(end);
|
2007-12-03 21:10:22 -08:00
|
|
|
if (mCheckMalware &&
|
|
|
|
FindInReadable(NS_LITERAL_CSTRING("-malware-"), begin, end)) {
|
2007-08-31 16:18:46 -07:00
|
|
|
response = NS_ERROR_MALWARE_URI;
|
2007-10-29 11:29:14 -07:00
|
|
|
} else {
|
|
|
|
// Reset begin before checking phishing table
|
|
|
|
tables.BeginReading(begin);
|
2007-12-03 21:10:22 -08:00
|
|
|
|
|
|
|
if (mCheckPhishing &&
|
|
|
|
FindInReadable(NS_LITERAL_CSTRING("-phish-"), begin, end)) {
|
2007-10-29 11:29:14 -07:00
|
|
|
response = NS_ERROR_PHISHING_URI;
|
|
|
|
}
|
2007-08-31 16:18:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mCallback->OnClassifyComplete(response);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// Proxy class implementation
|
|
|
|
|
2007-08-31 16:18:46 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsUrlClassifierDBService,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIUrlClassifierDBService,
|
2007-08-31 16:18:46 -07:00
|
|
|
nsIURIClassifier,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIObserver)
|
|
|
|
|
|
|
|
/* static */ nsUrlClassifierDBService*
|
2008-01-04 22:38:30 -08:00
|
|
|
nsUrlClassifierDBService::GetInstance(nsresult *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-04 22:38:30 -08:00
|
|
|
*result = NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!sUrlClassifierDBService) {
|
|
|
|
sUrlClassifierDBService = new nsUrlClassifierDBService();
|
2008-01-04 22:38:30 -08:00
|
|
|
if (!sUrlClassifierDBService) {
|
|
|
|
*result = NS_ERROR_OUT_OF_MEMORY;
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2008-01-04 22:38:30 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ADDREF(sUrlClassifierDBService); // addref the global
|
|
|
|
|
2008-01-04 22:38:30 -08:00
|
|
|
*result = sUrlClassifierDBService->Init();
|
|
|
|
if (NS_FAILED(*result)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_RELEASE(sUrlClassifierDBService);
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Already exists, just add a ref
|
|
|
|
NS_ADDREF(sUrlClassifierDBService); // addref the return result
|
|
|
|
}
|
|
|
|
return sUrlClassifierDBService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsUrlClassifierDBService::nsUrlClassifierDBService()
|
2007-08-31 16:18:46 -07:00
|
|
|
: mCheckMalware(CHECK_MALWARE_DEFAULT)
|
2007-12-03 21:10:22 -08:00
|
|
|
, mCheckPhishing(CHECK_PHISHING_DEFAULT)
|
2012-08-15 00:07:10 -07:00
|
|
|
, mPerClientRandomize(true)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mInUpdate(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsUrlClassifierDBService::~nsUrlClassifierDBService()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
sUrlClassifierDBService = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsUrlClassifierDBService::Init()
|
|
|
|
{
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!gUrlClassifierDbServiceLog)
|
|
|
|
gUrlClassifierDbServiceLog = PR_NewLogModule("UrlClassifierDbService");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult rv;
|
2011-09-08 13:15:18 -07:00
|
|
|
|
2007-08-31 16:18:46 -07:00
|
|
|
// Should we check document loads for malware URIs?
|
2012-01-16 17:18:29 -08:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
2007-08-31 16:18:46 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t gethashNoise = 0;
|
2007-08-31 16:18:46 -07:00
|
|
|
if (prefs) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool tmpbool;
|
2007-08-31 16:18:46 -07:00
|
|
|
rv = prefs->GetBoolPref(CHECK_MALWARE_PREF, &tmpbool);
|
|
|
|
mCheckMalware = NS_SUCCEEDED(rv) ? tmpbool : CHECK_MALWARE_DEFAULT;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
prefs->AddObserver(CHECK_MALWARE_PREF, this, false);
|
2007-12-03 21:10:22 -08:00
|
|
|
|
|
|
|
rv = prefs->GetBoolPref(CHECK_PHISHING_PREF, &tmpbool);
|
|
|
|
mCheckPhishing = NS_SUCCEEDED(rv) ? tmpbool : CHECK_PHISHING_DEFAULT;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
prefs->AddObserver(CHECK_PHISHING_PREF, this, false);
|
2008-02-26 21:51:28 -08:00
|
|
|
|
|
|
|
if (NS_FAILED(prefs->GetIntPref(GETHASH_NOISE_PREF, &gethashNoise))) {
|
|
|
|
gethashNoise = GETHASH_NOISE_DEFAULT;
|
|
|
|
}
|
2008-04-15 15:39:44 -07:00
|
|
|
|
|
|
|
nsXPIDLCString tmpstr;
|
|
|
|
if (NS_SUCCEEDED(prefs->GetCharPref(GETHASH_TABLES_PREF, getter_Copies(tmpstr)))) {
|
|
|
|
SplitTables(tmpstr, mGethashWhitelist);
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
prefs->AddObserver(GETHASH_TABLES_PREF, this, false);
|
2008-04-15 15:39:44 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t tmpint;
|
2008-04-15 15:39:44 -07:00
|
|
|
rv = prefs->GetIntPref(CONFIRM_AGE_PREF, &tmpint);
|
2011-03-28 12:58:49 -07:00
|
|
|
PR_ATOMIC_SET(&gFreshnessGuarantee, NS_SUCCEEDED(rv) ? tmpint : CONFIRM_AGE_DEFAULT_SEC);
|
2008-04-15 15:39:44 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
prefs->AddObserver(CONFIRM_AGE_PREF, this, false);
|
2012-08-15 00:07:10 -07:00
|
|
|
|
|
|
|
rv = prefs->GetBoolPref(RANDOMIZE_CLIENT_PREF, &tmpbool);
|
|
|
|
mPerClientRandomize = NS_SUCCEEDED(rv) ? tmpbool : RANDOMIZE_CLIENT_DEFAULT;
|
|
|
|
|
|
|
|
LOG(("Per client randomization is %s",
|
|
|
|
mPerClientRandomize ? "enabled" : "DISABLED"));
|
|
|
|
|
|
|
|
/* We do not observe for runtime changes as changing this preference
|
|
|
|
in flight kills the database, so it's not really supported. */
|
2012-08-15 00:04:19 -07:00
|
|
|
}
|
2008-04-15 15:39:44 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Force PSM loading on main thread
|
|
|
|
nsCOMPtr<nsICryptoHash> acryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-19 22:46:47 -07:00
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
// Directory providers must also be accessed on the main thread.
|
|
|
|
nsCOMPtr<nsIFile> cacheDir;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_LOCAL_50_DIR,
|
|
|
|
getter_AddRefs(cacheDir));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
|
|
|
|
getter_AddRefs(cacheDir));
|
2007-08-31 16:18:46 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Start the background thread.
|
2012-06-12 10:06:20 -07:00
|
|
|
rv = NS_NewNamedThread("URL Classifier", &gDbBackgroundThread);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
mWorker = new nsUrlClassifierDBServiceWorker();
|
|
|
|
if (!mWorker)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-08-15 00:07:10 -07:00
|
|
|
rv = mWorker->Init(gethashNoise, cacheDir, mPerClientRandomize);
|
2007-07-25 23:38:43 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mWorker = nullptr;
|
2007-07-25 23:38:43 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
// Proxy for calling the worker on the background thread
|
2011-08-19 08:50:04 -07:00
|
|
|
mWorkerProxy = new UrlClassifierDBServiceWorkerProxy(mWorker);
|
2008-01-12 14:22:03 -08:00
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
mCompleters.Init();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Add an observer for shutdown
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
mozilla::services::GetObserverService();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!observerService)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
observerService->AddObserver(this, "profile-before-change", false);
|
|
|
|
observerService->AddObserver(this, "xpcom-shutdown-threads", false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-08-31 16:18:46 -07:00
|
|
|
NS_IMETHODIMP
|
2012-07-20 09:46:59 -07:00
|
|
|
nsUrlClassifierDBService::Classify(nsIPrincipal* aPrincipal,
|
2007-08-31 16:18:46 -07:00
|
|
|
nsIURIClassifierCallback* c,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* result)
|
2007-08-31 16:18:46 -07:00
|
|
|
{
|
2012-07-20 09:46:59 -07:00
|
|
|
NS_ENSURE_ARG(aPrincipal);
|
2007-08-31 16:18:46 -07:00
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2007-12-03 21:10:22 -08:00
|
|
|
if (!(mCheckMalware || mCheckPhishing)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false;
|
2007-08-31 16:18:46 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsUrlClassifierClassifyCallback> callback =
|
2007-12-03 21:10:22 -08:00
|
|
|
new nsUrlClassifierClassifyCallback(c, mCheckMalware, mCheckPhishing);
|
2007-08-31 16:18:46 -07:00
|
|
|
if (!callback) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-07-22 12:30:30 -07:00
|
|
|
nsresult rv = LookupURI(aPrincipal, callback, false, result);
|
2007-11-27 12:08:02 -08:00
|
|
|
if (rv == NS_ERROR_MALFORMED_URI) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false;
|
2008-10-19 20:07:46 -07:00
|
|
|
// The URI had no hostname, don't try to classify it.
|
2007-11-27 12:08:02 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-02-21 02:45:56 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-27 12:08:02 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
2007-08-31 16:18:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-22 12:30:30 -07:00
|
|
|
nsUrlClassifierDBService::Lookup(nsIPrincipal* aPrincipal,
|
2008-01-29 12:57:18 -08:00
|
|
|
nsIUrlClassifierCallback* c)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2012-07-22 12:30:30 -07:00
|
|
|
bool dummy;
|
|
|
|
return LookupURI(aPrincipal, c, true, &dummy);
|
2007-08-31 16:18:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-07-22 12:30:30 -07:00
|
|
|
nsUrlClassifierDBService::LookupURI(nsIPrincipal* aPrincipal,
|
2008-10-19 20:07:46 -07:00
|
|
|
nsIUrlClassifierCallback* c,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool forceLookup,
|
|
|
|
bool *didLookup)
|
2007-08-31 16:18:46 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
2012-07-22 12:30:30 -07:00
|
|
|
NS_ENSURE_ARG(aPrincipal);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = aPrincipal->GetURI(getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
uri = NS_GetInnermostURI(uri);
|
|
|
|
NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
|
2007-08-31 16:18:46 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString key;
|
2007-07-25 23:38:43 -07:00
|
|
|
// Canonicalize the url
|
|
|
|
nsCOMPtr<nsIUrlClassifierUtils> utilsService =
|
|
|
|
do_GetService(NS_URLCLASSIFIERUTILS_CONTRACTID);
|
2012-07-22 12:30:30 -07:00
|
|
|
rv = utilsService->GetKeyForURI(uri, key);
|
2008-02-21 02:45:56 -08:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-10-19 20:07:46 -07:00
|
|
|
if (forceLookup) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*didLookup = true;
|
2008-10-19 20:07:46 -07:00
|
|
|
} else {
|
2012-08-15 00:04:19 -07:00
|
|
|
bool clean = false;
|
2008-10-19 20:07:46 -07:00
|
|
|
|
2011-05-03 10:39:28 -07:00
|
|
|
if (!clean) {
|
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
|
|
|
|
|
|
|
if (permissionManager) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t perm;
|
2012-07-22 12:30:30 -07:00
|
|
|
rv = permissionManager->TestPermissionFromPrincipal(aPrincipal,
|
|
|
|
"safe-browsing", &perm);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-05-03 10:39:28 -07:00
|
|
|
clean |= (perm == nsIPermissionManager::ALLOW_ACTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-19 20:07:46 -07:00
|
|
|
*didLookup = !clean;
|
|
|
|
if (clean) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
// Create an nsUrlClassifierLookupCallback object. This object will
|
|
|
|
// take care of confirming partial hash matches if necessary before
|
|
|
|
// calling the client's callback.
|
|
|
|
nsCOMPtr<nsIUrlClassifierLookupCallback> callback =
|
|
|
|
new nsUrlClassifierLookupCallback(this, c);
|
|
|
|
if (!callback)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2011-08-19 08:50:04 -07:00
|
|
|
nsCOMPtr<nsIUrlClassifierLookupCallback> proxyCallback =
|
|
|
|
new UrlClassifierLookupCallbackProxy(callback);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-25 23:38:43 -07:00
|
|
|
// Queue this lookup and call the lookup function to flush the queue if
|
|
|
|
// necessary.
|
|
|
|
rv = mWorker->QueueLookup(key, proxyCallback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return mWorkerProxy->Lookup(nullptr, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-07-25 23:38:43 -07:00
|
|
|
nsUrlClassifierDBService::GetTables(nsIUrlClassifierCallback* c)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
// The proxy callback uses the current thread.
|
2011-08-19 08:50:04 -07:00
|
|
|
nsCOMPtr<nsIUrlClassifierCallback> proxyCallback =
|
|
|
|
new UrlClassifierCallbackProxy(c);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
return mWorkerProxy->GetTables(proxyCallback);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBService::SetHashCompleter(const nsACString &tableName,
|
|
|
|
nsIUrlClassifierHashCompleter *completer)
|
|
|
|
{
|
|
|
|
if (completer) {
|
2012-05-18 10:30:49 -07:00
|
|
|
mCompleters.Put(tableName, completer);
|
2008-01-29 12:57:18 -08:00
|
|
|
} else {
|
|
|
|
mCompleters.Remove(tableName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2008-02-27 00:51:02 -08:00
|
|
|
nsUrlClassifierDBService::BeginUpdate(nsIUrlClassifierUpdateObserver *observer,
|
2008-04-15 15:39:44 -07:00
|
|
|
const nsACString &updateTables,
|
2008-02-27 00:51:02 -08:00
|
|
|
const nsACString &clientKey)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2008-01-23 11:30:54 -08:00
|
|
|
if (mInUpdate)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mInUpdate = true;
|
2008-01-23 11:30:54 -08:00
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
// The proxy observer uses the current thread
|
2011-08-19 08:50:04 -07:00
|
|
|
nsCOMPtr<nsIUrlClassifierUpdateObserver> proxyObserver =
|
|
|
|
new UrlClassifierUpdateObserverProxy(observer);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-04-15 15:39:44 -07:00
|
|
|
return mWorkerProxy->BeginUpdate(proxyObserver, updateTables, clientKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-02-27 00:51:02 -08:00
|
|
|
nsUrlClassifierDBService::BeginStream(const nsACString &table,
|
|
|
|
const nsACString &serverMAC)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2008-02-27 00:51:02 -08:00
|
|
|
return mWorkerProxy->BeginStream(table, serverMAC);
|
2008-01-12 14:22:03 -08:00
|
|
|
}
|
2007-07-25 23:38:43 -07:00
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBService::UpdateStream(const nsACString& aUpdateChunk)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
return mWorkerProxy->UpdateStream(aUpdateChunk);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBService::FinishStream()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
return mWorkerProxy->FinishStream();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-01-12 13:32:01 -08:00
|
|
|
NS_IMETHODIMP
|
2008-01-12 14:22:03 -08:00
|
|
|
nsUrlClassifierDBService::FinishUpdate()
|
2008-01-12 13:32:01 -08:00
|
|
|
{
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
2008-01-12 13:32:01 -08:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mInUpdate = false;
|
2008-01-23 11:30:54 -08:00
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
return mWorkerProxy->FinishUpdate();
|
2008-01-12 13:32:01 -08:00
|
|
|
}
|
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2008-01-12 14:22:03 -08:00
|
|
|
nsUrlClassifierDBService::CancelUpdate()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mInUpdate = false;
|
2008-01-23 11:30:54 -08:00
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
return mWorkerProxy->CancelUpdate();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBService::ResetDatabase()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
return mWorkerProxy->ResetDatabase();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-01-29 19:15:13 -08:00
|
|
|
nsresult
|
2012-08-15 00:04:19 -07:00
|
|
|
nsUrlClassifierDBService::CacheCompletions(CacheResultArray *results)
|
2008-01-29 18:26:44 -08:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
return mWorkerProxy->CacheCompletions(results);
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
nsresult
|
|
|
|
nsUrlClassifierDBService::CacheMisses(PrefixArray *results)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
return mWorkerProxy->CacheMisses(results);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-03-04 14:05:05 -08:00
|
|
|
nsUrlClassifierDBService::GetCompleter(const nsACString &tableName,
|
|
|
|
nsIUrlClassifierHashCompleter **completer)
|
|
|
|
{
|
|
|
|
if (mCompleters.Get(tableName, completer)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-03-04 14:05:05 -08:00
|
|
|
}
|
|
|
|
|
2008-04-15 15:39:44 -07:00
|
|
|
if (!mGethashWhitelist.Contains(tableName)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-04-15 15:39:44 -07:00
|
|
|
}
|
|
|
|
|
2008-03-04 14:05:05 -08:00
|
|
|
return NS_SUCCEEDED(CallGetService(NS_URLCLASSIFIERHASHCOMPLETER_CONTRACTID,
|
|
|
|
completer));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUrlClassifierDBService::Observe(nsISupports *aSubject, const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
2007-08-31 16:18:46 -07:00
|
|
|
if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs(do_QueryInterface(aSubject, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (NS_LITERAL_STRING(CHECK_MALWARE_PREF).Equals(aData)) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool tmpbool;
|
2007-08-31 16:18:46 -07:00
|
|
|
rv = prefs->GetBoolPref(CHECK_MALWARE_PREF, &tmpbool);
|
|
|
|
mCheckMalware = NS_SUCCEEDED(rv) ? tmpbool : CHECK_MALWARE_DEFAULT;
|
2007-12-03 21:10:22 -08:00
|
|
|
} else if (NS_LITERAL_STRING(CHECK_PHISHING_PREF).Equals(aData)) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool tmpbool;
|
2007-12-03 21:10:22 -08:00
|
|
|
rv = prefs->GetBoolPref(CHECK_PHISHING_PREF, &tmpbool);
|
|
|
|
mCheckPhishing = NS_SUCCEEDED(rv) ? tmpbool : CHECK_PHISHING_DEFAULT;
|
2008-04-15 15:39:44 -07:00
|
|
|
} else if (NS_LITERAL_STRING(GETHASH_TABLES_PREF).Equals(aData)) {
|
|
|
|
mGethashWhitelist.Clear();
|
|
|
|
nsXPIDLCString val;
|
|
|
|
if (NS_SUCCEEDED(prefs->GetCharPref(GETHASH_TABLES_PREF, getter_Copies(val)))) {
|
|
|
|
SplitTables(val, mGethashWhitelist);
|
|
|
|
}
|
|
|
|
} else if (NS_LITERAL_STRING(CONFIRM_AGE_PREF).Equals(aData)) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t tmpint;
|
2008-04-15 15:39:44 -07:00
|
|
|
rv = prefs->GetIntPref(CONFIRM_AGE_PREF, &tmpint);
|
2011-03-28 12:58:49 -07:00
|
|
|
PR_ATOMIC_SET(&gFreshnessGuarantee, NS_SUCCEEDED(rv) ? tmpint : CONFIRM_AGE_DEFAULT_SEC);
|
2007-08-31 16:18:46 -07:00
|
|
|
}
|
|
|
|
} else if (!strcmp(aTopic, "profile-before-change") ||
|
|
|
|
!strcmp(aTopic, "xpcom-shutdown-threads")) {
|
|
|
|
Shutdown();
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2007-07-11 09:52:12 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join the background thread if it exists.
|
|
|
|
nsresult
|
|
|
|
nsUrlClassifierDBService::Shutdown()
|
|
|
|
{
|
2007-07-11 09:52:12 -07:00
|
|
|
LOG(("shutting down db service\n"));
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gDbBackgroundThread)
|
|
|
|
return NS_OK;
|
|
|
|
|
2008-01-29 12:57:18 -08:00
|
|
|
mCompleters.Clear();
|
|
|
|
|
2012-01-16 17:18:29 -08:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
2007-08-31 16:18:46 -07:00
|
|
|
if (prefs) {
|
|
|
|
prefs->RemoveObserver(CHECK_MALWARE_PREF, this);
|
2007-12-03 21:10:22 -08:00
|
|
|
prefs->RemoveObserver(CHECK_PHISHING_PREF, this);
|
2008-04-15 15:39:44 -07:00
|
|
|
prefs->RemoveObserver(GETHASH_TABLES_PREF, this);
|
|
|
|
prefs->RemoveObserver(CONFIRM_AGE_PREF, this);
|
2007-08-31 16:18:46 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
// First close the db connection.
|
|
|
|
if (mWorker) {
|
2008-05-07 13:33:45 -07:00
|
|
|
rv = mWorkerProxy->CancelUpdate();
|
2009-06-17 03:47:08 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to post cancel update event");
|
2011-09-08 13:16:59 -07:00
|
|
|
|
2008-01-12 14:22:03 -08:00
|
|
|
rv = mWorkerProxy->CloseDb();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to post close db event");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-01-12 14:22:03 -08:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mWorkerProxy = nullptr;
|
2008-01-12 14:22:03 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("joining background thread"));
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
gShuttingDownThread = true;
|
2008-01-16 11:20:45 -08:00
|
|
|
|
|
|
|
nsIThread *backgroundThread = gDbBackgroundThread;
|
2012-07-30 07:20:58 -07:00
|
|
|
gDbBackgroundThread = nullptr;
|
2008-01-16 11:20:45 -08:00
|
|
|
backgroundThread->Shutdown();
|
|
|
|
NS_RELEASE(backgroundThread);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-08-19 08:50:04 -07:00
|
|
|
|
|
|
|
nsIThread*
|
|
|
|
nsUrlClassifierDBService::BackgroundThread()
|
|
|
|
{
|
|
|
|
return gDbBackgroundThread;
|
|
|
|
}
|
|
|
|
|