Commit 59fcc98d authored by Ken Rockot's avatar Ken Rockot Committed by Commit Bot

Fix closure compilation for WebUI + mojo modules

Several WebUI pages have recently been migrated to mojo JS modules. In
the process, full closure type checking was inadvertently disabled due
to incorrect use of js_type_check closure_flags.

This re-enables full type checking for the affected pages and fixes any
existing flaws revealed as a result

Bug: 1004256
Change-Id: If11808a64501606a784495baeb2543cee33daf0f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2518089
Commit-Queue: Ken Rockot <rockot@google.com>
Reviewed-by: default avatardpapad <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824164}
parent 68c3c49e
...@@ -7,6 +7,8 @@ import("//tools/polymer/polymer.gni") ...@@ -7,6 +7,8 @@ import("//tools/polymer/polymer.gni")
js_type_check("closure_compile") { js_type_check("closure_compile") {
is_polymer3 = true is_polymer3 = true
closure_flags = default_closure_args + mojom_js_args +
[ "js_module_root=" + rebase_path(".", root_build_dir) ]
deps = [ deps = [
":database_tab", ":database_tab",
":discards", ":discards",
...@@ -15,7 +17,6 @@ js_type_check("closure_compile") { ...@@ -15,7 +17,6 @@ js_type_check("closure_compile") {
":graph_doc", ":graph_doc",
":graph_tab_template", ":graph_tab_template",
] ]
closure_flags = mojom_js_args
} }
js_library("discards") { js_library("discards") {
......
...@@ -12,6 +12,15 @@ import string ...@@ -12,6 +12,15 @@ import string
import sys import sys
def strip_js_imports(js_contents):
"""The input JS may use imports for Closure compilation. These must be
stripped from the output since the resulting data: URL cannot use imports
within its webview."""
def not_an_import(line):
return not line.startswith('import ')
return '\n'.join(filter(not_an_import, js_contents.splitlines()))
def main(): def main():
argument_parser = argparse.ArgumentParser() argument_parser = argparse.ArgumentParser()
argument_parser.add_argument('output_file', help='The file to write to') argument_parser.add_argument('output_file', help='The file to write to')
...@@ -27,7 +36,8 @@ def main(): ...@@ -27,7 +36,8 @@ def main():
output_template = string.Template(open(args.output_template, 'r').read()); output_template = string.Template(open(args.output_template, 'r').read());
# Stamp the javacript contents into the HTML template. # Stamp the javacript contents into the HTML template.
html_doc = html_template.substitute({'javascript_file': js_file_contents}); html_doc = html_template.substitute(
{'javascript_file': strip_js_imports(js_file_contents)})
# Construct the data: URL that contains the combined doc. # Construct the data: URL that contains the combined doc.
data_url = "data:text/html;base64,%s" % base64.b64encode( data_url = "data:text/html;base64,%s" % base64.b64encode(
......
...@@ -6,6 +6,11 @@ ...@@ -6,6 +6,11 @@
// resources. The only communication to and from this implementation and the // resources. The only communication to and from this implementation and the
// WebUI is through postMessage. // WebUI is through postMessage.
// Note that these imports are stripped by a build step before being packaged.
// They're only present to help Closure compiler do type checks and must be
// referenced only within Closure annotations.
import {FavIconInfo, FrameInfo, GraphChangeStreamInterface, PageInfo, ProcessInfo, WorkerInfo} from './chrome/browser/ui/webui/discards/discards.mojom-webui.js';
// Radius of a node circle. // Radius of a node circle.
const /** number */ kNodeRadius = 6; const /** number */ kNodeRadius = 6;
......
...@@ -72,8 +72,8 @@ preprocess_grit("preprocess") { ...@@ -72,8 +72,8 @@ preprocess_grit("preprocess") {
in_files = [ in_files = [
"browser_proxy.js", "browser_proxy.js",
"constants.js", "constants.js",
"data.js",
"downloads.js", "downloads.js",
"externs.js",
"icon_loader.js", "icon_loader.js",
"search_service.js", "search_service.js",
] ]
...@@ -127,15 +127,22 @@ grit("downloads_resources") { ...@@ -127,15 +127,22 @@ grit("downloads_resources") {
js_type_check("closure_compile") { js_type_check("closure_compile") {
is_polymer3 = true is_polymer3 = true
closure_flags =
default_closure_args + mojom_js_args + [
"js_module_root=" + rebase_path(".", root_build_dir),
"js_module_root=" + rebase_path(
"$root_gen_dir/mojom-webui/chrome/browser/ui/webui/downloads",
root_build_dir),
]
deps = [ deps = [
":browser_proxy", ":browser_proxy",
":constants", ":constants",
":data",
":item", ":item",
":manager", ":manager",
":search_service", ":search_service",
":toolbar", ":toolbar",
] ]
closure_flags = mojom_js_args
} }
js_library("browser_proxy") { js_library("browser_proxy") {
...@@ -143,12 +150,15 @@ js_library("browser_proxy") { ...@@ -143,12 +150,15 @@ js_library("browser_proxy") {
"//chrome/browser/ui/webui/downloads:mojo_bindings_webui_js", "//chrome/browser/ui/webui/downloads:mojo_bindings_webui_js",
"//ui/webui/resources/js:cr.m", "//ui/webui/resources/js:cr.m",
] ]
externs_list = [ "externs.js" ]
} }
js_library("constants") { js_library("constants") {
} }
js_library("data") {
deps = [ "//chrome/browser/ui/webui/downloads:mojo_bindings_webui_js" ]
}
js_library("icon_loader") { js_library("icon_loader") {
deps = [ deps = [
"//ui/webui/resources/js:assert.m", "//ui/webui/resources/js:assert.m",
...@@ -162,6 +172,7 @@ js_library("item") { ...@@ -162,6 +172,7 @@ js_library("item") {
deps = [ deps = [
":browser_proxy", ":browser_proxy",
":constants", ":constants",
":data",
":icon_loader", ":icon_loader",
"//ui/webui/resources/cr_elements/cr_toast:cr_toast_manager.m", "//ui/webui/resources/cr_elements/cr_toast:cr_toast_manager.m",
"//ui/webui/resources/js:assert.m", "//ui/webui/resources/js:assert.m",
...@@ -175,6 +186,7 @@ js_library("item") { ...@@ -175,6 +186,7 @@ js_library("item") {
js_library("manager") { js_library("manager") {
deps = [ deps = [
":browser_proxy", ":browser_proxy",
":data",
":item", ":item",
":search_service", ":search_service",
":toolbar", ":toolbar",
......
...@@ -3,16 +3,11 @@ ...@@ -3,16 +3,11 @@
// found in the LICENSE file. // found in the LICENSE file.
/** /**
* @fileoverview Externs for objects sent from C++ to JS for chrome://downloads. * @fileoverview Typedef to allow augmentation of Data objects sent from C++ to
* @externs * JS for chrome://downloads.
*/ */
// eslint-disable-next-line no-var import {Data as MojomData} from './downloads.mojom-webui.js';
var downloads = {};
/** /** @typedef {MojomData | {hideDate: boolean}} */
* The type of the download object. The definition is based on the Data struct export let Data;
* in downloads.mojom.
* @typedef {downloads.mojom.Data | {hideDate: boolean}}
*/
downloads.Data;
...@@ -26,6 +26,7 @@ import {beforeNextRender, html, Polymer} from 'chrome://resources/polymer/v3_0/p ...@@ -26,6 +26,7 @@ import {beforeNextRender, html, Polymer} from 'chrome://resources/polymer/v3_0/p
import {BrowserProxy} from './browser_proxy.js'; import {BrowserProxy} from './browser_proxy.js';
import {DangerType, States} from './constants.js'; import {DangerType, States} from './constants.js';
import {Data} from './data.js';
import {PageHandlerInterface} from './downloads.mojom-webui.js'; import {PageHandlerInterface} from './downloads.mojom-webui.js';
import {IconLoader} from './icon_loader.js'; import {IconLoader} from './icon_loader.js';
...@@ -42,7 +43,7 @@ Polymer({ ...@@ -42,7 +43,7 @@ Polymer({
overrideCustomEquivalent: true, overrideCustomEquivalent: true,
properties: { properties: {
/** @type {!downloads.Data} */ /** @type {!Data} */
data: Object, data: Object,
/** @private */ /** @private */
......
...@@ -23,6 +23,7 @@ import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bun ...@@ -23,6 +23,7 @@ import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bun
import {BrowserProxy} from './browser_proxy.js'; import {BrowserProxy} from './browser_proxy.js';
import {States} from './constants.js'; import {States} from './constants.js';
import {Data} from './data.js';
import {PageCallbackRouter, PageHandlerInterface} from './downloads.mojom-webui.js'; import {PageCallbackRouter, PageHandlerInterface} from './downloads.mojom-webui.js';
import {SearchService} from './search_service.js'; import {SearchService} from './search_service.js';
...@@ -55,7 +56,7 @@ Polymer({ ...@@ -55,7 +56,7 @@ Polymer({
value: false, value: false,
}, },
/** @private {!Array<!downloads.Data>} */ /** @private {!Array<!Data>} */
items_: { items_: {
type: Array, type: Array,
value() { value() {
...@@ -168,7 +169,7 @@ Polymer({ ...@@ -168,7 +169,7 @@ Polymer({
/** /**
* @param {number} index * @param {number} index
* @param {!Array<downloads.Data>} items * @param {!Array<Data>} items
* @private * @private
*/ */
insertItems_(index, items) { insertItems_(index, items) {
...@@ -339,7 +340,7 @@ Polymer({ ...@@ -339,7 +340,7 @@ Polymer({
/** /**
* @param {number} index * @param {number} index
* @param {!downloads.Data} data * @param {!Data} data
* @private * @private
*/ */
updateItem_(index, data) { updateItem_(index, data) {
......
...@@ -18,6 +18,7 @@ import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js'; ...@@ -18,6 +18,7 @@ import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {BrowserProxy} from './browser_proxy.js'; import {BrowserProxy} from './browser_proxy.js';
import {Data} from './data.js';
import {PageHandlerInterface} from './downloads.mojom-webui.js'; import {PageHandlerInterface} from './downloads.mojom-webui.js';
import {SearchService} from './search_service.js'; import {SearchService} from './search_service.js';
...@@ -33,7 +34,7 @@ Polymer({ ...@@ -33,7 +34,7 @@ Polymer({
observer: 'updateClearAll_', observer: 'updateClearAll_',
}, },
/** @type {!Array<!downloads.Data>} */ /** @type {!Array<!Data>} */
items: { items: {
type: Array, type: Array,
value: Array, value: Array,
......
...@@ -6,8 +6,13 @@ import("//third_party/closure_compiler/compile_js.gni") ...@@ -6,8 +6,13 @@ import("//third_party/closure_compiler/compile_js.gni")
js_type_check("closure_compile") { js_type_check("closure_compile") {
uses_js_modules = true uses_js_modules = true
closure_flags = default_closure_args + mojom_js_args + [
"js_module_root=" + rebase_path(".", root_build_dir),
"js_module_root=" + rebase_path(
"$root_gen_dir/mojom-webui/components/site_engagement/core/mojom",
root_build_dir),
]
deps = [ ":site_engagement" ] deps = [ ":site_engagement" ]
closure_flags = mojom_js_args
} }
js_library("site_engagement") { js_library("site_engagement") {
......
...@@ -8,7 +8,8 @@ import("//tools/grit/grit_rule.gni") ...@@ -8,7 +8,8 @@ import("//tools/grit/grit_rule.gni")
js_type_check("closure_compile") { js_type_check("closure_compile") {
uses_js_modules = true uses_js_modules = true
closure_flags = mojom_js_args closure_flags = default_closure_args + mojom_js_args +
[ "js_module_root=" + rebase_path(".", root_build_dir) ]
deps = [ deps = [
":omnibox", ":omnibox",
":omnibox_element", ":omnibox_element",
......
...@@ -613,15 +613,16 @@ class Generator(generator.Generator): ...@@ -613,15 +613,16 @@ class Generator(generator.Generator):
if mojom.IsEnumKind(kind): if mojom.IsEnumKind(kind):
return "number" return "number"
prefix = "" if mojom.IsNullableKind(kind) else "!" prefix = "" if mojom.IsNullableKind(kind) else "!"
if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
return prefix + "Object"
if mojom.IsArrayKind(kind): if mojom.IsArrayKind(kind):
return prefix + ("Array<%s>" % get_type_name(kind.kind)) return prefix + ("Array<%s>" % get_type_name(kind.kind))
if mojom.IsMapKind(kind): if mojom.IsMapKind(kind) and self._IsStringableKind(kind.key_kind):
return "%sMap<%s, %s>|%sObject<%s, %s>" % ( return "(%sMap<%s, %s>|%sObject<%s, %s>)" % (
prefix, get_type_name(kind.key_kind), get_type_name( prefix, get_type_name(kind.key_kind), get_type_name(
kind.value_kind), prefix, get_type_name( kind.value_kind), prefix, get_type_name(
kind.key_kind), get_type_name(kind.value_kind)) kind.key_kind), get_type_name(kind.value_kind))
if mojom.IsMapKind(kind):
return "{}Map<{}, {}>".format(prefix, get_type_name(kind.key_kind),
get_type_name(kind.value_kind))
return prefix + self._GetTypeNameForNewBindings(kind, return prefix + self._GetTypeNameForNewBindings(kind,
for_module=for_module) for_module=for_module)
......
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