1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
11 from telemetry.core import util
12 from telemetry.core.backends.chrome import chrome_browser_backend
13 from telemetry.core.backends.chrome import system_info_backend
16 class IosBrowserBackend(chrome_browser_backend.ChromeBrowserBackend):
17 _DEBUGGER_URL_BUILDER = 'ws://localhost:%i/devtools/page/%i'
18 _DEBUGGER_URL_REGEX = 'ws://localhost:(\d+)/devtools/page/(\d+)'
19 _DEVICE_LIST_URL = 'http://localhost:9221/json'
21 def __init__(self, ios_platform_backend, browser_options):
22 super(IosBrowserBackend, self).__init__(
24 supports_tab_control=False,
25 supports_extensions=False,
26 browser_options=browser_options,
27 output_profile_path=".",
28 extensions_to_load=None)
32 self.UpdateRunningBrowsersInfo()
34 def UpdateRunningBrowsersInfo(self):
35 """ Refresh to match current state of the running browser.
37 device_urls = self.GetDeviceUrls()
38 urls = self.GetWebSocketDebuggerUrls(device_urls)
40 m = re.match(self._DEBUGGER_URL_REGEX, url)
42 self._webviews.append([int(m.group(1)), int(m.group(2))])
44 logging.error('Unexpected url format: %s' % url)
46 # TODO(baxley): For now, grab first item from |_webviews|. Ideally, we'd
47 # prefer to have the currently displayed tab, or something similar.
49 self._port = self._webviews[0][0]
50 self._page = self._webviews[0][1]
52 def GetDeviceUrls(self):
55 with contextlib.closing(
56 urllib2.urlopen(self._DEVICE_LIST_URL)) as device_list:
57 json_urls = device_list.read()
58 device_urls = json.loads(json_urls)
60 logging.debug('No iOS devices found. Will not try searching for iOS '
63 except urllib2.URLError as e:
64 logging.debug('Error communicating with iOS device.')
69 def GetWebSocketDebuggerUrls(self, device_urls):
70 """ Get a list of the websocket debugger URLs to communicate with
71 all running UIWebViews.
74 # Loop through all devices.
78 with contextlib.closing(
79 urllib2.urlopen('http://%s/json' % d['url'])) as f:
80 json_result = f.read()
81 data = json.loads(json_result)
83 except urllib2.URLError as e:
84 logging.debug('Error communicating with iOS device.')
88 # Retry a few times since it can take a few seconds for this API to be
89 # ready, if ios_webkit_debug_proxy is just launched.
90 data = util.WaitFor(GetData, 5)
91 except util.TimeoutException as e:
92 logging.debug('Timeout retrieving data from iOS device')
96 # Find all running UIWebViews.
99 debug_urls.append(j['webSocketDebuggerUrl'])
103 def GetSystemInfo(self):
104 if self._system_info_backend is None:
105 self._system_info_backend = system_info_backend.SystemInfoBackend(
106 self._port, self._page)
107 return self._system_info_backend.GetSystemInfo()
109 def ListInspectableContexts(self):
110 response = json.loads(self.Request(''))
111 if len(response) != len(self._webviews):
112 self.UpdateRunningBrowsersInfo()
113 for i in range(len(response)):
114 response[i]['id'] = 1
117 def IsBrowserRunning(self):
118 return bool(self._webviews)
120 #TODO(baxley): The following were stubbed out to get the sunspider benchmark
121 # running. These should be implemented.
123 def browser_directory(self):
124 logging.warn('Not implemented')
128 def profile_directory(self):
129 logging.warn('Not implemented')
133 logging.warn('Not implemented')
135 def extension_backend(self):
136 logging.warn('Not implemented')
139 def GetBrowserStartupArgs(self):
140 logging.warn('Not implemented')
143 def HasBrowserFinishedLaunching(self):
144 logging.warn('Not implemented')
147 def GetStandardOutput(self):
148 raise NotImplementedError()
150 def GetStackTrace(self):
151 raise NotImplementedError()