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 #ifndef ModuleScriptLoader_h | 5 #ifndef ModuleScriptLoader_h |
6 #define ModuleScriptLoader_h | 6 #define ModuleScriptLoader_h |
7 | 7 |
8 #include "core/CoreExport.h" | 8 #include "core/CoreExport.h" |
| 9 #include "core/loader/modulescript/ModuleScriptCreationParams.h" |
9 #include "core/loader/modulescript/ModuleScriptFetchRequest.h" | 10 #include "core/loader/modulescript/ModuleScriptFetchRequest.h" |
10 #include "core/loader/resource/ScriptResource.h" | 11 #include "core/loader/modulescript/ModuleScriptFetcher.h" |
11 #include "platform/heap/Handle.h" | 12 #include "platform/heap/Handle.h" |
12 #include "platform/loader/fetch/ResourceOwner.h" | |
13 #include "platform/weborigin/KURL.h" | 13 #include "platform/weborigin/KURL.h" |
14 #include "public/platform/WebURLRequest.h" | 14 #include "public/platform/WebURLRequest.h" |
15 | 15 |
16 namespace blink { | 16 namespace blink { |
17 | 17 |
18 class Modulator; | 18 class Modulator; |
19 class ModuleScript; | 19 class ModuleScript; |
20 class ModuleScriptLoaderClient; | 20 class ModuleScriptLoaderClient; |
21 class ModuleScriptLoaderRegistry; | 21 class ModuleScriptLoaderRegistry; |
| 22 class ScriptResource; |
22 enum class ModuleGraphLevel; | 23 enum class ModuleGraphLevel; |
23 | 24 |
24 // A ModuleScriptLoader is responsible for loading a new single ModuleScript. | 25 // A ModuleScriptLoader is responsible for loading a new single ModuleScript. |
25 // | 26 // |
26 // A ModuleScriptLoader constructs and emits FetchParameters to ResourceFetcher | 27 // A ModuleScriptLoader constructs and emits FetchParameters to ResourceFetcher |
27 // (via ScriptResource::fetch). Then, it keeps track of the fetch progress by | 28 // (via ScriptResource::fetch). Then, it keeps track of the fetch progress by |
28 // being a ScriptResourceClient. Finally, it returns its client a compiled | 29 // being a ScriptResourceClient. Finally, it returns its client a compiled |
29 // ModuleScript. | 30 // ModuleScript. |
30 // | 31 // |
31 // ModuleScriptLoader(s) should only be used via Modulator and its ModuleMap. | 32 // ModuleScriptLoader(s) should only be used via Modulator and its ModuleMap. |
32 class CORE_EXPORT ModuleScriptLoader final | 33 class CORE_EXPORT ModuleScriptLoader final |
33 : public GarbageCollectedFinalized<ModuleScriptLoader>, | 34 : public GarbageCollectedFinalized<ModuleScriptLoader> { |
34 public ResourceOwner<ScriptResource> { | |
35 WTF_MAKE_NONCOPYABLE(ModuleScriptLoader); | 35 WTF_MAKE_NONCOPYABLE(ModuleScriptLoader); |
36 USING_GARBAGE_COLLECTED_MIXIN(ModuleScriptLoader); | |
37 | 36 |
38 enum class State { | 37 enum class State { |
39 kInitial, | 38 kInitial, |
40 // FetchParameters is being processed, and ModuleScriptLoader hasn't | 39 // FetchParameters is being processed, and ModuleScriptLoader hasn't |
41 // notifyFinished(). | 40 // notifyFinished(). |
42 kFetching, | 41 kFetching, |
43 // Finished successfully or w/ error. | 42 // Finished successfully or w/ error. |
44 kFinished, | 43 kFinished, |
45 }; | 44 }; |
46 | 45 |
47 public: | 46 public: |
48 static ModuleScriptLoader* Create(Modulator* modulator, | 47 static ModuleScriptLoader* Create(Modulator* modulator, |
49 ModuleScriptLoaderRegistry* registry, | 48 ModuleScriptLoaderRegistry* registry, |
50 ModuleScriptLoaderClient* client) { | 49 ModuleScriptLoaderClient* client) { |
51 return new ModuleScriptLoader(modulator, registry, client); | 50 return new ModuleScriptLoader(modulator, registry, client); |
52 } | 51 } |
53 | 52 |
54 ~ModuleScriptLoader() override; | 53 ~ModuleScriptLoader(); |
55 | 54 |
56 // Note: fetch may notify |m_client| synchronously or asynchronously. | |
57 void Fetch(const ModuleScriptFetchRequest&, | 55 void Fetch(const ModuleScriptFetchRequest&, |
58 ResourceFetcher*, | 56 ResourceFetcher*, |
59 ModuleGraphLevel); | 57 ModuleGraphLevel); |
60 | 58 |
| 59 void NotifyFetchFinished(WTF::Optional<ModuleScriptCreationParams>); |
| 60 |
61 bool IsInitialState() const { return state_ == State::kInitial; } | 61 bool IsInitialState() const { return state_ == State::kInitial; } |
62 bool HasFinished() const { return state_ == State::kFinished; } | 62 bool HasFinished() const { return state_ == State::kFinished; } |
63 | 63 |
64 DECLARE_TRACE(); | 64 DECLARE_TRACE(); |
65 | 65 |
66 private: | 66 private: |
67 ModuleScriptLoader(Modulator*, | 67 ModuleScriptLoader(Modulator*, |
68 ModuleScriptLoaderRegistry*, | 68 ModuleScriptLoaderRegistry*, |
69 ModuleScriptLoaderClient*); | 69 ModuleScriptLoaderClient*); |
70 | 70 |
71 void AdvanceState(State new_state); | 71 void AdvanceState(State new_state); |
72 #if DCHECK_IS_ON() | 72 #if DCHECK_IS_ON() |
73 static const char* StateToString(State); | 73 static const char* StateToString(State); |
74 #endif | 74 #endif |
75 | 75 |
76 // Implements ScriptResourceClient | |
77 void NotifyFinished(Resource*) override; | |
78 String DebugName() const override { return "ModuleScriptLoader"; } | |
79 | |
80 Member<Modulator> modulator_; | 76 Member<Modulator> modulator_; |
81 State state_ = State::kInitial; | 77 State state_ = State::kInitial; |
82 String nonce_; | 78 String nonce_; |
83 ParserDisposition parser_state_; | 79 ParserDisposition parser_state_; |
84 Member<ModuleScript> module_script_; | 80 Member<ModuleScript> module_script_; |
85 Member<ModuleScriptLoaderRegistry> registry_; | 81 Member<ModuleScriptLoaderRegistry> registry_; |
86 Member<ModuleScriptLoaderClient> client_; | 82 Member<ModuleScriptLoaderClient> client_; |
| 83 Member<ModuleScriptFetcher> module_fetcher_; |
87 #if DCHECK_IS_ON() | 84 #if DCHECK_IS_ON() |
88 KURL url_; | 85 KURL url_; |
89 #endif | 86 #endif |
90 }; | 87 }; |
91 | 88 |
92 } // namespace blink | 89 } // namespace blink |
93 | 90 |
94 #endif | 91 #endif |
OLD | NEW |