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 /* JSPrincipals and related interfaces. */
9 #ifndef js_Principals_h
10 #define js_Principals_h
12 #include "mozilla/Atomics.h"
18 #include "js/TypeDecls.h"
20 struct JSStructuredCloneReader
;
21 struct JSStructuredCloneWriter
;
24 /* Don't call "destroy"; use reference counting macros below. */
25 mozilla::Atomic
<int32_t, mozilla::SequentiallyConsistent
> refcount
{0};
28 /* A helper to facilitate principals debugging. */
32 JSPrincipals() = default;
34 void setDebugToken(uint32_t token
) {
41 * Write the principals with the given |writer|. Return false on failure,
44 virtual bool write(JSContext
* cx
, JSStructuredCloneWriter
* writer
) = 0;
47 * Whether the principal corresponds to a System or AddOn Principal.
48 * Technically this also checks for an ExpandedAddonPrincipal.
50 virtual bool isSystemOrAddonPrincipal() = 0;
53 * This is not defined by the JS engine but should be provided by the
56 JS_PUBLIC_API
void dump();
59 extern JS_PUBLIC_API
void JS_HoldPrincipals(JSPrincipals
* principals
);
61 extern JS_PUBLIC_API
void JS_DropPrincipals(JSContext
* cx
,
62 JSPrincipals
* principals
);
64 // Return whether the first principal subsumes the second. The exact meaning of
65 // 'subsumes' is left up to the browser. Subsumption is checked inside the JS
66 // engine when determining, e.g., which stack frames to display in a backtrace.
67 typedef bool (*JSSubsumesOp
)(JSPrincipals
* first
, JSPrincipals
* second
);
70 * Used to check if a CSP instance wants to disable eval() and friends.
71 * See GlobalObject::isRuntimeCodeGenEnabled() in vm/GlobalObject.cpp.
73 typedef bool (*JSCSPEvalChecker
)(JSContext
* cx
, JS::HandleString code
);
75 struct JSSecurityCallbacks
{
76 JSCSPEvalChecker contentSecurityPolicyAllows
;
77 JSSubsumesOp subsumes
;
80 extern JS_PUBLIC_API
void JS_SetSecurityCallbacks(
81 JSContext
* cx
, const JSSecurityCallbacks
* callbacks
);
83 extern JS_PUBLIC_API
const JSSecurityCallbacks
* JS_GetSecurityCallbacks(
87 * Code running with "trusted" principals will be given a deeper stack
88 * allocation than ordinary scripts. This allows trusted script to run after
89 * untrusted script has exhausted the stack. This function sets the
90 * runtime-wide trusted principal.
92 * This principals is not held (via JS_HoldPrincipals/JS_DropPrincipals).
93 * Instead, the caller must ensure that the given principals stays valid for as
94 * long as 'cx' may point to it. If the principals would be destroyed before
95 * 'cx', JS_SetTrustedPrincipals must be called again, passing nullptr for
98 extern JS_PUBLIC_API
void JS_SetTrustedPrincipals(JSContext
* cx
,
101 typedef void (*JSDestroyPrincipalsOp
)(JSPrincipals
* principals
);
104 * Initialize the callback that is called to destroy JSPrincipals instance
105 * when its reference counter drops to zero. The initialization can be done
106 * only once per JS runtime.
108 extern JS_PUBLIC_API
void JS_InitDestroyPrincipalsCallback(
109 JSContext
* cx
, JSDestroyPrincipalsOp destroyPrincipals
);
112 * Read a JSPrincipals instance from the given |reader| and initialize the out
113 * paratemer |outPrincipals| to the JSPrincipals instance read.
115 * Return false on failure, true on success. The |outPrincipals| parameter
116 * should not be modified if false is returned.
118 * The caller is not responsible for calling JS_HoldPrincipals on the resulting
119 * JSPrincipals instance, the JSReadPrincipalsOp must increment the refcount of
120 * the resulting JSPrincipals on behalf of the caller.
122 using JSReadPrincipalsOp
= bool (*)(JSContext
* cx
,
123 JSStructuredCloneReader
* reader
,
124 JSPrincipals
** outPrincipals
);
127 * Initialize the callback that is called to read JSPrincipals instances from a
128 * buffer. The initialization can be done only once per JS runtime.
130 extern JS_PUBLIC_API
void JS_InitReadPrincipalsCallback(
131 JSContext
* cx
, JSReadPrincipalsOp read
);
135 class MOZ_RAII AutoHoldPrincipals
{
137 JSPrincipals
* principals_
= nullptr;
140 explicit AutoHoldPrincipals(JSContext
* cx
, JSPrincipals
* principals
= nullptr)
145 ~AutoHoldPrincipals() { reset(nullptr); }
147 void reset(JSPrincipals
* principals
) {
149 JS_HoldPrincipals(principals
);
152 JS_DropPrincipals(cx_
, principals_
);
154 principals_
= principals
;
157 JSPrincipals
* get() const { return principals_
; }
162 #endif /* js_Principals_h */