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/loader/resource/ScriptResource.h" | |
10 #include "core/fetch/ResourceOwner.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 ResourceFetcher; | |
20 class ModuleLoader; | |
21 | |
22 enum class ModuleLoaderState { | |
23 Initial, | |
24 Fetching, | |
25 Fetched, | |
kouhei (in TOK)
2016/12/12 07:38:51
TODO: Fetched, Failed -> Complete or Finished and
| |
26 Failed, | |
27 }; | |
28 | |
29 class ModuleLoaderClient : public GarbageCollectedMixin { | |
30 public: | |
31 DEFINE_INLINE_VIRTUAL_TRACE() {} | |
32 | |
33 protected: | |
34 ModuleLoaderClient() {}; | |
35 virtual ~ModuleLoaderClient(); | |
36 | |
37 private: | |
38 friend class ModuleLoader; | |
39 virtual void notifyFinished() = nullptr; | |
40 | |
41 // Called by ModuleLoader to catch up the client to the latest ModuleLoader st ate. | |
42 virtual void catchUpToLatestStateIfNeeded(ModuleLoaderState); | |
43 | |
44 ModuleLoaderState m_state = ModuleLoaderState::Initial; | |
45 }; | |
46 | |
47 // A ModuleLoader is responsible for loading a single module. | |
48 // | |
49 // A ModuleLoader constructs and emit FetchRequest to RequestFetcher (via | |
50 // ScriptResource::fetch). | |
51 // Then, it keeps track of the fetch progress by being a ScriptResourceClient. | |
52 // | |
53 // either a module script, null (used to represent failed fetches), or a | |
54 // placeholder value "fetching". Module maps are used to ensure that imported | |
55 // JavaScript modules are only fetched, parsed, and evaluated once per | |
56 // Document or worker. | |
57 class ModuleLoader final : public GarbageCollectedFinalized<ModuleLoader>, | |
58 public ResourceOwner<ScriptResource> { | |
59 WTF_MAKE_NONCOPYABLE(ModuleLoader); | |
60 USING_GARBAGE_COLLECTED_MIXIN(ModuleLoader); | |
61 | |
62 public: | |
63 static ModuleLoader* create(const KURL& url) { return new ModuleLoader(url); } | |
64 | |
65 ~ModuleLoader(); | |
66 | |
67 void fetch(ResourceFetcher* fetcher); | |
68 bool isFetching() const { return m_state == State::Fetching; } | |
69 bool isFinished() const { return m_state == State::Fetched || m_state == State ::Failed; } | |
70 | |
71 // Run callbacks for the given client till current state, and register the | |
72 // client to receive further callbacks if !isFinished() loading. | |
73 void addClient(ModuleLoaderClient*); | |
74 | |
75 ScriptModule& moduleScript() { | |
76 if (m_scriptModule.isNull()) | |
77 return m_scriptModule; | |
78 | |
79 DCHECK_EQ(m_state, State::Fetched); | |
80 return m_scriptModule; | |
81 } | |
82 | |
83 DECLARE_TRACE(); | |
84 | |
85 private: | |
86 ModuleLoader(const KURL& url); | |
87 | |
88 void advanceState(State newState); | |
89 | |
90 enum class FlushType : bool { | |
91 NoStateChange, | |
92 MayHaveAdvancedState | |
93 }; | |
94 | |
95 // Ensure all clients (including those added during notification callbacks) re ceives notifications up to | |
96 void notifyAndFlushPendingClients(); | |
97 void addSinglePendingClient(); | |
98 | |
99 // Implements ScriptResourceClient | |
100 void notifyFinished(Resource*) override; | |
101 String debugName() const override { return "ModuleLoader"; } | |
102 | |
103 KURL m_url; | |
104 State m_state; | |
105 ScriptModule m_scriptModule; | |
106 | |
107 bool m_insideAddPendingClientsLoop = false; | |
108 HeapHashSet<Member<ModuleLoaderClient>> m_clients; | |
109 HeapHashSet<Member<ModuleLoaderClient>> m_pendingClients; | |
110 }; | |
111 | |
112 class ModuleMap : public GarbageCollected<ModuleMap> { | |
113 WTF_MAKE_NONCOPYABLE(ModuleMap); | |
114 | |
115 public: | |
116 static ModuleMap* create(LocalFrame* frame, ResourceFetcher* fetcher) { | |
117 return new ModuleMap(frame, fetcher); | |
118 } | |
119 DECLARE_TRACE(); | |
120 | |
121 // https://html.spec.whatwg.org/#fetch-a-single-module-script | |
122 void fetch( | |
123 const KURL& url, | |
124 ModuleLoaderClient* client | |
125 /*, 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*/); // => ScriptModule* | |
126 | |
127 private: | |
128 ModuleMap(LocalFrame*, ResourceFetcher*); | |
129 ModuleMap() = delete; | |
130 | |
131 using MapImpl = HeapHashMap<KURL, Member<ModuleLoader>>; | |
132 | |
133 // A module map is a map of absolute URLs to values | |
134 MapImpl m_map; | |
135 | |
136 Member<LocalFrame> m_frame; | |
137 Member<ResourceFetcher> m_fetcher; | |
138 }; | |
139 | |
140 } // namespace blink | |
141 | |
142 #endif | |
OLD | NEW |