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..8008875f8b693236eb74d71d0b6bb72770865ff3 |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/dom/ModuleMap.h |
@@ -0,0 +1,172 @@ |
+// 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/fetch/ResourceOwner.h" |
+#include "core/loader/resource/ScriptResource.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 ModuleLoader; |
+class ResourceFetcher; |
+class ScriptController; |
+ |
+enum class ModuleLoaderState { |
+ Initial, |
+ // FetchRequest is being processed, and ModuleLoader hasn't notifyFinished(). |
+ Fetching, |
dominicc (has gone to gerrit)
2016/12/13 07:45:29
Which client needs this state? I wonder if exposin
hiroshige
2016/12/14 23:24:22
I also think not exposing ModuleLoaderState is sim
kouhei (in TOK)
2016/12/15 04:56:27
Done.
|
+ // Finished successfully or w/ error. |
+ Finished, |
+}; |
+ |
+class ModuleLoaderClient : public GarbageCollectedMixin { |
+ public: |
+ DEFINE_INLINE_VIRTUAL_TRACE() { visitor->trace(m_loader); } |
+ |
+ protected: |
+ ModuleLoaderClient(){}; |
+ virtual ~ModuleLoaderClient(); |
+ |
+ ModuleLoader* loader() { return m_loader.get(); } |
+ void dispose() { m_loader = nullptr; } |
+ |
+ private: |
+ friend class ModuleLoader; |
+ void setLoader(ModuleLoader* loader) { |
+ DCHECK(!m_loader); |
+ m_loader = loader; |
+ } |
+ virtual void notifyFinished() = 0; |
+ |
+ // Called by ModuleLoader to catch up the client to the latest ModuleLoader |
+ // ModuleLoaderState. |
+ virtual void catchUpToLatestStateIfNeeded(ModuleLoaderState); |
+ |
+ Member<ModuleLoader> m_loader; |
+ ModuleLoaderState m_state = ModuleLoaderState::Initial; |
+}; |
+ |
+// A ModuleLoader is responsible for loading a single ScriptModule. |
+// |
+// 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* fetch(const KURL&, ScriptController*, ResourceFetcher*); |
+ |
+ ~ModuleLoader(); |
+ |
+ // TODO(kouhei): This should be static and return ModuleLoader* |
+ void fetch(ResourceFetcher*); |
+ bool isFetching() const { return m_state == ModuleLoaderState::Fetching; } |
+ bool hasFinished() const { return m_state == ModuleLoaderState::Finished; } |
+ bool hasFinishedSuccessfully() const { |
+ if (m_wasLoadSuccessful) { |
+ DCHECK_EQ(m_state, ModuleLoaderState::Finished); |
+ return true; |
+ } |
+ return false; |
+ } |
+ |
+ // Run callbacks for the given client till current state, and register the |
+ // client to receive further callbacks if !isFinished() loading. |
+ void addClient(ModuleLoaderClient*); |
+ |
+ ScriptModule& scriptModule() { |
+ if (m_scriptModule.isNull()) |
+ return m_scriptModule; |
+ |
+ DCHECK(hasFinishedSuccessfully()); |
+ return m_scriptModule; |
+ } |
+ |
+ DECLARE_TRACE(); |
+ |
+ private: |
+ explicit ModuleLoader(const KURL&, ScriptController*); |
+ |
+ void fetchInternal(ResourceFetcher*); |
+ |
+ void advanceState(ModuleLoaderState newState); |
+ |
+ // Ensure all clients (including those added during notification callbacks) |
+ // receives notifications up to m_state. |
+ void notifyAndFlushPendingClients(); |
+ void addSinglePendingClient(); |
+ |
+ // Implements ScriptResourceClient |
+ void notifyFinished(Resource*) override; |
+ String debugName() const override { return "ModuleLoader"; } |
+ |
+ // TODO(kouhei): May be this method should live in ModuleResource? or should |
+ // it be here to consolidate the spec impl code in ModuleMap.cpp? |
+ static bool wasModuleLoadSuccessful(Resource*); |
+ |
+ KURL m_url; |
+ Member<ScriptController> m_scriptController; |
+ ModuleLoaderState m_state = ModuleLoaderState::Initial; |
+ bool m_wasLoadSuccessful = true; |
+ ScriptModule m_scriptModule; |
+ |
+ // TODO(kouhei): Isolate below and their related logic to |
+ // ModuleLoaderClientRegistry. |
+ |
+ bool m_isFlushingPendingClients = false; |
+ // m_lastClientNotifiedState should be only modified within |
+ // notifyAndFlushPendingClients(). |
+ ModuleLoaderState m_lastClientNotifiedState = ModuleLoaderState::Initial; |
+ HeapHashSet<Member<ModuleLoaderClient>> m_clients; |
+ HeapHashSet<Member<ModuleLoaderClient>> m_pendingClients; |
hiroshige
2016/12/14 23:24:22
Can this client management much simpler? These lin
kouhei (in TOK)
2016/12/15 04:56:27
Chatted offline. PTAL at PS6. Do you think it is s
|
+}; |
+ |
+class ModuleMap : public GarbageCollected<ModuleMap> { |
+ WTF_MAKE_NONCOPYABLE(ModuleMap); |
+ |
+ public: |
+ static ModuleMap* create(ScriptController* controller, |
+ ResourceFetcher* fetcher) { |
+ return new ModuleMap(controller, fetcher); |
+ } |
+ DECLARE_TRACE(); |
+ |
+ // https://html.spec.whatwg.org/#fetch-a-single-module-script |
+ void fetchSingle( |
+ const KURL&, |
+ ModuleLoaderClient* |
+ /*, 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*/); |
+ |
+ private: |
+ ModuleMap(ScriptController*, ResourceFetcher*); |
+ ModuleMap() = delete; |
+ |
+ using MapImpl = HeapHashMap<KURL, Member<ModuleLoader>>; |
hiroshige
2016/12/14 23:24:22
Conceptually, I think the value should be a simple
kouhei (in TOK)
2016/12/15 04:56:27
Agreed. Will address in the next PS
kouhei (in TOK)
2016/12/19 04:49:40
Done.
|
+ |
+ // A module map is a map of absolute URLs to values |
+ MapImpl m_map; |
+ |
+ Member<ScriptController> m_scriptController; |
+ Member<ResourceFetcher> m_fetcher; |
+}; |
+ |
+} // namespace blink |
+ |
+#endif |