Bug 1434206 - Keep LookupCache objects in smart pointers. r=gcp
The existing mix of UniquePtr and raw pointers is confusing when trying to figure out the exact lifetime of these objects. MozReview-Commit-ID: Br4S7BXEFKs
This commit is contained in:
@@ -284,7 +284,7 @@ Classifier::ResetTables(ClearType aType, const nsTArray<nsCString>& aTables)
|
||||
{
|
||||
for (uint32_t i = 0; i < aTables.Length(); i++) {
|
||||
LOG(("Resetting table: %s", aTables[i].get()));
|
||||
LookupCache *cache = GetLookupCache(aTables[i]);
|
||||
RefPtr<LookupCache> cache = GetLookupCache(aTables[i]);
|
||||
if (cache) {
|
||||
// Remove any cached Completes for this table if clear type is Clear_Cache
|
||||
if (aType == Clear_Cache) {
|
||||
@@ -427,10 +427,10 @@ Classifier::Check(const nsACString& aSpec,
|
||||
nsTArray<nsCString> activeTables;
|
||||
SplitTables(aTables, activeTables);
|
||||
|
||||
nsTArray<LookupCache*> cacheArray;
|
||||
LookupCacheArray cacheArray;
|
||||
for (uint32_t i = 0; i < activeTables.Length(); i++) {
|
||||
LOG(("Checking table %s", activeTables[i].get()));
|
||||
LookupCache *cache = GetLookupCache(activeTables[i]);
|
||||
RefPtr<LookupCache> cache = GetLookupCache(activeTables[i]);
|
||||
if (cache) {
|
||||
cacheArray.AppendElement(cache);
|
||||
} else {
|
||||
@@ -451,7 +451,7 @@ Classifier::Check(const nsACString& aSpec,
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < cacheArray.Length(); i++) {
|
||||
LookupCache *cache = cacheArray[i];
|
||||
RefPtr<LookupCache> cache = cacheArray[i];
|
||||
bool has, confirmed;
|
||||
uint32_t matchLength;
|
||||
|
||||
@@ -553,9 +553,6 @@ void
|
||||
Classifier::RemoveUpdateIntermediaries()
|
||||
{
|
||||
// Remove old LookupCaches.
|
||||
for (auto c: mNewLookupCaches) {
|
||||
delete c;
|
||||
}
|
||||
mNewLookupCaches.Clear();
|
||||
|
||||
// Remove the "old" directory. (despite its looking-new name)
|
||||
@@ -876,7 +873,7 @@ void
|
||||
Classifier::GetCacheInfo(const nsACString& aTable,
|
||||
nsIUrlClassifierCacheInfo** aCache)
|
||||
{
|
||||
LookupCache* lookupCache = GetLookupCache(aTable);
|
||||
RefPtr<LookupCache> lookupCache = GetLookupCache(aTable);
|
||||
if (!lookupCache) {
|
||||
return;
|
||||
}
|
||||
@@ -887,9 +884,6 @@ Classifier::GetCacheInfo(const nsACString& aTable,
|
||||
void
|
||||
Classifier::DropStores()
|
||||
{
|
||||
for (uint32_t i = 0; i < mLookupCaches.Length(); i++) {
|
||||
delete mLookupCaches[i];
|
||||
}
|
||||
mLookupCaches.Clear();
|
||||
}
|
||||
|
||||
@@ -904,7 +898,7 @@ Classifier::RegenActiveTables()
|
||||
for (uint32_t i = 0; i < foundTables.Length(); i++) {
|
||||
nsCString table(foundTables[i]);
|
||||
|
||||
LookupCache *lookupCache = GetLookupCache(table);
|
||||
RefPtr<LookupCache> lookupCache = GetLookupCache(table);
|
||||
if (!lookupCache) {
|
||||
LOG(("Inactive table (no cache): %s", table.get()));
|
||||
continue;
|
||||
@@ -1200,14 +1194,19 @@ Classifier::UpdateHashStore(TableUpdateArray& aUpdates,
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
// Read the part of the store that is (only) in the cache
|
||||
LookupCacheV2* lookupCache =
|
||||
LookupCache::Cast<LookupCacheV2>(GetLookupCacheForUpdate(store.TableName()));
|
||||
if (!lookupCache) {
|
||||
RefPtr<LookupCacheV2> lookupCacheV2;
|
||||
{
|
||||
RefPtr<LookupCache> lookupCache = GetLookupCacheForUpdate(store.TableName());
|
||||
if (lookupCache) {
|
||||
lookupCacheV2 = LookupCache::Cast<LookupCacheV2>(lookupCache);
|
||||
}
|
||||
}
|
||||
if (!lookupCacheV2) {
|
||||
return NS_ERROR_UC_UPDATE_TABLE_NOT_FOUND;
|
||||
}
|
||||
|
||||
FallibleTArray<uint32_t> AddPrefixHashes;
|
||||
rv = lookupCache->GetPrefixes(AddPrefixHashes);
|
||||
rv = lookupCacheV2->GetPrefixes(AddPrefixHashes);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
rv = store.AugmentAdds(AddPrefixHashes);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
@@ -1260,13 +1259,13 @@ Classifier::UpdateHashStore(TableUpdateArray& aUpdates,
|
||||
|
||||
// At this point the store is updated and written out to disk, but
|
||||
// the data is still in memory. Build our quick-lookup table here.
|
||||
rv = lookupCache->Build(store.AddPrefixes(), store.AddCompletes());
|
||||
rv = lookupCacheV2->Build(store.AddPrefixes(), store.AddCompletes());
|
||||
NS_ENSURE_SUCCESS(rv, NS_ERROR_UC_UPDATE_BUILD_PREFIX_FAILURE);
|
||||
|
||||
#if defined(DEBUG)
|
||||
lookupCache->DumpCompletions();
|
||||
lookupCacheV2->DumpCompletions();
|
||||
#endif
|
||||
rv = lookupCache->WriteFile();
|
||||
rv = lookupCacheV2->WriteFile();
|
||||
NS_ENSURE_SUCCESS(rv, NS_ERROR_UC_UPDATE_FAIL_TO_WRITE_DISK);
|
||||
|
||||
LOG(("Successfully updated %s", store.TableName().get()));
|
||||
@@ -1290,9 +1289,14 @@ Classifier::UpdateTableV4(TableUpdateArray& aUpdates,
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
LookupCacheV4* lookupCache =
|
||||
LookupCache::Cast<LookupCacheV4>(GetLookupCacheForUpdate(aTable));
|
||||
if (!lookupCache) {
|
||||
RefPtr<LookupCacheV4> lookupCacheV4;
|
||||
{
|
||||
RefPtr<LookupCache> lookupCache = GetLookupCacheForUpdate(aTable);
|
||||
if (lookupCache) {
|
||||
lookupCacheV4 = LookupCache::Cast<LookupCacheV4>(lookupCache);
|
||||
}
|
||||
}
|
||||
if (!lookupCacheV4) {
|
||||
return NS_ERROR_UC_UPDATE_TABLE_NOT_FOUND;
|
||||
}
|
||||
|
||||
@@ -1317,7 +1321,7 @@ Classifier::UpdateTableV4(TableUpdateArray& aUpdates,
|
||||
if (updateV4->IsFullUpdate()) {
|
||||
input->Clear();
|
||||
output->Clear();
|
||||
rv = lookupCache->ApplyUpdate(updateV4, *input, *output);
|
||||
rv = lookupCacheV4->ApplyUpdate(updateV4, *input, *output);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
@@ -1326,7 +1330,7 @@ Classifier::UpdateTableV4(TableUpdateArray& aUpdates,
|
||||
// without a prior full/partial update in the loop. In this case we should
|
||||
// get prefixes from the lookup cache first.
|
||||
if (prefixes1.IsEmpty() && prefixes2.IsEmpty()) {
|
||||
lookupCache->GetPrefixes(prefixes1);
|
||||
lookupCacheV4->GetPrefixes(prefixes1);
|
||||
} else {
|
||||
MOZ_ASSERT(prefixes1.IsEmpty() ^ prefixes2.IsEmpty());
|
||||
|
||||
@@ -1337,7 +1341,7 @@ Classifier::UpdateTableV4(TableUpdateArray& aUpdates,
|
||||
output = prefixes1.IsEmpty() ? &prefixes1 : &prefixes2;
|
||||
}
|
||||
|
||||
rv = lookupCache->ApplyUpdate(updateV4, *input, *output);
|
||||
rv = lookupCacheV4->ApplyUpdate(updateV4, *input, *output);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
@@ -1351,15 +1355,15 @@ Classifier::UpdateTableV4(TableUpdateArray& aUpdates,
|
||||
aUpdates[i] = nullptr;
|
||||
}
|
||||
|
||||
rv = lookupCache->Build(*output);
|
||||
rv = lookupCacheV4->Build(*output);
|
||||
NS_ENSURE_SUCCESS(rv, NS_ERROR_UC_UPDATE_BUILD_PREFIX_FAILURE);
|
||||
|
||||
rv = lookupCache->WriteFile();
|
||||
rv = lookupCacheV4->WriteFile();
|
||||
NS_ENSURE_SUCCESS(rv, NS_ERROR_UC_UPDATE_FAIL_TO_WRITE_DISK);
|
||||
|
||||
if (lastAppliedUpdate) {
|
||||
LOG(("Write meta data of the last applied update."));
|
||||
rv = lookupCache->WriteMetadata(lastAppliedUpdate);
|
||||
rv = lookupCacheV4->WriteMetadata(lastAppliedUpdate);
|
||||
NS_ENSURE_SUCCESS(rv, NS_ERROR_UC_UPDATE_FAIL_TO_WRITE_DISK);
|
||||
}
|
||||
|
||||
@@ -1378,18 +1382,18 @@ Classifier::UpdateCache(RefPtr<const TableUpdate> aUpdate)
|
||||
nsAutoCString table(aUpdate->TableName());
|
||||
LOG(("Classifier::UpdateCache(%s)", table.get()));
|
||||
|
||||
LookupCache *lookupCache = GetLookupCache(table);
|
||||
RefPtr<LookupCache> lookupCache = GetLookupCache(table);
|
||||
if (!lookupCache) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
auto lookupV2 = LookupCache::Cast<LookupCacheV2>(lookupCache);
|
||||
RefPtr<LookupCacheV2> lookupV2 = LookupCache::Cast<LookupCacheV2>(lookupCache);
|
||||
if (lookupV2) {
|
||||
RefPtr<const TableUpdateV2> updateV2 = TableUpdate::Cast<TableUpdateV2>(aUpdate);
|
||||
lookupV2->AddGethashResultToCache(updateV2->AddCompletes(),
|
||||
updateV2->MissPrefixes());
|
||||
} else {
|
||||
auto lookupV4 = LookupCache::Cast<LookupCacheV4>(lookupCache);
|
||||
RefPtr<LookupCacheV4> lookupV4 = LookupCache::Cast<LookupCacheV4>(lookupCache);
|
||||
if (!lookupV4) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
@@ -1405,14 +1409,14 @@ Classifier::UpdateCache(RefPtr<const TableUpdate> aUpdate)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
LookupCache *
|
||||
RefPtr<LookupCache>
|
||||
Classifier::GetLookupCache(const nsACString& aTable, bool aForUpdate)
|
||||
{
|
||||
// GetLookupCache(aForUpdate==true) can only be called on update thread.
|
||||
MOZ_ASSERT_IF(aForUpdate, NS_GetCurrentThread() == mUpdateThread);
|
||||
|
||||
nsTArray<LookupCache*>& lookupCaches = aForUpdate ? mNewLookupCaches
|
||||
: mLookupCaches;
|
||||
LookupCacheArray& lookupCaches = aForUpdate ? mNewLookupCaches
|
||||
: mLookupCaches;
|
||||
auto& rootStoreDirectory = aForUpdate ? mUpdatingDirectory
|
||||
: mRootStoreDirectory;
|
||||
|
||||
@@ -1430,12 +1434,12 @@ Classifier::GetLookupCache(const nsACString& aTable, bool aForUpdate)
|
||||
// TODO : Bug 1302600, It would be better if we have a more general non-main
|
||||
// thread method to convert table name to protocol version. Currently
|
||||
// we can only know this by checking if the table name ends with '-proto'.
|
||||
UniquePtr<LookupCache> cache;
|
||||
RefPtr<LookupCache> cache;
|
||||
nsCString provider = GetProvider(aTable);
|
||||
if (StringEndsWith(aTable, NS_LITERAL_CSTRING("-proto"))) {
|
||||
cache = MakeUnique<LookupCacheV4>(aTable, provider, rootStoreDirectory);
|
||||
cache = new LookupCacheV4(aTable, provider, rootStoreDirectory);
|
||||
} else {
|
||||
cache = MakeUnique<LookupCacheV2>(aTable, provider, rootStoreDirectory);
|
||||
cache = new LookupCacheV2(aTable, provider, rootStoreDirectory);
|
||||
}
|
||||
|
||||
nsresult rv = cache->Init();
|
||||
@@ -1444,8 +1448,8 @@ Classifier::GetLookupCache(const nsACString& aTable, bool aForUpdate)
|
||||
}
|
||||
rv = cache->Open();
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
lookupCaches.AppendElement(cache.get());
|
||||
return cache.release();
|
||||
lookupCaches.AppendElement(cache);
|
||||
return cache;
|
||||
}
|
||||
|
||||
// At this point we failed to open LookupCache.
|
||||
@@ -1476,12 +1480,12 @@ Classifier::ReadNoiseEntries(const Prefix& aPrefix,
|
||||
FallibleTArray<uint32_t> prefixes;
|
||||
nsresult rv;
|
||||
|
||||
LookupCache *cache = GetLookupCache(aTableName);
|
||||
RefPtr<LookupCache> cache = GetLookupCache(aTableName);
|
||||
if (!cache) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
LookupCacheV2* cacheV2 = LookupCache::Cast<LookupCacheV2>(cache);
|
||||
RefPtr<LookupCacheV2> cacheV2 = LookupCache::Cast<LookupCacheV2>(cache);
|
||||
if (cacheV2) {
|
||||
rv = cacheV2->GetPrefixes(prefixes);
|
||||
} else {
|
||||
@@ -1562,15 +1566,20 @@ Classifier::LoadMetadata(nsIFile* aDirectory, nsACString& aResult)
|
||||
}
|
||||
tableName.Cut(dot, METADATA_SUFFIX.Length());
|
||||
|
||||
LookupCacheV4* lookupCache =
|
||||
LookupCache::Cast<LookupCacheV4>(GetLookupCache(tableName));
|
||||
if (!lookupCache) {
|
||||
RefPtr<LookupCacheV4> lookupCacheV4;
|
||||
{
|
||||
RefPtr<LookupCache> lookupCache = GetLookupCache(tableName);
|
||||
if (lookupCache) {
|
||||
lookupCacheV4 = LookupCache::Cast<LookupCacheV4>(lookupCache);
|
||||
}
|
||||
}
|
||||
if (!lookupCacheV4) {
|
||||
continue;
|
||||
}
|
||||
|
||||
nsCString state;
|
||||
nsCString checksum;
|
||||
rv = lookupCache->LoadMetadata(state, checksum);
|
||||
rv = lookupCacheV4->LoadMetadata(state, checksum);
|
||||
if (NS_FAILED(rv)) {
|
||||
LOG(("Failed to get metadata for table %s", tableName.get()));
|
||||
continue;
|
||||
|
||||
@@ -116,8 +116,8 @@ public:
|
||||
// update intermediaries.
|
||||
nsresult SwapInNewTablesAndCleanup();
|
||||
|
||||
LookupCache *GetLookupCache(const nsACString& aTable,
|
||||
bool aForUpdate = false);
|
||||
RefPtr<LookupCache> GetLookupCache(const nsACString& aTable,
|
||||
bool aForUpdate = false);
|
||||
|
||||
void GetCacheInfo(const nsACString& aTable,
|
||||
nsIUrlClassifierCacheInfo** aCache);
|
||||
@@ -156,13 +156,13 @@ private:
|
||||
|
||||
nsresult UpdateCache(RefPtr<const TableUpdate> aUpdates);
|
||||
|
||||
LookupCache *GetLookupCacheForUpdate(const nsACString& aTable) {
|
||||
RefPtr<LookupCache> GetLookupCacheForUpdate(const nsACString& aTable) {
|
||||
return GetLookupCache(aTable, true);
|
||||
}
|
||||
|
||||
LookupCache *GetLookupCacheFrom(const nsACString& aTable,
|
||||
nsTArray<LookupCache*>& aLookupCaches,
|
||||
nsIFile* aRootStoreDirectory);
|
||||
RefPtr<LookupCache> GetLookupCacheFrom(const nsACString& aTable,
|
||||
LookupCacheArray& aLookupCaches,
|
||||
nsIFile* aRootStoreDirectory);
|
||||
|
||||
|
||||
bool CheckValidUpdate(TableUpdateArray& aUpdates,
|
||||
@@ -202,7 +202,7 @@ private:
|
||||
nsCOMPtr<nsIFile> mBackupDirectory;
|
||||
nsCOMPtr<nsIFile> mUpdatingDirectory; // For update only.
|
||||
nsCOMPtr<nsIFile> mToDeleteDirectory;
|
||||
nsTArray<LookupCache*> mLookupCaches; // For query only.
|
||||
LookupCacheArray mLookupCaches; // For query only.
|
||||
nsTArray<nsCString> mActiveTablesCache;
|
||||
uint32_t mHashKey;
|
||||
|
||||
@@ -215,7 +215,7 @@ private:
|
||||
bool mIsTableRequestResultOutdated;
|
||||
|
||||
// The copy of mLookupCaches for update only.
|
||||
nsTArray<LookupCache*> mNewLookupCaches;
|
||||
LookupCacheArray mNewLookupCaches;
|
||||
|
||||
bool mUpdateInterrupted;
|
||||
|
||||
|
||||
@@ -184,7 +184,8 @@ public:
|
||||
LookupCache(const nsACString& aTableName,
|
||||
const nsACString& aProvider,
|
||||
nsIFile* aStoreFile);
|
||||
virtual ~LookupCache() {}
|
||||
|
||||
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(LookupCache);
|
||||
|
||||
const nsCString &TableName() const { return mTableName; }
|
||||
|
||||
@@ -243,6 +244,8 @@ private:
|
||||
virtual int Ver() const = 0;
|
||||
|
||||
protected:
|
||||
virtual ~LookupCache() {}
|
||||
|
||||
// Check completions in positive cache and prefix in negative cache.
|
||||
// 'aHas' and 'aConfirmed' are output parameters.
|
||||
nsresult CheckCache(const Completion& aCompletion,
|
||||
@@ -262,6 +265,8 @@ protected:
|
||||
FullHashResponseMap mFullHashCache;
|
||||
};
|
||||
|
||||
typedef nsTArray<RefPtr<LookupCache>> LookupCacheArray;
|
||||
|
||||
class LookupCacheV2 final : public LookupCache
|
||||
{
|
||||
public:
|
||||
@@ -269,7 +274,6 @@ public:
|
||||
const nsACString& aProvider,
|
||||
nsIFile* aStoreFile)
|
||||
: LookupCache(aTableName, aProvider, aStoreFile) {}
|
||||
~LookupCacheV2() {}
|
||||
|
||||
virtual nsresult Init() override;
|
||||
virtual nsresult Open() override;
|
||||
@@ -307,6 +311,8 @@ protected:
|
||||
virtual size_t SizeOfPrefixSet() const override;
|
||||
|
||||
private:
|
||||
~LookupCacheV2() {}
|
||||
|
||||
virtual int Ver() const override { return VER; }
|
||||
|
||||
// Construct a Prefix Set with known prefixes.
|
||||
|
||||
@@ -21,7 +21,6 @@ public:
|
||||
const nsACString& aProvider,
|
||||
nsIFile* aStoreFile)
|
||||
: LookupCache(aTableName, aProvider, aStoreFile) {}
|
||||
~LookupCacheV4() {}
|
||||
|
||||
virtual nsresult Init() override;
|
||||
virtual nsresult Has(const Completion& aCompletion,
|
||||
@@ -56,6 +55,8 @@ protected:
|
||||
virtual size_t SizeOfPrefixSet() const override;
|
||||
|
||||
private:
|
||||
~LookupCacheV4() {}
|
||||
|
||||
virtual int Ver() const override { return VER; }
|
||||
|
||||
nsresult VerifyChecksum(const nsACString& aChecksum);
|
||||
|
||||
@@ -182,7 +182,7 @@ BuildCache(LookupCacheV4* cache, const _PrefixArray& prefixArray)
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
UniquePtr<T>
|
||||
RefPtr<T>
|
||||
SetupLookupCache(const _PrefixArray& prefixArray)
|
||||
{
|
||||
nsCOMPtr<nsIFile> file;
|
||||
@@ -190,12 +190,12 @@ SetupLookupCache(const _PrefixArray& prefixArray)
|
||||
|
||||
file->AppendNative(GTEST_SAFEBROWSING_DIR);
|
||||
|
||||
UniquePtr<T> cache = MakeUnique<T>(GTEST_TABLE, EmptyCString(), file);
|
||||
RefPtr<T> cache = new T(GTEST_TABLE, EmptyCString(), file);
|
||||
nsresult rv = cache->Init();
|
||||
EXPECT_EQ(rv, NS_OK);
|
||||
|
||||
rv = BuildCache(cache.get(), prefixArray);
|
||||
rv = BuildCache(cache, prefixArray);
|
||||
EXPECT_EQ(rv, NS_OK);
|
||||
|
||||
return std::move(cache);
|
||||
return cache;
|
||||
}
|
||||
|
||||
@@ -46,4 +46,4 @@ nsCString GeneratePrefix(const nsCString& aFragment, uint8_t aLength);
|
||||
|
||||
// Create a LookupCacheV4 object with sepecified prefix array.
|
||||
template<typename T>
|
||||
UniquePtr<T> SetupLookupCache(const _PrefixArray& prefixArray);
|
||||
RefPtr<T> SetupLookupCache(const _PrefixArray& prefixArray);
|
||||
|
||||
@@ -75,11 +75,11 @@ TestCache(const Completion aCompletion,
|
||||
GeneratePrefix(_Fragment("small.com/"), 4)
|
||||
};
|
||||
|
||||
UniquePtr<T> cache = SetupLookupCache<T>(array);
|
||||
RefPtr<T> cache = SetupLookupCache<T>(array);
|
||||
|
||||
// Create an expired entry and a non-expired entry
|
||||
SetupCacheEntry(cache.get(), _Fragment("cache.notexpired.com/"));
|
||||
SetupCacheEntry(cache.get(), _Fragment("cache.expired.com/"), true, true);
|
||||
SetupCacheEntry(cache, _Fragment("cache.notexpired.com/"));
|
||||
SetupCacheEntry(cache, _Fragment("cache.expired.com/"), true, true);
|
||||
|
||||
cache->Has(aCompletion, &has, &matchLength, &confirmed);
|
||||
inCache = cache->IsInCache(aCompletion.ToUint32());
|
||||
@@ -198,12 +198,12 @@ void TestInvalidateExpiredCacheEntry()
|
||||
GeneratePrefix(POS_CACHE_EXPIRED_URL, 5),
|
||||
GeneratePrefix(BOTH_CACHE_EXPIRED_URL, 4)
|
||||
};
|
||||
UniquePtr<T> cache = SetupLookupCache<T>(array);
|
||||
RefPtr<T> cache = SetupLookupCache<T>(array);
|
||||
|
||||
SetupCacheEntry(cache.get(), CACHED_URL, false, false);
|
||||
SetupCacheEntry(cache.get(), NEG_CACHE_EXPIRED_URL, true, false);
|
||||
SetupCacheEntry(cache.get(), POS_CACHE_EXPIRED_URL, false, true);
|
||||
SetupCacheEntry(cache.get(), BOTH_CACHE_EXPIRED_URL, true, true);
|
||||
SetupCacheEntry(cache, CACHED_URL, false, false);
|
||||
SetupCacheEntry(cache, NEG_CACHE_EXPIRED_URL, true, false);
|
||||
SetupCacheEntry(cache, POS_CACHE_EXPIRED_URL, false, true);
|
||||
SetupCacheEntry(cache, BOTH_CACHE_EXPIRED_URL, true, true);
|
||||
|
||||
// Before invalidate
|
||||
TestCache<T>(CACHED_URL, true, true, true, cache.get());
|
||||
@@ -241,7 +241,7 @@ TEST(UrlClassifierCaching, InvalidateExpiredCacheEntryV4)
|
||||
TEST(UrlClassifierCaching, NegativeCacheExpireV2)
|
||||
{
|
||||
_PrefixArray array = { GeneratePrefix(NEG_CACHE_EXPIRED_URL, 8) };
|
||||
UniquePtr<LookupCacheV2> cache = SetupLookupCache<LookupCacheV2>(array);
|
||||
RefPtr<LookupCacheV2> cache = SetupLookupCache<LookupCacheV2>(array);
|
||||
|
||||
nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
|
||||
|
||||
@@ -262,7 +262,7 @@ TEST(UrlClassifierCaching, NegativeCacheExpireV2)
|
||||
TEST(UrlClassifierCaching, NegativeCacheExpireV4)
|
||||
{
|
||||
_PrefixArray array = { GeneratePrefix(NEG_CACHE_EXPIRED_URL, 8) };
|
||||
UniquePtr<LookupCacheV4> cache = SetupLookupCache<LookupCacheV4>(array);
|
||||
RefPtr<LookupCacheV4> cache = SetupLookupCache<LookupCacheV4>(array);
|
||||
|
||||
FullHashResponseMap map;
|
||||
Prefix prefix;
|
||||
|
||||
@@ -30,16 +30,19 @@ SetupLookupCacheV4(Classifier* classifier,
|
||||
const _PrefixArray& aPrefixArray,
|
||||
const nsACString& aTable)
|
||||
{
|
||||
LookupCacheV4* lookupCache =
|
||||
LookupCache::Cast<LookupCacheV4>(classifier->GetLookupCache(aTable, false));
|
||||
RefPtr<LookupCache> lookupCache = classifier->GetLookupCache(aTable, false);
|
||||
if (!lookupCache) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
RefPtr<LookupCacheV4> lookupCacheV4 = LookupCache::Cast<LookupCacheV4>(lookupCache);
|
||||
if (!lookupCacheV4) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
PrefixStringMap map;
|
||||
PrefixArrayToPrefixStringMap(aPrefixArray, map);
|
||||
|
||||
return lookupCache->Build(map);
|
||||
return lookupCacheV4->Build(map);
|
||||
}
|
||||
|
||||
static nsresult
|
||||
@@ -47,11 +50,15 @@ SetupLookupCacheV2(Classifier* classifier,
|
||||
const _PrefixArray& aPrefixArray,
|
||||
const nsACString& aTable)
|
||||
{
|
||||
LookupCacheV2* lookupCache =
|
||||
LookupCache::Cast<LookupCacheV2>(classifier->GetLookupCache(aTable, false));
|
||||
RefPtr<LookupCache> lookupCache = classifier->GetLookupCache(aTable, false);
|
||||
if (!lookupCache) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
RefPtr<LookupCacheV2> lookupCacheV2 =
|
||||
LookupCache::Cast<LookupCacheV2>(lookupCache);
|
||||
if (!lookupCacheV2) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
AddPrefixArray prefixes;
|
||||
AddCompleteArray completions;
|
||||
@@ -61,7 +68,7 @@ SetupLookupCacheV2(Classifier* classifier,
|
||||
}
|
||||
|
||||
EntrySort(prefixes);
|
||||
return lookupCache->Build(prefixes, completions);
|
||||
return lookupCacheV2->Build(prefixes, completions);
|
||||
}
|
||||
|
||||
static void
|
||||
|
||||
@@ -14,7 +14,7 @@ TestHasPrefix(const _Fragment& aFragment, bool aExpectedHas, bool aExpectedCompl
|
||||
};
|
||||
|
||||
RunTestInNewThread([&] () -> void {
|
||||
UniquePtr<LookupCache> cache = SetupLookupCache<LookupCacheV4>(array);
|
||||
RefPtr<LookupCache> cache = SetupLookupCache<LookupCacheV4>(array);
|
||||
|
||||
Completion lookupHash;
|
||||
lookupHash.FromPlaintext(aFragment);
|
||||
|
||||
@@ -23,8 +23,9 @@ using namespace mozilla;
|
||||
using namespace mozilla::safebrowsing;
|
||||
|
||||
template<typename T>
|
||||
void VerifyPrivateStorePath(const char* aTableName,
|
||||
const char* aProvider,
|
||||
void VerifyPrivateStorePath(T* target,
|
||||
const nsCString& aTableName,
|
||||
const nsCString& aProvider,
|
||||
nsIFile* aRootDir,
|
||||
bool aUsePerProviderStore)
|
||||
{
|
||||
@@ -32,10 +33,8 @@ void VerifyPrivateStorePath(const char* aTableName,
|
||||
nsresult rv = aRootDir->GetPath(rootStorePath);
|
||||
EXPECT_EQ(rv, NS_OK);
|
||||
|
||||
T target(nsCString(aTableName), nsCString(aProvider), aRootDir);
|
||||
|
||||
nsIFile* privateStoreDirectory =
|
||||
PerProviderDirectoryTestUtils::InspectStoreDirectory(target);
|
||||
PerProviderDirectoryTestUtils::InspectStoreDirectory(*target);
|
||||
|
||||
nsString privateStorePath;
|
||||
rv = privateStoreDirectory->GetPath(privateStorePath);
|
||||
@@ -49,14 +48,14 @@ void VerifyPrivateStorePath(const char* aTableName,
|
||||
rv = aRootDir->Clone(getter_AddRefs(expectedPrivateStoreDir));
|
||||
ASSERT_EQ(rv, NS_OK);
|
||||
|
||||
expectedPrivateStoreDir->AppendNative(nsCString(aProvider));
|
||||
expectedPrivateStoreDir->AppendNative(aProvider);
|
||||
rv = expectedPrivateStoreDir->GetPath(expectedPrivateStorePath);
|
||||
ASSERT_EQ(rv, NS_OK);
|
||||
}
|
||||
|
||||
printf("table: %s\nprovider: %s\nroot path: %s\nprivate path: %s\n\n",
|
||||
aTableName,
|
||||
aProvider,
|
||||
aTableName.get(),
|
||||
aProvider.get(),
|
||||
NS_ConvertUTF16toUTF8(rootStorePath).get(),
|
||||
NS_ConvertUTF16toUTF8(privateStorePath).get());
|
||||
|
||||
@@ -71,12 +70,27 @@ TEST(UrlClassifierPerProviderDirectory, LookupCache)
|
||||
|
||||
// For V2 tables (NOT ending with '-proto'), root directory should be
|
||||
// used as the private store.
|
||||
VerifyPrivateStorePath<LookupCacheV2>("goog-phish-shavar", "google", rootDir, false);
|
||||
{
|
||||
nsAutoCString table("goog-phish-shavar");
|
||||
nsAutoCString provider("google");
|
||||
RefPtr<LookupCacheV2> lc = new LookupCacheV2(table, provider, rootDir);
|
||||
VerifyPrivateStorePath<LookupCacheV2>(lc, table, provider, rootDir, false);
|
||||
}
|
||||
|
||||
// For V4 tables, if provider is found, use per-provider subdirectory;
|
||||
// If not found, use root directory.
|
||||
VerifyPrivateStorePath<LookupCacheV4>("goog-noprovider-proto", "", rootDir, false);
|
||||
VerifyPrivateStorePath<LookupCacheV4>("goog-phish-proto", "google4", rootDir, true);
|
||||
{
|
||||
nsAutoCString table("goog-noprovider-proto");
|
||||
nsAutoCString provider("");
|
||||
RefPtr<LookupCacheV4> lc = new LookupCacheV4(table, provider, rootDir);
|
||||
VerifyPrivateStorePath<LookupCacheV4>(lc, table, provider, rootDir, false);
|
||||
}
|
||||
{
|
||||
nsAutoCString table("goog-phish-proto");
|
||||
nsAutoCString provider("google4");
|
||||
RefPtr<LookupCacheV4> lc = new LookupCacheV4(table, provider, rootDir);
|
||||
VerifyPrivateStorePath<LookupCacheV4>(lc, table, provider, rootDir, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -88,11 +102,25 @@ TEST(UrlClassifierPerProviderDirectory, HashStore)
|
||||
|
||||
// For V2 tables (NOT ending with '-proto'), root directory should be
|
||||
// used as the private store.
|
||||
VerifyPrivateStorePath<HashStore>("goog-phish-shavar", "google", rootDir, false);
|
||||
|
||||
{
|
||||
nsAutoCString table("goog-phish-shavar");
|
||||
nsAutoCString provider("google");
|
||||
HashStore hs(table, provider, rootDir);
|
||||
VerifyPrivateStorePath(&hs, table, provider, rootDir, false);
|
||||
}
|
||||
// For V4 tables, if provider is found, use per-provider subdirectory;
|
||||
// If not found, use root directory.
|
||||
VerifyPrivateStorePath<HashStore>("goog-noprovider-proto", "", rootDir, false);
|
||||
VerifyPrivateStorePath<HashStore>("goog-phish-proto", "google4", rootDir, true);
|
||||
{
|
||||
nsAutoCString table("goog-noprovider-proto");
|
||||
nsAutoCString provider("");
|
||||
HashStore hs(table, provider, rootDir);
|
||||
VerifyPrivateStorePath(&hs, table, provider, rootDir, false);
|
||||
}
|
||||
{
|
||||
nsAutoCString table("goog-phish-proto");
|
||||
nsAutoCString provider("google4");
|
||||
HashStore hs(table, provider, rootDir);
|
||||
VerifyPrivateStorePath(&hs, table, provider, rootDir, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -255,11 +255,12 @@ testOpenLookupCache()
|
||||
file->AppendNative(GTEST_SAFEBROWSING_DIR);
|
||||
|
||||
RunTestInNewThread([&] () -> void {
|
||||
LookupCacheV4 cache(nsCString(GTEST_TABLE), EmptyCString(), file);
|
||||
nsresult rv = cache.Init();
|
||||
RefPtr<LookupCacheV4> cache = new LookupCacheV4(nsCString(GTEST_TABLE),
|
||||
EmptyCString(), file);
|
||||
nsresult rv = cache->Init();
|
||||
ASSERT_EQ(rv, NS_OK);
|
||||
|
||||
rv = cache.Open();
|
||||
rv = cache->Open();
|
||||
ASSERT_EQ(rv, NS_OK);
|
||||
});
|
||||
}
|
||||
@@ -798,7 +799,7 @@ TEST(UrlClassifierTableUpdateV4, EmptyUpdate2)
|
||||
_PrefixArray array;
|
||||
CreateRandomSortedPrefixArray(100, 4, 4, array);
|
||||
CreateRandomSortedPrefixArray(10, 5, 32, array);
|
||||
UniquePtr<LookupCacheV4> cache = SetupLookupCache<LookupCacheV4>(array);
|
||||
RefPtr<LookupCacheV4> cache = SetupLookupCache<LookupCacheV4>(array);
|
||||
|
||||
// Setup TableUpdate object with only checksum from previous update(initial data).
|
||||
nsCString checksum;
|
||||
|
||||
Reference in New Issue
Block a user