Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / quota / StringifyUtils.cpp
blobc85698ef60ac251724fe31c6b93172158416fef7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/ThreadLocal.h"
8 #include "mozilla/dom/quota/StringifyUtils.h"
9 #include "nsTHashSet.h"
11 namespace mozilla {
13 MOZ_THREAD_LOCAL(nsTHashSet<Stringifyable*>*)
14 Stringifyable::sActiveStringifyableInstances;
16 #ifdef DEBUG
17 Atomic<bool> sStringifyableTLSInitialized(false);
18 #endif
20 void Stringifyable::Stringify(nsACString& aData) {
21 if (IsActive()) {
22 aData.Append("(...)"_ns);
23 return;
26 SetActive(true);
27 aData.Append(kStringifyStartInstance);
28 DoStringify(aData);
29 aData.Append(kStringifyEndInstance);
30 SetActive(false);
33 /* static */ void Stringifyable::InitTLS() {
34 if (sActiveStringifyableInstances.init()) {
35 #ifdef DEBUG
36 sStringifyableTLSInitialized = true;
37 #endif
41 bool Stringifyable::IsActive() {
42 MOZ_ASSERT(sStringifyableTLSInitialized);
43 auto* set = sActiveStringifyableInstances.get();
44 return set && set->Contains(this);
47 void Stringifyable::SetActive(bool aIsActive) {
48 MOZ_ASSERT(sStringifyableTLSInitialized);
49 auto* myset = sActiveStringifyableInstances.get();
50 if (!myset && aIsActive) {
51 myset = new nsTHashSet<Stringifyable*>();
52 sActiveStringifyableInstances.set(myset);
54 MOZ_ASSERT(myset);
55 if (aIsActive) {
56 myset->Insert(this);
57 } else {
58 myset->Remove(this);
59 if (myset->IsEmpty()) {
60 sActiveStringifyableInstances.set(nullptr);
61 delete myset;
66 } // namespace mozilla