tor-browser

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

ScriptErrorHelper.cpp (4998B)


      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 "ScriptErrorHelper.h"
      8 
      9 #include "MainThreadUtils.h"
     10 #include "mozilla/SchedulerGroup.h"
     11 #include "nsContentUtils.h"
     12 #include "nsThreadUtils.h"
     13 
     14 namespace {
     15 
     16 class ScriptErrorRunnable final : public mozilla::Runnable {
     17  nsString mMessage;
     18  nsCString mMessageName;
     19  mozilla::JSCallingLocation mCallingLocation;
     20  uint32_t mSeverityFlag;
     21  uint64_t mInnerWindowID;
     22  bool mIsChrome;
     23 
     24 public:
     25  ScriptErrorRunnable(const nsAString& aMessage,
     26                      const mozilla::JSCallingLocation& aCallingLocation,
     27                      uint32_t aSeverityFlag, bool aIsChrome,
     28                      uint64_t aInnerWindowID)
     29      : mozilla::Runnable("ScriptErrorRunnable"),
     30        mMessage(aMessage),
     31        mCallingLocation(aCallingLocation),
     32        mSeverityFlag(aSeverityFlag),
     33        mInnerWindowID(aInnerWindowID),
     34        mIsChrome(aIsChrome) {
     35    MOZ_ASSERT(!NS_IsMainThread());
     36    mMessageName.SetIsVoid(true);
     37  }
     38 
     39  ScriptErrorRunnable(const nsACString& aMessageName,
     40                      const mozilla::JSCallingLocation& aCallingLocation,
     41                      uint32_t aSeverityFlag, bool aIsChrome,
     42                      uint64_t aInnerWindowID)
     43      : mozilla::Runnable("ScriptErrorRunnable"),
     44        mMessageName(aMessageName),
     45        mCallingLocation(aCallingLocation),
     46        mSeverityFlag(aSeverityFlag),
     47        mInnerWindowID(aInnerWindowID),
     48        mIsChrome(aIsChrome) {
     49    MOZ_ASSERT(!NS_IsMainThread());
     50    mMessage.SetIsVoid(true);
     51  }
     52 
     53  static void DumpLocalizedMessage(
     54      const nsACString& aMessageName,
     55      const mozilla::JSCallingLocation& aCallingLocation,
     56      uint32_t aSeverityFlag, bool aIsChrome, uint64_t aInnerWindowID) {
     57    MOZ_ASSERT(NS_IsMainThread());
     58    MOZ_ASSERT(!aMessageName.IsEmpty());
     59 
     60    nsAutoString localizedMessage;
     61    if (NS_WARN_IF(NS_FAILED(nsContentUtils::GetLocalizedString(
     62            nsContentUtils::eDOM_PROPERTIES, aMessageName.BeginReading(),
     63            localizedMessage)))) {
     64      return;
     65    }
     66    Dump(localizedMessage, aCallingLocation, aSeverityFlag, aIsChrome,
     67         aInnerWindowID);
     68  }
     69 
     70  static void Dump(const nsAString& aMessage,
     71                   const mozilla::JSCallingLocation& aCallingLocation,
     72                   uint32_t aSeverityFlag, bool aIsChrome,
     73                   uint64_t aInnerWindowID) {
     74    MOZ_ASSERT(NS_IsMainThread());
     75 
     76    nsAutoCString category;
     77    if (aIsChrome) {
     78      category.AssignLiteral("chrome ");
     79    } else {
     80      category.AssignLiteral("content ");
     81    }
     82    category.AppendLiteral("javascript");
     83    nsContentUtils::ReportToConsoleByWindowID(aMessage, aSeverityFlag, category,
     84                                              aInnerWindowID, aCallingLocation);
     85  }
     86 
     87  NS_IMETHOD
     88  Run() override {
     89    MOZ_ASSERT(NS_IsMainThread());
     90    MOZ_ASSERT(mMessage.IsVoid() != mMessageName.IsVoid());
     91 
     92    if (!mMessage.IsVoid()) {
     93      Dump(mMessage, mCallingLocation, mSeverityFlag, mIsChrome,
     94           mInnerWindowID);
     95      return NS_OK;
     96    }
     97 
     98    DumpLocalizedMessage(mMessageName, mCallingLocation, mSeverityFlag,
     99                         mIsChrome, mInnerWindowID);
    100 
    101    return NS_OK;
    102  }
    103 
    104 private:
    105  virtual ~ScriptErrorRunnable() = default;
    106 };
    107 
    108 }  // namespace
    109 
    110 namespace mozilla::dom::indexedDB {
    111 
    112 /*static*/
    113 void ScriptErrorHelper::Dump(const nsAString& aMessage,
    114                             const JSCallingLocation& aCallingLocation,
    115                             uint32_t aSeverityFlag, bool aIsChrome,
    116                             uint64_t aInnerWindowID) {
    117  if (NS_IsMainThread()) {
    118    ScriptErrorRunnable::Dump(aMessage, aCallingLocation, aSeverityFlag,
    119                              aIsChrome, aInnerWindowID);
    120  } else {
    121    RefPtr<ScriptErrorRunnable> runnable = new ScriptErrorRunnable(
    122        aMessage, aCallingLocation, aSeverityFlag, aIsChrome, aInnerWindowID);
    123    MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(runnable.forget()));
    124  }
    125 }
    126 
    127 /*static*/
    128 void ScriptErrorHelper::DumpLocalizedMessage(
    129    const nsACString& aMessageName, const JSCallingLocation& aCallingLocation,
    130    uint32_t aSeverityFlag, bool aIsChrome, uint64_t aInnerWindowID) {
    131  if (NS_IsMainThread()) {
    132    ScriptErrorRunnable::DumpLocalizedMessage(aMessageName, aCallingLocation,
    133                                              aSeverityFlag, aIsChrome,
    134                                              aInnerWindowID);
    135  } else {
    136    RefPtr<ScriptErrorRunnable> runnable =
    137        new ScriptErrorRunnable(aMessageName, aCallingLocation, aSeverityFlag,
    138                                aIsChrome, aInnerWindowID);
    139    MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(runnable.forget()));
    140  }
    141 }
    142 
    143 }  // namespace mozilla::dom::indexedDB