tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ScopedLogExtraInfo.cpp (2665B)


      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/. */
      6 
      7 #include "ScopedLogExtraInfo.h"
      8 
      9 namespace mozilla::dom::quota {
     10 
     11 #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
     12 MOZ_THREAD_LOCAL(const Tainted<nsCString>*)
     13 ScopedLogExtraInfo::sQueryValueTainted;
     14 MOZ_THREAD_LOCAL(const Tainted<nsCString>*)
     15 ScopedLogExtraInfo::sContextValueTainted;
     16 MOZ_THREAD_LOCAL(const Tainted<nsCString>*)
     17 ScopedLogExtraInfo::sStorageOriginValueTainted;
     18 
     19 /* static */
     20 auto ScopedLogExtraInfo::FindSlot(const char* aTag) {
     21  // XXX For now, don't use a real map but just allow the known tag values.
     22 
     23  if (aTag == kTagQueryTainted) {
     24    return &sQueryValueTainted;
     25  }
     26 
     27  if (aTag == kTagContextTainted) {
     28    return &sContextValueTainted;
     29  }
     30 
     31  if (aTag == kTagStorageOriginTainted) {
     32    return &sStorageOriginValueTainted;
     33  }
     34 
     35  MOZ_CRASH("Unknown tag!");
     36 }
     37 
     38 ScopedLogExtraInfo::~ScopedLogExtraInfo() {
     39  if (mTag) {
     40    MOZ_ASSERT(&mCurrentValue == FindSlot(mTag)->get(),
     41               "Bad scoping of ScopedLogExtraInfo, must not be interleaved!");
     42 
     43    FindSlot(mTag)->set(mPreviousValue);
     44  }
     45 }
     46 
     47 ScopedLogExtraInfo::ScopedLogExtraInfo(ScopedLogExtraInfo&& aOther) noexcept
     48    : mTag(aOther.mTag),
     49      mPreviousValue(aOther.mPreviousValue),
     50      mCurrentValue(std::move(aOther.mCurrentValue)) {
     51  aOther.mTag = nullptr;
     52  FindSlot(mTag)->set(&mCurrentValue);
     53 }
     54 
     55 /* static */ ScopedLogExtraInfo::ScopedLogExtraInfoMap
     56 ScopedLogExtraInfo::GetExtraInfoMap() {
     57  // This could be done in a cheaper way, but this is never called on a hot
     58  // path, so we anticipate using a real map inside here to make use simpler for
     59  // the caller(s).
     60 
     61  ScopedLogExtraInfoMap map;
     62  if (sQueryValueTainted.get()) {
     63    map.emplace(kTagQueryTainted, sQueryValueTainted.get());
     64  }
     65 
     66  if (sContextValueTainted.get()) {
     67    map.emplace(kTagContextTainted, sContextValueTainted.get());
     68  }
     69 
     70  if (sStorageOriginValueTainted.get()) {
     71    map.emplace(kTagStorageOriginTainted, sStorageOriginValueTainted.get());
     72  }
     73 
     74  return map;
     75 }
     76 
     77 /* static */ void ScopedLogExtraInfo::Initialize() {
     78  MOZ_ALWAYS_TRUE(sQueryValueTainted.init());
     79  MOZ_ALWAYS_TRUE(sContextValueTainted.init());
     80  MOZ_ALWAYS_TRUE(sStorageOriginValueTainted.init());
     81 }
     82 
     83 void ScopedLogExtraInfo::AddInfo() {
     84  auto* slot = FindSlot(mTag);
     85  MOZ_ASSERT(slot);
     86  mPreviousValue = slot->get();
     87 
     88  slot->set(&mCurrentValue);
     89 }
     90 #endif
     91 
     92 }  // namespace mozilla::dom::quota