Bug 1164518 - Avoid unnecessary DB updates when caching Safe Browsing results. r=gcp

MozReview-Commit-ID: HYNaTdCRohL

--HG--
extra : rebase_source : 681b33066b21719296c9d33f8d71a72335513188
This commit is contained in:
Francois Marier 2016-03-04 12:33:20 -08:00
Родитель 5e966c74d9
Коммит 5f9205dae9
7 изменённых файлов: 71 добавлений и 1 удалений

Просмотреть файл

@ -181,6 +181,13 @@ struct AddComplete {
}
return addChunk - other.addChunk;
}
bool operator!=(const AddComplete& aOther) const {
if (addChunk != aOther.addChunk) {
return true;
}
return complete != aOther.complete;
}
};
struct SubPrefix {

Просмотреть файл

@ -67,6 +67,13 @@ typedef nsTArray<LookupResult> LookupResultArray;
struct CacheResult {
AddComplete entry;
nsCString table;
bool operator==(const CacheResult& aOther) const {
if (entry != aOther.entry) {
return false;
}
return table == aOther.table;
}
};
typedef nsTArray<CacheResult> CacheResultArray;

Просмотреть файл

@ -107,6 +107,11 @@ interface nsIUrlClassifierDBService : nsISupports
void setLastUpdateTime(in ACString tableName,
in unsigned long long lastUpdateTime);
/**
* Forget the results that were used in the last DB update.
*/
void clearLastResults();
////////////////////////////////////////////////////////////////////////////
// Incremental update methods.
//

Просмотреть файл

@ -660,6 +660,11 @@ nsUrlClassifierDBServiceWorker::CacheCompletions(CacheResultArray *results)
// Ownership is transferred in to us
nsAutoPtr<CacheResultArray> resultsPtr(results);
if (mLastResults == *resultsPtr) {
LOG(("Skipping completions that have just been cached already."));
return NS_OK;
}
nsAutoPtr<ProtocolParser> pParse(new ProtocolParser());
nsTArray<TableUpdate*> updates;
@ -702,6 +707,7 @@ nsUrlClassifierDBServiceWorker::CacheCompletions(CacheResultArray *results)
}
mClassifier->ApplyUpdates(&updates);
mLastResults = *resultsPtr;
return NS_OK;
}
@ -758,6 +764,15 @@ nsUrlClassifierDBServiceWorker::SetLastUpdateTime(const nsACString &table,
return NS_OK;
}
NS_IMETHODIMP
nsUrlClassifierDBServiceWorker::ClearLastResults()
{
MOZ_ASSERT(!NS_IsMainThread(), "Must be on the background thread");
mLastResults.Clear();
return NS_OK;
}
// -------------------------------------------------------------------------
// nsUrlClassifierLookupCallback
//
@ -1458,7 +1473,7 @@ nsUrlClassifierDBService::SetHashCompleter(const nsACString &tableName,
} else {
mCompleters.Remove(tableName);
}
ClearLastResults();
return NS_OK;
}
@ -1471,6 +1486,14 @@ nsUrlClassifierDBService::SetLastUpdateTime(const nsACString &tableName,
return mWorkerProxy->SetLastUpdateTime(tableName, lastUpdateTime);
}
NS_IMETHODIMP
nsUrlClassifierDBService::ClearLastResults()
{
NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
return mWorkerProxy->ClearLastResults();
}
NS_IMETHODIMP
nsUrlClassifierDBService::BeginUpdate(nsIUrlClassifierUpdateObserver *observer,
const nsACString &updateTables)

Просмотреть файл

@ -212,6 +212,9 @@ private:
// the next update
PrefixArray mMissCache;
// Stores the last results that triggered a table update.
CacheResultArray mLastResults;
nsresult mUpdateStatus;
nsTArray<nsCString> mUpdateTables;

Просмотреть файл

@ -232,6 +232,19 @@ UrlClassifierDBServiceWorkerProxy::SetLastUpdateTimeRunnable::Run()
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::ClearLastResults()
{
nsCOMPtr<nsIRunnable> r = new ClearLastResultsRunnable(mTarget);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::ClearLastResultsRunnable::Run()
{
return mTarget->ClearLastResults();
}
NS_IMPL_ISUPPORTS(UrlClassifierLookupCallbackProxy,
nsIUrlClassifierLookupCallback)

Просмотреть файл

@ -191,6 +191,18 @@ public:
uint64_t mUpdateTime;
};
class ClearLastResultsRunnable : public nsRunnable
{
public:
explicit ClearLastResultsRunnable(nsUrlClassifierDBServiceWorker* aTarget)
: mTarget(aTarget)
{ }
NS_DECL_NSIRUNNABLE
private:
RefPtr<nsUrlClassifierDBServiceWorker> mTarget;
};
public:
nsresult DoLocalLookup(const nsACString& spec,
const nsACString& tables,