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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ScopedLogExtraInfo.h"
9 namespace mozilla::dom::quota
{
11 #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
12 MOZ_THREAD_LOCAL(const nsACString
*) ScopedLogExtraInfo::sQueryValue
;
13 MOZ_THREAD_LOCAL(const nsACString
*) ScopedLogExtraInfo::sContextValue
;
16 auto ScopedLogExtraInfo::FindSlot(const char* aTag
) {
17 // XXX For now, don't use a real map but just allow the known tag values.
19 if (aTag
== kTagQuery
) {
23 if (aTag
== kTagContext
) {
24 return &sContextValue
;
27 MOZ_CRASH("Unknown tag!");
30 ScopedLogExtraInfo::~ScopedLogExtraInfo() {
32 MOZ_ASSERT(&mCurrentValue
== FindSlot(mTag
)->get(),
33 "Bad scoping of ScopedLogExtraInfo, must not be interleaved!");
35 FindSlot(mTag
)->set(mPreviousValue
);
39 ScopedLogExtraInfo::ScopedLogExtraInfo(ScopedLogExtraInfo
&& aOther
) noexcept
41 mPreviousValue(aOther
.mPreviousValue
),
42 mCurrentValue(std::move(aOther
.mCurrentValue
)) {
43 aOther
.mTag
= nullptr;
44 FindSlot(mTag
)->set(&mCurrentValue
);
47 /* static */ ScopedLogExtraInfo::ScopedLogExtraInfoMap
48 ScopedLogExtraInfo::GetExtraInfoMap() {
49 // This could be done in a cheaper way, but this is never called on a hot
50 // path, so we anticipate using a real map inside here to make use simpler for
53 ScopedLogExtraInfoMap map
;
54 if (sQueryValue
.get()) {
55 map
.emplace(kTagQuery
, sQueryValue
.get());
58 if (sContextValue
.get()) {
59 map
.emplace(kTagContext
, sContextValue
.get());
65 /* static */ void ScopedLogExtraInfo::Initialize() {
66 MOZ_ALWAYS_TRUE(sQueryValue
.init());
67 MOZ_ALWAYS_TRUE(sContextValue
.init());
70 void ScopedLogExtraInfo::AddInfo() {
71 auto* slot
= FindSlot(mTag
);
73 mPreviousValue
= slot
->get();
75 slot
->set(&mCurrentValue
);
79 } // namespace mozilla::dom::quota