Chromium Code Reviews| Index: third_party/WebKit/Source/core/dom/ModuleMap.h |
| diff --git a/third_party/WebKit/Source/core/dom/ModuleMap.h b/third_party/WebKit/Source/core/dom/ModuleMap.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5b5d489ed98773e5bdbbecb0ebbf0eab1358a5ca |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/dom/ModuleMap.h |
| @@ -0,0 +1,142 @@ |
| +// 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. |
| + |
| +#ifndef ModuleMap_h |
| +#define ModuleMap_h |
| + |
| +#include "bindings/core/v8/ScriptModule.h" |
| +#include "core/loader/resource/ScriptResource.h" |
| +#include "core/fetch/ResourceOwner.h" |
| +#include "platform/heap/Handle.h" |
| +#include "platform/weborigin/KURL.h" |
| +#include "platform/weborigin/KURLHash.h" |
| +#include "wtf/HashMap.h" |
| +#include "wtf/HashTableDeletedValueType.h" |
| + |
| +namespace blink { |
| + |
| +class ResourceFetcher; |
| +class ModuleLoader; |
| + |
| +enum class ModuleLoaderState { |
| + Initial, |
| + Fetching, |
| + Fetched, |
|
kouhei (in TOK)
2016/12/12 07:38:51
TODO: Fetched, Failed -> Complete or Finished and
|
| + Failed, |
| +}; |
| + |
| +class ModuleLoaderClient : public GarbageCollectedMixin { |
| + public: |
| + DEFINE_INLINE_VIRTUAL_TRACE() {} |
| + |
| + protected: |
| + ModuleLoaderClient() {}; |
| + virtual ~ModuleLoaderClient(); |
| + |
| + private: |
| + friend class ModuleLoader; |
| + virtual void notifyFinished() = nullptr; |
| + |
| + // Called by ModuleLoader to catch up the client to the latest ModuleLoader state. |
| + virtual void catchUpToLatestStateIfNeeded(ModuleLoaderState); |
| + |
| + ModuleLoaderState m_state = ModuleLoaderState::Initial; |
| +}; |
| + |
| +// A ModuleLoader is responsible for loading a single module. |
| +// |
| +// A ModuleLoader constructs and emit FetchRequest to RequestFetcher (via |
| +// ScriptResource::fetch). |
| +// Then, it keeps track of the fetch progress by being a ScriptResourceClient. |
| +// |
| +// either a module script, null (used to represent failed fetches), or a |
| +// placeholder value "fetching". Module maps are used to ensure that imported |
| +// JavaScript modules are only fetched, parsed, and evaluated once per |
| +// Document or worker. |
| +class ModuleLoader final : public GarbageCollectedFinalized<ModuleLoader>, |
| + public ResourceOwner<ScriptResource> { |
| + WTF_MAKE_NONCOPYABLE(ModuleLoader); |
| + USING_GARBAGE_COLLECTED_MIXIN(ModuleLoader); |
| + |
| + public: |
| + static ModuleLoader* create(const KURL& url) { return new ModuleLoader(url); } |
| + |
| + ~ModuleLoader(); |
| + |
| + void fetch(ResourceFetcher* fetcher); |
| + bool isFetching() const { return m_state == State::Fetching; } |
| + bool isFinished() const { return m_state == State::Fetched || m_state == State::Failed; } |
| + |
| + // Run callbacks for the given client till current state, and register the |
| + // client to receive further callbacks if !isFinished() loading. |
| + void addClient(ModuleLoaderClient*); |
| + |
| + ScriptModule& moduleScript() { |
| + if (m_scriptModule.isNull()) |
| + return m_scriptModule; |
| + |
| + DCHECK_EQ(m_state, State::Fetched); |
| + return m_scriptModule; |
| + } |
| + |
| + DECLARE_TRACE(); |
| + |
| + private: |
| + ModuleLoader(const KURL& url); |
| + |
| + void advanceState(State newState); |
| + |
| + enum class FlushType : bool { |
| + NoStateChange, |
| + MayHaveAdvancedState |
| + }; |
| + |
| + // Ensure all clients (including those added during notification callbacks) receives notifications up to |
| + void notifyAndFlushPendingClients(); |
| + void addSinglePendingClient(); |
| + |
| + // Implements ScriptResourceClient |
| + void notifyFinished(Resource*) override; |
| + String debugName() const override { return "ModuleLoader"; } |
| + |
| + KURL m_url; |
| + State m_state; |
| + ScriptModule m_scriptModule; |
| + |
| + bool m_insideAddPendingClientsLoop = false; |
| + HeapHashSet<Member<ModuleLoaderClient>> m_clients; |
| + HeapHashSet<Member<ModuleLoaderClient>> m_pendingClients; |
| +}; |
| + |
| +class ModuleMap : public GarbageCollected<ModuleMap> { |
| + WTF_MAKE_NONCOPYABLE(ModuleMap); |
| + |
| + public: |
| + static ModuleMap* create(LocalFrame* frame, ResourceFetcher* fetcher) { |
| + return new ModuleMap(frame, fetcher); |
| + } |
| + DECLARE_TRACE(); |
| + |
| + // https://html.spec.whatwg.org/#fetch-a-single-module-script |
| + void fetch( |
| + const KURL& url, |
| + ModuleLoaderClient* client |
| + /*, a fetch client settings object, a destination, a cryptographic nonce, a parser state, a credentials mode, a module map settings object, a referrer, and a top-level module fetch flag*/); // => ScriptModule* |
| + |
| + private: |
| + ModuleMap(LocalFrame*, ResourceFetcher*); |
| + ModuleMap() = delete; |
| + |
| + using MapImpl = HeapHashMap<KURL, Member<ModuleLoader>>; |
| + |
| + // A module map is a map of absolute URLs to values |
| + MapImpl m_map; |
| + |
| + Member<LocalFrame> m_frame; |
| + Member<ResourceFetcher> m_fetcher; |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif |