ScriptFetchOptions.h (4208B)
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 #ifndef js_loader_ScriptFecthOptions_h 8 #define js_loader_ScriptFecthOptions_h 9 10 #include "mozilla/AlreadyAddRefed.h" 11 #include "mozilla/CORSMode.h" 12 #include "mozilla/dom/ReferrerPolicyBinding.h" 13 #include "mozilla/dom/RequestBinding.h" // RequestPriority 14 #include "nsCOMPtr.h" 15 #include "nsIPrincipal.h" 16 17 namespace JS::loader { 18 19 // https://fetch.spec.whatwg.org/#concept-request-parser-metadata 20 // All scripts are either "parser-inserted" or "not-parser-inserted", so 21 // the empty string is not necessary. 22 enum class ParserMetadata : uint8_t { 23 NotParserInserted, 24 ParserInserted, 25 }; 26 27 /* 28 * ScriptFetchOptions loosely corresponds to HTML's "script fetch options", 29 * https://html.spec.whatwg.org/multipage/webappapis.html#script-fetch-options 30 * with the exception of the following properties: 31 * integrity metadata 32 * The integrity metadata used for the initial fetch. This is 33 * implemented in ScriptLoadRequest, as it changes for every 34 * ScriptLoadRequest. 35 * 36 * referrerPolicy 37 * For a module script, its referrerPolicy will be updated if there is a 38 * HTTP Response 'REFERRER-POLICY' header, given this value may be different 39 * for every ScriptLoadRequest, so we store it directly in 40 * ScriptLoadRequest. 41 * 42 * In the case of classic scripts without dynamic import, this object is 43 * used once. For modules, this object is propogated throughout the module 44 * tree. If there is a dynamically imported module in any type of script, 45 * the ScriptFetchOptions object will be propogated from its importer. 46 */ 47 48 class ScriptFetchOptions { 49 ~ScriptFetchOptions(); 50 51 public: 52 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(ScriptFetchOptions) 53 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(ScriptFetchOptions) 54 55 ScriptFetchOptions(mozilla::CORSMode aCORSMode, const nsAString& aNonce, 56 mozilla::dom::RequestPriority aFetchPriority, 57 const ParserMetadata aParserMetadata, 58 nsIPrincipal* aTriggeringPrincipal = nullptr); 59 60 // https://html.spec.whatwg.org/#default-script-fetch-options 61 static already_AddRefed<ScriptFetchOptions> CreateDefault(); 62 63 void SetTriggeringPrincipal(nsIPrincipal* aTriggeringPrincipal); 64 65 // Returns true if given fetch option is compatible with this fetch option 66 // in term of sharing the server response. 67 inline bool IsCompatible(ScriptFetchOptions* other) { 68 if (this == other) { 69 return true; 70 } 71 72 if (mTriggeringPrincipal && other->mTriggeringPrincipal) { 73 bool equals; 74 (void)mTriggeringPrincipal->Equals(other->mTriggeringPrincipal, &equals); 75 if (!equals) { 76 return false; 77 } 78 } else if (mTriggeringPrincipal || other->mTriggeringPrincipal) { 79 return false; 80 } 81 82 // NOTE: mParserMetadata can be ignored. 83 return mCORSMode == other->mCORSMode && mNonce == other->mNonce && 84 mFetchPriority == other->mFetchPriority; 85 } 86 87 public: 88 /* Fields */ 89 90 /* 91 * The credentials mode used for the initial fetch (for module scripts) 92 * and for fetching any imported modules (for both module scripts and 93 * classic scripts) 94 */ 95 const mozilla::CORSMode mCORSMode; 96 97 /* 98 * <https://html.spec.whatwg.org/multipage/webappapis.html#script-fetch-options>. 99 */ 100 const mozilla::dom::RequestPriority mFetchPriority; 101 102 /* 103 * The parser metadata used for the initial fetch and for fetching any 104 * imported modules 105 */ 106 const ParserMetadata mParserMetadata; 107 108 /* 109 * Used to determine CSP and if we are on the About page. 110 * Only used in DOM content scripts. 111 * TODO: Move to ScriptLoadContext 112 */ 113 nsCOMPtr<nsIPrincipal> mTriggeringPrincipal; 114 115 /* 116 * The cryptographic nonce metadata used for the initial fetch and for 117 * fetching any imported modules. 118 */ 119 const nsString mNonce; 120 }; 121 122 } // namespace JS::loader 123 124 #endif // js_loader_ScriptFetchOptions_h