def ListInspectableContexts(self): try: return self._devtools_client.ListInspectableContexts() except devtools_http.DevToolsClientConnectionError as e: if not self.IsBrowserRunning(): raise exceptions.BrowserGoneException(self.browser, e) raise exceptions.BrowserConnectionGoneException(self.browser, e)
def GetSystemInfo(self): try: return self.devtools_client.GetSystemInfo() except (inspector_websocket.WebSocketException, socket.error) as e: if not self.IsBrowserRunning(): raise exceptions.BrowserGoneException(self.browser, e) raise exceptions.BrowserConnectionGoneException(self.browser, e)
def _HandleDevToolsConnectionError(self, err_msg): if not self._browser_backend.IsAppRunning(): raise exceptions.BrowserGoneException(self.app, err_msg) elif not self._browser_backend.HasBrowserFinishedLaunching(): raise exceptions.BrowserConnectionGoneException(self.app, err_msg) else: raise exceptions.DevtoolsTargetCrashException(self.app, err_msg)
def __init__(self, browser_backend, context, timeout=60): super(InspectorBackend, self).__init__(self._HandleError) self.RegisterDomain('Inspector', self._HandleInspectorDomainNotification) self._browser_backend = browser_backend self._context = context logging.debug('InspectorBackend._Connect() to %s', self.debugger_url) try: self.Connect(self.debugger_url) except (websocket.WebSocketException, util.TimeoutException): err_msg = sys.exc_info()[1] if not self._browser_backend.IsAppRunning(): raise exceptions.BrowserGoneException(self.app, err_msg) elif not self._browser_backend.HasBrowserFinishedLaunching(): raise exceptions.BrowserConnectionGoneException(self.app, err_msg) else: raise exceptions.DevtoolsTargetCrashException(self.app, err_msg) self._console = inspector_console.InspectorConsole(self) self._memory = inspector_memory.InspectorMemory(self) self._page = inspector_page.InspectorPage(self, timeout=timeout) self._runtime = inspector_runtime.InspectorRuntime(self) self._timeline = inspector_timeline.InspectorTimeline(self) self._network = inspector_network.InspectorNetwork(self) self._timeline_model = None
def _ListWebContents(self, timeout=None): try: data = self._browser_backend.Request('', timeout=timeout) return json.loads(data) except (socket.error, httplib.BadStatusLine, urllib2.URLError): if not self._browser_backend.IsBrowserRunning(): raise exceptions.BrowserGoneException() raise exceptions.BrowserConnectionGoneException()
def _WaitForBrowserToComeUp(self): """ Wait for browser to come up. """ try: timeout = self.browser_options.browser_startup_timeout py_utils.WaitFor(self.HasBrowserFinishedLaunching, timeout=timeout) except (py_utils.TimeoutException, exceptions.ProcessGoneException) as e: if not self.IsBrowserRunning(): raise exceptions.BrowserGoneException(self.browser, e) raise exceptions.BrowserConnectionGoneException(self.browser, e)
def _WaitForBrowserToComeUp(self, wait_for_extensions=True): try: util.WaitFor(self.HasBrowserFinishedLaunching, timeout=30) except (util.TimeoutException, exceptions.ProcessGoneException) as e: if not self.IsBrowserRunning(): raise exceptions.BrowserGoneException(self.browser, e) raise exceptions.BrowserConnectionGoneException(self.browser, e) def AllExtensionsLoaded(): # Extension pages are loaded from an about:blank page, # so we need to check that the document URL is the extension # page in addition to the ready state. extension_ready_js = """ document.URL.lastIndexOf('chrome-extension://%s/', 0) == 0 && (document.readyState == 'complete' || document.readyState == 'interactive') """ for e in self._extensions_to_load: try: extension_objects = self.extension_backend[e.extension_id] except KeyError: return False for extension_object in extension_objects: try: res = extension_object.EvaluateJavaScript( extension_ready_js % e.extension_id) except exceptions.EvaluateException: # If the inspected page is not ready, we will get an error # when we evaluate a JS expression, but we can just keep polling # until the page is ready (crbug.com/251913). res = None # TODO(tengs): We don't have full support for getting the Chrome # version before launch, so for now we use a generic workaround to # check for an extension binding bug in old versions of Chrome. # See crbug.com/263162 for details. if res and extension_object.EvaluateJavaScript( 'chrome.runtime == null'): extension_object.Reload() if not res: return False return True if wait_for_extensions and self._supports_extensions: try: util.WaitFor(AllExtensionsLoaded, timeout=60) except util.TimeoutException: logging.error( 'ExtensionsToLoad: ' + repr([e.extension_id for e in self._extensions_to_load])) logging.error('Extension list: ' + pprint.pformat(self.extension_backend, indent=4)) raise
def Request(self, path, timeout=None, throw_network_exception=False): url = 'http://localhost:%i/json' % self._port if path: url += '/' + path try: req = urllib2.urlopen(url, timeout=timeout) return req.read() except (socket.error, httplib.BadStatusLine, urllib2.URLError) as e: if throw_network_exception: raise e if not self.IsBrowserRunning(): raise exceptions.BrowserGoneException() raise exceptions.BrowserConnectionGoneException()
def Request(self, path, timeout=30, throw_network_exception=False): url = 'http://127.0.0.1:%i/json' % self._port if path: url += '/' + path try: proxy_handler = urllib2.ProxyHandler({}) # Bypass any system proxy. opener = urllib2.build_opener(proxy_handler) with contextlib.closing(opener.open(url, timeout=timeout)) as req: return req.read() except (socket.error, httplib.BadStatusLine, urllib2.URLError) as e: if throw_network_exception: raise e if not self.IsBrowserRunning(): raise exceptions.BrowserGoneException(self.browser, e) raise exceptions.BrowserConnectionGoneException(self.browser, e)
def _ListTabs(self, timeout=None): def _IsTab(context): if 'type' in context: return context['type'] == 'page' # TODO: For compatibility with Chrome before r177683. # This check is not completely correct, see crbug.com/190592. return not context['url'].startswith('chrome-extension://') try: data = self._browser_backend.Request('', timeout=timeout) all_contexts = json.loads(data) tabs = filter(_IsTab, all_contexts) return tabs except (socket.error, httplib.BadStatusLine, urllib2.URLError): if not self._browser_backend.IsBrowserRunning(): raise exceptions.BrowserGoneException() raise exceptions.BrowserConnectionGoneException()
def _Connect(self, timeout=10): assert not self._socket logging.debug('InspectorBackend._Connect() to %s' % self.debugger_url) try: self._socket = websocket.create_connection(self.debugger_url, timeout=timeout) except (websocket.WebSocketException, util.TimeoutException): err_msg = sys.exc_info()[1] if not self._browser_backend.IsBrowserRunning(): raise exceptions.BrowserGoneException(err_msg) elif not self._browser_backend.HasBrowserFinishedLaunching(): raise exceptions.BrowserConnectionGoneException(err_msg) else: raise exceptions.TabCrashException(err_msg) self._cur_socket_timeout = 0 self._next_request_id = 0
def BindDevToolsClient(self): """Find an existing DevTools agent and bind this browser backend to it.""" if self._devtools_client: # In case we are launching a second browser instance (as is done by # the CrOS backend), ensure that the old devtools_client is closed, # otherwise re-creating it will fail. self._devtools_client.Close() self._devtools_client = None try: self._devtools_client = py_utils.WaitFor( self._GetDevToolsClient, timeout=self.browser_options.browser_startup_timeout) except (py_utils.TimeoutException, exceptions.ProcessGoneException) as e: if not self.IsBrowserRunning(): logging.exception(e) # crbug.com/940075 raise exceptions.BrowserGoneException(self.browser, e) raise exceptions.BrowserConnectionGoneException(self.browser, e)
def _WaitForBrowserToComeUp(self, remote_devtools_port=None): """ Wait for browser to come up. Args: remote_devtools_port: The remote devtools port, if any. Otherwise assumed to be the same as self._port. """ try: timeout = self.browser_options.browser_startup_timeout py_utils.WaitFor(self.HasBrowserFinishedLaunching, timeout=timeout) except (py_utils.TimeoutException, exceptions.ProcessGoneException) as e: if not self.IsBrowserRunning(): raise exceptions.BrowserGoneException(self.browser, e) raise exceptions.BrowserConnectionGoneException(self.browser, e) self._devtools_client = devtools_client_backend.DevToolsClientBackend( self._port, self._browser_target, remote_devtools_port or self._port, self)
def BindDevToolsClient(self): """Find an existing DevTools agent and bind this browser backend to it.""" if self._devtools_client: # In case we are launching a second browser instance (as is done by # the CrOS backend), ensure that the old devtools_client is closed, # otherwise re-creating it will fail. self._devtools_client.Close() self._devtools_client = None try: timeout = self.browser_options.browser_startup_timeout # TODO(crbug.com/787834): Subclasses should WaitFor the config if needed. devtools_config = py_utils.WaitFor(self._GetDevToolsClientConfig, timeout=timeout) self._devtools_client = devtools_config.WaitForAndCreate( timeout=timeout) except (py_utils.TimeoutException, exceptions.ProcessGoneException) as e: if not self.IsBrowserRunning(): raise exceptions.BrowserGoneException(self.browser, e) raise exceptions.BrowserConnectionGoneException(self.browser, e)
def _WaitForBrowserToComeUp(self, remote_devtools_port=None): """ Wait for browser to come up. Args: remote_devtools_port: The remote devtools port, if any. Otherwise assumed to be the same as self._port. """ if self._devtools_client: # In case we are launching a second browser instance (as is done by # the CrOS backend), ensure that the old devtools_client is closed, # otherwise re-creating it will fail. self._devtools_client.Close() self._devtools_client = None try: timeout = self.browser_options.browser_startup_timeout py_utils.WaitFor(self.HasBrowserFinishedLaunching, timeout=timeout) except (py_utils.TimeoutException, exceptions.ProcessGoneException) as e: if not self.IsBrowserRunning(): raise exceptions.BrowserGoneException(self.browser, e) raise exceptions.BrowserConnectionGoneException(self.browser, e) self._devtools_client = devtools_client_backend.DevToolsClientBackend( self._port, self._browser_target, remote_devtools_port or self._port, self)