Bug 1319286 - Cache nsIUrlClassifierDBService.getTables result until next update. r=francois.

MozReview-Commit-ID: ItjTQNzCVED
This commit is contained in:
Henry Chang 2016-11-22 10:39:58 +08:00
Родитель f8f6fe4d63
Коммит a5327a4bbb
3 изменённых файлов: 45 добавлений и 3 удалений

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

@ -22,6 +22,7 @@
#include "mozilla/Unused.h"
#include "mozilla/TypedEnumBits.h"
#include "nsIUrlClassifierUtils.h"
#include "nsUrlClassifierDBService.h"
// MOZ_LOG=UrlClassifierDbService:5
extern mozilla::LazyLogModule gUrlClassifierDbServiceLog;
@ -144,6 +145,7 @@ Classifier::GetPrivateStoreDirectory(nsIFile* aRootStoreDirectory,
}
Classifier::Classifier()
: mIsTableRequestResultOutdated(true)
{
}
@ -348,6 +350,16 @@ Classifier::AbortUpdateAndReset(const nsCString& aTable)
void
Classifier::TableRequest(nsACString& aResult)
{
MOZ_ASSERT(!NS_IsMainThread(),
"TableRequest must be called on the classifier worker thread.");
// This function and all disk I/O are guaranteed to occur
// on the same thread so we don't need to add a lock around.
if (!mIsTableRequestResultOutdated) {
aResult = mTableRequestResult;
return;
}
// Generating v2 table info.
nsTArray<nsCString> tables;
ActiveTables(tables);
@ -387,8 +399,13 @@ Classifier::TableRequest(nsACString& aResult)
// Specifically for v4 tables.
nsCString metadata;
nsresult rv = LoadMetadata(mRootStoreDirectory, metadata);
NS_ENSURE_SUCCESS_VOID(rv);
aResult.Append(metadata);
if (NS_SUCCEEDED(rv)) {
aResult.Append(metadata);
}
// Update the TableRequest result in-memory cache.
mTableRequestResult = aResult;
mIsTableRequestResultOutdated = false;
}
// This is used to record the matching statistics for v2 and v4.
@ -534,6 +551,10 @@ Classifier::ApplyUpdates(nsTArray<TableUpdate*>* aUpdates)
rv = UpdateTableV4(aUpdates, updateTable);
}
// We mark the table associated info outdated no matter the
// update is successful to avoid any possibile non-atomic update.
mIsTableRequestResultOutdated = true;
if (NS_FAILED(rv)) {
if (rv != NS_ERROR_OUT_OF_MEMORY) {
#ifdef MOZ_SAFEBROWSING_DUMP_FAILED_UPDATES
@ -1009,6 +1030,9 @@ nsresult
Classifier::UpdateTableV4(nsTArray<TableUpdate*>* aUpdates,
const nsACString& aTable)
{
MOZ_ASSERT(!NS_IsMainThread(),
"UpdateTableV4 must be called on the classifier worker thread.");
LOG(("Classifier::UpdateTableV4(%s)", PromiseFlatCString(aTable).get()));
if (!CheckValidUpdate(aUpdates, aTable)) {

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

@ -159,6 +159,14 @@ private:
uint32_t mHashKey;
// Stores the last time a given table was updated (seconds).
nsDataHashtable<nsCStringHashKey, int64_t> mTableFreshness;
// In-memory cache for the result of TableRequest. See
// nsIUrlClassifierDBService.getTables for the format.
nsCString mTableRequestResult;
// Whether mTableRequestResult is outdated and needs to
// be reloaded from disk.
bool mIsTableRequestResultOutdated;
};
} // namespace safebrowsing

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

@ -360,7 +360,17 @@ function waitUntilMetaDataSaved(expectedState, expectedChecksum, callback) {
if (stateBase64 === btoa(expectedState) &&
checksumBase64 === btoa(expectedChecksum)) {
do_print('State has been saved to disk!');
callback();
// We slightly defer the callback to see if the in-memory
// |getTables| caching works correctly.
dbService.getTables(cachedMetadata => {
equal(cachedMetadata, metaData);
callback();
});
// Even though we haven't done callback at this moment
// but we still claim "we have" in order to stop repeating
// a new timer.
didCallback = true;
}