Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef ModuleMap_h | |
| 6 #define ModuleMap_h | |
| 7 | |
| 8 #include "bindings/core/v8/ScriptModule.h" | |
| 9 #include "core/fetch/ResourceOwner.h" | |
| 10 #include "core/loader/resource/ScriptResource.h" | |
| 11 #include "platform/heap/Handle.h" | |
| 12 #include "platform/weborigin/KURL.h" | |
| 13 #include "platform/weborigin/KURLHash.h" | |
| 14 #include "wtf/HashMap.h" | |
| 15 #include "wtf/HashTableDeletedValueType.h" | |
| 16 | |
| 17 namespace blink { | |
| 18 | |
| 19 class ModuleLoader; | |
| 20 class ResourceFetcher; | |
| 21 class ScriptController; | |
| 22 class ModuleLoaderRegistry; | |
| 23 | |
| 24 // A ModuleLoader is responsible for loading a single ScriptModule. | |
| 25 // | |
| 26 // A ModuleLoader constructs and emit FetchRequest to RequestFetcher (via | |
| 27 // ScriptResource::fetch). | |
| 28 // Then, it keeps track of the fetch progress by being a ScriptResourceClient. | |
| 29 // | |
| 30 // 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
| |
| 31 // placeholder value "fetching". Module maps are used to ensure that imported | |
| 32 // JavaScript modules are only fetched, parsed, and evaluated once per | |
| 33 // Document or worker. | |
| 34 class ModuleLoader final : public GarbageCollectedFinalized<ModuleLoader>, | |
| 35 public ResourceOwner<ScriptResource> { | |
| 36 WTF_MAKE_NONCOPYABLE(ModuleLoader); | |
| 37 USING_GARBAGE_COLLECTED_MIXIN(ModuleLoader); | |
| 38 | |
| 39 enum class State { | |
| 40 Initial, | |
| 41 // FetchRequest is being processed, and ModuleLoader hasn't | |
| 42 // notifyFinished(). | |
| 43 Fetching, | |
| 44 // Finished successfully or w/ error. | |
| 45 Finished, | |
| 46 }; | |
| 47 | |
| 48 public: | |
| 49 class Client : public GarbageCollectedMixin { | |
| 50 private: | |
| 51 friend class ModuleLoader; | |
| 52 virtual void notifyFinished(ScriptModule) = 0; | |
| 53 }; | |
| 54 // Call via ModuleLoaderRegistry::fetch. | |
| 55 static ModuleLoader* fetch(const KURL&, | |
| 56 ScriptController*, | |
| 57 ResourceFetcher*, | |
| 58 ModuleLoaderRegistry*, | |
| 59 Client*); | |
| 60 | |
| 61 ~ModuleLoader() = default; | |
| 62 | |
| 63 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
| |
| 64 bool hasFinished() const { return m_state == State::Finished; } | |
| 65 | |
| 66 DECLARE_TRACE(); | |
| 67 | |
| 68 private: | |
| 69 explicit ModuleLoader(const KURL&, | |
| 70 ScriptController*, | |
| 71 ModuleLoaderRegistry*, | |
| 72 Client*); | |
| 73 | |
| 74 void fetchInternal(ResourceFetcher*); | |
| 75 | |
| 76 void advanceState(State newState); | |
| 77 | |
| 78 // Implements ScriptResourceClient | |
| 79 void notifyFinished(Resource*) override; | |
| 80 String debugName() const override { return "ModuleLoader"; } | |
| 81 | |
| 82 // TODO(kouhei): May be this method should live in ModuleResource? or should | |
| 83 // it be here to consolidate the spec impl code in ModuleMap.cpp? | |
| 84 static bool wasModuleLoadSuccessful(Resource*); | |
| 85 | |
| 86 KURL m_url; | |
| 87 Member<ScriptController> m_scriptController; | |
| 88 State m_state = State::Initial; | |
| 89 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.
| |
| 90 ScriptModule m_scriptModule; | |
| 91 Member<ModuleLoaderRegistry> m_registry; | |
| 92 Member<Client> m_client; | |
| 93 }; | |
| 94 | |
| 95 // ModuleLoaderRegistry keeps active ModuleLoaders alive. | |
| 96 class ModuleLoaderRegistry : public GarbageCollected<ModuleLoaderRegistry> { | |
| 97 public: | |
| 98 static ModuleLoaderRegistry* create() { return new ModuleLoaderRegistry; } | |
| 99 DECLARE_TRACE(); | |
| 100 | |
| 101 ModuleLoader* fetch(const KURL&, | |
| 102 ScriptController*, | |
| 103 ResourceFetcher*, | |
| 104 ModuleLoader::Client*); | |
| 105 | |
| 106 private: | |
| 107 ModuleLoaderRegistry() = default; | |
| 108 | |
| 109 friend class ModuleLoader; | |
| 110 void releaseFinishedLoader(ModuleLoader*); | |
| 111 | |
| 112 HeapHashSet<Member<ModuleLoader>> m_activeLoaders; | |
| 113 }; | |
| 114 | |
| 115 class ModuleMap : public GarbageCollected<ModuleMap> { | |
| 116 WTF_MAKE_NONCOPYABLE(ModuleMap); | |
| 117 class Entry; | |
| 118 class LoaderHost; | |
| 119 | |
| 120 public: | |
| 121 class Client : public GarbageCollectedMixin { | |
| 122 private: | |
| 123 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
| |
| 124 virtual void notifyFinished(ScriptModule) = 0; | |
| 125 }; | |
| 126 | |
| 127 static ModuleMap* create(ScriptController* controller, | |
| 128 ResourceFetcher* fetcher) { | |
| 129 return new ModuleMap(controller, fetcher); | |
| 130 } | |
| 131 DECLARE_TRACE(); | |
| 132 | |
| 133 // https://html.spec.whatwg.org/#fetch-a-single-module-script | |
| 134 void fetchSingle( | |
| 135 const KURL&, | |
| 136 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
| |
| 137 /*, a fetch client settings object, a destination, a cryptographic nonce, a parser state, a credentials mode, a module map settings object, a referrer, an d a top-level module fetch flag*/); | |
| 138 | |
| 139 private: | |
| 140 ModuleMap(ScriptController*, ResourceFetcher*); | |
| 141 ModuleMap() = delete; | |
| 142 | |
| 143 using MapImpl = HeapHashMap<KURL, Member<Entry>>; | |
| 144 | |
| 145 // A module map is a map of absolute URLs to map entry. | |
| 146 MapImpl m_map; | |
| 147 | |
| 148 // Below should be moved to the modulator. | |
| 149 Member<ScriptController> m_scriptController; | |
| 150 Member<ResourceFetcher> m_fetcher; | |
| 151 Member<ModuleLoaderRegistry> m_loaderRegistry; | |
| 152 }; | |
| 153 | |
| 154 } // namespace blink | |
| 155 | |
| 156 #endif | |
| OLD | NEW |