|
| 1 | +#!/usr/bin/env vpython3 |
| 2 | +# Copyright 2019 The Chromium Authors. All rights reserved. |
| 3 | +# Use of this source code is governed by a BSD-style license that can be |
| 4 | +# found in the LICENSE file. |
| 5 | +""" |
| 6 | +Start local DevTools front-end in Chrome. |
| 7 | +""" |
| 8 | + |
| 9 | +from os import path |
| 10 | +import argparse |
| 11 | +import devtools_paths |
| 12 | +import logging |
| 13 | +import platform |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | +# The list of features that are enabled by default. |
| 20 | +# These can be overridden with --disable-feature=FOO on the commandline |
| 21 | +# if necessary. |
| 22 | +ENABLE_FEATURES = [ |
| 23 | + 'DevToolsAutomaticFileSystems', |
| 24 | + 'DevToolsFreeStyler:patching/true,user_tier/TESTERS', |
| 25 | + 'DevToolsWellKnown', |
| 26 | +] |
| 27 | + |
| 28 | +# The list of features that are disabled by default. |
| 29 | +# These can be overridden with --enable-feature=FOO on the commandline |
| 30 | +# if necessary. |
| 31 | +DISABLE_FEATURES = ['MediaRouter'] if platform.system() == 'Darwin' else [] |
| 32 | + |
| 33 | + |
| 34 | +def parse_options(args): |
| 35 | + parser = argparse.ArgumentParser( |
| 36 | + description='Run local DevTools front-end') |
| 37 | + parser.add_argument( |
| 38 | + '--browser', |
| 39 | + choices=['cft', 'canary'], |
| 40 | + default='cft', |
| 41 | + help= |
| 42 | + 'launch in the specified browser, can be either "cft" (the default) or "canary"' |
| 43 | + ) |
| 44 | + parser.add_argument('--canary', |
| 45 | + action='store_const', |
| 46 | + const='canary', |
| 47 | + dest='browser', |
| 48 | + help='launch in Chrome Canary') |
| 49 | + parser.add_argument('--enable-features', |
| 50 | + action='append', |
| 51 | + default=[], |
| 52 | + help='enable experimental Chrome features') |
| 53 | + parser.add_argument('--disable-features', |
| 54 | + action='append', |
| 55 | + default=[], |
| 56 | + help='disable experimental Chrome features') |
| 57 | + parser.add_argument('--no-auto-open-devtools-for-tabs', |
| 58 | + action='store_true', |
| 59 | + help='don\'t automatically open DevTools for new tabs') |
| 60 | + parser.add_argument( |
| 61 | + '-t', |
| 62 | + '--target', |
| 63 | + default='Default', |
| 64 | + help= |
| 65 | + 'specify the target build subdirectory under //out (defaults to "Default")' |
| 66 | + ) |
| 67 | + parser.add_argument('--verbose', |
| 68 | + action='store_true', |
| 69 | + help='enable verbose logging') |
| 70 | + parser.add_argument('URL', |
| 71 | + nargs='*', |
| 72 | + help='specify URL(s) to open by default') |
| 73 | + return parser.parse_args(args) |
| 74 | + |
| 75 | + |
| 76 | +def build(options): |
| 77 | + """ |
| 78 | + Build the DevTools front-end in the target identified by the `options`. |
| 79 | + """ |
| 80 | + cwd = devtools_paths.root_path() |
| 81 | + outdir = path.join('out', options.target) |
| 82 | + autoninja = devtools_paths.autoninja_path() |
| 83 | + subprocess.check_call([autoninja, '-C', outdir], cwd=cwd) |
| 84 | + |
| 85 | + |
| 86 | +def find_browser_binary(options): |
| 87 | + """ |
| 88 | + Locate the correct browser binary (per `options`). |
| 89 | + """ |
| 90 | + if options.browser == 'canary': |
| 91 | + binary = path.abspath( |
| 92 | + path.join( |
| 93 | + *{ |
| 94 | + 'Linux': ('/usr', 'bin', 'google-chrome-canary'), |
| 95 | + 'Darwin': ('/Applications', 'Google Chrome Canary.app', |
| 96 | + 'Contents', 'MacOS', 'Google Chrome Canary'), |
| 97 | + }[platform.system()])) |
| 98 | + logger.debug('Located Chrome Canary binary in %s.', binary) |
| 99 | + return binary |
| 100 | + else: |
| 101 | + binary = devtools_paths.downloaded_chrome_binary_path() |
| 102 | + logger.debug('Located Chrome for Testing binary in %s.', binary) |
| 103 | + return binary |
| 104 | + |
| 105 | + |
| 106 | +def start(options): |
| 107 | + """ |
| 108 | + Launch Chrome with our custom DevTools front-end. |
| 109 | + """ |
| 110 | + cwd = devtools_paths.root_path() |
| 111 | + args = [find_browser_binary(options)] |
| 112 | + |
| 113 | + # Custom flags for CfT. |
| 114 | + if options.browser == 'cft': |
| 115 | + args += ['--disable-infobars'] # Disable the CfT warning infobar. |
| 116 | + args += ['--use-mock-keychain' |
| 117 | + ] if platform.system() == 'Darwin' else [] |
| 118 | + |
| 119 | + # Disable/Enable experimental features, starting with defaults. |
| 120 | + args += ['--disable-features=%s' % f for f in DISABLE_FEATURES] |
| 121 | + args += ['--enable-features=%s' % f for f in ENABLE_FEATURES] |
| 122 | + args += ['--disable-features=%s' % f for f in options.disable_features] |
| 123 | + args += ['--enable-features=%s' % f for f in options.enable_features] |
| 124 | + |
| 125 | + # Open with our freshly built DevTools front-end. |
| 126 | + args += [ |
| 127 | + '--custom-devtools-frontend=file://%s' % |
| 128 | + devtools_paths.custom_devtools_frontend_path(options.target) |
| 129 | + ] |
| 130 | + |
| 131 | + # Chrome flags and URLs. |
| 132 | + if not options.no_auto_open_devtools_for_tabs: |
| 133 | + args += ['--auto-open-devtools-for-tabs'] |
| 134 | + args += options.URL |
| 135 | + |
| 136 | + # Launch Chrome. |
| 137 | + logger.debug('Launch Chrome: %s', ' '.join(args)) |
| 138 | + subprocess.check_call(args, cwd=cwd) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + OPTIONS = parse_options(sys.argv[1:]) |
| 143 | + logging.basicConfig( |
| 144 | + level=logging.DEBUG if OPTIONS.verbose else logging.INFO) |
| 145 | + build(OPTIONS) |
| 146 | + start(OPTIONS) |
0 commit comments