| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 import glob | 4 import glob |
| 5 import heapq | 5 import heapq |
| 6 import logging | 6 import logging |
| 7 import os | 7 import os |
| 8 import subprocess as subprocess | 8 import subprocess as subprocess |
| 9 import shutil | 9 import shutil |
| 10 import sys | 10 import sys |
| 11 import tempfile | 11 import tempfile |
| 12 import time | 12 import time |
| 13 | 13 |
| 14 from telemetry.core import util | 14 from telemetry.core import util |
| 15 from telemetry.core.backends import browser_backend | 15 from telemetry.core.backends import browser_backend |
| 16 from telemetry.core.backends.chrome import chrome_browser_backend | 16 from telemetry.core.backends.chrome import chrome_browser_backend |
| 17 | 17 |
| 18 class DesktopBrowserBackend(chrome_browser_backend.ChromeBrowserBackend): | 18 class DesktopBrowserBackend(chrome_browser_backend.ChromeBrowserBackend): |
| 19 """The backend for controlling a locally-executed browser instance, on Linux, | 19 """The backend for controlling a locally-executed browser instance, on Linux, |
| 20 Mac or Windows. | 20 Mac or Windows. |
| 21 """ | 21 """ |
| 22 def __init__(self, options, executable, flash_path, is_content_shell, | 22 def __init__(self, options, executable, flash_path, is_content_shell, |
| 23 browser_directory, delete_profile_dir_after_run=True): | 23 browser_directory, output_profile_path=None): |
| 24 super(DesktopBrowserBackend, self).__init__( | 24 super(DesktopBrowserBackend, self).__init__( |
| 25 is_content_shell=is_content_shell, | 25 is_content_shell=is_content_shell, |
| 26 supports_extensions=not is_content_shell, | 26 supports_extensions=not is_content_shell, |
| 27 options=options) | 27 options=options) |
| 28 | 28 |
| 29 # Initialize fields so that an explosion during init doesn't break in Close. | 29 # Initialize fields so that an explosion during init doesn't break in Close. |
| 30 self._proc = None | 30 self._proc = None |
| 31 self._tmp_profile_dir = None | 31 self._tmp_profile_dir = None |
| 32 self._tmp_output_file = None | 32 self._tmp_output_file = None |
| 33 | 33 |
| 34 self._executable = executable | 34 self._executable = executable |
| 35 if not self._executable: | 35 if not self._executable: |
| 36 raise Exception('Cannot create browser, no executable found!') | 36 raise Exception('Cannot create browser, no executable found!') |
| 37 | 37 |
| 38 self._flash_path = flash_path | 38 self._flash_path = flash_path |
| 39 if self._flash_path and not os.path.exists(self._flash_path): | 39 if self._flash_path and not os.path.exists(self._flash_path): |
| 40 logging.warning(('Could not find flash at %s. Running without flash.\n\n' | 40 logging.warning(('Could not find flash at %s. Running without flash.\n\n' |
| 41 'To fix this see http://go/read-src-internal') % | 41 'To fix this see http://go/read-src-internal') % |
| 42 self._flash_path) | 42 self._flash_path) |
| 43 self._flash_path = None | 43 self._flash_path = None |
| 44 | 44 |
| 45 if len(options.extensions_to_load) > 0 and is_content_shell: | 45 if len(options.extensions_to_load) > 0 and is_content_shell: |
| 46 raise browser_backend.ExtensionsNotSupportedException( | 46 raise browser_backend.ExtensionsNotSupportedException( |
| 47 'Content shell does not support extensions.') | 47 'Content shell does not support extensions.') |
| 48 | 48 |
| 49 self._browser_directory = browser_directory | 49 self._browser_directory = browser_directory |
| 50 self._port = util.GetAvailableLocalPort() | 50 self._port = util.GetAvailableLocalPort() |
| 51 self._profile_dir = None | 51 self._profile_dir = None |
| 52 self._supports_net_benchmarking = True | 52 self._supports_net_benchmarking = True |
| 53 self._delete_profile_dir_after_run = delete_profile_dir_after_run | 53 self._output_profile_path = output_profile_path |
| 54 self._tmp_minidump_dir = tempfile.mkdtemp() | 54 self._tmp_minidump_dir = tempfile.mkdtemp() |
| 55 | 55 |
| 56 self._SetupProfile() | 56 self._SetupProfile() |
| 57 | 57 |
| 58 def _SetupProfile(self): | 58 def _SetupProfile(self): |
| 59 if not self.options.dont_override_profile: | 59 if not self.options.dont_override_profile: |
| 60 self._tmp_profile_dir = tempfile.mkdtemp() | 60 self._tmp_profile_dir = tempfile.mkdtemp() |
| 61 profile_dir = self._profile_dir or self.options.profile_dir | 61 profile_dir = self._profile_dir or self.options.profile_dir |
| 62 if profile_dir: | 62 if profile_dir: |
| 63 if self.is_content_shell: | 63 if self.is_content_shell: |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 # Kill it. | 214 # Kill it. |
| 215 if not IsClosed(): | 215 if not IsClosed(): |
| 216 self._proc.kill() | 216 self._proc.kill() |
| 217 try: | 217 try: |
| 218 util.WaitFor(IsClosed, timeout=5) | 218 util.WaitFor(IsClosed, timeout=5) |
| 219 self._proc = None | 219 self._proc = None |
| 220 except util.TimeoutException: | 220 except util.TimeoutException: |
| 221 self._proc = None | 221 self._proc = None |
| 222 raise Exception('Could not shutdown the browser.') | 222 raise Exception('Could not shutdown the browser.') |
| 223 | 223 |
| 224 if self._delete_profile_dir_after_run and \ | 224 # Copy profile aside if requested. |
| 225 self._tmp_profile_dir and os.path.exists(self._tmp_profile_dir): | 225 if self._output_profile_path: |
| 226 if not (self._tmp_profile_dir and os.path.exists(self._tmp_profile_dir)): |
| 227 raise Exception("No profile directory generated by Chrome: '%s'." % |
| 228 self._tmp_profile_dir) |
| 229 shutil.move(self._tmp_profile_dir, self._output_profile_path) |
| 230 logging.info("Generated profile moved teporarily to '%s'." % |
| 231 self._output_profile_path) |
| 232 |
| 233 if self._tmp_profile_dir and os.path.exists(self._tmp_profile_dir): |
| 226 shutil.rmtree(self._tmp_profile_dir, ignore_errors=True) | 234 shutil.rmtree(self._tmp_profile_dir, ignore_errors=True) |
| 227 self._tmp_profile_dir = None | 235 self._tmp_profile_dir = None |
| 228 | 236 |
| 229 if self._tmp_output_file: | 237 if self._tmp_output_file: |
| 230 self._tmp_output_file.close() | 238 self._tmp_output_file.close() |
| 231 self._tmp_output_file = None | 239 self._tmp_output_file = None |
| 232 | 240 |
| 233 def CreateForwarder(self, *port_pairs): | 241 def CreateForwarder(self, *port_pairs): |
| 234 return browser_backend.DoNothingForwarder(*port_pairs) | 242 return browser_backend.DoNothingForwarder(*port_pairs) |
| OLD | NEW |