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 js_loader_ScriptFecthOptions_h
8 #define js_loader_ScriptFecthOptions_h
10 #include "mozilla/CORSMode.h"
11 #include "mozilla/dom/ReferrerPolicyBinding.h"
12 #include "mozilla/dom/RequestBinding.h" // RequestPriority
14 #include "nsIPrincipal.h"
16 namespace JS::loader
{
18 // https://fetch.spec.whatwg.org/#concept-request-parser-metadata
19 // All scripts are either "parser-inserted" or "not-parser-inserted", so
20 // the empty string is not necessary.
21 enum class ParserMetadata
{
27 * ScriptFetchOptions loosely corresponds to HTML's "script fetch options",
28 * https://html.spec.whatwg.org/multipage/webappapis.html#script-fetch-options
29 * with the exception of the following properties:
31 * The integrity metadata used for the initial fetch. This is
32 * implemented in ScriptLoadRequest, as it changes for every
36 * For a module script, its referrerPolicy will be updated if there is a
37 * HTTP Response 'REFERRER-POLICY' header, given this value may be different
38 * for every ScriptLoadRequest, so we store it directly in
41 * In the case of classic scripts without dynamic import, this object is
42 * used once. For modules, this object is propogated throughout the module
43 * tree. If there is a dynamically imported module in any type of script,
44 * the ScriptFetchOptions object will be propogated from its importer.
47 class ScriptFetchOptions
{
48 ~ScriptFetchOptions();
51 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(ScriptFetchOptions
)
52 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(ScriptFetchOptions
)
54 ScriptFetchOptions(mozilla::CORSMode aCORSMode
, const nsAString
& aNonce
,
55 mozilla::dom::RequestPriority aFetchPriority
,
56 const ParserMetadata aParserMetadata
,
57 nsIPrincipal
* aTriggeringPrincipal
);
60 * The credentials mode used for the initial fetch (for module scripts)
61 * and for fetching any imported modules (for both module scripts and
64 const mozilla::CORSMode mCORSMode
;
67 * The cryptographic nonce metadata used for the initial fetch and for
68 * fetching any imported modules.
70 const nsString mNonce
;
73 * <https://html.spec.whatwg.org/multipage/webappapis.html#script-fetch-options>.
75 const mozilla::dom::RequestPriority mFetchPriority
;
78 * The parser metadata used for the initial fetch and for fetching any
81 const ParserMetadata mParserMetadata
;
84 * Used to determine CSP and if we are on the About page.
85 * Only used in DOM content scripts.
86 * TODO: Move to ScriptLoadContext
88 nsCOMPtr
<nsIPrincipal
> mTriggeringPrincipal
;
90 // Returns true if given fetch option is compatible with this fetch option
91 // in term of sharing the server response.
92 inline bool IsCompatible(ScriptFetchOptions
* other
) {
94 (void)mTriggeringPrincipal
->Equals(other
->mTriggeringPrincipal
, &equals
);
100 // NOTE: mParserMetadata can be ignored.
101 return mCORSMode
== other
->mCORSMode
&& mNonce
== other
->mNonce
&&
102 mFetchPriority
== other
->mFetchPriority
;
106 } // namespace JS::loader
108 #endif // js_loader_ScriptFetchOptions_h