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

Unified Diff: third_party/WebKit/Source/core/dom/ModuleMap.h

Issue 2555653002: [WIP Prototype] ES6 https://html.spec.whatwg.org/#fetch-a-single-module-script implementation (Closed)
Patch Set: snapshot Dec19 Created 4 years 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/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..7616b2e36fa165c7ee4edc2f08250b51cb034b7d
--- /dev/null
+++ b/third_party/WebKit/Source/core/dom/ModuleMap.h
@@ -0,0 +1,156 @@
+// 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;
+class ModuleLoaderRegistry;
+
+// 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
dominicc (has gone to gerrit) 2016/12/19 10:07:51 still some disfluency here
+// 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);
+
+ enum class State {
+ Initial,
+ // FetchRequest is being processed, and ModuleLoader hasn't
+ // notifyFinished().
+ Fetching,
+ // Finished successfully or w/ error.
+ Finished,
+ };
+
+ public:
+ class Client : public GarbageCollectedMixin {
+ private:
+ friend class ModuleLoader;
+ virtual void notifyFinished(ScriptModule) = 0;
+ };
+ // Call via ModuleLoaderRegistry::fetch.
+ static ModuleLoader* fetch(const KURL&,
+ ScriptController*,
+ ResourceFetcher*,
+ ModuleLoaderRegistry*,
+ Client*);
+
+ ~ModuleLoader() = default;
+
+ bool isFetching() const { return m_state == State::Fetching; }
dominicc (has gone to gerrit) 2016/12/19 10:07:51 Why not just have a state() accessor?
kouhei (in TOK) 2016/12/19 11:19:59 I'm not sure if we want to expose all internal sta
+ bool hasFinished() const { return m_state == State::Finished; }
+
+ DECLARE_TRACE();
+
+ private:
+ explicit ModuleLoader(const KURL&,
+ ScriptController*,
+ ModuleLoaderRegistry*,
+ Client*);
+
+ void fetchInternal(ResourceFetcher*);
+
+ void advanceState(State newState);
+
+ // 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;
+ State m_state = State::Initial;
+ bool m_wasLoadSuccessful = true;
dominicc (has gone to gerrit) 2016/12/19 10:07:51 Maybe it's more robust to represent this in m_stat
kouhei (in TOK) 2016/12/19 11:19:59 This flag is no longer used. Let me remove this fl
kouhei (in TOK) 2016/12/20 10:47:35 Done.
+ ScriptModule m_scriptModule;
+ Member<ModuleLoaderRegistry> m_registry;
+ Member<Client> m_client;
+};
+
+// ModuleLoaderRegistry keeps active ModuleLoaders alive.
+class ModuleLoaderRegistry : public GarbageCollected<ModuleLoaderRegistry> {
+ public:
+ static ModuleLoaderRegistry* create() { return new ModuleLoaderRegistry; }
+ DECLARE_TRACE();
+
+ ModuleLoader* fetch(const KURL&,
+ ScriptController*,
+ ResourceFetcher*,
+ ModuleLoader::Client*);
+
+ private:
+ ModuleLoaderRegistry() = default;
+
+ friend class ModuleLoader;
+ void releaseFinishedLoader(ModuleLoader*);
+
+ HeapHashSet<Member<ModuleLoader>> m_activeLoaders;
+};
+
+class ModuleMap : public GarbageCollected<ModuleMap> {
+ WTF_MAKE_NONCOPYABLE(ModuleMap);
+ class Entry;
+ class LoaderHost;
+
+ public:
+ class Client : public GarbageCollectedMixin {
+ private:
+ friend class ModuleMap::Entry;
dominicc (has gone to gerrit) 2016/12/19 10:07:51 Inline module scripts aren't entered into the map!
kouhei (in TOK) 2016/12/19 11:20:00 Ack. Let this be Modulator::Client after ModuleMap
+ virtual void notifyFinished(ScriptModule) = 0;
+ };
+
+ 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&,
+ Client*
dominicc (has gone to gerrit) 2016/12/19 10:07:51 Yay. I guess we shouldn't just use a callback beca
kouhei (in TOK) 2016/12/19 11:20:00 Callback supports Oilpan, but via Persistent, whic
+ /*, 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<Entry>>;
+
+ // A module map is a map of absolute URLs to map entry.
+ MapImpl m_map;
+
+ // Below should be moved to the modulator.
+ Member<ScriptController> m_scriptController;
+ Member<ResourceFetcher> m_fetcher;
+ Member<ModuleLoaderRegistry> m_loaderRegistry;
+};
+
+} // namespace blink
+
+#endif

Powered by Google App Engine
This is Rietveld 408576698