def __init__(self, browser_backend, path, is_record_mode, is_append_mode, make_javascript_deterministic): self._browser_backend = browser_backend self._forwarder = None self._web_page_replay = None self._is_record_mode = is_record_mode self._is_append_mode = is_append_mode self._forwarder = browser_backend.CreateForwarder( util.PortPair(browser_backend.webpagereplay_local_http_port, browser_backend.webpagereplay_remote_http_port), util.PortPair(browser_backend.webpagereplay_local_https_port, browser_backend.webpagereplay_remote_https_port)) wpr_args = browser_backend.browser_options.extra_wpr_args if self._is_record_mode: if self._is_append_mode: wpr_args.append('--append') else: wpr_args.append('--record') if not make_javascript_deterministic: wpr_args.append('--inject_scripts=') browser_backend.AddReplayServerOptions(wpr_args) self._web_page_replay = webpagereplay.ReplayServer( path, browser_backend.WEBPAGEREPLAY_HOST, browser_backend.webpagereplay_local_http_port, browser_backend.webpagereplay_local_https_port, wpr_args) # Remove --no-dns_forwarding if it wasn't explicitly requested by backend. if '--no-dns_forwarding' not in wpr_args: self._web_page_replay.replay_options.remove('--no-dns_forwarding') self._web_page_replay.StartServer()
def __init__(self, browser_backend, path, is_record_mode, webpagereplay_host, webpagereplay_local_http_port, webpagereplay_local_https_port, webpagereplay_remote_http_port, webpagereplay_remote_https_port): self._browser_backend = browser_backend self._forwarder = None self._web_page_replay = None self._is_record_mode = is_record_mode self._webpagereplay_host = webpagereplay_host self._webpagereplay_local_http_port = webpagereplay_local_http_port self._webpagereplay_local_https_port = webpagereplay_local_https_port self._webpagereplay_remote_http_port = webpagereplay_remote_http_port self._webpagereplay_remote_https_port = webpagereplay_remote_https_port self._forwarder = browser_backend.CreateForwarder( util.PortPair(self._webpagereplay_local_http_port, self._webpagereplay_remote_http_port), util.PortPair(self._webpagereplay_local_https_port, self._webpagereplay_remote_https_port)) options = browser_backend.options.extra_wpr_args if self._is_record_mode: options.append('--record') if not browser_backend.options.wpr_make_javascript_deterministic: options.append('--inject_scripts=') self._web_page_replay = webpagereplay.ReplayServer( path, self._webpagereplay_host, self._webpagereplay_local_http_port, self._webpagereplay_local_https_port, options) self._web_page_replay.StartServer()
def __init__(self, is_content_shell, supports_extensions, browser_options, output_profile_path, extensions_to_load): super(ChromeBrowserBackend, self).__init__(is_content_shell=is_content_shell, supports_extensions=supports_extensions, browser_options=browser_options, tab_list_backend=tab_list_backend.TabListBackend) self._port = None self._inspector_protocol_version = 0 self._chrome_branch_number = None self._tracing_backend = None self._system_info_backend = None self._output_profile_path = output_profile_path self._extensions_to_load = extensions_to_load self.wpr_http_port_pair = util.PortPair(0, 0) self.wpr_https_port_pair = util.PortPair(0, 0) if (self.browser_options.dont_override_profile and not options_for_unittests.AreSet()): sys.stderr.write( 'Warning: Not overriding profile. This can cause ' 'unexpected effects due to profile-specific settings, ' 'such as about:flags settings, cookies, and ' 'extensions.\n') self._misc_web_contents_backend = ( misc_web_contents_backend.MiscWebContentsBackend(self)) self._extension_dict_backend = None if supports_extensions: self._extension_dict_backend = ( extension_dict_backend.ExtensionDictBackend(self))
def __init__(self, driver_creator, supports_extensions, browser_options): super(WebDriverBrowserBackend, self).__init__(is_content_shell=False, supports_extensions=supports_extensions, browser_options=browser_options, tab_list_backend=webdriver_tab_list_backend. WebDriverTabListBackend) self._driver_creator = driver_creator self._driver = None self.wpr_http_port_pair = util.PortPair(80, 80) self.wpr_https_port_pair = util.PortPair(443, 443)
def testGetRemotePortAndIsHTTPServerRunningOnPort(self): remote = options_for_unittests.GetCopy().cros_remote cri = cros_interface.CrOSInterface( remote, options_for_unittests.GetCopy().cros_ssh_identity) # Create local server. sock = socket.socket() sock.bind(('', 0)) port = sock.getsockname()[1] sock.listen(0) # Get remote port and ensure that it was unused. remote_port = cri.GetRemotePort() self.assertFalse(cri.IsHTTPServerRunningOnPort(remote_port)) # Forward local server's port to remote device's remote_port. forwarder = cros_browser_backend.SSHForwarder( cri, 'R', util.PortPair(port, remote_port)) # At this point, remote device should be able to connect to local server. self.assertTrue(cri.IsHTTPServerRunningOnPort(remote_port)) # Next remote port shouldn't be the same as remote_port, since remote_port # is now in use. self.assertTrue(cri.GetRemotePort() != remote_port) # Close forwarder and local server ports. forwarder.Close() sock.close() # Device should no longer be able to connect to remote_port since it is no # longer in use. self.assertFalse(cri.IsHTTPServerRunningOnPort(remote_port))
def __init__(self, browser_backend, paths): self._server = None self._devnull = None self._paths = paths self._forwarder = None self._host_port = util.GetAvailableLocalPort() for path in self._paths: assert os.path.exists(path), path self._devnull = open(os.devnull, 'w') cmd = [ sys.executable, '-m', 'memory_cache_http_server', str(self._host_port) ] cmd.extend(self._paths) env = os.environ.copy() env['PYTHONPATH'] = os.path.abspath(os.path.dirname(__file__)) self._server = subprocess.Popen(cmd, cwd=os.path.commonprefix(self._paths), env=env, stdout=self._devnull, stderr=self._devnull) self._forwarder = browser_backend.CreateForwarder( util.PortPair(self._host_port, browser_backend.GetRemotePort(self._host_port))) def IsServerUp(): return not socket.socket().connect_ex( ('localhost', self._host_port)) util.WaitFor(IsServerUp, 10)
def Start(self): # Escape all commas in the startup arguments we pass to Chrome # because dbus-send delimits array elements by commas startup_args = [ a.replace(',', '\\,') for a in self.GetBrowserStartupArgs() ] # Restart Chrome with the login extension and remote debugging. logging.info('Restarting Chrome with flags and login') args = [ 'dbus-send', '--system', '--type=method_call', '--dest=org.chromium.SessionManager', '/org/chromium/SessionManager', 'org.chromium.SessionManagerInterface.EnableChromeTesting', 'boolean:true', 'array:string:"%s"' % ','.join(startup_args) ] self._cri.RunCmdOnDevice(args) if not self._cri.local: # Find a free local port. self._port = util.GetAvailableLocalPort() # Forward the remote debugging port. logging.info('Forwarding remote debugging port') self._forwarder = SSHForwarder( self._cri, 'L', util.PortPair(self._port, self._remote_debugging_port)) # Wait for the browser to come up. logging.info('Waiting for browser to be ready') try: self._WaitForBrowserToComeUp() self._PostBrowserStartupInitialization() except: import traceback traceback.print_exc() self.Close() raise # chrome_branch_number is set in _PostBrowserStartupInitialization. # Without --skip-hwid-check (introduced in crrev.com/203397), devices/VMs # will be stuck on the bad hwid screen. if self.chrome_branch_number <= 1500 and not self.hwid: raise exceptions.LoginException( 'Hardware id not set on device/VM. --skip-hwid-check not supported ' 'with chrome branches 1500 or earlier.') if self._is_guest: pid = self.pid self._NavigateGuestLogin() # Guest browsing shuts down the current browser and launches an incognito # browser in a separate process, which we need to wait for. util.WaitFor(lambda: pid != self.pid, 10) self._WaitForBrowserToComeUp() else: self._NavigateLogin() logging.info('Browser is up!')
def __init__(self, browser_backend, paths): self._server = None self._devnull = None self._forwarder = None for path in paths: assert os.path.exists(path), '%s does not exist.' % path self._paths = set(map(os.path.realpath, paths)) common_prefix = os.path.commonprefix(self._paths) if os.path.isdir(common_prefix): self._base_dir = common_prefix else: self._base_dir = os.path.dirname(common_prefix) self._devnull = open(os.devnull, 'w') cmd = [sys.executable, '-m', 'memory_cache_http_server'] cmd.extend(self._paths) env = os.environ.copy() env['PYTHONPATH'] = os.path.abspath(os.path.dirname(__file__)) self._server = subprocess.Popen(cmd, cwd=self._base_dir, env=env, stdout=subprocess.PIPE, stderr=self._devnull) port_re = re.compile( '.*(?P<protocol>HTTPS?) server started on (?P<host>.*):(?P<port>\d+)') while self._server.poll() == None: m = port_re.match(self._server.stdout.readline()) if m: port = int(m.group('port')) break self._forwarder = browser_backend.CreateForwarder( util.PortPair(port, browser_backend.GetRemotePort(port))) def IsServerUp(): return not socket.socket().connect_ex(('localhost', port)) util.WaitFor(IsServerUp, 10)
def __init__(self, browser_type, options, cri, is_guest): super(CrOSBrowserBackend, self).__init__(is_content_shell=False, supports_extensions=not is_guest, options=options) # Initialize fields so that an explosion during init doesn't break in Close. self._browser_type = browser_type self._options = options self._cri = cri self._is_guest = is_guest self._remote_debugging_port = self._cri.GetRemotePort() self._port = self._remote_debugging_port self._login_ext_dir = os.path.join(os.path.dirname(__file__), 'chromeos_login_ext') # Push a dummy login extension to the device. # This extension automatically logs in as [email protected] # Note that we also perform this copy locally to ensure that # the owner of the extensions is set to chronos. logging.info('Copying dummy login extension to the device') cri.PushFile(self._login_ext_dir, '/tmp/') self._login_ext_dir = '/tmp/chromeos_login_ext' cri.RunCmdOnDevice(['chown', '-R', 'chronos:chronos', self._login_ext_dir]) # Copy extensions to temp directories on the device. # Note that we also perform this copy locally to ensure that # the owner of the extensions is set to chronos. for e in options.extensions_to_load: output = cri.RunCmdOnDevice(['mktemp', '-d', '/tmp/extension_XXXXX']) extension_dir = output[0].rstrip() cri.PushFile(e.path, extension_dir) cri.RunCmdOnDevice(['chown', '-R', 'chronos:chronos', extension_dir]) e.local_path = os.path.join(extension_dir, os.path.basename(e.path)) # Ensure the UI is running and logged out. self._RestartUI() util.WaitFor(lambda: self.IsBrowserRunning(), 20) # pylint: disable=W0108 # Delete [email protected]'s cryptohome vault (user data directory). if not options.dont_override_profile: logging.info('Deleting user\'s cryptohome vault (the user data dir)') self._cri.RunCmdOnDevice( ['cryptohome', '--action=remove', '--force', '[email protected]']) if options.profile_dir: profile_dir = '/home/chronos/Default' cri.RunCmdOnDevice(['rm', '-rf', profile_dir]) cri.PushFile(options.profile_dir + '/Default', profile_dir) cri.RunCmdOnDevice(['chown', '-R', 'chronos:chronos', profile_dir]) # Escape all commas in the startup arguments we pass to Chrome # because dbus-send delimits array elements by commas startup_args = [a.replace(',', '\\,') for a in self.GetBrowserStartupArgs()] # Restart Chrome with the login extension and remote debugging. logging.info('Restarting Chrome with flags and login') args = ['dbus-send', '--system', '--type=method_call', '--dest=org.chromium.SessionManager', '/org/chromium/SessionManager', 'org.chromium.SessionManagerInterface.EnableChromeTesting', 'boolean:true', 'array:string:"%s"' % ','.join(startup_args)] cri.RunCmdOnDevice(args) if not cri.local: # Find a free local port. self._port = util.GetAvailableLocalPort() # Forward the remote debugging port. logging.info('Forwarding remote debugging port') self._forwarder = SSHForwarder( cri, 'L', util.PortPair(self._port, self._remote_debugging_port)) # Wait for the browser to come up. logging.info('Waiting for browser to be ready') try: self._WaitForBrowserToComeUp() self._PostBrowserStartupInitialization() except: import traceback traceback.print_exc() self.Close() raise # chrome_branch_number is set in _PostBrowserStartupInitialization. # Without --skip-hwid-check (introduced in crrev.com/203397), devices/VMs # will be stuck on the bad hwid screen. if self.chrome_branch_number <= 1500 and not self.hwid: raise exceptions.LoginException( 'Hardware id not set on device/VM. --skip-hwid-check not supported ' 'with chrome branches 1500 or earlier.') if self._is_guest: pid = self.pid cros_util.NavigateGuestLogin(self, cri) # Guest browsing shuts down the current browser and launches an incognito # browser in a separate process, which we need to wait for. util.WaitFor(lambda: pid != self.pid, 10) self._WaitForBrowserToComeUp() else: cros_util.NavigateLogin(self, cri) logging.info('Browser is up!')
def __init__(self, browser_type, options, cri, is_guest): super(CrOSBrowserBackend, self).__init__(is_content_shell=False, supports_extensions=True, options=options) # Initialize fields so that an explosion during init doesn't break in Close. self._browser_type = browser_type self._options = options self._cri = cri self._is_guest = is_guest self._remote_debugging_port = self._cri.GetRemotePort() self._port = self._remote_debugging_port self._login_ext_dir = os.path.join(os.path.dirname(__file__), 'chromeos_login_ext') if not cri.local: # Push a dummy login extension to the device. # This extension automatically logs in as [email protected] logging.info('Copying dummy login extension to the device') cri.PushFile(self._login_ext_dir, '/tmp/') self._login_ext_dir = '/tmp/chromeos_login_ext' cri.RunCmdOnDevice(['chown', '-R', 'chronos:chronos', self._login_ext_dir]) # Copy local extensions to temp directories on the device. for e in options.extensions_to_load: output = cri.RunCmdOnDevice(['mktemp', '-d', '/tmp/extension_XXXXX']) extension_dir = output[0].rstrip() cri.PushFile(e.path, extension_dir) cri.RunCmdOnDevice(['chown', '-R', 'chronos:chronos', extension_dir]) e.local_path = os.path.join(extension_dir, os.path.basename(e.path)) # Ensure the UI is running and logged out. self._RestartUI() util.WaitFor(lambda: self.IsBrowserRunning(), 20) # pylint: disable=W0108 # Delete [email protected]'s cryptohome vault (user data directory). if not options.dont_override_profile: logging.info('Deleting user\'s cryptohome vault (the user data dir)') self._cri.RunCmdOnDevice( ['cryptohome', '--action=remove', '--force', '[email protected]']) if options.profile_dir: profile_dir = '/home/chronos/Default' cri.GetCmdOutput(['rm', '-rf', profile_dir]) cri.PushFile(options.profile_dir + '/Default', profile_dir) cri.GetCmdOutput(['chown', '-R', 'chronos:chronos', profile_dir]) # Restart Chrome with the login extension and remote debugging. logging.info('Restarting Chrome with flags and login') args = ['dbus-send', '--system', '--type=method_call', '--dest=org.chromium.SessionManager', '/org/chromium/SessionManager', 'org.chromium.SessionManagerInterface.EnableChromeTesting', 'boolean:true', 'array:string:"%s"' % ','.join(self.GetBrowserStartupArgs())] cri.RunCmdOnDevice(args) if not cri.local: # Find a free local port. self._port = util.GetAvailableLocalPort() # Forward the remote debugging port. logging.info('Forwarding remote debugging port') self._forwarder = SSHForwarder( cri, 'L', util.PortPair(self._port, self._remote_debugging_port)) # Wait for the browser to come up. logging.info('Waiting for browser to be ready') try: self._WaitForBrowserToComeUp() self._PostBrowserStartupInitialization() except: import traceback traceback.print_exc() self.Close() raise if self._is_guest: cros_util.NavigateGuestLogin(self, cri) # Guest browsing shuts down the current browser and launches an incognito # browser, which we need to wait for. self._WaitForBrowserToComeUp() else: cros_util.NavigateLogin(self) logging.info('Browser is up!')