Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 #include "core/dom/ModuleMap.h" | |
| 6 | |
| 7 #include "core/dom/Modulator.h" | |
| 8 #include "core/dom/ModuleScript.h" | |
| 9 #include "core/dom/ScriptModuleResolver.h" | |
| 10 #include "core/loader/modulescript/ModuleScriptFetchRequest.h" | |
| 11 #include "core/loader/modulescript/ModuleScriptLoaderClient.h" | |
| 12 #include "platform/WebTaskRunner.h" | |
| 13 | |
| 14 namespace blink { | |
| 15 | |
| 16 // Entry struct represents a value in "module map" spec object. | |
| 17 // https://html.spec.whatwg.org/multipage/webappapis.html#module-map | |
| 18 class ModuleMap::Entry final : public GarbageCollectedFinalized<Entry>, | |
| 19 public ModuleScriptLoaderClient { | |
| 20 USING_GARBAGE_COLLECTED_MIXIN(ModuleMap::Entry); | |
| 21 | |
| 22 public: | |
| 23 static Entry* create(ModuleMap* map) { return new Entry(map); } | |
| 24 ~Entry() override {} | |
| 25 | |
| 26 DECLARE_TRACE(); | |
| 27 | |
| 28 // Notify fetched |m_moduleScript| to the client asynchronously. | |
| 29 void addClient(SingleModuleClient*); | |
| 30 | |
| 31 // This is only to be used from ScriptModuleResolver implementations. | |
| 32 ModuleScript* getModuleScript() const; | |
| 33 | |
| 34 private: | |
| 35 Entry(ModuleMap*); | |
|
yhirano
2017/02/22 09:10:42
+explicit
kouhei (in TOK)
2017/02/24 02:10:31
Done.
| |
| 36 | |
| 37 void dispatchFinishedNotificationAsync(SingleModuleClient*); | |
| 38 | |
| 39 // Implements ModuleScriptLoaderClient | |
| 40 void notifyNewSingleModuleFinished(ModuleScript*) override; | |
| 41 | |
| 42 Member<ModuleScript> m_moduleScript; | |
| 43 Member<ModuleMap> m_map; | |
| 44 | |
| 45 // Correspond to the HTML spec: "fetching" state. | |
| 46 bool m_isFetching = true; | |
| 47 | |
| 48 HeapHashSet<Member<SingleModuleClient>> m_clients; | |
| 49 }; | |
| 50 | |
| 51 ModuleMap::Entry::Entry(ModuleMap* map) : m_map(map) { | |
| 52 DCHECK(m_map); | |
| 53 } | |
| 54 | |
| 55 DEFINE_TRACE(ModuleMap::Entry) { | |
| 56 visitor->trace(m_moduleScript); | |
| 57 visitor->trace(m_map); | |
| 58 visitor->trace(m_clients); | |
| 59 } | |
| 60 | |
| 61 void ModuleMap::Entry::dispatchFinishedNotificationAsync( | |
| 62 SingleModuleClient* client) { | |
| 63 m_map->modulator()->taskRunner()->postTask( | |
| 64 BLINK_FROM_HERE, | |
| 65 WTF::bind(&SingleModuleClient::notifyModuleLoadFinished, | |
| 66 wrapPersistent(client), wrapPersistent(m_moduleScript.get()))); | |
| 67 } | |
| 68 | |
| 69 void ModuleMap::Entry::addClient(SingleModuleClient* newClient) { | |
| 70 DCHECK(!m_clients.contains(newClient)); | |
| 71 if (!m_isFetching) { | |
| 72 DCHECK(m_clients.isEmpty()); | |
| 73 dispatchFinishedNotificationAsync(newClient); | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 m_clients.insert(newClient); | |
| 78 } | |
| 79 | |
| 80 void ModuleMap::Entry::notifyNewSingleModuleFinished( | |
| 81 ModuleScript* moduleScript) { | |
| 82 CHECK(m_isFetching); | |
| 83 m_moduleScript = moduleScript; | |
| 84 m_isFetching = false; | |
| 85 | |
| 86 if (m_moduleScript) { | |
| 87 m_map->modulator()->scriptModuleResolver()->registerModuleScript( | |
| 88 m_moduleScript); | |
| 89 } | |
| 90 | |
| 91 for (const auto& client : m_clients) { | |
| 92 dispatchFinishedNotificationAsync(client); | |
| 93 } | |
| 94 m_clients.clear(); | |
| 95 } | |
| 96 | |
| 97 ModuleScript* ModuleMap::Entry::getModuleScript() const { | |
| 98 DCHECK(!m_isFetching); | |
| 99 return m_moduleScript.get(); | |
| 100 } | |
| 101 | |
| 102 ModuleMap::ModuleMap(Modulator* modulator) : m_modulator(modulator) { | |
| 103 DCHECK(modulator); | |
| 104 } | |
| 105 | |
| 106 DEFINE_TRACE(ModuleMap) { | |
| 107 visitor->trace(m_map); | |
| 108 visitor->trace(m_modulator); | |
| 109 } | |
| 110 | |
| 111 void ModuleMap::fetchSingleModuleScript(const ModuleScriptFetchRequest& request, | |
| 112 ModuleGraphLevel level, | |
| 113 SingleModuleClient* client) { | |
| 114 // https://html.spec.whatwg.org/#fetch-a-single-module-script | |
| 115 | |
| 116 // Step 1. Let moduleMap be module map settings object's module map. | |
| 117 // Note: This is the ModuleMap. | |
| 118 | |
| 119 // Step 2. If moduleMap[url] is "fetching", wait in parallel until that | |
| 120 // entry's value changes, then queue a task on the networking task source to | |
| 121 // proceed with running the following steps. | |
| 122 MapImpl::AddResult result = m_map.insert(request.url(), nullptr); | |
| 123 Member<Entry>& entry = result.storedValue->value; | |
| 124 if (result.isNewEntry) { | |
| 125 entry = Entry::create(this); | |
| 126 | |
| 127 // Steps 4-9 loads a new single module script. | |
| 128 // Delegates to ModuleScriptLoader via Modulator. | |
| 129 m_modulator->fetchNewSingleModule(request, level, entry); | |
| 130 } | |
| 131 DCHECK(entry); | |
| 132 | |
| 133 // Step 3. If moduleMap[url] exists, asynchronously complete this algorithm | |
| 134 // with moduleMap[url], and abort these steps. | |
| 135 // Step 10. Set moduleMap[url] to module script, and asynchronously complete | |
| 136 // this algorithm with module script. | |
| 137 entry->addClient(client); | |
| 138 } | |
| 139 | |
| 140 ModuleScript* ModuleMap::getFetchedModuleScript(const KURL& url) const { | |
| 141 MapImpl::const_iterator it = m_map.find(url); | |
| 142 CHECK_NE(it, m_map.end()); | |
| 143 return it->value->getModuleScript(); | |
| 144 } | |
| 145 | |
| 146 } // namespace blink | |
| OLD | NEW |