gecko-dev/netwerk/cookie/CookieStorage.h
Valentin Gosu 1a1f42da37 Bug 1714307 - Run modernize-use-default-member-init --fix check on netwerk r=necko-reviewers,kershaw
This changeset is the result of adding modernize-use-default-member-init to
tools/clang-tidy/config.yaml then proceeding to run
`./mach static-analysis check netwerk/ --fix`
I then went through the resulting fix and manually updated all of the member
variables which were missed due to them having a non-trivial constructor.

Note that the tool was only run on Linux, so code that only runs on some
platforms may have been missed.

The member variables that are still initialized in the contructor definition
are:
  - bitfields (not all currently supported compilers allow default-member-init
  - variables that are initialized via a parameter
  - variables that use code not visible in the header file

There are a few advantages to landing this change:
- fewer lines of code - now declaration is in the same place as initialization
  this also makes it easier to see when looking at the header.
- it makes it harder to miss initializing a member when adding a new contructor
- variables that depend on an include guard look much nicer now

Additionally I removed some unnecessary reinitialization of NetAddr members
(it has a constructor that does that now), and changed nsWifiScannerDBus to
use the thread-safe strtok_r instead of strtok.

Differential Revision: https://phabricator.services.mozilla.com/D116980
2021-06-11 07:10:41 +00:00

203 lines
6.9 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_net_CookieStorage_h
#define mozilla_net_CookieStorage_h
#include "CookieKey.h"
#include "nsIObserver.h"
#include "nsTHashtable.h"
#include "nsWeakReference.h"
#include <functional>
#include "CookieCommons.h"
class nsIArray;
class nsICookie;
class nsIPrefBranch;
namespace mozilla {
namespace net {
class Cookie;
// Inherit from CookieKey so this can be stored in nsTHashTable
// TODO: why aren't we using nsClassHashTable<CookieKey, ArrayType>?
class CookieEntry : public CookieKey {
public:
// Hash methods
using ArrayType = nsTArray<RefPtr<Cookie>>;
using IndexType = ArrayType::index_type;
explicit CookieEntry(KeyTypePointer aKey) : CookieKey(aKey) {}
CookieEntry(const CookieEntry& toCopy) {
// if we end up here, things will break. nsTHashtable shouldn't
// allow this, since we set ALLOW_MEMMOVE to true.
MOZ_ASSERT_UNREACHABLE("CookieEntry copy constructor is forbidden!");
}
~CookieEntry() = default;
inline ArrayType& GetCookies() { return mCookies; }
inline const ArrayType& GetCookies() const { return mCookies; }
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
private:
ArrayType mCookies;
};
// stores the CookieEntry entryclass and an index into the cookie array within
// that entryclass, for purposes of storing an iteration state that points to a
// certain cookie.
struct CookieListIter {
// default (non-initializing) constructor.
CookieListIter() = default;
// explicit constructor to a given iterator state with entryclass 'aEntry'
// and index 'aIndex'.
explicit CookieListIter(CookieEntry* aEntry, CookieEntry::IndexType aIndex)
: entry(aEntry), index(aIndex) {}
// get the Cookie * the iterator currently points to.
mozilla::net::Cookie* Cookie() const { return entry->GetCookies()[index]; }
CookieEntry* entry;
CookieEntry::IndexType index;
};
class CookieStorage : public nsIObserver, public nsSupportsWeakReference {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIOBSERVER
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
void GetCookies(nsTArray<RefPtr<nsICookie>>& aCookies) const;
void GetSessionCookies(nsTArray<RefPtr<nsICookie>>& aCookies) const;
bool FindCookie(const nsACString& aBaseDomain,
const OriginAttributes& aOriginAttributes,
const nsACString& aHost, const nsACString& aName,
const nsACString& aPath, CookieListIter& aIter);
uint32_t CountCookiesFromHost(const nsACString& aBaseDomain,
uint32_t aPrivateBrowsingId);
void GetAll(nsTArray<RefPtr<nsICookie>>& aResult) const;
const nsTArray<RefPtr<Cookie>>* GetCookiesFromHost(
const nsACString& aBaseDomain, const OriginAttributes& aOriginAttributes);
void GetCookiesWithOriginAttributes(const OriginAttributesPattern& aPattern,
const nsACString& aBaseDomain,
nsTArray<RefPtr<nsICookie>>& aResult);
void RemoveCookie(const nsACString& aBaseDomain,
const OriginAttributes& aOriginAttributes,
const nsACString& aHost, const nsACString& aName,
const nsACString& aPath);
virtual void RemoveCookiesWithOriginAttributes(
const OriginAttributesPattern& aPattern, const nsACString& aBaseDomain);
virtual void RemoveCookiesFromExactHost(
const nsACString& aHost, const nsACString& aBaseDomain,
const OriginAttributesPattern& aPattern);
void RemoveAll();
void NotifyChanged(nsISupports* aSubject, const char16_t* aData,
bool aOldCookieIsSession = false);
void AddCookie(nsIConsoleReportCollector* aCRC, const nsACString& aBaseDomain,
const OriginAttributes& aOriginAttributes, Cookie* aCookie,
int64_t aCurrentTimeInUsec, nsIURI* aHostURI,
const nsACString& aCookieHeader, bool aFromHttp);
static void CreateOrUpdatePurgeList(nsIArray** aPurgedList,
nsICookie* aCookie);
virtual void StaleCookies(const nsTArray<Cookie*>& aCookieList,
int64_t aCurrentTimeInUsec) = 0;
virtual void Close() = 0;
protected:
CookieStorage() = default;
virtual ~CookieStorage() = default;
void Init();
void AddCookieToList(const nsACString& aBaseDomain,
const OriginAttributes& aOriginAttributes,
Cookie* aCookie);
virtual void StoreCookie(const nsACString& aBaseDomain,
const OriginAttributes& aOriginAttributes,
Cookie* aCookie) = 0;
virtual const char* NotificationTopic() const = 0;
virtual void NotifyChangedInternal(nsISupports* aSubject,
const char16_t* aData,
bool aOldCookieIsSession) = 0;
virtual void RemoveAllInternal() = 0;
// This method calls RemoveCookieFromDB + RemoveCookieFromListInternal.
void RemoveCookieFromList(const CookieListIter& aIter);
void RemoveCookieFromListInternal(const CookieListIter& aIter);
virtual void RemoveCookieFromDB(const CookieListIter& aIter) = 0;
already_AddRefed<nsIArray> PurgeCookiesWithCallbacks(
int64_t aCurrentTimeInUsec, uint16_t aMaxNumberOfCookies,
int64_t aCookiePurgeAge,
std::function<void(const CookieListIter&)>&& aRemoveCookieCallback,
std::function<void()>&& aFinalizeCallback);
nsTHashtable<CookieEntry> mHostTable;
uint32_t mCookieCount{0};
private:
void PrefChanged(nsIPrefBranch* aPrefBranch);
bool FindSecureCookie(const nsACString& aBaseDomain,
const OriginAttributes& aOriginAttributes,
Cookie* aCookie);
static void FindStaleCookies(CookieEntry* aEntry, int64_t aCurrentTime,
bool aIsSecure,
nsTArray<CookieListIter>& aOutput,
uint32_t aLimit);
void UpdateCookieOldestTime(Cookie* aCookie);
void MergeCookieSchemeMap(Cookie* aOldCookie, Cookie* aNewCookie);
static already_AddRefed<nsIArray> CreatePurgeList(nsICookie* aCookie);
virtual already_AddRefed<nsIArray> PurgeCookies(int64_t aCurrentTimeInUsec,
uint16_t aMaxNumberOfCookies,
int64_t aCookiePurgeAge) = 0;
int64_t mCookieOldestTime{INT64_MAX};
uint16_t mMaxNumberOfCookies{kMaxNumberOfCookies};
uint16_t mMaxCookiesPerHost{kMaxCookiesPerHost};
uint16_t mCookieQuotaPerHost{kCookieQuotaPerHost};
int64_t mCookiePurgeAge{kCookiePurgeAge};
};
} // namespace net
} // namespace mozilla
#endif // mozilla_net_CookieStorage_h