Esempio n. 1
0
  def __init__(self, options, executable, is_content_shell, use_login):
    super(DesktopBrowserBackend, self).__init__(
        is_content_shell=is_content_shell,
        supports_extensions=not is_content_shell, options=options)

    # Initialize fields so that an explosion during init doesn't break in Close.
    self._proc = None
    self._tmpdir = None
    self._tmp_output_file = None

    self._use_login = use_login

    self._executable = executable
    if not self._executable:
      raise Exception('Cannot create browser, no executable found!')

    if len(options.extensions_to_load) > 0 and is_content_shell:
      raise browser_backend.ExtensionsNotSupportedException(
          'Content shell does not support extensions.')

    self._port = util.GetAvailableLocalPort()
    self._supports_net_benchmarking = True
    self._LaunchBrowser(options)

    # For old chrome versions, might have to relaunch to have the
    # correct benchmarking switch.
    if self._chrome_branch_number < 1418:
      self.Close()
      self._supports_net_benchmarking = False
      self._LaunchBrowser(options)

    if self._use_login:
      cros_util.NavigateLogin(self)
    def __init__(self, options, adb, package, is_content_shell, cmdline_file,
                 activity, devtools_remote_port):
        super(AndroidBrowserBackend,
              self).__init__(is_content_shell=is_content_shell,
                             supports_extensions=False,
                             options=options)
        if len(options.extensions_to_load) > 0:
            raise browser_backend.ExtensionsNotSupportedException(
                'Android browser does not support extensions.')
        # Initialize fields so that an explosion during init doesn't break in Close.
        self._options = options
        self._adb = adb
        self._package = package
        self._cmdline_file = cmdline_file
        self._saved_cmdline = None
        self._activity = activity
        if not options.keep_test_server_ports:
            adb_commands.ResetTestServerPortAllocation()
        self._port = adb_commands.AllocateTestServerPort()
        self._devtools_remote_port = devtools_remote_port
        self._profile_dir = '/data/data/%s/' % self._package
        if is_content_shell:
            self._profile_dir += 'app_content_shell/'
        else:
            self._profile_dir += 'app_chrome/'

        # Kill old browser.
        self._adb.CloseApplication(self._package)
        self._adb.KillAll('device_forwarder')
        self._adb.Forward('tcp:%d' % self._port, self._devtools_remote_port)

        if self._adb.Adb().CanAccessProtectedFileContents():
            if not options.dont_override_profile:
                self._adb.RunShellCommand('su -c rm -r "%s"' %
                                          self._profile_dir)
            if options.profile_dir:
                if is_content_shell:
                    logging.critical(
                        'Profiles cannot be used with content shell')
                    sys.exit(1)
                self._adb.Push(options.profile_dir, self._profile_dir)

        # Set up the command line.
        self._saved_cmdline = ''.join(
            self._adb.Adb().GetProtectedFileContents(cmdline_file) or [])
        if is_content_shell:
            pseudo_exec_name = 'content_shell'
        else:
            pseudo_exec_name = 'chrome'

        args = [pseudo_exec_name]
        args.extend(self.GetBrowserStartupArgs())

        def QuoteIfNeeded(arg):
            # Escape 'key=valueA valueB' to 'key="valueA valueB"'
            # Already quoted values, or values without space are left untouched.
            # This is required so CommandLine.java can parse valueB correctly rather
            # than as a separate switch.
            params = arg.split('=')
            if len(params) != 2:
                return arg
            key, values = params
            if ' ' not in values:
                return arg
            if values[0] in '"\'' and values[-1] == values[0]:
                return arg
            return '%s="%s"' % (key, values)

        args = map(QuoteIfNeeded, args)
        self._adb.Adb().SetProtectedFileContents(cmdline_file, ' '.join(args))

        # TODO: Once --enable-remote-debugging flag makes its way to the oldest
        # version under test (m27 goes to stable), remove this hack.
        # Force devtools protocol on, if not already done and we can access
        # protected files.
        if (not is_content_shell
                and self._adb.Adb().CanAccessProtectedFileContents()):
            # Make sure we can find the apps' prefs file
            prefs_file = self._profile_dir + 'Default/Preferences'
            if not self._adb.FileExistsOnDevice(prefs_file):
                # Start it up the first time so we can tweak the prefs.
                self._adb.StartActivity(self._package, self._activity, True,
                                        None, None)
                retries = 0
                timeout = 3
                time.sleep(timeout)
                while not self._adb.Adb().GetProtectedFileContents(prefs_file):
                    time.sleep(timeout)
                    retries += 1
                    timeout *= 2
                    if retries == 3:
                        logging.critical(
                            'android_browser_backend: Could not find '
                            'preferences file %s for %s', prefs_file,
                            self._package)
                        raise exceptions.BrowserGoneException(
                            'Missing preferences file.')
                self._adb.CloseApplication(self._package)

            preferences = json.loads(''.join(
                self._adb.Adb().GetProtectedFileContents(prefs_file)))
            changed = False
            if 'devtools' not in preferences:
                preferences['devtools'] = {}
                changed = True
            if not preferences['devtools'].get('remote_enabled'):
                preferences['devtools']['remote_enabled'] = True
                changed = True
            if changed:
                logging.warning('Manually enabled devtools protocol on %s' %
                                self._package)
                txt = json.dumps(preferences, indent=2)
                self._adb.Adb().SetProtectedFileContents(prefs_file, txt)

        # Start it up with a fresh log.
        self._adb.RunShellCommand('logcat -c')
        self._adb.StartActivity(self._package, self._activity, True, None,
                                'chrome://newtab/')
        try:
            self._WaitForBrowserToComeUp()
            self._PostBrowserStartupInitialization()
        except exceptions.BrowserGoneException:
            logging.critical('Failed to connect to browser.')
            if not self._adb.IsRootEnabled():
                logging.critical(
                    'Ensure web debugging is enabled in Chrome at '
                    '"Settings > Developer tools > Enable USB Web debugging".')
            sys.exit(1)
        except:
            import traceback
            traceback.print_exc()
            self.Close()
            raise
Esempio n. 3
0
    def __init__(self, options, adb, package, is_content_shell, cmdline_file,
                 activity, devtools_remote_port):
        super(AndroidBrowserBackend,
              self).__init__(is_content_shell=is_content_shell,
                             supports_extensions=False,
                             options=options)
        if len(options.extensions_to_load) > 0:
            raise browser_backend.ExtensionsNotSupportedException(
                'Android browser does not support extensions.')
        # Initialize fields so that an explosion during init doesn't break in Close.
        self._options = options
        self._adb = adb
        self._package = package
        self._cmdline_file = cmdline_file
        self._saved_cmdline = None
        self._activity = activity
        if not options.keep_test_server_ports:
            adb_commands.ResetTestServerPortAllocation()
        self._port = adb_commands.AllocateTestServerPort()
        self._devtools_remote_port = devtools_remote_port
        self._profile_dir = '/data/data/%s/' % self._package
        if is_content_shell:
            self._profile_dir += 'app_content_shell/'
        else:
            self._profile_dir += 'app_chrome/'

        # Kill old browser.
        self._adb.CloseApplication(self._package)
        self._adb.KillAll('device_forwarder')
        self._adb.Forward('tcp:%d' % self._port, self._devtools_remote_port)

        if self._adb.Adb().CanAccessProtectedFileContents():
            if not options.dont_override_profile:
                self._adb.RunShellCommand('su -c rm -r "%s"' %
                                          self._profile_dir)
            if options.profile_dir:
                if is_content_shell:
                    logging.critical(
                        'Profiles cannot be used with content shell')
                    sys.exit(1)
                self._adb.Push(options.profile_dir, self._profile_dir)

        # Set up the command line.
        self._saved_cmdline = ''.join(
            self._adb.Adb().GetProtectedFileContents(cmdline_file) or [])
        if is_content_shell:
            pseudo_exec_name = 'content_shell'
        else:
            pseudo_exec_name = 'chrome'

        args = [pseudo_exec_name]
        args.extend(self.GetBrowserStartupArgs())

        def QuoteIfNeeded(arg):
            # Escape 'key=valueA valueB' to 'key="valueA valueB"'
            # Already quoted values, or values without space are left untouched.
            # This is required so CommandLine.java can parse valueB correctly rather
            # than as a separate switch.
            params = arg.split('=')
            if len(params) != 2:
                return arg
            key, values = params
            if ' ' not in values:
                return arg
            if values[0] in '"\'' and values[-1] == values[0]:
                return arg
            return '%s="%s"' % (key, values)

        args = map(QuoteIfNeeded, args)
        self._adb.Adb().SetProtectedFileContents(cmdline_file, ' '.join(args))

        self._SetPreferencesIfNeeded(is_content_shell)

        # Start it up with a fresh log.
        self._adb.RunShellCommand('logcat -c')
        self._adb.StartActivity(self._package, self._activity, True, None,
                                'chrome://newtab/')
        try:
            self._WaitForBrowserToComeUp()
            self._PostBrowserStartupInitialization()
        except exceptions.BrowserGoneException:
            logging.critical('Failed to connect to browser.')
            if not self._adb.IsRootEnabled():
                logging.critical(
                    'Ensure web debugging is enabled in Chrome at '
                    '"Settings > Developer tools > Enable USB Web debugging".')
            sys.exit(1)
        except:
            import traceback
            traceback.print_exc()
            self.Close()
            raise
Esempio n. 4
0
 def extensions(self):
   """Returns the extension dictionary if it exists."""
   if not self.supports_extensions:
     raise browser_backend.ExtensionsNotSupportedException(
         'Extensions not supported')
   return self._extensions