Bug 1833854 - Part 4: Move all code that deals with maintaining invariants into a...
[gecko.git] / dom / quota / ScopedLogExtraInfo.h
blobd8412ae5b1b9b9879fc9c7daf02d29d3b9730b9c
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 #ifndef DOM_QUOTA_SCOPEDLOGEXTRAINFO_H_
8 #define DOM_QUOTA_SCOPEDLOGEXTRAINFO_H_
10 #include "mozilla/dom/quota/Config.h"
12 #include <map>
13 #include "mozilla/Assertions.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/ThreadLocal.h"
16 #include "nsString.h"
17 #include "nsXULAppAPI.h"
19 namespace mozilla::dom::quota {
21 struct MOZ_STACK_CLASS ScopedLogExtraInfo {
22 static constexpr const char kTagQuery[] = "query";
23 static constexpr const char kTagContext[] = "context";
25 #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
26 private:
27 static auto FindSlot(const char* aTag);
29 public:
30 template <size_t N>
31 ScopedLogExtraInfo(const char (&aTag)[N], const nsACString& aExtraInfo)
32 : mTag{aTag}, mCurrentValue{aExtraInfo} {
33 // Initialize is currently only called in the parent process, we could call
34 // it directly from nsLayoutStatics::Initialize in the content process to
35 // allow use of ScopedLogExtraInfo in that too. The check in GetExtraInfoMap
36 // must be removed then.
37 MOZ_ASSERT(XRE_IsParentProcess());
39 AddInfo();
42 ~ScopedLogExtraInfo();
44 ScopedLogExtraInfo(ScopedLogExtraInfo&& aOther);
45 ScopedLogExtraInfo& operator=(ScopedLogExtraInfo&& aOther) = delete;
47 ScopedLogExtraInfo(const ScopedLogExtraInfo&) = delete;
48 ScopedLogExtraInfo& operator=(const ScopedLogExtraInfo&) = delete;
50 using ScopedLogExtraInfoMap = std::map<const char*, const nsACString*>;
51 static ScopedLogExtraInfoMap GetExtraInfoMap();
53 static void Initialize();
55 private:
56 const char* mTag;
57 const nsACString* mPreviousValue;
58 nsCString mCurrentValue;
60 static MOZ_THREAD_LOCAL(const nsACString*) sQueryValue;
61 static MOZ_THREAD_LOCAL(const nsACString*) sContextValue;
63 void AddInfo();
64 #else
65 template <size_t N>
66 ScopedLogExtraInfo(const char (&aTag)[N], const nsACString& aExtraInfo) {}
68 // user-defined to silence unused variable warnings
69 ~ScopedLogExtraInfo() {}
70 #endif
73 } // namespace mozilla::dom::quota
75 #endif // DOM_QUOTA_SCOPEDLOGEXTRAINFO_H_