Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/loader/modulescript/ModuleScriptLoader.h" | |
| 6 | |
| 7 #include "core/dom/Modulator.h" | |
| 8 #include "core/dom/ModuleScript.h" | |
| 9 #include "core/loader/modulescript/ModuleScriptLoaderClient.h" | |
| 10 #include "core/loader/modulescript/ModuleScriptLoaderRegistry.h" | |
| 11 #include "platform/loader/fetch/ResourceFetcher.h" | |
| 12 #include "platform/loader/fetch/ResourceLoadingLog.h" | |
| 13 #include "platform/network/mime/MIMETypeRegistry.h" | |
| 14 #include "platform/weborigin/SecurityPolicy.h" | |
| 15 #include "wtf/text/AtomicString.h" | |
| 16 | |
| 17 namespace blink { | |
| 18 | |
| 19 ModuleScriptLoader::ModuleScriptLoader(Modulator* modulator, | |
| 20 ModuleScriptLoaderRegistry* registry, | |
| 21 ModuleScriptLoaderClient* client) | |
| 22 : m_modulator(modulator), m_registry(registry), m_client(client) { | |
| 23 DCHECK(modulator); | |
| 24 DCHECK(registry); | |
| 25 DCHECK(client); | |
| 26 } | |
| 27 | |
| 28 ModuleScriptLoader::~ModuleScriptLoader() {} | |
| 29 | |
| 30 #ifndef NDEBUG | |
| 31 const char* ModuleScriptLoader::stateToString(ModuleScriptLoader::State state) { | |
| 32 switch (state) { | |
| 33 case State::Initial: | |
| 34 return "Initial"; | |
| 35 case State::Fetching: | |
| 36 return "Fetching"; | |
| 37 case State::Finished: | |
| 38 return "Finished"; | |
| 39 } | |
| 40 NOTREACHED(); | |
| 41 return ""; | |
| 42 } | |
| 43 #endif | |
| 44 | |
| 45 void ModuleScriptLoader::advanceState(ModuleScriptLoader::State newState) { | |
| 46 switch (m_state) { | |
| 47 case State::Initial: | |
| 48 DCHECK_EQ(newState, State::Fetching); | |
| 49 break; | |
| 50 case State::Fetching: | |
| 51 DCHECK_EQ(newState, State::Finished); | |
| 52 break; | |
| 53 case State::Finished: | |
| 54 NOTREACHED(); | |
| 55 break; | |
| 56 } | |
| 57 | |
| 58 #ifndef NDEBUG | |
|
yhirano
2017/02/22 08:02:57
According to base/logging.h, DVLOG is enabled iff
kouhei (in TOK)
2017/02/24 03:18:18
Done.
| |
| 59 RESOURCE_LOADING_DVLOG(1) << "ModuleLoader[" << m_url.getString() | |
| 60 << "]::advanceState(" << stateToString(m_state) | |
| 61 << " -> " << stateToString(newState) << ")"; | |
| 62 #endif | |
| 63 m_state = newState; | |
| 64 | |
| 65 if (m_state == State::Finished) { | |
| 66 m_registry->releaseFinishedLoader(this); | |
| 67 m_client->notifyNewSingleModuleFinished(m_moduleScript); | |
| 68 setResource(nullptr); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void ModuleScriptLoader::fetch(const ModuleScriptFetchRequest& moduleRequest, | |
| 73 ResourceFetcher* fetcher, | |
| 74 ModuleGraphLevel level) { | |
| 75 // https://html.spec.whatwg.org/#fetch-a-single-module-script | |
| 76 | |
| 77 // Step 4. Set moduleMap[url] to "fetching". | |
| 78 advanceState(State::Fetching); | |
| 79 | |
| 80 // Step 5. Let request be a new request whose url is url, ... | |
| 81 ResourceRequest resourceRequest(moduleRequest.url()); | |
| 82 #ifndef NDEBUG | |
| 83 m_url = moduleRequest.url(); | |
| 84 #endif | |
| 85 | |
| 86 // TODO(kouhei): handle "destination is destination," | |
| 87 | |
| 88 // ... type is "script", ... | |
| 89 // -> FetchResourceType is specified by ScriptResource::fetch | |
| 90 | |
| 91 // ... mode is "cors", ... | |
| 92 resourceRequest.setFetchRequestMode(WebURLRequest::FetchRequestModeCORS); | |
| 93 // ... credentials mode is credentials mode, ... | |
| 94 resourceRequest.setFetchCredentialsMode(moduleRequest.credentialsMode()); | |
| 95 // parser metadata is parser state, | |
| 96 ResourceLoaderOptions options; | |
| 97 options.parserDisposition = moduleRequest.parserState(); | |
| 98 // referrer is referrer, | |
| 99 if (!moduleRequest.referrer().isNull()) { | |
| 100 resourceRequest.setHTTPReferrer(SecurityPolicy::generateReferrer( | |
| 101 m_modulator->referrerPolicy(), moduleRequest.url(), | |
| 102 moduleRequest.referrer())); | |
| 103 } | |
| 104 // and client is fetch client settings object. -> set by ResourceFetcher | |
| 105 | |
| 106 // As initiator for module script fetch is not specified in HTML spec, | |
| 107 // we specity "" as initiator per: | |
| 108 // https://fetch.spec.whatwg.org/#concept-request-initiator | |
| 109 const AtomicString& initiatorName = emptyAtom; | |
| 110 | |
| 111 FetchRequest fetchRequest(resourceRequest, initiatorName, options); | |
| 112 // ... cryptographic nonce metadata is cryptographic nonce, ... | |
| 113 fetchRequest.setContentSecurityPolicyNonce(moduleRequest.nonce()); | |
| 114 // Note: The fetch request's "origin" isn't specified in | |
| 115 // https://html.spec.whatwg.org/#fetch-a-single-module-script | |
| 116 // Thus, the "origin" is "client" per | |
| 117 // https://fetch.spec.whatwg.org/#concept-request-origin | |
| 118 CrossOriginAttributeValue crossOrigin = | |
| 119 moduleRequest.credentialsMode() == | |
| 120 WebURLRequest::FetchCredentialsModeInclude | |
| 121 ? CrossOriginAttributeUseCredentials | |
| 122 : CrossOriginAttributeAnonymous; | |
| 123 fetchRequest.setCrossOriginAccessControl(m_modulator->securityOrigin(), | |
| 124 crossOrigin); | |
| 125 | |
| 126 // Module scripts are always async. | |
| 127 fetchRequest.setDefer(FetchRequest::LazyLoad); | |
| 128 | |
| 129 // Step 6. If the caller specified custom steps to perform the fetch, | |
| 130 // perform them on request, setting the is top-level flag if the top-level | |
| 131 // module fetch flag is set. Return from this algorithm, and when the custom | |
| 132 // perform the fetch steps complete with response response, run the remaining | |
| 133 // steps. | |
| 134 // Otherwise, fetch request. Return from this algorithm, and run the remaining | |
| 135 // steps as part of the fetch's process response for the response response. | |
| 136 // TODO(ServiceWorker team): Perform the "custom steps" for module usage | |
| 137 // inside service worker. | |
| 138 (void)level; | |
| 139 ScriptResource* resource = ScriptResource::fetch(fetchRequest, fetcher); | |
| 140 DCHECK(resource); | |
| 141 setResource(resource); | |
| 142 | |
| 143 m_nonce = moduleRequest.nonce(); | |
| 144 m_parserState = moduleRequest.parserState(); | |
| 145 } | |
| 146 | |
| 147 bool ModuleScriptLoader::wasModuleLoadSuccessful(Resource* resource) { | |
| 148 // Implements conditions in Step 7 of | |
| 149 // https://html.spec.whatwg.org/#fetch-a-single-module-script | |
| 150 | |
| 151 // - response's type is "error" | |
| 152 if (resource->errorOccurred()) { | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 156 const auto& response = resource->response(); | |
| 157 // - response's status is not an ok status | |
| 158 if (response.isHTTP() && | |
| 159 (response.httpStatusCode() < 200 || response.httpStatusCode() >= 300)) { | |
| 160 return false; | |
| 161 } | |
| 162 | |
| 163 // The result of extracting a MIME type from response's header list | |
| 164 // (ignoring parameters) is not a JavaScript MIME type | |
| 165 // Note: For historical reasons, fetching a classic script does not include | |
| 166 // MIME type checking. In contrast, module scripts will fail to load if they | |
| 167 // are not of a correct MIME type. | |
| 168 if (!MIMETypeRegistry::isSupportedJavaScriptMIMEType(response.mimeType())) | |
| 169 return false; | |
| 170 | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 // ScriptResourceClient callback handler | |
| 175 void ModuleScriptLoader::notifyFinished(Resource*) { | |
| 176 // Note: "conditions" referred in Step 7 is implemented in | |
| 177 // wasModuleLoadSuccessful(). | |
| 178 // Step 7. If any of the following conditions are met, set moduleMap[url] to | |
| 179 // null, asynchronously complete this algorithm with null, and abort these | |
| 180 // steps. | |
| 181 if (!wasModuleLoadSuccessful(resource())) { | |
| 182 advanceState(State::Finished); | |
| 183 return; | |
| 184 } | |
| 185 | |
| 186 // Step 8. Let source text be the result of UTF-8 decoding response's body. | |
| 187 String sourceText = resource()->script(); | |
| 188 | |
| 189 // Step 9. Let module script be the result of creating a module script given | |
| 190 // source text, module map settings object, response's url, cryptographic | |
| 191 // nonce, parser state, and credentials mode. | |
| 192 m_moduleScript = createModuleScript( | |
| 193 sourceText, resource()->response().url(), m_modulator, m_nonce, | |
| 194 m_parserState, resource()->resourceRequest().fetchCredentialsMode()); | |
| 195 | |
| 196 advanceState(State::Finished); | |
| 197 } | |
| 198 | |
| 199 // https://html.spec.whatwg.org/#creating-a-module-script | |
| 200 ModuleScript* ModuleScriptLoader::createModuleScript( | |
| 201 const String& sourceText, | |
| 202 const KURL& url, | |
| 203 Modulator* modulator, | |
| 204 const String& nonce, | |
| 205 ParserDisposition parserState, | |
| 206 WebURLRequest::FetchCredentialsMode credentialsMode) { | |
| 207 // Step 1. Let script be a new module script that this algorithm will | |
| 208 // subsequently initialize. | |
| 209 // Step 2. Set script's settings object to the environment settings object | |
| 210 // provided. | |
| 211 // Note: "script's settings object" will be "modulator". | |
| 212 | |
| 213 // Delegate to Modulator::compileModule to process Steps 3-6. | |
| 214 ScriptModule result = modulator->compileModule(sourceText, url.getString()); | |
| 215 // Step 6: "...return null, and abort these steps." | |
| 216 if (result.isNull()) | |
| 217 return nullptr; | |
| 218 // Step 7. Set script's module record to result. | |
| 219 // Step 8. Set script's base URL to the script base URL provided. | |
| 220 // Step 9. Set script's cryptographic nonce to the cryptographic nonce | |
| 221 // provided. | |
| 222 // Step 10. Set script's parser state to the parser state. | |
| 223 // Step 11. Set script's credentials mode to the credentials mode provided. | |
| 224 // Step 12. Return script. | |
| 225 return ModuleScript::create(result, url, nonce, parserState, credentialsMode); | |
| 226 } | |
| 227 | |
| 228 DEFINE_TRACE(ModuleScriptLoader) { | |
| 229 visitor->trace(m_modulator); | |
| 230 visitor->trace(m_moduleScript); | |
| 231 visitor->trace(m_registry); | |
| 232 visitor->trace(m_client); | |
| 233 ResourceOwner<ScriptResource>::trace(visitor); | |
| 234 } | |
| 235 | |
| 236 } // namespace blink | |
| OLD | NEW |