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

Side by Side Diff: third_party/WebKit/Source/core/dom/ModuleMap.cpp

Issue 2555653002: [WIP Prototype] ES6 https://html.spec.whatwg.org/#fetch-a-single-module-script implementation (Closed)
Patch Set: snapshot Created 3 years, 10 months 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 unified diff | Download patch
OLDNEW
(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 class ModuleMap::Entry : public GarbageCollectedFinalized<Entry>,
18 public ModuleScriptLoaderClient {
19 USING_GARBAGE_COLLECTED_MIXIN(ModuleMap::Entry);
20
21 public:
22 static Entry* create(ModuleMap* map) { return new Entry(map); }
23 virtual ~Entry() {}
24
25 DECLARE_TRACE();
26
27 // Asynchronously notify the client fetched |m_moduleScript|, or register the
28 // client to receive further callbacks if |m_isFetching|.
29 void addClient(SingleModuleClient*);
30
31 // This is only to be used from ScriptModuleResolver implementations.
32 ModuleScript* retrieveModuleScriptSync() const;
33
34 private:
35 Entry(ModuleMap*);
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 m_moduleScript = moduleScript;
83 m_isFetching = false;
84
85 if (m_moduleScript) {
86 m_map->modulator()->scriptModuleResolver()->registerModuleScript(
87 m_moduleScript);
88 }
89
90 for (const auto& client : m_clients) {
91 dispatchFinishedNotificationAsync(client);
92 }
93 m_clients.clear();
94 }
95
96 ModuleScript* ModuleMap::Entry::retrieveModuleScriptSync() const {
97 DCHECK(!m_isFetching);
98 return m_moduleScript.get();
99 }
100
101 ModuleMap::ModuleMap(Modulator* modulator) : m_modulator(modulator) {
102 DCHECK(modulator);
103 }
104
105 DEFINE_TRACE(ModuleMap) {
106 visitor->trace(m_map);
107 visitor->trace(m_modulator);
108 }
109
110 void ModuleMap::retrieveAndFetchIfNeeded(
111 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 // Delegate 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::retrieveFetchedModuleScript(const KURL& url) {
141 MapImpl::iterator it = m_map.find(url);
142 CHECK_NE(it, m_map.end());
143 return it->value->retrieveModuleScriptSync();
144 }
145
146 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/dom/ModuleMap.h ('k') | third_party/WebKit/Source/core/dom/ModuleMapTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698