StringifyUtils.cpp (1712B)
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/. */ 6 7 #include "mozilla/dom/quota/StringifyUtils.h" 8 9 #include "mozilla/ThreadLocal.h" 10 #include "nsTHashSet.h" 11 12 namespace mozilla { 13 14 MOZ_THREAD_LOCAL(nsTHashSet<Stringifyable*>*) 15 Stringifyable::sActiveStringifyableInstances; 16 17 #ifdef DEBUG 18 Atomic<bool> sStringifyableTLSInitialized(false); 19 #endif 20 21 void Stringifyable::Stringify(nsACString& aData) { 22 if (IsActive()) { 23 aData.Append("(...)"_ns); 24 return; 25 } 26 27 SetActive(true); 28 aData.Append(kStringifyStartInstance); 29 DoStringify(aData); 30 aData.Append(kStringifyEndInstance); 31 SetActive(false); 32 } 33 34 /* static */ void Stringifyable::InitTLS() { 35 if (sActiveStringifyableInstances.init()) { 36 #ifdef DEBUG 37 sStringifyableTLSInitialized = true; 38 #endif 39 } 40 } 41 42 bool Stringifyable::IsActive() { 43 MOZ_ASSERT(sStringifyableTLSInitialized); 44 auto* set = sActiveStringifyableInstances.get(); 45 return set && set->Contains(this); 46 } 47 48 void Stringifyable::SetActive(bool aIsActive) { 49 MOZ_ASSERT(sStringifyableTLSInitialized); 50 auto* myset = sActiveStringifyableInstances.get(); 51 if (!myset && aIsActive) { 52 myset = new nsTHashSet<Stringifyable*>(); 53 sActiveStringifyableInstances.set(myset); 54 } 55 MOZ_ASSERT(myset); 56 if (aIsActive) { 57 myset->Insert(this); 58 } else { 59 myset->Remove(this); 60 if (myset->IsEmpty()) { 61 sActiveStringifyableInstances.set(nullptr); 62 delete myset; 63 } 64 } 65 } 66 67 } // namespace mozilla