Bug 1890793: Assert CallArgs::newTarget is not gray. r=spidermonkey-reviewers,sfink...
[gecko.git] / dom / script / AutoEntryScript.h
blob905baf0857cda99aa733fb26a7f08f72b0a3ec23
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/. */
7 #ifndef DOM_SCRIPT_AUTOENTRYSCRIPT_H_
8 #define DOM_SCRIPT_AUTOENTRYSCRIPT_H_
10 #include "MainThreadUtils.h"
11 #include "js/Debug.h"
12 #include "js/TypeDecls.h"
13 #include "jsapi.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/Maybe.h"
16 #include "mozilla/ProfilerLabels.h"
17 #include "mozilla/dom/JSExecutionManager.h"
18 #include "mozilla/dom/ScriptSettings.h"
20 class nsIGlobalObject;
21 class nsIPrincipal;
23 namespace xpc {
24 class AutoScriptActivity;
27 namespace mozilla::dom {
30 * A class that represents a new script entry point.
32 * |aReason| should be a statically-allocated C string naming the reason we're
33 * invoking JavaScript code: "setTimeout", "event", and so on. The devtools use
34 * these strings to label JS execution in timeline and profiling displays.
37 class MOZ_STACK_CLASS AutoEntryScript : public AutoJSAPI {
38 public:
39 // Constructing the AutoEntryScript will ensure that it enters the
40 // Realm of aGlobalObject's JSObject and exposes that JSObject to active JS.
41 AutoEntryScript(nsIGlobalObject* aGlobalObject, const char* aReason,
42 bool aIsMainThread = NS_IsMainThread());
44 // aObject can be any object from the relevant global. It must not be a
45 // cross-compartment wrapper because CCWs are not associated with a single
46 // global.
48 // Constructing the AutoEntryScript will ensure that it enters the
49 // Realm of aObject JSObject and exposes aObject's global to active JS.
50 AutoEntryScript(JSObject* aObject, const char* aReason,
51 bool aIsMainThread = NS_IsMainThread());
53 ~AutoEntryScript();
55 void SetWebIDLCallerPrincipal(nsIPrincipal* aPrincipal) {
56 mWebIDLCallerPrincipal = aPrincipal;
59 private:
60 // It's safe to make this a weak pointer, since it's the subject principal
61 // when we go on the stack, so can't go away until after we're gone. In
62 // particular, this is only used from the CallSetup constructor, and only in
63 // the aIsJSImplementedWebIDL case. And in that case, the subject principal
64 // is the principal of the callee function that is part of the CallArgs just a
65 // bit up the stack, and which will outlive us. So we know the principal
66 // can't go away until then either.
67 nsIPrincipal* MOZ_NON_OWNING_REF mWebIDLCallerPrincipal;
68 friend nsIPrincipal* GetWebIDLCallerPrincipal();
70 Maybe<xpc::AutoScriptActivity> mScriptActivity;
71 JS::AutoHideScriptedCaller mCallerOverride;
72 AutoProfilerLabel mAutoProfilerLabel;
73 AutoRequestJSThreadExecution mJSThreadExecution;
76 } // namespace mozilla::dom
78 #endif // DOM_SCRIPT_AUTOENTRYSCRIPT_H_