Bug 1434206 - Add const to members and functions that can take it. r?gcp
MozReview-Commit-ID: B2aaQTttPAV
--- a/toolkit/components/url-classifier/Classifier.cpp
+++ b/toolkit/components/url-classifier/Classifier.cpp
@@ -993,17 +993,17 @@ Classifier::ScanStoreDir(nsIFile* aDirec
}
}
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
nsresult
-Classifier::ActiveTables(nsTArray<nsCString>& aTables)
+Classifier::ActiveTables(nsTArray<nsCString>& aTables) const
{
aTables = mActiveTablesCache;
return NS_OK;
}
nsresult
Classifier::CleanToDelete()
{
--- a/toolkit/components/url-classifier/Classifier.h
+++ b/toolkit/components/url-classifier/Classifier.h
@@ -47,17 +47,17 @@ public:
* Get the list of active tables and their chunks in a format
* suitable for an update request.
*/
void TableRequest(nsACString& aResult);
/*
* Get all tables that we know about.
*/
- nsresult ActiveTables(nsTArray<nsCString>& aTables);
+ nsresult ActiveTables(nsTArray<nsCString>& aTables) const;
/**
* Check a URL against the specified tables.
*/
nsresult Check(const nsACString& aSpec,
const nsACString& tables,
LookupResultArray& aResults);
@@ -167,17 +167,17 @@ private:
nsIFile* aRootStoreDirectory);
bool CheckValidUpdate(nsTArray<TableUpdate*>* aUpdates,
const nsACString& aTable);
nsresult LoadMetadata(nsIFile* aDirectory, nsACString& aResult);
- nsCString GetProvider(const nsACString& aTableName);
+ static nsCString GetProvider(const nsACString& aTableName);
/**
* The "background" part of ApplyUpdates. Once the background update
* is called, the foreground update has to be called along with the
* background result no matter whether the background update is
* successful or not.
*/
nsresult ApplyUpdatesBackground(nsTArray<TableUpdate*>* aUpdates,
--- a/toolkit/components/url-classifier/HashStore.cpp
+++ b/toolkit/components/url-classifier/HashStore.cpp
@@ -215,17 +215,17 @@ TableUpdateV4::NewRemovalIndices(const u
void
TableUpdateV4::NewChecksum(const std::string& aChecksum)
{
mChecksum.Assign(aChecksum.data(), aChecksum.size());
}
nsresult
TableUpdateV4::NewFullHashResponse(const Prefix& aPrefix,
- CachedFullHashResponse& aResponse)
+ const CachedFullHashResponse& aResponse)
{
CachedFullHashResponse* response =
mFullHashResponseMap.LookupOrAdd(aPrefix.ToUint32());
if (!response) {
return NS_ERROR_OUT_OF_MEMORY;
}
*response = aResponse;
return NS_OK;
@@ -369,17 +369,17 @@ HashStore::ReadHeader()
&buffer,
sizeof(Header));
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
nsresult
-HashStore::SanityCheck()
+HashStore::SanityCheck() const
{
if (mHeader.magic != STORE_MAGIC || mHeader.version != CURRENT_VERSION) {
NS_WARNING("Unexpected header data in the store.");
return NS_ERROR_FAILURE;
}
return NS_OK;
}
@@ -1240,29 +1240,29 @@ SubCompleteArray&
HashStore::SubCompletes()
{
ReadCompletions();
return mSubCompletes;
}
bool
-HashStore::AlreadyReadChunkNumbers()
+HashStore::AlreadyReadChunkNumbers() const
{
// If there are chunks but chunk set not yet contains any data
// Then we haven't read chunk numbers.
if ((mHeader.numAddChunks != 0 && mAddChunks.Length() == 0) ||
(mHeader.numSubChunks != 0 && mSubChunks.Length() == 0)) {
return false;
}
return true;
}
bool
-HashStore::AlreadyReadCompletions()
+HashStore::AlreadyReadCompletions() const
{
// If there are completions but completion set not yet contains any data
// Then we haven't read completions.
if ((mHeader.numAddCompletes != 0 && mAddCompletes.Length() == 0) ||
(mHeader.numSubCompletes != 0 && mSubCompletes.Length() == 0)) {
return false;
}
return true;
--- a/toolkit/components/url-classifier/HashStore.h
+++ b/toolkit/components/url-classifier/HashStore.h
@@ -41,17 +41,17 @@ public:
template<typename T>
static T* Cast(TableUpdate* aThat) {
return (T::TAG == aThat->Tag() ? reinterpret_cast<T*>(aThat) : nullptr);
}
private:
virtual int Tag() const = 0;
- nsCString mTable;
+ const nsCString mTable;
};
// A table update is built from a single update chunk from the server. As the
// protocol parser processes each chunk, it constructs a table update with the
// new hashes.
class TableUpdateV2 : public TableUpdate {
public:
explicit TableUpdateV2(const nsACString& aTable)
@@ -168,17 +168,17 @@ public:
void SetFullUpdate(bool aIsFullUpdate) { mFullUpdate = aIsFullUpdate; }
void NewPrefixes(int32_t aSize, const nsACString& aPrefixes);
void SetNewClientState(const nsACString& aState) { mClientState = aState; }
void NewChecksum(const std::string& aChecksum);
nsresult NewRemovalIndices(const uint32_t* aIndices, size_t aNumOfIndices);
nsresult NewFullHashResponse(const Prefix& aPrefix,
- CachedFullHashResponse& aResponse);
+ const CachedFullHashResponse& aResponse);
private:
virtual int Tag() const override { return TAG; }
bool mFullUpdate;
PrefixStringMap mPrefixesMap;
RemovalIndiceArray mRemovalIndiceArray;
nsCString mClientState;
@@ -235,17 +235,17 @@ public:
// Wipe out all Completes.
void ClearCompletes();
private:
nsresult Reset();
nsresult ReadHeader();
- nsresult SanityCheck();
+ nsresult SanityCheck() const;
nsresult CalculateChecksum(nsAutoCString& aChecksum, uint32_t aFileSize,
bool aChecksumPresent);
nsresult CheckChecksum(uint32_t aFileSize);
void UpdateHeader();
nsresult ReadCompletions();
nsresult ReadChunkNumbers();
nsresult ReadHashes();
@@ -255,18 +255,18 @@ private:
nsresult WriteAddPrefixes(nsIOutputStream* aOut);
nsresult WriteSubPrefixes(nsIOutputStream* aOut);
nsresult ProcessSubs();
nsresult PrepareForUpdate();
- bool AlreadyReadChunkNumbers();
- bool AlreadyReadCompletions();
+ bool AlreadyReadChunkNumbers() const;
+ bool AlreadyReadCompletions() const;
// This is used for checking that the database is correct and for figuring out
// the number of chunks, etc. to read from disk on restart.
struct Header {
uint32_t magic;
uint32_t version;
uint32_t numAddChunks;
uint32_t numSubChunks;
@@ -275,17 +275,17 @@ private:
uint32_t numAddCompletes;
uint32_t numSubCompletes;
};
Header mHeader;
// The name of the table (must end in -shavar or -digest256, or evidently
// -simple for unittesting.
- nsCString mTableName;
+ const nsCString mTableName;
nsCOMPtr<nsIFile> mStoreDirectory;
bool mInUpdate;
nsCOMPtr<nsIInputStream> mInputStream;
// Chunk numbers, stored as uint32_t arrays.
ChunkSet mAddChunks;
--- a/toolkit/components/url-classifier/LookupCache.cpp
+++ b/toolkit/components/url-classifier/LookupCache.cpp
@@ -595,17 +595,17 @@ LookupCacheV2::Has(const Completion& aCo
LOG(("Probe in %s: %X, has %d, confirmed %d",
mTableName.get(), prefix, *aHas, *aConfirmed));
return rv;
}
bool
-LookupCacheV2::IsEmpty()
+LookupCacheV2::IsEmpty() const
{
bool isEmpty;
mPrefixSet->IsEmpty(&isEmpty);
return isEmpty;
}
nsresult
LookupCacheV2::Build(AddPrefixArray& aAddPrefixes,
@@ -711,17 +711,17 @@ LookupCacheV2::StoreToFile(nsIFile* aFil
nsresult
LookupCacheV2::LoadFromFile(nsIFile* aFile)
{
return mPrefixSet->LoadFromFile(aFile);
}
size_t
-LookupCacheV2::SizeOfPrefixSet()
+LookupCacheV2::SizeOfPrefixSet() const
{
return mPrefixSet->SizeOfIncludingThis(moz_malloc_size_of);
}
#ifdef DEBUG
template <class T>
static void EnsureSorted(T* aArray)
{
--- a/toolkit/components/url-classifier/LookupCache.h
+++ b/toolkit/components/url-classifier/LookupCache.h
@@ -32,33 +32,33 @@ public:
mProtocolV2(true) {}
// The fragment that matched in the LookupCache
union {
Prefix fixedLengthPrefix;
Completion complete;
} hash;
- const Completion &CompleteHash() {
+ const Completion &CompleteHash() const {
MOZ_ASSERT(!mNoise);
return hash.complete;
}
- nsCString PartialHash() {
+ nsCString PartialHash() const {
MOZ_ASSERT(mPartialHashLength <= COMPLETE_SIZE);
if (mNoise) {
- return nsCString(reinterpret_cast<char*>(hash.fixedLengthPrefix.buf),
+ return nsCString(reinterpret_cast<const char*>(hash.fixedLengthPrefix.buf),
PREFIX_SIZE);
} else {
- return nsCString(reinterpret_cast<char*>(hash.complete.buf),
+ return nsCString(reinterpret_cast<const char*>(hash.complete.buf),
mPartialHashLength);
}
}
- nsCString PartialHashHex() {
+ nsCString PartialHashHex() const {
nsAutoCString hex;
for (size_t i = 0; i < mPartialHashLength; i++) {
hex.AppendPrintf("%.2X", hash.complete.buf[i]);
}
return hex;
}
bool Confirmed() const { return mConfirmed || mProtocolConfirmed; }
@@ -203,60 +203,60 @@ public:
// Copy fullhash cache from another LookupCache.
void CopyFullHashCache(const LookupCache* aSource);
// Clear fullhash cache from fullhash/gethash response.
void ClearCache();
// Check if completions can be found in cache.
// Currently this is only used by testcase.
- bool IsInCache(uint32_t key) { return mFullHashCache.Get(key); };
+ bool IsInCache(uint32_t key) const { return mFullHashCache.Get(key); };
#if DEBUG
void DumpCache();
#endif
void GetCacheInfo(nsIUrlClassifierCacheInfo** aCache);
virtual nsresult Open();
virtual nsresult Init() = 0;
virtual nsresult ClearPrefixes() = 0;
virtual nsresult Has(const Completion& aCompletion,
bool* aHas,
uint32_t* aMatchLength,
bool* aConfirmed) = 0;
- virtual bool IsEmpty() = 0;
+ virtual bool IsEmpty() const = 0;
virtual void ClearAll();
template<typename T>
static T* Cast(LookupCache* aThat) {
return ((aThat && T::VER == aThat->Ver()) ? reinterpret_cast<T*>(aThat) : nullptr);
}
private:
nsresult LoadPrefixSet();
virtual nsresult StoreToFile(nsIFile* aFile) = 0;
virtual nsresult LoadFromFile(nsIFile* aFile) = 0;
- virtual size_t SizeOfPrefixSet() = 0;
+ virtual size_t SizeOfPrefixSet() const = 0;
virtual int Ver() const = 0;
protected:
// Check completions in positive cache and prefix in negative cache.
// 'aHas' and 'aConfirmed' are output parameters.
nsresult CheckCache(const Completion& aCompletion,
bool* aHas,
bool* aConfirmed);
bool mPrimed; // true when the PrefixSet has been loaded (or constructed)
- nsCString mTableName;
- nsCString mProvider;
+ const nsCString mTableName;
+ const nsCString mProvider;
nsCOMPtr<nsIFile> mRootStoreDirectory;
nsCOMPtr<nsIFile> mStoreDirectory;
// For gtest to inspect private members.
friend class PerProviderDirectoryTestUtils;
// Cache stores fullhash response(V4)/gethash response(V2)
FullHashResponseMap mFullHashCache;
@@ -274,17 +274,17 @@ public:
virtual nsresult Init() override;
virtual nsresult Open() override;
virtual void ClearAll() override;
virtual nsresult Has(const Completion& aCompletion,
bool* aHas,
uint32_t* aMatchLength,
bool* aConfirmed) override;
- virtual bool IsEmpty() override;
+ virtual bool IsEmpty() const override;
nsresult Build(AddPrefixArray& aAddPrefixes,
AddCompleteArray& aAddCompletes);
nsresult GetPrefixes(FallibleTArray<uint32_t>& aAddPrefixes);
// This will Clear() the passed arrays when done.
// 'aExpirySec' is used by testcase to config an expired time.
@@ -299,17 +299,17 @@ public:
static const int VER;
protected:
nsresult ReadCompletions();
virtual nsresult ClearPrefixes() override;
virtual nsresult StoreToFile(nsIFile* aFile) override;
virtual nsresult LoadFromFile(nsIFile* aFile) override;
- virtual size_t SizeOfPrefixSet() override;
+ virtual size_t SizeOfPrefixSet() const override;
private:
virtual int Ver() const override { return VER; }
// Construct a Prefix Set with known prefixes.
// This will Clear() aAddPrefixes when done.
nsresult ConstructPrefixSet(AddPrefixArray& aAddPrefixes);
--- a/toolkit/components/url-classifier/LookupCacheV4.cpp
+++ b/toolkit/components/url-classifier/LookupCacheV4.cpp
@@ -31,17 +31,17 @@ class VLPrefixSet
{
public:
explicit VLPrefixSet(const PrefixStringMap& aMap);
// This function will merge the prefix map in VLPrefixSet to aPrefixMap.
void Merge(PrefixStringMap& aPrefixMap);
// Find the smallest string from the map in VLPrefixSet.
- bool GetSmallestPrefix(nsACString& aOutString);
+ bool GetSmallestPrefix(nsACString& aOutString) const;
// Return the number of prefixes in the map
uint32_t Count() const { return mCount; }
private:
// PrefixString structure contains a lexicographic-sorted string with
// a |pos| variable to indicate which substring we are pointing to right now.
// |pos| increases each time GetSmallestPrefix finds the smallest string.
@@ -131,17 +131,17 @@ LookupCacheV4::Has(const Completion& aCo
}
// Even though V4 supports variable-length prefix, we always send 4-bytes for
// completion (Bug 1323953). This means cached prefix length is also 4-bytes.
return CheckCache(aCompletion, aHas, aConfirmed);
}
bool
-LookupCacheV4::IsEmpty()
+LookupCacheV4::IsEmpty() const
{
bool isEmpty;
mVLPrefixSet->IsEmpty(&isEmpty);
return isEmpty;
}
nsresult
LookupCacheV4::Build(PrefixStringMap& aPrefixMap)
@@ -205,17 +205,17 @@ LookupCacheV4::LoadFromFile(nsIFile* aFi
rv = VerifyChecksum(checksum);
Telemetry::Accumulate(Telemetry::URLCLASSIFIER_VLPS_LOAD_CORRUPT,
rv == NS_ERROR_FILE_CORRUPTED);
return rv;
}
size_t
-LookupCacheV4::SizeOfPrefixSet()
+LookupCacheV4::SizeOfPrefixSet() const
{
return mVLPrefixSet->SizeOfIncludingThis(moz_malloc_size_of);
}
static nsresult
AppendPrefixToMap(PrefixStringMap& prefixes, const nsACString& prefix)
{
uint32_t len = prefix.Length();
@@ -227,16 +227,31 @@ AppendPrefixToMap(PrefixStringMap& prefi
nsCString* prefixString = prefixes.LookupOrAdd(len);
if (!prefixString->Append(prefix, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
+static nsresult
+InitCrypto(nsCOMPtr<nsICryptoHash>& aCrypto)
+{
+ nsresult rv;
+ aCrypto = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
+ if (NS_WARN_IF(NS_FAILED(rv))) {
+ return rv;
+ }
+
+ rv = aCrypto->Init(nsICryptoHash::SHA256);
+ NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "InitCrypto failed");
+
+ return rv;
+}
+
// Read prefix into a buffer and also update the hash which
// keeps track of the checksum
static void
UpdateChecksum(nsICryptoHash* aCrypto, const nsACString& aPrefix)
{
MOZ_ASSERT(aCrypto);
aCrypto->Update(reinterpret_cast<uint8_t*>(const_cast<char*>(
aPrefix.BeginReading())),
@@ -375,31 +390,16 @@ nsresult
LookupCacheV4::AddFullHashResponseToCache(const FullHashResponseMap& aResponseMap)
{
CopyClassHashTable<FullHashResponseMap>(aResponseMap, mFullHashCache);
return NS_OK;
}
nsresult
-LookupCacheV4::InitCrypto(nsCOMPtr<nsICryptoHash>& aCrypto)
-{
- nsresult rv;
- aCrypto = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
- if (NS_WARN_IF(NS_FAILED(rv))) {
- return rv;
- }
-
- rv = aCrypto->Init(nsICryptoHash::SHA256);
- NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "InitCrypto failed");
-
- return rv;
-}
-
-nsresult
LookupCacheV4::VerifyChecksum(const nsACString& aChecksum)
{
nsCOMPtr<nsICryptoHash> crypto;
nsresult rv = InitCrypto(crypto);
if (NS_FAILED(rv)) {
return rv;
}
@@ -613,17 +613,17 @@ VLPrefixSet::Merge(PrefixStringMap& aPre
if (!remainingString.IsEmpty()) {
MOZ_ASSERT(remainingString.Length() == str->remaining());
prefixString->Append(remainingString);
}
}
}
bool
-VLPrefixSet::GetSmallestPrefix(nsACString& aOutString) {
+VLPrefixSet::GetSmallestPrefix(nsACString& aOutString) const {
PrefixString* pick = nullptr;
for (auto iter = mMap.ConstIter(); !iter.Done(); iter.Next()) {
PrefixString* str = iter.Data();
if (str->remaining() <= 0) {
continue;
}
--- a/toolkit/components/url-classifier/LookupCacheV4.h
+++ b/toolkit/components/url-classifier/LookupCacheV4.h
@@ -24,17 +24,17 @@ public:
~LookupCacheV4() {}
virtual nsresult Init() override;
virtual nsresult Has(const Completion& aCompletion,
bool* aHas,
uint32_t* aMatchLength,
bool* aConfirmed) override;
- virtual bool IsEmpty() override;
+ virtual bool IsEmpty() const override;
nsresult Build(PrefixStringMap& aPrefixMap);
nsresult GetPrefixes(PrefixStringMap& aPrefixMap);
nsresult GetFixedLengthPrefixes(FallibleTArray<uint32_t>& aPrefixes);
// ApplyUpdate will merge data stored in aTableUpdate with prefixes in aInputMap.
nsresult ApplyUpdate(TableUpdateV4* aTableUpdate,
@@ -48,22 +48,21 @@ public:
static const int VER;
static const uint32_t MAX_METADATA_VALUE_LENGTH;
protected:
virtual nsresult ClearPrefixes() override;
virtual nsresult StoreToFile(nsIFile* aFile) override;
virtual nsresult LoadFromFile(nsIFile* aFile) override;
- virtual size_t SizeOfPrefixSet() override;
+ virtual size_t SizeOfPrefixSet() const override;
private:
virtual int Ver() const override { return VER; }
- nsresult InitCrypto(nsCOMPtr<nsICryptoHash>& aCrypto);
nsresult VerifyChecksum(const nsACString& aChecksum);
RefPtr<VariableLengthPrefixSet> mVLPrefixSet;
};
} // namespace safebrowsing
} // namespace mozilla
--- a/toolkit/components/url-classifier/ProtocolParser.h
+++ b/toolkit/components/url-classifier/ProtocolParser.h
@@ -47,17 +47,17 @@ public:
// Notify that the inbound data is ready for parsing if progressive
// parsing is not supported, for example in V4.
virtual void End() = 0;
// Forget the table updates that were created by this pass. It
// becomes the caller's responsibility to free them. This is shitty.
TableUpdate *GetTableUpdate(const nsACString& aTable);
void ForgetTableUpdates() { mTableUpdates.Clear(); }
- nsTArray<TableUpdate*> &GetTableUpdates() { return mTableUpdates; }
+ const nsTArray<TableUpdate*>& GetTableUpdates() { return mTableUpdates; }
// These are only meaningful to V2. Since they were originally public,
// moving them to ProtocolParserV2 requires a dymamic cast in the call
// sites. As a result, we will leave them until we remove support
// for V2 entirely..
virtual const nsTArray<ForwardedUpdate> &Forwards() const { return mForwards; }
bool ResetRequested() const { return !mTablesToReset.IsEmpty(); }
const nsTArray<nsCString>& TablesToReset() const { return mTablesToReset; }
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
@@ -910,17 +910,17 @@ nsUrlClassifierDBServiceWorker::CacheCom
}
nsresult
nsUrlClassifierDBServiceWorker::CacheResultToTableUpdate(CacheResult* aCacheResult,
TableUpdate* aUpdate)
{
auto tuV2 = TableUpdate::Cast<TableUpdateV2>(aUpdate);
if (tuV2) {
- auto result = CacheResult::Cast<CacheResultV2>(aCacheResult);
+ const CacheResultV2* result = CacheResult::Cast<CacheResultV2>(aCacheResult);
MOZ_ASSERT(result);
if (result->miss) {
return tuV2->NewMissPrefix(result->prefix);
} else {
LOG(("CacheCompletion hash %X, Addchunk %d", result->completion.ToUint32(),
result->addChunk));
@@ -929,17 +929,17 @@ nsUrlClassifierDBServiceWorker::CacheRes
return rv;
}
return tuV2->NewAddChunk(result->addChunk);
}
}
auto tuV4 = TableUpdate::Cast<TableUpdateV4>(aUpdate);
if (tuV4) {
- auto result = CacheResult::Cast<CacheResultV4>(aCacheResult);
+ const CacheResultV4* result = CacheResult::Cast<CacheResultV4>(aCacheResult);
MOZ_ASSERT(result);
if (LOG_ENABLED()) {
const FullHashExpiryCache& fullHashes = result->response.fullHashes;
for (auto iter = fullHashes.ConstIter(); !iter.Done(); iter.Next()) {
Completion completion;
completion.Assign(iter.Key());
LOG(("CacheCompletion(v4) hash %X, CacheExpireTime %" PRId64,
@@ -1001,17 +1001,17 @@ nsUrlClassifierDBServiceWorker::GetCache
return NS_ERROR_NOT_AVAILABLE;
}
mClassifier->GetCacheInfo(aTable, aCache);
return NS_OK;
}
bool
-nsUrlClassifierDBServiceWorker::IsSameAsLastResults(CacheResultArray& aResult)
+nsUrlClassifierDBServiceWorker::IsSameAsLastResults(const CacheResultArray& aResult) const
{
if (!mLastResults || mLastResults->Length() != aResult.Length()) {
return false;
}
bool equal = true;
for (uint32_t i = 0; i < mLastResults->Length() && equal; i++) {
CacheResult* lhs = mLastResults->ElementAt(i).get();
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.h
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.h
@@ -264,17 +264,17 @@ private:
nsresult AddNoise(const Prefix aPrefix,
const nsCString tableName,
uint32_t aCount,
LookupResultArray& results);
nsresult CacheResultToTableUpdate(CacheResult* aCacheResult,
TableUpdate* aUpdate);
- bool IsSameAsLastResults(CacheResultArray& aResult);
+ bool IsSameAsLastResults(const CacheResultArray& aResult) const;
nsAutoPtr<mozilla::safebrowsing::Classifier> mClassifier;
// The class that actually parses the update chunks.
nsAutoPtr<ProtocolParser> mProtocolParser;
// Directory where to store the SB databases.
nsCOMPtr<nsIFile> mCacheDir;