Add the ability to pass arguments to runner.jar

This is used mainly so that the Chrome-specific compiler pass
can be enabled and disabled.

BUG=619091
R=aberent@chromium.org, jamiewalch@chromium.org

Review URL: https://codereview.chromium.org/2117653002 .

Cr-Commit-Position: refs/heads/master@{#409336}
diff --git a/remoting/compile_js.gypi b/remoting/compile_js.gypi
index 9ad7d3b..cecac12 100644
--- a/remoting/compile_js.gypi
+++ b/remoting/compile_js.gypi
@@ -4,10 +4,13 @@
 
 {
   'variables': {
-    'externs': ['../third_party/closure_compiler/externs/chrome_extensions.js',
-      '../third_party/closure_compiler/externs/metrics_private.js'],
-    'script_args': ['--no-single-file'],
+    'externs': [
+      '../third_party/closure_compiler/externs/chrome_extensions.js',
+      '../third_party/closure_compiler/externs/metrics_private.js'
+    ],
+    'script_args': ['--no_single_file'],
     'closure_args': [
+      '<@(default_closure_args)',
       'jscomp_error=duplicate',
       'jscomp_error=misplacedTypeAnnotation',
     ],
diff --git a/remoting/webapp/build_template.gni b/remoting/webapp/build_template.gni
index df443fb..1c893570 100644
--- a/remoting/webapp/build_template.gni
+++ b/remoting/webapp/build_template.gni
@@ -78,10 +78,10 @@
 
       args = rebase_path(js_files, root_build_dir)
       args += [
-                "--no-single-file",
-                "--out-file",
+                "--no_single_file",
+                "--out_file",
                 rebase_path(target_jscompile_stamp, root_build_dir),
-                "--closure-args",
+                "--closure_args",
               ] + closure_args + extra_closure_args
       args += [ "--externs" ] + rebase_path(externs, root_build_dir)
     }
diff --git a/third_party/closure_compiler/closure_args.gni b/third_party/closure_compiler/closure_args.gni
index b1cd189..425165b 100644
--- a/third_party/closure_compiler/closure_args.gni
+++ b/third_party/closure_compiler/closure_args.gni
@@ -3,7 +3,7 @@
 # found in the LICENSE file.
 
 # GYP version: third_party/closure_compiler/closure_args.gypi
-closure_args = [
+default_closure_args = [
   "compilation_level=SIMPLE_OPTIMIZATIONS",
 
   "extra_annotation_name=attribute",
diff --git a/third_party/closure_compiler/closure_args.gypi b/third_party/closure_compiler/closure_args.gypi
index 5496406..2fc245e 100644
--- a/third_party/closure_compiler/closure_args.gypi
+++ b/third_party/closure_compiler/closure_args.gypi
@@ -4,7 +4,7 @@
 
 # GN version: third_party/closure_compiler/closure_args.gni
 {
-  'closure_args': [
+  'default_closure_args': [
     'compilation_level=SIMPLE_OPTIMIZATIONS',
 
     # Keep this in sync with chrome/browser/web_dev_style/js_checker.py.
@@ -43,11 +43,12 @@
 
     'source_map_format=V3',
   ],
+
   'default_disabled_closure_args': [
     # TODO(dbeam): happens when the same file is <include>d multiple times.
     'jscomp_off=duplicate',
     # TODO(fukino): happens when cr.defineProperty() has a type annotation.
     # Avoiding parse-time warnings needs 2 pass compiling. crbug.com/421562.
     'jscomp_off=misplacedTypeAnnotation',
-  ]
+  ],
 }
diff --git a/third_party/closure_compiler/compile.py b/third_party/closure_compiler/compile.py
index c287884..a25b744 100755
--- a/third_party/closure_compiler/compile.py
+++ b/third_party/closure_compiler/compile.py
@@ -186,13 +186,14 @@
     return tmp_file.name
 
   def _run_js_check(self, sources, out_file=None, externs=None,
-                    closure_args=None):
+                    runner_args=None, closure_args=None):
     """Check |sources| for type errors.
 
     Args:
       sources: Files to check.
       out_file: A file where the compiled output is written to.
       externs: @extern files that inform the compiler about custom globals.
+      runner_args: Arguments passed to runner.jar.
       closure_args: Arguments passed directly to the Closure compiler.
 
     Returns:
@@ -220,8 +221,9 @@
     args_file = self._create_temp_file(args_file_content)
     self._log_debug("Args file: %s" % args_file)
 
-    runner_args = ["--compiler-args-file=%s" % args_file]
-    _, stderr = self._run_jar(self._runner_jar, runner_args)
+    processed_runner_args = ["--%s" % arg for arg in runner_args or []]
+    processed_runner_args += ["--compiler-args-file=%s" % args_file]
+    _, stderr = self._run_jar(self._runner_jar, processed_runner_args)
 
     errors = stderr.strip().split("\n\n")
     maybe_summary = errors.pop()
@@ -243,7 +245,7 @@
     return errors, stderr
 
   def check(self, source_file, out_file=None, depends=None, externs=None,
-            closure_args=None):
+            runner_args=None, closure_args=None):
     """Closure compiler |source_file| while checking for errors.
 
     Args:
@@ -251,6 +253,7 @@
       out_file: A file where the compiled output is written to.
       depends: Files that |source_file| requires to run (e.g. earlier <script>).
       externs: @extern files that inform the compiler about custom globals.
+      runner_args: Arguments passed to runner.jar.
       closure_args: Arguments passed directly to the Closure compiler.
 
     Returns:
@@ -280,6 +283,7 @@
 
     errors, stderr = self._run_js_check([self._expanded_file],
                                         out_file=out_file, externs=externs,
+                                        runner_args=runner_args,
                                         closure_args=closure_args)
     filtered_errors = self._filter_errors(errors)
     cleaned_errors = map(self._clean_up_error, filtered_errors)
@@ -295,13 +299,14 @@
     return bool(cleaned_errors), stderr
 
   def check_multiple(self, sources, out_file=None, externs=None,
-                     closure_args=None):
+                     runner_args=None, closure_args=None):
     """Closure compile a set of files and check for errors.
 
     Args:
       sources: An array of files to check.
       out_file: A file where the compiled output is written to.
       externs: @extern files that inform the compiler about custom globals.
+      runner_args: Arguments passed to runner.jar.
       closure_args: Arguments passed directly to the Closure compiler.
 
     Returns:
@@ -310,6 +315,7 @@
     """
     errors, stderr = self._run_js_check(sources, out_file=out_file,
                                         externs=externs,
+                                        runner_args=runner_args,
                                         closure_args=closure_args)
     self._nuke_temp_files()
     return bool(errors), stderr
@@ -321,19 +327,20 @@
   parser.add_argument("sources", nargs=argparse.ONE_OR_MORE,
                       help="Path to a source file to typecheck")
   single_file_group = parser.add_mutually_exclusive_group()
-  single_file_group.add_argument("--single-file", dest="single_file",
+  single_file_group.add_argument("--single_file", dest="single_file",
                                  action="store_true",
                                  help="Process each source file individually")
-  # TODO(twellington): remove --no-single-file and use len(opts.sources).
-  single_file_group.add_argument("--no-single-file", dest="single_file",
+  # TODO(twellington): remove --no_single_file and use len(opts.sources).
+  single_file_group.add_argument("--no_single_file", dest="single_file",
                                  action="store_false",
                                  help="Process all source files as a group")
   parser.add_argument("-d", "--depends", nargs=argparse.ZERO_OR_MORE)
   parser.add_argument("-e", "--externs", nargs=argparse.ZERO_OR_MORE)
-  parser.add_argument("-o", "--out-file", dest="out_file",
+  parser.add_argument("-o", "--out_file",
                       help="A file where the compiled output is written to")
-  parser.add_argument("-c", "--closure-args", dest="closure_args",
-                      nargs=argparse.ZERO_OR_MORE,
+  parser.add_argument("-r", "--runner_args", nargs=argparse.ZERO_OR_MORE,
+                      help="Arguments passed to runner.jar")
+  parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE,
                       help="Arguments passed directly to the Closure compiler")
   parser.add_argument("-v", "--verbose", action="store_true",
                       help="Show more information as this script runs")
@@ -359,6 +366,7 @@
 
       found_errors, _ = checker.check(source, out_file=opts.out_file,
                                       depends=depends, externs=externs,
+                                      runner_args=opts.runner_args,
                                       closure_args=opts.closure_args)
       if found_errors:
         sys.exit(1)
@@ -367,6 +375,7 @@
         sources,
         out_file=opts.out_file,
         externs=externs,
+        runner_args=opts.runner_args,
         closure_args=opts.closure_args)
     if found_errors:
       print stderr
diff --git a/third_party/closure_compiler/compile2.py b/third_party/closure_compiler/compile2.py
index df844d1..20376ed 100755
--- a/third_party/closure_compiler/compile2.py
+++ b/third_party/closure_compiler/compile2.py
@@ -189,7 +189,7 @@
       tmp_file.write(contents)
     return tmp_file.name
 
-  def check(self, sources, out_file=None, closure_args=None,
+  def check(self, sources, out_file=None, runner_args=None, closure_args=None,
             custom_sources=True):
     """Closure compile |sources| while checking for errors.
 
@@ -198,6 +198,7 @@
           sources[1:] are externs and dependencies in topological order. Order
           is not guaranteed if custom_sources is True.
       out_file: A file where the compiled output is written to.
+      runner_args: Arguments passed to runner.jar.
       closure_args: Arguments passed directly to the Closure compiler.
       custom_sources: Whether |sources| was customized by the target (e.g. not
           in GYP dependency order).
@@ -264,8 +265,9 @@
     args_file = self._create_temp_file(args_file_content)
     self._log_debug("Args file: %s" % args_file)
 
-    runner_args = ["--compiler-args-file=%s" % args_file]
-    _, stderr = self._run_jar(self._runner_jar, runner_args)
+    processed_runner_args = ["--%s" % arg for arg in runner_args or []]
+    processed_runner_args += ["--compiler-args-file=%s" % args_file]
+    _, stderr = self._run_jar(self._runner_jar, processed_runner_args)
 
     errors = stderr.strip().split("\n\n")
     maybe_summary = errors.pop()
@@ -308,6 +310,8 @@
                       help="Whether this rules has custom sources.")
   parser.add_argument("-o", "--out_file",
                       help="A file where the compiled output is written to")
+  parser.add_argument("-r", "--runner_args", nargs=argparse.ZERO_OR_MORE,
+                      help="Arguments passed to runner.jar")
   parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE,
                       help="Arguments passed directly to the Closure compiler")
   parser.add_argument("-v", "--verbose", action="store_true",
@@ -318,6 +322,7 @@
 
   found_errors, stderr = checker.check(opts.sources, out_file=opts.out_file,
                                        closure_args=opts.closure_args,
+                                       runner_args=opts.runner_args,
                                        custom_sources=opts.custom_sources)
 
   if found_errors:
diff --git a/third_party/closure_compiler/compile_js.gypi b/third_party/closure_compiler/compile_js.gypi
index f947c99..f718246 100644
--- a/third_party/closure_compiler/compile_js.gypi
+++ b/third_party/closure_compiler/compile_js.gypi
@@ -32,7 +32,10 @@
         'out_file%': '<(SHARED_INTERMEDIATE_DIR)/closure/<!(python <(CLOSURE_DIR)/build/outputs.py <(_target_name).js)',
         'externs%': [],
         'depends%': [],
+        'runner_args%': ['enable-chrome-pass'],
+        # TODO(dbeam): remove when no longer used from remoting/.
         'script_args%': [],
+        'closure_args%': '<(default_closure_args)',
         'disabled_closure_args%': '<(default_disabled_closure_args)',
       },
       'inputs': [
@@ -55,9 +58,10 @@
         '<@(script_args)',
         '--depends', '<@(depends)',
         '--externs', '<@(externs)',
-        '--out-file', '<(out_file)',
-        '--closure-args', '<@(closure_args)', '<@(disabled_closure_args)',
-        # Add '--verbose' for make glorious log spam of Closure compiler.
+        '--out_file', '<(out_file)',
+        '--runner_args', '<@(runner_args)',
+        '--closure_args', '<@(closure_args)', '<@(disabled_closure_args)',
+        # '--verbose' # for make glorious log spam of Closure compiler.
       ],
       'message': 'Compiling <(_target_name)',
     }
diff --git a/third_party/closure_compiler/compile_js2.gypi b/third_party/closure_compiler/compile_js2.gypi
index 9d231d9..a0c77ea 100644
--- a/third_party/closure_compiler/compile_js2.gypi
+++ b/third_party/closure_compiler/compile_js2.gypi
@@ -39,8 +39,10 @@
       'variables': {
         'target_path': '<!(python <(CLOSURE_DIR)/build/outputs.py <(default_source_file))',
         'out_file%': '<(SHARED_INTERMEDIATE_DIR)/closure/<(target_path)',
-        # TODO(dbeam): add --custom_sources when 'source_files' is set?
+        'runner_args%': ['enable-chrome-pass'],
+        # TODO(dbeam): remove when no longer used from remoting/.
         'script_args%': [],
+        'closure_args%': '<(default_closure_args)',
         'disabled_closure_args%': '<(default_disabled_closure_args)',
       },
 
@@ -63,6 +65,7 @@
         '<@(script_args)',
         '>@(_sources)',
         '--out_file', '<(out_file)',
+        '--runner_args', '<@(runner_args)',
         '--closure_args', '<@(closure_args)', '<@(disabled_closure_args)',
         # '--verbose' # for make glorious log spam of Closure compiler.
       ],
diff --git a/third_party/closure_compiler/compiler_test.py b/third_party/closure_compiler/compiler_test.py
index 9f17c49..9da4d115 100755
--- a/third_party/closure_compiler/compiler_test.py
+++ b/third_party/closure_compiler/compiler_test.py
@@ -24,9 +24,9 @@
                                     "externs", "chrome_send.js")
 _CLOSURE_ARGS_GYPI = os.path.join(_SCRIPT_DIR, "closure_args.gypi")
 _GYPI_DICT = literal_eval(open(_CLOSURE_ARGS_GYPI).read())
-_COMMON_CLOSURE_ARGS = _GYPI_DICT["closure_args"] + \
+_COMMON_CLOSURE_ARGS = _GYPI_DICT["default_closure_args"] + \
                        _GYPI_DICT["default_disabled_closure_args"]
-
+_RUNNER_ARGS = ["enable-chrome-pass"]
 
 class CompilerTest(unittest.TestCase):
   _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents
@@ -55,6 +55,7 @@
     found_errors, stderr = self._checker.check(file_path,
                                                externs=externs,
                                                out_file=out_file,
+                                               runner_args=_RUNNER_ARGS,
                                                closure_args=args)
     return found_errors, stderr, out_file, out_map
 
diff --git a/third_party/closure_compiler/runner/runner.jar b/third_party/closure_compiler/runner/runner.jar
index 8594a93..cca3253 100644
--- a/third_party/closure_compiler/runner/runner.jar
+++ b/third_party/closure_compiler/runner/runner.jar
Binary files differ
diff --git a/ui/file_manager/compile_js.gypi b/ui/file_manager/compile_js.gypi
index f82d56e..b8df0b9 100644
--- a/ui/file_manager/compile_js.gypi
+++ b/ui/file_manager/compile_js.gypi
@@ -4,6 +4,7 @@
 {
   'variables': {
     'closure_args': [
+      '<@(default_closure_args)',
       'warning_level=VERBOSE',
     ],
   },