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.
5 """Finds iOS browsers that can be controlled by telemetry."""
14 from telemetry.core import platform
15 from telemetry.core import possible_browser
16 from telemetry.core import util
17 from telemetry.core.backends.chrome import inspector_backend
20 class PossibleIOSBrowser(possible_browser.PossibleBrowser):
22 """A running iOS browser instance."""
23 def __init__(self, browser_type, finder_options):
24 super(PossibleIOSBrowser, self).__init__(browser_type, 'ios',
27 # TODO(baxley): Implement the following methods for iOS.
29 raise NotImplementedError()
31 def SupportOptions(self, finder_options):
32 raise NotImplementedError()
34 # Key matches output from ios-webkit-debug-proxy and the value is a readable
35 # description of the browser.
36 IOS_BROWSERS = {'CriOS': 'ios-chrome', 'Version': 'ios-safari'}
38 DEVICE_LIST_URL = 'http://127.0.0.1:9221/json'
40 IOS_WEBKIT_DEBUG_PROXY = 'ios_webkit_debug_proxy'
43 def SelectDefaultBrowser(_):
44 return None # TODO(baxley): Implement me.
47 def CanFindAvailableBrowsers():
48 return False # TODO(baxley): Implement me.
51 def FindAllBrowserTypes():
52 return IOS_BROWSERS.values()
55 def FindAllAvailableBrowsers(finder_options):
56 """Find all running iOS browsers on connected devices."""
57 ios_device_attached = False
58 host = platform.GetHostPlatform()
59 if host.GetOSName() == 'mac':
60 devices = subprocess.check_output(
61 'system_profiler SPUSBDataType', shell=True)
62 ios_devices = 'iPod|iPhone|iPad'
63 for line in devices.split('\n'):
65 m = re.match('\s*(%s):' % ios_devices, line)
67 ios_device_attached = True
70 # TODO(baxley): Add support for all platforms possible. Probably Linux,
71 # probably not Windows.
74 if ios_device_attached:
75 # TODO(baxley) Use idevice to wake up device or log debug statement.
76 if not host.IsApplicationRunning(IOS_WEBKIT_DEBUG_PROXY):
77 host.LaunchApplication(IOS_WEBKIT_DEBUG_PROXY)
78 if not host.IsApplicationRunning(IOS_WEBKIT_DEBUG_PROXY):
84 # TODO(baxley): Refactor this into a backend file.
85 with contextlib.closing(
86 urllib2.urlopen(DEVICE_LIST_URL), timeout=10) as device_list:
87 json_urls = device_list.read()
88 device_urls = json.loads(json_urls)
90 logging.debug('No iOS devices found. Will not try searching for iOS '
93 except urllib2.URLError as e:
94 logging.error('Error communicating with devices over %s.'
95 % IOS_WEBKIT_DEBUG_PROXY)
99 # TODO(baxley): Move to ios-webkit-debug-proxy command class, similar
100 # to GetAttachedDevices() in adb_commands.
102 # Loop through all devices.
103 for d in device_urls:
104 # Retry a few times since it can take a few seconds for this API to be
105 # ready, if ios_webkit_debug_proxy is just launched.
108 with contextlib.closing(
109 urllib2.urlopen('http://%s/json' % d['url']), timeout=10) as f:
110 json_result = f.read()
111 data = json.loads(json_result)
113 except urllib2.URLError as e:
114 logging.error('Error communicating with device over %s.'
115 % IOS_WEBKIT_DEBUG_PROXY)
116 logging.error(str(e))
119 data = util.WaitFor(GetData, 5)
120 except util.TimeoutException as e:
123 # Find all running UIWebViews.
126 debug_urls.append(j['webSocketDebuggerUrl'])
128 # Get the userAgent for each UIWebView to find the browsers.
129 browser_pattern = ('\)\s(%s)\/(\d+[\.\d]*)\sMobile'
130 % '|'.join(IOS_BROWSERS.keys()))
132 for url in debug_urls:
133 context = {'webSocketDebuggerUrl':url , 'id':1}
134 # TODO(baxley): Replace None with ios_browser_backend, once implemented.
135 inspector_alt = inspector_backend.InspectorBackend(None, context)
136 res = inspector_alt.EvaluateJavaScript("navigator.userAgent")
137 match_browsers = re.search(browser_pattern, res)
139 browsers.append(PossibleIOSBrowser(IOS_BROWSERS[match_browsers.group(1)],