Chromium Code Reviews| Index: tools/chrome_remote_control/chrome_remote_control/local_browser.py |
| diff --git a/tools/chrome_remote_control/chrome_remote_control/local_browser.py b/tools/chrome_remote_control/chrome_remote_control/local_browser.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5a781936a7181e4c64cbd59aa98700d12feca631 |
| --- /dev/null |
| +++ b/tools/chrome_remote_control/chrome_remote_control/local_browser.py |
| @@ -0,0 +1,87 @@ |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
dtu
2012/08/27 22:27:27
Was this file intended to be committed? Seems redu
nduca
2012/08/28 00:20:16
Thanks!
|
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| +import os as real_os |
| +import sys as real_sys |
| +import subprocess as real_subprocess |
| +import urllib2 |
| +import json |
| +import tab |
| + |
| +import browser_finder |
| + |
| +class LocalBrowser(object): |
| + """A running browser instance that can be controled in a limited way by |
|
dtu
2012/08/27 22:27:27
controlled
|
| + python. |
| + |
| + To create a browser instance, use browser_finder.FindBestPossibleBrowser. |
| + """ |
| + |
| + def __init__(self, options): |
| + self._executable = browser_finder.Find(options) |
| + if not self._executable: |
| + raise Exception("Cannot create browser, no executable found!") |
| + |
| + # TODO(nduca): Delete the user data dir or create one that is unique. |
| + tmpdir = "/tmp/simple-browser-controller-profile" |
| + if real_os.path.exists(tmpdir): |
| + ret = real_os.system("rm -rf -- %s" % tmpdir) |
| + assert ret == 0 |
| + self._port = options.remote_debugging_port |
| + args = [self._executable, |
| + "--no-first-run", |
| + "--remote-debugging-port=%i" % self._port] |
| + if not options.dont_override_profile: |
| + args.append("--user-data-dir=%s" % tmpdir) |
| + args.extend(options.args) |
| + if options.hide_stdout: |
| + self._devnull = open(real_os.devnull, 'w') |
| + self._proc = real_subprocess.Popen( |
| + args,stdout=self._devnull, stderr=self._devnull) |
| + else: |
| + self._devnull = None |
| + self._proc = real_subprocess.Popen(args) |
| + |
| + try: |
| + self._WaitForBrowserToComeUp() |
| + except: |
| + self.Close() |
| + raise |
| + |
| + def _WaitForBrowserToComeUp(self): |
| + while True: |
| + try: |
| + self._ListTabs() |
| + break |
| + except urllib2.URLError, ex: |
| + pass |
| + |
| + def _ListTabs(self): |
| + req = urllib2.urlopen("http://localhost:%i/json" % self._port) |
| + data = req.read() |
| + return json.loads(data) |
| + |
| + @property |
| + def num_tabs(self): |
| + return len(self._ListTabs()) |
| + |
| + def GetNthTabURL(self, index): |
| + return self._ListTabs()[index]["url"] |
| + |
| + def ConnectToNthTab(self, index): |
| + return tab.Tab(self, self._ListTabs()[index]) |
| + |
| + def __enter__(self): |
| + return self |
| + |
| + def __exit__(self, *args): |
| + if self._proc: |
| + self.Close() |
| + |
| + def Close(self): |
| + self._proc.terminate() |
| + self._proc.wait() |
| + self._proc = None |
| + |
| + if self._devnull: |
| + self._devnull.close() |