Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(923)

Unified Diff: third_party/WebKit/Source/core/loader/modulescript/ModuleScriptLoader.cpp

Issue 2555653002: [WIP Prototype] ES6 https://html.spec.whatwg.org/#fetch-a-single-module-script implementation (Closed)
Patch Set: snapshot Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/loader/modulescript/ModuleScriptLoader.cpp
diff --git a/third_party/WebKit/Source/core/loader/modulescript/ModuleScriptLoader.cpp b/third_party/WebKit/Source/core/loader/modulescript/ModuleScriptLoader.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..9930b5e08beb5a0887c678377cea73cf3e146a1b
--- /dev/null
+++ b/third_party/WebKit/Source/core/loader/modulescript/ModuleScriptLoader.cpp
@@ -0,0 +1,228 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/loader/modulescript/ModuleScriptLoader.h"
+
+#include "core/dom/Modulator.h"
+#include "core/dom/ModuleScript.h"
+#include "core/loader/modulescript/ModuleScriptLoaderClient.h"
+#include "core/loader/modulescript/ModuleScriptLoaderRegistry.h"
+#include "platform/loader/fetch/ResourceFetcher.h"
+#include "platform/network/mime/MIMETypeRegistry.h"
+#include "platform/weborigin/SecurityPolicy.h"
+
+namespace blink {
+
+ModuleScriptLoader::ModuleScriptLoader(Modulator* modulator,
+ ModuleScriptLoaderRegistry* registry,
+ ModuleScriptLoaderClient* client)
+ : m_modulator(modulator), m_registry(registry), m_client(client) {
+ DCHECK(modulator);
+ DCHECK(registry);
+ DCHECK(client);
+}
+
+ModuleScriptLoader::~ModuleScriptLoader() {}
+
+#ifndef NDEBUG
+const char* ModuleScriptLoader::stateToString(ModuleScriptLoader::State state) {
+ switch (state) {
+ case State::Initial:
+ return "Initial";
+ case State::Fetching:
+ return "Fetching";
+ case State::Finished:
+ return "Finished";
+ default:
+ return "<unknown>";
+ }
+}
+#endif
+
+void ModuleScriptLoader::advanceState(ModuleScriptLoader::State newState) {
+ switch (m_state) {
+ case State::Initial:
+ DCHECK_EQ(newState, State::Fetching);
+ break;
+ case State::Fetching:
+ DCHECK_EQ(newState, State::Finished);
+ break;
+ case State::Finished:
+ NOTREACHED();
+ break;
+ default:
+ NOTREACHED();
+ }
+
+#ifndef NDEBUG
+ fprintf(stderr, "ModuleScriptLoader[%s] state %s -> %s. ModuleScript: %p\n",
+ m_url.getString().utf8().data(), stateToString(m_state),
+ stateToString(newState), m_moduleScript.get());
+#endif
+ m_state = newState;
+
+ if (m_state == State::Finished) {
+ m_registry->releaseFinishedLoader(this);
+ m_client->notifyNewSingleModuleFinished(m_moduleScript);
+ }
+}
+
+void ModuleScriptLoader::fetch(const ModuleScriptFetchRequest& moduleRequest,
+ ResourceFetcher* fetcher,
+ ModuleGraphLevel level) {
+ // Step 4. Set moduleMap[url] to "fetching".
+ advanceState(State::Fetching);
+
+ // Step 5. Let request be a new request whose url is url, ...
+ ResourceRequest resourceRequest(moduleRequest.url());
+#ifndef NDEBUG
+ m_url = moduleRequest.url();
+#endif
+
+ // TODO(kouhei): handle "destination is destination,"
+ // ... type is "script", -> FetchResourceType is specified by
+ // ScriptResource::fetch mode is "cors", ...
+ resourceRequest.setFetchRequestMode(WebURLRequest::FetchRequestModeCORS);
+ // ... credentials mode is credentials mode, ...
+ resourceRequest.setFetchCredentialsMode(moduleRequest.credentialsMode());
+ // parser metadata is parser state,
+ ResourceLoaderOptions options;
+ options.parserDisposition = moduleRequest.parserState();
+ // referrer is referrer,
+ if (!moduleRequest.referrer().isNull()) {
+ resourceRequest.setHTTPReferrer(SecurityPolicy::generateReferrer(
+ m_modulator->referrerPolicy(), moduleRequest.url(),
+ moduleRequest.referrer()));
+ }
+ // and client is fetch client settings object. -> set by ResourceFetcher
+
+ // TODO(kouhei): what initiatorName should we set here? should we set "module"?
+ // ... cryptographic nonce metadata is cryptographic nonce, ...
+ FetchRequest fetchRequest(resourceRequest, "module", options);
+ fetchRequest.setContentSecurityPolicyNonce(moduleRequest.nonce());
+
+ // Module scripts are always async.
+ fetchRequest.setDefer(FetchRequest::LazyLoad);
+
+ // Step 6. If the caller specified custom steps to perform the fetch,
+ // perform them on request, setting the is top-level flag if the top-level
+ // module fetch flag is set. Return from this algorithm, and when the custom
+ // perform the fetch steps complete with response response, run the remaining
+ // steps.
+ // Otherwise, fetch request. Return from this algorithm, and run the remaining
+ // steps as part of the fetch's process response for the response response.
+ // TODO(ServiceWorker team): Perform the "custom steps" for module usage
+ // inside service worker.
+ (void)level;
+ // Otherwise, fetch request. Return from this algorithm, and run the remaining
+ // steps as part of the fetch's process response for the response response.
+ // Note: response is always CORS-same-origin.
+ ScriptResource* resource = ScriptResource::fetch(fetchRequest, fetcher);
+ setResource(resource);
+
+ m_nonce = moduleRequest.nonce();
+ m_parserState = moduleRequest.parserState();
+ m_credentialsMode = moduleRequest.credentialsMode();
+}
+
+bool ModuleScriptLoader::wasModuleLoadSuccessful(Resource* resource) {
+ // Implements conditions in Step 7 of
+ // https://html.spec.whatwg.org/#fetch-a-single-module-script
+
+ // - response's type is "error"
+ if (resource->errorOccurred()) {
+#ifndef NDEBUG
+ fprintf(stderr, "resc getStatus %d error %d %s\n", resource->getStatus(),
+ resource->resourceError().errorCode(),
+ resource->resourceError().localizedDescription().utf8().data());
+#endif
+ return false;
+ }
+
+ const auto& response = resource->response();
+ // - response's status is not an ok status
+ if (response.isHTTP() &&
+ (response.httpStatusCode() < 200 || response.httpStatusCode() >= 300)) {
+ return false;
+ }
+
+ // The result of extracting a MIME type from response's header list
+ // (ignoring parameters) is not a JavaScript MIME type
+ // Note: For historical reasons, fetching a classic script does not include
+ // MIME type checking. In contrast, module scripts will fail to load if they
+ // are not of a correct MIME type.
+ if (!MIMETypeRegistry::isSupportedJavaScriptMIMEType(response.mimeType()))
+ return false;
+
+ return true;
+}
+
+// ScriptResourceClient callback handler
+void ModuleScriptLoader::notifyFinished(Resource*) {
+ // Note: "conditions" referred in Step 7 is implemented in
+ // wasModuleLoadSuccessful().
+ // Step 7. If any of the following conditions are met, set moduleMap[url] to
+ // null, asynchronously complete this algorithm with null, and abort these
+ // steps.
+ if (!wasModuleLoadSuccessful(resource())) {
+ advanceState(State::Finished);
+ setResource(nullptr);
+ return;
+ }
+#ifndef NDEBUG
+ fprintf(stderr, "ModuleScriptLoader[%s] load successful\n",
+ m_url.getString().utf8().data());
+#endif
+
+ // Step 8. Let source text be the result of UTF-8 decoding response's body.
+ String sourceText = resource()->script();
+
+ // Step 9. Let module script be the result of creating a module script given
+ // source text, module map settings object, response's url, cryptographic
+ // nonce, parser state, and credentials mode.
+ m_moduleScript =
+ createModuleScript(sourceText, resource()->response().url(), m_modulator,
+ m_nonce, m_parserState, m_credentialsMode);
+
+ advanceState(State::Finished);
+ setResource(nullptr);
+}
+
+// https://html.spec.whatwg.org/#creating-a-module-script
+ModuleScript* ModuleScriptLoader::createModuleScript(
+ const String& sourceText,
+ const KURL& url,
+ Modulator* modulator,
+ const String& nonce,
+ ParserDisposition parserState,
+ WebURLRequest::FetchCredentialsMode credentialsMode) {
+ // Step 1. Let script be a new module script that this algorithm will
+ // subsequently initialize.
+ // Step 2. Set script's settings object to the environment settings object
+ // provided.
+ // Note: "script's settings object" will be "modulator".
+
+ // Delegate to Modulator::compileModule to process Steps 3-6.
+ ScriptModule record = modulator->compileModule(sourceText, url.getString());
+ if (record.isNull())
+ return nullptr;
+ // Step 7. Set script's module record to result.
+ // Step 8. Set script's base URL to the script base URL provided.
+ // Step 9. Set script's cryptographic nonce to the cryptographic nonce
+ // provided.
+ // Step 10. Set script's parser state to the parser state.
+ // Step 11. Set script's credentials mode to the credentials mode provided.
+ // Step 12. Return script.
+ return ModuleScript::create(record, url, nonce, parserState, credentialsMode);
+}
+
+DEFINE_TRACE(ModuleScriptLoader) {
+ visitor->trace(m_modulator);
+ visitor->trace(m_moduleScript);
+ visitor->trace(m_registry);
+ visitor->trace(m_client);
+ ResourceOwner<ScriptResource>::trace(visitor);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698