Chromium Code Reviews| Index: third_party/WebKit/Source/core/dom/ModuleMap.cpp |
| diff --git a/third_party/WebKit/Source/core/dom/ModuleMap.cpp b/third_party/WebKit/Source/core/dom/ModuleMap.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9f726a3062f85f82bba87b6664f6298e62c3a90c |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/dom/ModuleMap.cpp |
| @@ -0,0 +1,234 @@ |
| +// Copyright 2016 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 "bindings/core/v8/V8PerIsolateData.h" |
| +#include "core/dom/ModuleMap.h" |
| +#include "core/fetch/ResourceFetcher.h" |
| +#include "core/frame/LocalFrame.h" |
| +#include "platform/network/mime/MIMETypeRegistry.h" |
| +#include "wtf/AutoReset.h" |
| +#include <v8.h> |
| + |
| +namespace blink { |
| + |
| +LocalFrame* g_localFrame = nullptr; |
| + |
| +ModuleLoaderClient::~ModuleLoaderClient() {} |
| + |
| +void ModuleLoaderClient::catchUpToLatestStateIfNeeded( |
| + ModuleLoaderState latestState) { |
| + if (m_state == ModuleLoaderState::Finished) |
| + return; |
| + if (latestState == ModuleLoaderState::Finished) { |
| + notifyFinished(); |
| + m_state = latestState; |
| + } |
| +} |
| + |
| +ModuleLoader::ModuleLoader(const KURL& url) : m_url(url) {} |
| + |
| +ModuleLoader::~ModuleLoader() {} |
| + |
| +void ModuleLoader::advanceState(ModuleLoaderState newState) { |
|
dominicc (has gone to gerrit)
2016/12/13 07:45:29
+1 to simple state machines with assertions on the
|
| + switch (m_state) { |
| + case ModuleLoaderState::Initial: |
| + DCHECK_EQ(newState, ModuleLoaderState::Fetching); |
| + break; |
| + case ModuleLoaderState::Fetching: |
| + DCHECK_EQ(newState, ModuleLoaderState::Finished); |
| + break; |
| + case ModuleLoaderState::Finished: |
| + default: |
| + NOTREACHED(); |
| + } |
| + m_state = newState; |
| + |
| + notifyAndFlushPendingClients(); |
| +} |
| + |
| +void ModuleLoader::fetch(ResourceFetcher* fetcher) { |
| + // Step 4. Set moduleMap[url] to "fetching". |
| + advanceState(ModuleLoaderState::Fetching); |
| + |
| + // Step 5. Let request be a new request whose url is url, destination is |
| + // destination, type is "script", mode is "cors", credentials mode is |
| + // credentials mode, cryptographic nonce metadata is cryptographic nonce, |
| + // parser metadata is parser state, referrer is referrer, and client is fetch |
| + // client settings object. |
| + FetchRequest request(ResourceRequest(m_url), "module"); |
| + // request.setDefer(FetchRequest::LazyLoad); //? always async -> defer? |
| + Member<ScriptResource> resource = ScriptResource::fetch(request, fetcher); |
| + setResource(resource); |
| + |
| + // 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. |
| + // Note: response is always CORS-same-origin. |
| + |
| + // TODO(kouhei): I don't know what Step 6 means. ScriptLoader seems to ignore |
| + // the step? |
| +} |
| + |
| +bool ModuleLoader::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()) |
| + 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; |
| +} |
| + |
| +void ModuleLoader::notifyFinished(Resource*) { |
| + printf("notifyFinished!\n"); |
| + |
| + // 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())) { |
| + printf("notifyFinished but load was not successful\n"); |
| + |
| + m_wasLoadSuccessful = false; |
| + advanceState(ModuleLoaderState::Finished); |
| + setResource(nullptr); |
| + return; |
| + } |
| + |
| + // Step 8. Let source text be the result of UTF-8 decoding response's body. |
| + String script = resource()->script(); |
| + printf("fetched script: %s\n", script.utf8().data()); |
| + |
| + // 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. |
| + v8::Isolate* isolate = V8PerIsolateData::mainThreadIsolate(); |
| + v8::HandleScope handleScope(isolate); |
| + |
| + ScriptState* scriptState = ScriptState::forMainWorld(g_localFrame); |
| + DCHECK(scriptState); |
| + |
| + // ??? : I'm not sure why this requres scriptState to run compile step. |
| + ScriptState::Scope scope(scriptState); |
| + m_scriptModule = ScriptModule::compile(isolate, script, m_url.getString()); |
| + |
| + m_wasLoadSuccessful = true; |
| + advanceState(ModuleLoaderState::Finished); |
| + setResource(nullptr); |
| +} |
| + |
| +void ModuleLoader::addClient(ModuleLoaderClient* newClient) { |
| + DCHECK(!m_pendingClients.contains(newClient)); |
| + DCHECK(!m_clients.contains(newClient)); |
| + newClient->setLoader(this); |
| + m_pendingClients.add(newClient); |
| + |
| + notifyAndFlushPendingClients(); |
| +} |
| + |
| +void ModuleLoader::notifyAndFlushPendingClients() { |
| + if (m_isFlushingPendingClients) { |
| + // This notifyAndFlushPendingClients call was reentrant. |
| + // Defer to first notifyAndFlushPendingClients to do the job. |
| + return; |
| + } |
| + |
| + AutoReset<bool> forbidClientModificationScope(&m_isFlushingPendingClients, |
| + true); |
| + |
| + if (m_lastClientNotifiedState != m_state) { |
| + // if m_state has changed since last update, put all m_clients back into |
| + // m_pendingClients so that they catchUpToLatestStateIfNeeded() |
| + m_pendingClients.swap(m_clients); |
| + while (!m_clients.isEmpty()) { |
| + m_pendingClients.add(m_clients.takeAny()); |
| + } |
| + |
| + m_lastClientNotifiedState = m_state; |
| + } |
| + |
| + while (!m_pendingClients.isEmpty()) { |
| + // Iterate on copy as m_pendingClients may be added in the notification |
| + // loop. |
| + HeapHashSet<Member<ModuleLoaderClient>> clientsToCatchUp; |
| + clientsToCatchUp.swap(m_pendingClients); |
| + for (const auto& client : clientsToCatchUp) { |
| + client->catchUpToLatestStateIfNeeded(m_state); |
| + } |
| + |
| + // Caught up clients do not have any notifications to receive after the |
| + // load is finished. |
| + if (hasFinished()) |
| + continue; |
| + |
| + while (!clientsToCatchUp.isEmpty()) |
| + m_clients.add(clientsToCatchUp.takeAny()); |
| + } |
| +} |
| + |
| +DEFINE_TRACE(ModuleLoader) { |
| + visitor->trace(m_pendingClients); |
| + visitor->trace(m_clients); |
| + ResourceOwner<ScriptResource>::trace(visitor); |
| +} |
| + |
| +ModuleMap::ModuleMap(LocalFrame* frame, ResourceFetcher* fetcher) |
| + : m_frame(frame), m_fetcher(fetcher) { |
| + DCHECK(m_frame); |
| + DCHECK(m_fetcher); |
| +} |
| + |
| +void ModuleMap::fetch(const KURL& url, ModuleLoaderClient* client) { |
| + g_localFrame = m_frame; // TODO(kouhei): AAAAAAAArrrrrgghhh |
| + printf("ModuleMap::fetch(%s)\n", url.getString().utf8().data()); |
| + |
| + MapImpl::AddResult result = m_map.set(url, nullptr); |
| + Member<ModuleLoader>& loaderEntry = result.storedValue->value; |
| + if (!result.isNewEntry) { |
| + printf("not new entry\n"); |
| + |
| + // - If moduleMap[url] is "fetching", wait (in parallel) until that |
| + // loaderEntry's value changes, then proceed to the next step. |
| + // - If moduleMap[url] exists, asynchronously complete this algorithm |
| + // with moduleMap[url], and abort these steps. |
| + loaderEntry->addClient(client); |
| + return; |
| + } |
| + |
| + ModuleLoader* loader = ModuleLoader::create(url); |
| + loader->fetch(m_fetcher.get()); |
| + |
| + // Step 10. Set moduleMap[url] to module script, and asynchronously complete |
| + // this algorithm with module script. |
| + loaderEntry = loader; |
| + loaderEntry->addClient(client); |
| +} |
| + |
| +DEFINE_TRACE(ModuleMap) { |
| + visitor->trace(m_map); |
| + visitor->trace(m_frame); |
| + visitor->trace(m_fetcher); |
| +} |
| + |
| +} // namespace blink |