Commit 0b0bbe7e authored by dpapad's avatar dpapad Committed by Commit Bot

Support chrome-extension:// URLs in polymer_modulizer().

With this CL, HTML imports inovlving chrome-extension:// URLs are
correctly translated to JS imports. For example

chrome-extension://path/to/folder/foo.html becomes
chrome-extension://path/to/folder/foo.m.js

This is necessary to unblock CrOS FileManager migration to JS
modules.

Fixed: 1145876
Change-Id: Ie2e02b6925108db280f626be9c1ad0c261ffd797
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2526501
Commit-Queue: dpapad <dpapad@chromium.org>
Reviewed-by: default avatarLuciano Pacheco <lucmult@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825755}
parent 7091ff7b
......@@ -86,8 +86,6 @@ _preserve_url_scheme = False
# Use an OrderedDict, since the order these redirects are applied matters.
_chrome_redirects = OrderedDict([
('chrome://resources/polymer/v1_0/', POLYMER_V1_DIR),
('chrome://resources/', 'ui/webui/resources/'),
('//resources/polymer/v1_0/', POLYMER_V1_DIR),
('//resources/', 'ui/webui/resources/'),
])
......@@ -105,7 +103,7 @@ _chrome_reverse_redirects = {
# 1) normalized, meaning converted from a chrome or scheme-relative or relative
# URL to to an absolute path starting at the repo's root
# 2) converted to an equivalent JS normalized path
# 3) de-normalized, meaning converted back to a chrome or scheme-relative or
# 3) de-normalized, meaning converted back to a scheme or scheme-relative or
# relative URL
# 4) converted to a JS import statement
class Dependency:
......@@ -114,7 +112,11 @@ class Dependency:
self.html_path = dst
if self.html_path.startswith('chrome://'):
self.input_format = 'chrome'
self.input_format = 'scheme'
self.input_scheme = 'chrome'
elif self.html_path.startswith('chrome-extension://'):
self.input_format = 'scheme'
self.input_scheme = 'chrome-extension'
elif self.html_path.startswith('//'):
self.input_format = 'scheme-relative'
else:
......@@ -126,12 +128,18 @@ class Dependency:
self.js_path = self._to_js()
def _to_html_normalized(self):
if self.input_format == 'chrome' or self.input_format == 'scheme-relative':
if self.input_format == 'scheme' or self.input_format == 'scheme-relative':
self.html_path_normalized = self.html_path
if self.input_format == 'scheme':
# Strip the URL scheme.
colon_index = self.html_path_normalized.find(':')
self.html_path_normalized = self.html_path_normalized[colon_index + 1:]
for r in _chrome_redirects:
if self.html_path.startswith(r):
self.html_path_normalized = (
self.html_path.replace(r, _chrome_redirects[r]))
if self.html_path_normalized.startswith(r):
self.html_path_normalized = (self.html_path_normalized.replace(
r, _chrome_redirects[r]))
break
return self.html_path_normalized
......@@ -147,7 +155,8 @@ class Dependency:
if self.html_path_normalized == 'ui/webui/resources/html/polymer.html':
if self.output_format == 'relative':
self.output_format = 'chrome'
self.output_format = 'scheme'
self.input_scheme = 'chrome'
return POLYMER_V3_DIR + 'polymer/polymer_bundled.min.js'
if re.match(r'ui/webui/resources/html/', self.html_path_normalized):
......@@ -162,16 +171,16 @@ class Dependency:
def _to_js(self):
js_path = self.js_path_normalized
if self.output_format == 'chrome' or self.output_format == 'scheme-relative':
if self.output_format == 'scheme' or self.output_format == 'scheme-relative':
for r in _chrome_reverse_redirects:
if self.js_path_normalized.startswith(r):
js_path = self.js_path_normalized.replace(
r, _chrome_reverse_redirects[r])
break
# Restore the chrome:// scheme if |preserve_url_scheme| is enabled.
if _preserve_url_scheme and self.output_format == 'chrome':
js_path = "chrome:" + js_path
# Restore the original scheme if |preserve_url_scheme| is enabled.
if _preserve_url_scheme and self.output_format == 'scheme':
js_path = self.input_scheme + ":" + js_path
return js_path
assert self.output_format == 'relative'
......
......@@ -208,6 +208,13 @@ class PolymerModulizerTest(unittest.TestCase):
'import \'chrome://resources/js/bar.m.js\';',
],
# chrome-extension:// paths cases.
[
'chrome-extension://path/to/folder/foo.html',
'import \'//path/to/folder/foo.m.js\';',
'import \'chrome-extension://path/to/folder/foo.m.js\';',
],
# Scheme-relative paths cases.
# Case where absolute path to a Polymer UI element is used.
[
......
......@@ -172,6 +172,7 @@ js_library("repeat_button.m") {
js_type_check("closure_compile_jsmodules") {
is_polymer3 = true
closure_flags = default_closure_args + [ "browser_resolver_prefix_replacements=\"chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/=../../ui/file_manager/file_manager/\"" ]
deps = [
":audio_player.m",
":control_panel.m",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment