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/Modulator.h" | 5 #include "core/dom/Modulator.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
| 8 #include "bindings/core/v8/V8PerContextData.h" |
| 9 #include "core/dom/Document.h" |
| 10 #include "core/dom/ModulatorImpl.h" |
| 11 #include "core/frame/LocalFrame.h" |
8 | 12 |
9 namespace blink { | 13 namespace blink { |
10 | 14 |
| 15 Modulator::~Modulator() {} |
| 16 |
11 Modulator* Modulator::from(LocalFrame* frame) { | 17 Modulator* Modulator::from(LocalFrame* frame) { |
12 ScriptState* scriptState = ScriptState::forMainWorld(frame); | 18 ScriptState* scriptState = ScriptState::forMainWorld(frame); |
13 if (!scriptState) | 19 if (!scriptState) |
14 return nullptr; | 20 return nullptr; |
15 // TODO(kouhei): setModulator in V8PerContextData when we land ModulatorImpl. | 21 Modulator* modulator = scriptState->perContextData()->modulator(); |
16 return scriptState->perContextData()->modulator(); | 22 if (!modulator) { |
| 23 if (Document* document = frame->document()) { |
| 24 ScriptState* scriptState = ScriptState::forMainWorld(frame); |
| 25 modulator = ModulatorImpl::create(scriptState, *document); |
| 26 scriptState->perContextData()->setModulator(modulator); |
| 27 } |
| 28 } |
| 29 return modulator; |
17 } | 30 } |
18 | 31 |
19 KURL Modulator::resolveModuleSpecifier(const String& moduleRequest, | 32 KURL Modulator::resolveModuleSpecifier(const String& moduleRequest0, |
20 const KURL& baseURL) { | 33 const KURL& baseURL) { |
| 34 String moduleRequest = moduleRequest0.isolatedCopy(); |
| 35 if (!moduleRequest.endsWith(".js") && !moduleRequest.endsWith(".glsl")) { |
| 36 moduleRequest.append('.'); |
| 37 moduleRequest.append('j'); |
| 38 moduleRequest.append('s'); |
| 39 } |
| 40 if (moduleRequest.endsWith(".glsl")) { |
| 41 moduleRequest = |
| 42 KURL(KURL(), "data:text/javascript, export default 'glslsrc';"); |
| 43 } |
21 // Step 1. Apply the URL parser to specifier. If the result is not failure, | 44 // Step 1. Apply the URL parser to specifier. If the result is not failure, |
22 // return the result. | 45 // return the result. |
23 KURL url(KURL(), moduleRequest); | 46 KURL url(KURL(), moduleRequest); |
24 if (url.isValid()) | 47 if (url.isValid()) |
25 return url; | 48 return url; |
26 | 49 |
27 // Step 2. If specifier does not start with the character U+002F SOLIDUS (/), | 50 // Step 2. If specifier does not start with the character U+002F SOLIDUS (/), |
28 // the two-character sequence U+002E FULL STOP, U+002F SOLIDUS (./), or the | 51 // the two-character sequence U+002E FULL STOP, U+002F SOLIDUS (./), or the |
29 // three-character sequence U+002E FULL STOP, U+002E FULL STOP, U+002F SOLIDUS | 52 // three-character sequence U+002E FULL STOP, U+002E FULL STOP, U+002F SOLIDUS |
30 // (../), return failure and abort these steps. | 53 // (../), return failure and abort these steps. |
31 if (!moduleRequest.startsWith("/") && !moduleRequest.startsWith("./") && | 54 if (!moduleRequest.startsWith("/") && !moduleRequest.startsWith("./") && |
32 !moduleRequest.startsWith("../")) | 55 !moduleRequest.startsWith("../")) |
33 return KURL(); | 56 return KURL(); |
34 | 57 |
35 // Step 3. Return the result of applying the URL parser to specifier with | 58 // Step 3. Return the result of applying the URL parser to specifier with |
36 // script's base URL as the base URL. | 59 // script's base URL as the base URL. |
37 DCHECK(baseURL.isValid()); | 60 DCHECK(baseURL.isValid()); |
38 KURL absoluteURL(baseURL, moduleRequest); | 61 KURL absoluteURL(baseURL, moduleRequest); |
39 if (absoluteURL.isValid()) | 62 if (absoluteURL.isValid()) |
40 return absoluteURL; | 63 return absoluteURL; |
41 | 64 |
42 return KURL(); | 65 return KURL(); |
43 } | 66 } |
44 | 67 |
45 } // namespace blink | 68 } // namespace blink |
OLD | NEW |