ported from UXP: Issue #2672 - Part 1: Load nsCookieService OMT and use sync reads. (aec27853)

This commit is contained in:
2024-12-28 22:22:49 +08:00
parent 405f94084b
commit 23befc27dc
3 changed files with 426 additions and 568 deletions
+5
View File
@@ -1457,6 +1457,11 @@ nsIOService::Observe(nsISupports *subject,
nsCOMPtr<nsIPrefBranch> prefBranch;
GetPrefBranch(getter_AddRefs(prefBranch));
PrefsChanged(prefBranch, MANAGE_OFFLINE_STATUS_PREF);
// Issue #2672 - Read cookie database at an early-as-possible time
// off main thread. Hence, we have more chance to finish db query
// before something calls into the cookie service.
nsCOMPtr<nsISupports> cookieServ = do_GetService(NS_COOKIESERVICE_CONTRACTID);
}
} else if (!strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
// Remember we passed XPCOM shutdown notification to prevent any
File diff suppressed because it is too large Load Diff
+55 -16
View File
@@ -28,9 +28,12 @@
#include "mozIStorageFunction.h"
#include "nsIVariant.h"
#include "nsIFile.h"
#include "mozilla/Atomics.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Maybe.h"
#include "mozilla/Monitor.h"
#include "mozilla/UniquePtr.h"
using mozilla::OriginAttributes;
@@ -42,6 +45,7 @@ class nsIObserverService;
class nsIURI;
class nsIChannel;
class nsIArray;
class nsIThread;
class mozIStorageService;
class mozIThirdPartyUtil;
class ReadCookieDBListener;
@@ -144,13 +148,49 @@ class nsCookieEntry : public nsCookieKey
ArrayType mCookies;
};
// struct for a constant cookie for threadsafe
struct ConstCookie
{
ConstCookie(const nsCString& aName,
const nsCString& aValue,
const nsCString& aHost,
const nsCString& aPath,
int64_t aExpiry,
int64_t aLastAccessed,
int64_t aCreationTime,
bool aIsSecure,
bool aIsHttpOnly,
const OriginAttributes &aOriginAttributes)
: name(aName)
, value(aValue)
, host(aHost)
, path(aPath)
, expiry(aExpiry)
, lastAccessed(aLastAccessed)
, creationTime(aCreationTime)
, isSecure(aIsSecure)
, isHttpOnly(aIsHttpOnly)
, originAttributes(aOriginAttributes)
{
}
const nsCString name;
const nsCString value;
const nsCString host;
const nsCString path;
const int64_t expiry;
const int64_t lastAccessed;
const int64_t creationTime;
const bool isSecure;
const bool isHttpOnly;
const OriginAttributes originAttributes;
};
// encapsulates a (key, nsCookie) tuple for temporary storage purposes.
struct CookieDomainTuple
{
nsCookieKey key;
RefPtr<nsCookie> cookie;
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
mozilla::UniquePtr<ConstCookie> cookie;
};
// encapsulates in-memory and on-disk DB states, so we can
@@ -193,17 +233,9 @@ public:
// while the background read is taking place.
nsCOMPtr<mozIStorageConnection> syncConn;
nsCOMPtr<mozIStorageStatement> stmtReadDomain;
nsCOMPtr<mozIStoragePendingStatement> pendingRead;
// The asynchronous read listener. This is a weak ref (storage has ownership)
// since it may need to outlive the DBState's database connection.
ReadCookieDBListener* readListener;
// An array of (baseDomain, cookie) tuples representing data read in
// asynchronously. This is merged into hostTable once read is complete.
nsTArray<CookieDomainTuple> hostArray;
// A hashset of baseDomains read in synchronously, while the async read is
// in flight. This is used to keep track of which data in hostArray is stale
// when the time comes to merge.
nsTHashtable<nsCookieKey> readSet;
// DB completion handlers.
nsCOMPtr<mozIStorageStatementCallback> insertListener;
@@ -265,6 +297,8 @@ class nsCookieService final : public nsICookieService
void PrefChanged(nsIPrefBranch *aPrefBranch);
void InitDBStates();
OpenDBResult TryInitDB(bool aDeleteExistingDB);
void InitDBConn();
nsresult InitDBConnInternal();
nsresult CreateTableWorker(const char* aName);
nsresult CreateIndex();
nsresult CreateTable();
@@ -277,11 +311,8 @@ class nsCookieService final : public nsICookieService
void HandleCorruptDB(DBState* aDBState);
void RebuildCorruptDB(DBState* aDBState);
OpenDBResult Read();
template<class T> nsCookie* GetCookieFromRow(T &aRow, const OriginAttributes& aOriginAttributes);
void AsyncReadComplete();
void CancelAsyncRead(bool aPurgeReadSet);
void EnsureReadDomain(const nsCookieKey &aKey);
void EnsureReadComplete();
mozilla::UniquePtr<ConstCookie> GetCookieFromRow(mozIStorageStatement *aRow, const OriginAttributes &aOriginAttributes);
void EnsureReadComplete(bool aInitDBConn);
nsresult NormalizeHost(nsCString &aHost);
nsresult GetBaseDomain(nsIURI *aHostURI, nsCString &aBaseDomain, bool &aRequireHostMatch);
nsresult GetBaseDomainFromHost(const nsACString &aHost, nsCString &aBaseDomain);
@@ -353,6 +384,14 @@ class nsCookieService final : public nsICookieService
uint16_t mMaxCookiesPerHost;
int64_t mCookiePurgeAge;
// thread props
nsCOMPtr<nsIThread> mThread;
mozilla::Monitor mMonitor;
mozilla::Atomic<bool> mInitializedDBStates;
mozilla::Atomic<bool> mInitializedDBConn;
bool mAccumulatedWaitTelemetry;
nsTArray<CookieDomainTuple> mReadArray;
// friends!
friend class DBListenerErrorHandler;
friend class ReadCookieDBListener;