Bug 1727526 - Have a Stringifyable base class. r=dom-storage-reviewers,janv

Differential Revision: https://phabricator.services.mozilla.com/D191082
This commit is contained in:
Jens Stutte 2023-12-11 15:17:12 +00:00
parent 0b9a7a9ba2
commit 6dfca922cc
10 changed files with 227 additions and 36 deletions

21
dom/cache/Context.cpp vendored
View File

@ -1113,30 +1113,29 @@ void Context::DoomTargetData() {
MOZ_DIAGNOSTIC_ASSERT(!mData);
}
void Context::Stringify(nsACString& aData) {
void Context::DoStringify(nsACString& aData) {
NS_ASSERT_OWNINGTHREAD(Context);
constexpr auto kQuotaGenericDelimiterString = "|"_ns;
aData.Append(
"Context ("_ns +
"Context "_ns + kStringifyStartInstance +
//
"State:"_ns + IntToCString(mState) + kQuotaGenericDelimiterString +
"State:"_ns + IntToCString(mState) + kStringifyDelimiter +
//
"OrphanedData:"_ns + IntToCString(mOrphanedData) +
kQuotaGenericDelimiterString +
"OrphanedData:"_ns + IntToCString(mOrphanedData) + kStringifyDelimiter +
//
"PendingActions:"_ns +
IntToCString(static_cast<uint64_t>(mPendingActions.Length())) +
kQuotaGenericDelimiterString +
kStringifyDelimiter +
//
"DirectoryLock:"_ns + IntToCString(static_cast<bool>(mDirectoryLock)) +
kQuotaGenericDelimiterString +
kStringifyDelimiter +
//
"NextContext:"_ns + IntToCString(static_cast<bool>(mNextContext)));
"NextContext:"_ns + IntToCString(static_cast<bool>(mNextContext)) +
//
kStringifyEndInstance);
if (mNextContext) {
aData.Append(kQuotaGenericDelimiterString);
aData.Append(kStringifyDelimiter);
mNextContext->Stringify(aData);
};
}

7
dom/cache/Context.h vendored
View File

@ -10,6 +10,7 @@
#include "CacheCipherKeyManager.h"
#include "mozilla/dom/SafeRefPtr.h"
#include "mozilla/dom/cache/Types.h"
#include "mozilla/dom/quota/StringifyUtils.h"
#include "nsCOMPtr.h"
#include "nsISupportsImpl.h"
#include "nsProxyRelease.h"
@ -63,7 +64,7 @@ class Manager;
// As an invariant, all Context objects must be destroyed before permitting
// the "profile-before-change" shutdown event to complete. This is ensured
// via the code in ShutdownObserver.cpp.
class Context final : public SafeRefCounted<Context> {
class Context final : public SafeRefCounted<Context>, public Stringifyable {
using DirectoryLock = mozilla::dom::quota::DirectoryLock;
public:
@ -192,6 +193,8 @@ class Context final : public SafeRefCounted<Context> {
void DoomTargetData();
void DoStringify(nsACString& aData) override;
SafeRefPtr<Manager> mManager;
nsCOMPtr<nsISerialEventTarget> mTarget;
RefPtr<Data> mData;
@ -221,8 +224,6 @@ class Context final : public SafeRefCounted<Context> {
SafeRefPtr<Action> aInitAction);
~Context();
void Stringify(nsACString& aData);
NS_DECL_OWNINGTHREAD
MOZ_DECLARE_REFCOUNTED_TYPENAME(cache::Context)
};

45
dom/cache/Manager.cpp vendored
View File

@ -24,6 +24,7 @@
#include "mozilla/dom/cache/Types.h"
#include "mozilla/dom/quota/Client.h"
#include "mozilla/dom/quota/ClientImpl.h"
#include "mozilla/dom/quota/StringifyUtils.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "mozStorageHelper.h"
@ -387,32 +388,15 @@ class Manager::Factory {
if (sFactory && !sFactory->mManagerList.IsEmpty()) {
data.Append(
"Managers: "_ns +
"ManagerList: "_ns +
IntToCString(static_cast<uint64_t>(sFactory->mManagerList.Length())) +
" ("_ns);
kStringifyStartSet);
for (const auto& manager : sFactory->mManagerList.NonObservingRange()) {
data.AppendLiteral("[");
data.Append(quota::AnonymizedOriginString(
manager->GetManagerId().QuotaOrigin()));
data.AppendLiteral(": ");
data.Append(manager->GetState() == State::Open ? "Open"_ns
: "Closing"_ns);
data.AppendLiteral(", ");
if (manager->mContext) {
manager->mContext->Stringify(data);
} else {
data.AppendLiteral("No Context");
}
data.AppendLiteral(", ");
data.AppendLiteral("]");
manager->Stringify(data);
}
data.AppendLiteral(" ) ");
data.Append(kStringifyEndSet);
if (sFactory->mPotentiallyUnreleasedCSCP.Length() > 0) {
data.Append(
"There have been CSCP instances whose"
@ -2198,4 +2182,23 @@ void Manager::MaybeAllowContextToClose() {
}
}
void Manager::DoStringify(nsACString& aData) {
aData.Append("Manager "_ns + kStringifyStartInstance +
//
"Origin:"_ns +
quota::AnonymizedOriginString(GetManagerId().QuotaOrigin()) +
kStringifyDelimiter +
//
"State:"_ns + IntToCString(mState) + kStringifyDelimiter);
aData.AppendLiteral("Context:");
if (mContext) {
mContext->Stringify(aData);
} else {
aData.AppendLiteral("0");
}
aData.Append(kStringifyEndInstance);
}
} // namespace mozilla::dom::cache

5
dom/cache/Manager.h vendored
View File

@ -11,6 +11,7 @@
#include "mozilla/dom/SafeRefPtr.h"
#include "mozilla/dom/cache/Types.h"
#include "mozilla/dom/quota/Client.h"
#include "mozilla/dom/quota/StringifyUtils.h"
#include "CacheCommon.h"
#include "nsCOMPtr.h"
#include "nsISupportsImpl.h"
@ -72,7 +73,7 @@ class StreamList;
// As an invariant, all Manager objects must cease all IO before shutdown. This
// is enforced by the Manager::Factory. If content still holds references to
// Cache DOM objects during shutdown, then all operations will begin rejecting.
class Manager final : public SafeRefCounted<Manager> {
class Manager final : public SafeRefCounted<Manager>, public Stringifyable {
using Client = quota::Client;
using DirectoryLock = quota::DirectoryLock;
@ -294,6 +295,8 @@ class Manager final : public SafeRefCounted<Manager> {
struct ConstructorGuard {};
void DoStringify(nsACString& aData) override;
public:
Manager(SafeRefPtr<ManagerId> aManagerId, nsIThread* aIOThread,
const ConstructorGuard&);

View File

@ -0,0 +1,66 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "mozilla/ThreadLocal.h"
#include "mozilla/dom/quota/StringifyUtils.h"
#include "nsTHashSet.h"
namespace mozilla {
MOZ_THREAD_LOCAL(nsTHashSet<Stringifyable*>*)
Stringifyable::sActiveStringifyableInstances;
#ifdef DEBUG
Atomic<bool> sStringifyableTLSInitialized(false);
#endif
void Stringifyable::Stringify(nsACString& aData) {
if (IsActive()) {
aData.Append("(...)"_ns);
return;
}
SetActive(true);
aData.Append(kStringifyStartInstance);
DoStringify(aData);
aData.Append(kStringifyEndInstance);
SetActive(false);
}
/* static */ void Stringifyable::InitTLS() {
if (sActiveStringifyableInstances.init()) {
#ifdef DEBUG
sStringifyableTLSInitialized = true;
#endif
}
}
bool Stringifyable::IsActive() {
MOZ_ASSERT(sStringifyableTLSInitialized);
auto* set = sActiveStringifyableInstances.get();
return set && set->Contains(this);
}
void Stringifyable::SetActive(bool aIsActive) {
MOZ_ASSERT(sStringifyableTLSInitialized);
auto* myset = sActiveStringifyableInstances.get();
if (!myset && aIsActive) {
myset = new nsTHashSet<Stringifyable*>();
sActiveStringifyableInstances.set(myset);
}
MOZ_ASSERT(myset);
if (aIsActive) {
myset->Insert(this);
} else {
myset->Remove(this);
if (myset->IsEmpty()) {
sActiveStringifyableInstances.set(nullptr);
delete myset;
}
}
}
} // namespace mozilla

View File

@ -0,0 +1,53 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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_dom_quota_stringifyutils_h__
#define mozilla_dom_quota_stringifyutils_h__
#include "mozilla/ThreadLocal.h"
#include "nsTHashSet.h"
#include "nsLiteralString.h"
namespace mozilla {
// Use these constants for common delimiters. Note that `Stringify` already
// encloses each call to `DoStringify` with `kStringify[Start/End]Instance`.
constexpr auto kStringifyDelimiter = "|"_ns;
constexpr auto kStringifyStartSet = "["_ns;
constexpr auto kStringifyEndSet = "]"_ns;
constexpr auto kStringifyStartInstance = "{"_ns;
constexpr auto kStringifyEndInstance = "}"_ns;
// A Stringifyable class provides a method `Stringify` that returns a string
// representation of the class content.
//
// It's content is just appended by the override of `DoStringify` but
// `Stringifyable` ensures that we won't call `DoStringify` twice for the same
// call and instance. A single `DoStringify` function thus needs not to be
// aware of "should I `Stringify` the content of this member or not", it can
// just do it always.
//
// Stringifyable does not bloat the object by additional members but uses
// thread local memory only when needed.
class Stringifyable {
public:
void Stringify(nsACString& aData);
static void InitTLS();
private:
virtual void DoStringify(nsACString& aData) = 0;
bool IsActive();
void SetActive(bool aIsActive);
static MOZ_THREAD_LOCAL(nsTHashSet<Stringifyable*>*)
sActiveStringifyableInstances;
};
} // namespace mozilla
#endif // mozilla_dom_quota_stringifyutils_h__

View File

@ -69,6 +69,7 @@ EXPORTS.mozilla.dom.quota += [
"SerializationHelpers.h",
"StorageHelpers.h",
"StreamUtils.h",
"StringifyUtils.h",
"UsageInfo.h",
]
@ -117,6 +118,7 @@ UNIFIED_SOURCES += [
"StorageHelpers.cpp",
"StorageManager.cpp",
"StreamUtils.cpp",
"StringifyUtils.cpp",
]
IPDL_SOURCES += [

View File

@ -0,0 +1,60 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "mozilla/dom/quota/StringifyUtils.h"
#include "gtest/gtest.h"
namespace mozilla::dom::quota {
class B;
class A : public Stringifyable {
public:
A() : mB(nullptr) {}
virtual void DoStringify(nsACString& aData) override;
void setB(B* aB) { mB = aB; }
private:
B* mB;
};
class B : public Stringifyable {
public:
B() : mA(nullptr) {}
virtual void DoStringify(nsACString& aData) override;
void setA(A* aA) { mA = aA; }
private:
A* mA;
};
void A::DoStringify(nsACString& aData) {
aData.Append("Class A content."_ns);
mB->Stringify(aData);
}
void B::DoStringify(nsACString& aData) {
aData.Append("Class B content.");
mA->Stringify(aData);
}
TEST(DOM_Quota_StringifyUtils, Nested)
{
A a1;
A a2;
B b;
a1.setB(&b);
b.setA(&a2);
a2.setB(&b);
nsCString msg;
a1.Stringify(msg);
EXPECT_EQ(
0, strcmp("{Class A content.{Class B content.{Class A content.(...)}}}",
msg.get()));
}
} // namespace mozilla::dom::quota

View File

@ -31,6 +31,7 @@ UNIFIED_SOURCES = [
"TestResultExtensions.cpp",
"TestScopedLogExtraInfo.cpp",
"TestStorageConnection.cpp",
"TestStringifyUtils.cpp",
"TestTelemetry.cpp",
"TestUsageInfo.cpp",
]

View File

@ -109,6 +109,7 @@
#include "mozilla/dom/BrowserParent.h"
#include "mozilla/dom/MIDIPlatformService.h"
#include "mozilla/dom/quota/ActorsParent.h"
#include "mozilla/dom/quota/StringifyUtils.h"
#include "mozilla/dom/localstorage/ActorsParent.h"
#include "mozilla/net/UrlClassifierFeatureFactory.h"
#include "mozilla/RemoteLazyInputStreamStorage.h"
@ -265,6 +266,8 @@ nsresult nsLayoutStatics::Initialize() {
InitializeScopedLogExtraInfo();
Stringifyable::InitTLS();
if (XRE_IsParentProcess()) {
InitializeQuotaManager();
InitializeLocalStorage();