Bug 1311933 - P1. Use integer as the key of safebrowsing cache. r=francois

In Bug 1323953, we always send 4-bytes prefix for completion and the prefix is also
used as the key to store cache result from gethash request.
Since it is always 4-bytes, we could convert it to integer for simplicity.

MozReview-Commit-ID: Lkvrg0wvX5Z

--HG--
extra : rebase_source : 7002b1f55bc0f88ed90340082574cc931f8db87a
This commit is contained in:
dimi 2017-04-11 16:07:26 +08:00
Родитель 5c1b11f1db
Коммит ec8a9a8f96
7 изменённых файлов: 24 добавлений и 38 удалений

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

@ -318,7 +318,6 @@ typedef nsClassHashtable<nsUint32HashKey, nsCString> PrefixStringMap;
typedef nsDataHashtable<nsCStringHashKey, int64_t> TableFreshnessMap;
typedef nsCStringHashKey VLHashPrefixString;
typedef nsCStringHashKey FullHashString;
typedef nsDataHashtable<FullHashString, int64_t> FullHashExpiryCache;
@ -354,7 +353,7 @@ struct CachedFullHashResponse {
}
};
typedef nsClassHashtable<VLHashPrefixString, CachedFullHashResponse> FullHashResponseMap;
typedef nsClassHashtable<nsUint32HashKey, CachedFullHashResponse> FullHashResponseMap;
} // namespace safebrowsing
} // namespace mozilla

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

@ -194,11 +194,11 @@ TableUpdateV4::NewChecksum(const std::string& aChecksum)
}
nsresult
TableUpdateV4::NewFullHashResponse(const nsACString& aPrefix,
TableUpdateV4::NewFullHashResponse(const Prefix& aPrefix,
CachedFullHashResponse& aResponse)
{
CachedFullHashResponse* response =
mFullHashResponseMap.LookupOrAdd(aPrefix);
mFullHashResponseMap.LookupOrAdd(aPrefix.ToUint32());
if (!response) {
return NS_ERROR_OUT_OF_MEMORY;
}

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

@ -179,7 +179,7 @@ public:
void NewRemovalIndices(const uint32_t* aIndices, size_t aNumOfIndices);
void SetNewClientState(const nsACString& aState) { mClientState = aState; }
void NewChecksum(const std::string& aChecksum);
nsresult NewFullHashResponse(const nsACString& aPrefix,
nsresult NewFullHashResponse(const Prefix& aPrefix,
CachedFullHashResponse& aResponse);
private:

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

@ -119,6 +119,7 @@ public:
}
nsCString table;
Prefix prefix;
};
class CacheResultV2 final : public CacheResult
@ -131,6 +132,7 @@ public:
bool operator==(const CacheResultV2& aOther) const {
return table == aOther.table &&
prefix == aOther.prefix &&
completion == aOther.completion &&
addChunk == aOther.addChunk;
}
@ -147,11 +149,11 @@ class CacheResultV4 final : public CacheResult
public:
static const int VER;
nsCString prefix;
CachedFullHashResponse response;
bool operator==(const CacheResultV4& aOther) const {
return prefix == aOther.prefix &&
return table == aOther.table &&
prefix == aOther.prefix &&
response == aOther.response;
}

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

@ -112,9 +112,8 @@ LookupCacheV4::Has(const Completion& aCompletion,
}
// We always send 4-bytes for completion(Bug 1323953) so the prefix used to
// lookup for cache should be 4-bytes too.
nsDependentCSubstring prefix(reinterpret_cast<const char*>(aCompletion.buf),
PREFIX_SIZE);
// lookup for cache should be 4-bytes(uint32_t) too.
uint32_t prefix = aCompletion.ToUint32();
// Check if prefix can be found in cache.
CachedFullHashResponse* fullHashResponse = mCache.Get(prefix);
@ -663,12 +662,9 @@ LookupCacheV4::DumpCache()
}
for (auto iter = mCache.ConstIter(); !iter.Done(); iter.Next()) {
nsAutoCString strPrefix;
CStringToHexString(iter.Key(), strPrefix);
CachedFullHashResponse* response = iter.Data();
LOG(("Caches prefix: %s, Expire time: %s",
strPrefix.get(),
LOG(("Caches prefix: %X, Expire time: %s",
iter.Key(),
GetFormattedTimeString(response->negativeCacheExpirySec).get()));
FullHashExpiryCache& fullHashes = response->fullHashes;

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

@ -1196,6 +1196,7 @@ nsUrlClassifierLookupCallback::CompletionV2(const nsACString& aCompleteHash,
auto result = new CacheResultV2;
result->table = aTableName;
result->prefix.Assign(aCompleteHash);
result->completion.Assign(aCompleteHash);
result->addChunk = aChunkId;
@ -1228,7 +1229,7 @@ nsUrlClassifierLookupCallback::CompletionV4(const nsACString& aPartialHash,
int64_t nowSec = PR_Now() / PR_USEC_PER_SEC;
result->table = aTableName;
result->prefix = aPartialHash;
result->prefix.Assign(aPartialHash);
result->response.negativeCacheExpirySec = nowSec + aNegativeCacheDuration;
// Fill in positive cache entries.

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

@ -14,8 +14,12 @@ SetupCacheEntry(LookupCacheV4* aLookupCache,
bool aPosExpired = false)
{
FullHashResponseMap map;
CachedFullHashResponse* response = map.LookupOrAdd(
GeneratePrefix(aCompletion, PREFIX_SIZE));
Prefix prefix;
nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
prefix.FromPlaintext(aCompletion, cryptoHash);
CachedFullHashResponse* response = map.LookupOrAdd(prefix.ToUint32());
response->negativeCacheExpirySec = aNegExpired ? EXPIRED_TIME_SEC : NOTEXPIRED_TIME_SEC;
response->fullHashes.Put(GeneratePrefix(aCompletion, COMPLETE_SIZE),
@ -198,8 +202,10 @@ TEST(CachingV4, NegativeCacheExpire)
UniquePtr<LookupCacheV4> cache = SetupLookupCacheV4(array);
FullHashResponseMap map;
CachedFullHashResponse* response = map.LookupOrAdd(
GeneratePrefix(NEG_CACHE_EXPIRED_URL, PREFIX_SIZE));
Prefix prefix;
nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
prefix.FromPlaintext(NEG_CACHE_EXPIRED_URL, cryptoHash);
CachedFullHashResponse* response = map.LookupOrAdd(prefix.ToUint32());
response->negativeCacheExpirySec = EXPIRED_TIME_SEC;
@ -212,21 +218,3 @@ TEST(CachingV4, NegativeCacheExpire)
// The second time it should not be found in the cache again
TestCache(NEG_CACHE_EXPIRED_URL, true, false, false, cache.get());
}
// This testcase check we only lookup cache with 4-bytes prefix
TEST(CachingV4, Ensure4BytesLookup)
{
_PrefixArray array = { GeneratePrefix(CACHED_URL, 8) };
UniquePtr<LookupCacheV4> cache = SetupLookupCacheV4(array);
FullHashResponseMap map;
CachedFullHashResponse* response = map.LookupOrAdd(
GeneratePrefix(CACHED_URL, 5));
response->negativeCacheExpirySec = NOTEXPIRED_TIME_SEC;
response->fullHashes.Put(GeneratePrefix(CACHED_URL, COMPLETE_SIZE),
NOTEXPIRED_TIME_SEC);
cache->AddFullHashResponseToCache(map);
TestCache(CACHED_URL, true, false, false, cache.get());
}