tor-browser

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

AutoEntryScript.cpp (2452B)


      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/AutoEntryScript.h"
      8 
      9 #include <stdint.h>
     10 
     11 #include "js/ProfilingCategory.h"
     12 #include "js/ProfilingStack.h"
     13 #include "jsapi.h"
     14 #include "mozilla/Assertions.h"
     15 #include "nsCOMPtr.h"
     16 #include "nsContentUtils.h"
     17 #include "nsGlobalWindowInner.h"
     18 #include "nsIDocShell.h"
     19 #include "nsJSUtils.h"
     20 #include "nsPIDOMWindow.h"
     21 #include "nsPIDOMWindowInlines.h"
     22 #include "nsString.h"
     23 #include "xpcpublic.h"
     24 
     25 namespace mozilla::dom {
     26 
     27 namespace {
     28 // Assert if it's not safe to run script. The helper class
     29 // AutoAllowLegacyScriptExecution allows to allow-list
     30 // legacy cases where it's actually not safe to run script.
     31 #ifdef DEBUG
     32 void AssertIfNotSafeToRunScript() {
     33  // if it's safe to run script, then there is nothing to do here.
     34  if (nsContentUtils::IsSafeToRunScript()) {
     35    return;
     36  }
     37 
     38  // auto allowing legacy script execution is fine for now.
     39  if (AutoAllowLegacyScriptExecution::IsAllowed()) {
     40    return;
     41  }
     42 
     43  MOZ_ASSERT(false, "is it safe to run script?");
     44 }
     45 #endif
     46 
     47 }  // namespace
     48 
     49 AutoEntryScript::AutoEntryScript(nsIGlobalObject* aGlobalObject,
     50                                 const char* aReason, bool aIsMainThread)
     51    : AutoJSAPI(aGlobalObject, aIsMainThread, eEntryScript),
     52      mWebIDLCallerPrincipal(nullptr)
     53      // This relies on us having a cx() because the AutoJSAPI constructor
     54      // already ran.
     55      ,
     56      mCallerOverride(cx()),
     57      mAutoProfilerLabel(
     58          "", aReason, JS::ProfilingCategoryPair::JS,
     59          uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)),
     60      mJSThreadExecution(aGlobalObject, aIsMainThread) {
     61  MOZ_ASSERT(aGlobalObject);
     62 
     63  if (aIsMainThread) {
     64 #ifdef DEBUG
     65    AssertIfNotSafeToRunScript();
     66 #endif
     67    mScriptActivity.emplace(true);
     68  }
     69 }
     70 
     71 AutoEntryScript::AutoEntryScript(JSObject* aObject, const char* aReason,
     72                                 bool aIsMainThread)
     73    : AutoEntryScript(xpc::NativeGlobal(aObject), aReason, aIsMainThread) {
     74  // xpc::NativeGlobal uses JS::GetNonCCWObjectGlobal, which asserts that
     75  // aObject is not a CCW.
     76 }
     77 
     78 AutoEntryScript::~AutoEntryScript() = default;
     79 
     80 }  // namespace mozilla::dom