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