Skip to content

Commit

Permalink
Bug 1676361 - Move AutoEntryScript to a separate header file to avoid…
Browse files Browse the repository at this point in the history
… pulling in GeckoProfiler.h everywhere. r=mccr8

Differential Revision: https://phabricator.services.mozilla.com/D97742
  • Loading branch information
sigiesec committed Mar 4, 2021
1 parent 5f997c8 commit 6c371fc
Show file tree
Hide file tree
Showing 43 changed files with 371 additions and 252 deletions.
1 change: 1 addition & 0 deletions docshell/base/nsDocShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "mozilla/Unused.h"
#include "mozilla/WidgetUtils.h"

#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/ChildProcessChannelListener.h"
#include "mozilla/dom/ClientChannelHelper.h"
#include "mozilla/dom/ClientHandle.h"
Expand Down
1 change: 1 addition & 0 deletions dom/base/BodyStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "BodyStream.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/DOMException.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/WorkerCommon.h"
Expand Down
1 change: 1 addition & 0 deletions dom/base/CustomElementRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/CustomElementRegistryBinding.h"
#include "mozilla/dom/ElementBinding.h"
#include "mozilla/dom/HTMLElementBinding.h"
Expand Down
4 changes: 4 additions & 0 deletions dom/base/Timeout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "mozilla/dom/TimeoutManager.h"
#include "nsGlobalWindowInner.h"

#ifdef MOZ_GECKO_PROFILER
# include "GeckoProfiler.h"
#endif

namespace mozilla::dom {

Timeout::Timeout()
Expand Down
1 change: 1 addition & 0 deletions dom/base/nsContentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
#include "mozilla/Variant.h"
#include "mozilla/ViewportUtils.h"
#include "mozilla/dom/AncestorIterator.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/AutocompleteInfoBinding.h"
#include "mozilla/dom/AutoSuppressEventHandlingAndSuspend.h"
#include "mozilla/dom/BindingDeclarations.h"
Expand Down
1 change: 1 addition & 0 deletions dom/base/nsFrameMessageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "mozilla/TimeStamp.h"
#include "mozilla/TypedEnumBits.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/CallbackObject.h"
#include "mozilla/dom/ChildProcessMessageManager.h"
Expand Down
1 change: 1 addition & 0 deletions dom/base/nsGlobalWindowInner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
#include "mozilla/UniquePtr.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/AudioContext.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/BarProps.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/BindingUtils.h"
Expand Down
1 change: 1 addition & 0 deletions dom/bindings/CallbackObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "mozilla/Maybe.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/RefPtr.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/BindingCallContext.h"
#include "mozilla/dom/ScriptSettings.h"
#include "nsCOMPtr.h"
Expand Down
1 change: 1 addition & 0 deletions dom/fetch/FetchStreamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "InternalResponse.h"
#include "js/Stream.h"
#include "mozilla/ConsoleReportCollector.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/DOMException.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseBinding.h"
Expand Down
1 change: 1 addition & 0 deletions dom/ipc/SharedMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "MemMapSnapshot.h"
#include "ScriptPreloader-inl.h"

#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/BlobImpl.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/ContentProcessMessageManager.h"
Expand Down
1 change: 1 addition & 0 deletions dom/ipc/jsactor/JSActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "chrome/common/ipc_channel.h"
#include "mozilla/Attributes.h"
#include "mozilla/FunctionRef.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/ClonedErrorHolder.h"
#include "mozilla/dom/ClonedErrorHolderBinding.h"
#include "mozilla/dom/DOMException.h"
Expand Down
2 changes: 2 additions & 0 deletions dom/ipc/jsactor/JSActorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/JSActorManager.h"

#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/JSActorService.h"
#include "mozilla/dom/PWindowGlobal.h"
#include "mozilla/ipc/ProtocolUtils.h"
Expand Down
1 change: 1 addition & 0 deletions dom/jsurl/nsJSProtocolHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "nsSandboxFlags.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/DOMSecurityMonitor.h"
#include "mozilla/dom/JSExecutionContext.h"
#include "mozilla/dom/ScriptSettings.h"
Expand Down
1 change: 1 addition & 0 deletions dom/l10n/DOMLocalization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "nsIScriptError.h"
#include "DOMLocalization.h"
#include "mozilla/intl/LocaleService.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/L10nOverlays.h"

Expand Down
1 change: 1 addition & 0 deletions dom/l10n/DocumentL10n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "DocumentL10n.h"
#include "nsIContentSink.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/DocumentL10nBinding.h"
#include "mozilla/Telemetry.h"
Expand Down
1 change: 1 addition & 0 deletions dom/media/webaudio/AudioWorkletNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "js/experimental/TypedData.h" // JS_NewFloat32Array, JS_GetFloat32ArrayData, JS_GetTypedArrayLength, JS_GetArrayBufferViewBuffer
#include "mozilla/dom/AudioWorkletNodeBinding.h"
#include "mozilla/dom/AudioParamMapBinding.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/RootedDictionary.h"
#include "mozilla/dom/ErrorEvent.h"
#include "mozilla/dom/Worklet.h"
Expand Down
4 changes: 2 additions & 2 deletions dom/media/webrtc/jsapi/RTCStatsReport.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "nsWrapperCache.h"
#include "nsCOMPtr.h"

#include "nsPIDOMWindow.h" // nsPIDOMWindowInner
#include "mozilla/dom/ScriptSettings.h" // AutoEntryScript
#include "nsPIDOMWindow.h" // nsPIDOMWindowInner
#include "mozilla/dom/AutoEntryScript.h"
#include "nsIGlobalObject.h"
#include "js/RootingAPI.h" // JS::Rooted
#include "js/Value.h"
Expand Down
1 change: 1 addition & 0 deletions dom/plugins/base/nsJSNPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "mozilla/HashFunctions.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/ScriptSettings.h"

#define NPRUNTIME_JSCLASS_NAME "NPObject JS wrapper class"
Expand Down
1 change: 1 addition & 0 deletions dom/plugins/base/nsNPAPIPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "nsPIDOMWindow.h"
#include "nsGlobalWindow.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/Document.h"
#include "nsIContent.h"
#include "nsIIDNService.h"
Expand Down
1 change: 1 addition & 0 deletions dom/promise/Promise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "mozilla/ResultExtensions.h"
#include "mozilla/Unused.h"

#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/DOMException.h"
#include "mozilla/dom/DOMExceptionBinding.h"
Expand Down
1 change: 1 addition & 0 deletions dom/promise/Promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "mozilla/RefPtr.h"
#include "mozilla/Result.h"
#include "mozilla/WeakPtr.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/ToJSValue.h"
#include "nsCycleCollectionParticipant.h"
Expand Down
1 change: 1 addition & 0 deletions dom/prototype/PrototypeDocumentContentSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "nsMimeTypes.h"
#include "nsHtml5SVGLoadDispatcher.h"
#include "nsTextNode.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/CDATASection.h"
#include "mozilla/dom/Comment.h"
#include "mozilla/dom/DocumentType.h"
Expand Down
164 changes: 164 additions & 0 deletions dom/script/AutoEntryScript.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/AutoEntryScript.h"

#include <stdint.h>
#include <utility>
#include "js/ProfilingCategory.h"
#include "js/ProfilingStack.h"
#include "jsapi.h"
#include "mozilla/Assertions.h"
#include "mozilla/Maybe.h"
#include "mozilla/Span.h"
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsGlobalWindowInner.h"
#include "nsIDocShell.h"
#include "nsJSUtils.h"
#include "nsPIDOMWindow.h"
#include "nsPIDOMWindowInlines.h"
#include "nsString.h"
#include "xpcpublic.h"

namespace mozilla::dom {

namespace {
// Assert if it's not safe to run script. The helper class
// AutoAllowLegacyScriptExecution allows to allow-list
// legacy cases where it's actually not safe to run script.
#ifdef DEBUG
void AssertIfNotSafeToRunScript() {
// if it's safe to run script, then there is nothing to do here.
if (nsContentUtils::IsSafeToRunScript()) {
return;
}

// auto allowing legacy script execution is fine for now.
if (AutoAllowLegacyScriptExecution::IsAllowed()) {
return;
}

MOZ_ASSERT(false, "is it safe to run script?");
}
#endif

static unsigned long gRunToCompletionListeners = 0;

} // namespace

void UseEntryScriptProfiling() {
MOZ_ASSERT(NS_IsMainThread());
++gRunToCompletionListeners;
}

void UnuseEntryScriptProfiling() {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(gRunToCompletionListeners > 0);
--gRunToCompletionListeners;
}

AutoEntryScript::AutoEntryScript(nsIGlobalObject* aGlobalObject,
const char* aReason, bool aIsMainThread)
: AutoJSAPI(aGlobalObject, aIsMainThread, eEntryScript),
mWebIDLCallerPrincipal(nullptr)
// This relies on us having a cx() because the AutoJSAPI constructor
// already ran.
,
mCallerOverride(cx())
#ifdef MOZ_GECKO_PROFILER
,
mAutoProfilerLabel(
"", aReason, JS::ProfilingCategoryPair::JS,
uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS))
#endif
,
mJSThreadExecution(aGlobalObject, aIsMainThread) {
MOZ_ASSERT(aGlobalObject);

if (aIsMainThread) {
#ifdef DEBUG
AssertIfNotSafeToRunScript();
#endif
if (gRunToCompletionListeners > 0) {
mDocShellEntryMonitor.emplace(cx(), aReason);
}
mScriptActivity.emplace(true);
}
}

AutoEntryScript::AutoEntryScript(JSObject* aObject, const char* aReason,
bool aIsMainThread)
: AutoEntryScript(xpc::NativeGlobal(aObject), aReason, aIsMainThread) {
// xpc::NativeGlobal uses JS::GetNonCCWObjectGlobal, which asserts that
// aObject is not a CCW.
}

AutoEntryScript::~AutoEntryScript() = default;

AutoEntryScript::DocshellEntryMonitor::DocshellEntryMonitor(JSContext* aCx,
const char* aReason)
: JS::dbg::AutoEntryMonitor(aCx), mReason(aReason) {}

void AutoEntryScript::DocshellEntryMonitor::Entry(
JSContext* aCx, JSFunction* aFunction, JSScript* aScript,
JS::Handle<JS::Value> aAsyncStack, const char* aAsyncCause) {
JS::Rooted<JSFunction*> rootedFunction(aCx);
if (aFunction) {
rootedFunction = aFunction;
}
JS::Rooted<JSScript*> rootedScript(aCx);
if (aScript) {
rootedScript = aScript;
}

nsCOMPtr<nsPIDOMWindowInner> window = xpc::CurrentWindowOrNull(aCx);
if (!window || !window->GetDocShell() ||
!window->GetDocShell()->GetRecordProfileTimelineMarkers()) {
return;
}

nsCOMPtr<nsIDocShell> docShellForJSRunToCompletion = window->GetDocShell();

nsAutoJSString functionName;
if (rootedFunction) {
JS::Rooted<JSString*> displayId(aCx,
JS_GetFunctionDisplayId(rootedFunction));
if (displayId) {
if (!functionName.init(aCx, displayId)) {
JS_ClearPendingException(aCx);
return;
}
}
}

nsString filename;
uint32_t lineNumber = 0;
if (!rootedScript) {
rootedScript = JS_GetFunctionScript(aCx, rootedFunction);
}
if (rootedScript) {
CopyUTF8toUTF16(MakeStringSpan(JS_GetScriptFilename(rootedScript)),
filename);
lineNumber = JS_GetScriptBaseLineNumber(aCx, rootedScript);
}

if (!filename.IsEmpty() || !functionName.IsEmpty()) {
docShellForJSRunToCompletion->NotifyJSRunToCompletionStart(
mReason, functionName, filename, lineNumber, aAsyncStack, aAsyncCause);
}
}

void AutoEntryScript::DocshellEntryMonitor::Exit(JSContext* aCx) {
nsCOMPtr<nsPIDOMWindowInner> window = xpc::CurrentWindowOrNull(aCx);
// Not really worth checking GetRecordProfileTimelineMarkers here.
if (window && window->GetDocShell()) {
nsCOMPtr<nsIDocShell> docShellForJSRunToCompletion = window->GetDocShell();
docShellForJSRunToCompletion->NotifyJSRunToCompletionStop();
}
}

} // namespace mozilla::dom
Loading

0 comments on commit 6c371fc

Please sign in to comment.