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."""
11 from telemetry import decorators
12 from telemetry.core import browser
13 from telemetry.core import platform
14 from telemetry.core import possible_browser
15 from telemetry.core.backends.chrome import inspector_backend
16 from telemetry.core.backends.chrome import ios_browser_backend
17 from telemetry.core.platform import ios_platform_backend
20 # Key matches output from ios-webkit-debug-proxy and the value is a readable
21 # description of the browser.
22 IOS_BROWSERS = {'CriOS': 'ios-chrome', 'Version': 'ios-safari'}
23 DEVICE_LIST_URL = 'http://127.0.0.1:9221/json'
24 IOS_WEBKIT_DEBUG_PROXY = 'ios_webkit_debug_proxy'
27 class PossibleIOSBrowser(possible_browser.PossibleBrowser):
29 """A running iOS browser instance."""
30 def __init__(self, browser_type, finder_options):
31 super(PossibleIOSBrowser, self).__init__(browser_type, 'ios',
34 # TODO(baxley): Implement the following methods for iOS.
36 backend = ios_browser_backend.IosBrowserBackend(
37 self.finder_options.browser_options)
38 return browser.Browser(backend,
39 self._platform_backend,
41 self._append_to_existing_wpr,
42 self._make_javascript_deterministic,
43 self._credentials_path)
45 def SupportsOptions(self, finder_options):
46 #TODO(baxley): Implement me.
49 def UpdateExecutableIfNeeded(self):
50 #TODO(baxley): Implement me.
53 def _InitPlatformIfNeeded(self):
57 self._platform_backend = ios_platform_backend.IosPlatformBackend()
58 self._platform = platform.Platform(self._platform_backend)
60 def SelectDefaultBrowser(_):
61 return None # TODO(baxley): Implement me.
64 def CanFindAvailableBrowsers():
65 # TODO(baxley): Add support for all platforms possible. Probably Linux,
66 # probably not Windows.
67 return platform.GetHostPlatform().GetOSName() == 'mac'
70 def FindAllBrowserTypes(_):
71 return IOS_BROWSERS.values()
75 def _IsIosDeviceAttached():
76 devices = subprocess.check_output('system_profiler SPUSBDataType', shell=True)
77 for line in devices.split('\n'):
78 if line and re.match('\s*(iPod|iPhone|iPad):', line):
83 def FindAllAvailableBrowsers(finder_options):
84 """Find all running iOS browsers on connected devices."""
85 if not CanFindAvailableBrowsers():
88 if not _IsIosDeviceAttached():
91 options = finder_options.browser_options
93 options.browser_type = 'ios-chrome'
94 backend = ios_browser_backend.IosBrowserBackend(options)
95 host = platform.GetHostPlatform()
96 # TODO(baxley): Use idevice to wake up device or log debug statement.
97 if not host.IsApplicationRunning(IOS_WEBKIT_DEBUG_PROXY):
98 host.LaunchApplication(IOS_WEBKIT_DEBUG_PROXY)
99 if not host.IsApplicationRunning(IOS_WEBKIT_DEBUG_PROXY):
102 device_urls = backend.GetDeviceUrls()
104 logging.debug('Could not find any devices over %s.'
105 % IOS_WEBKIT_DEBUG_PROXY)
108 debug_urls = backend.GetWebSocketDebuggerUrls(device_urls)
110 # Get the userAgent for each UIWebView to find the browsers.
111 browser_pattern = ('\)\s(%s)\/(\d+[\.\d]*)\sMobile'
112 % '|'.join(IOS_BROWSERS.keys()))
113 browser_types = set()
114 for url in debug_urls:
115 context = {'webSocketDebuggerUrl':url , 'id':1}
116 inspector = inspector_backend.InspectorBackend(backend, context)
117 res = inspector.EvaluateJavaScript("navigator.userAgent")
118 match_browsers = re.search(browser_pattern, res)
120 browser_types.add(match_browsers.group(1))
123 for browser_type in browser_types:
124 browsers.append(PossibleIOSBrowser(IOS_BROWSERS[browser_type],
126 return list(browsers)