OLD | NEW |
---|---|
(Empty) | |
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 | |
3 # found in the LICENSE file. | |
4 import os as real_os | |
5 import sys as real_sys | |
6 import subprocess as real_subprocess | |
7 import urllib2 | |
8 import json | |
9 | |
10 import browser_finder | |
11 import inspector_backend | |
12 import tab | |
13 | |
14 DEFAULT_PORT = 9273 | |
15 | |
16 class DesktopBrowserBackend(object): | |
17 """The backend for controlling a locally-executed browser instance, on Linux, | |
18 Mac or Windows. | |
19 """ | |
20 def __init__(self, executable, options): | |
21 self._executable = executable | |
22 if not self._executable: | |
23 raise Exception("Cannot create browser, no executable found!") | |
24 | |
25 # TODO(nduca): Delete the user data dir or create one that is unique. | |
dtu
2012/08/27 22:27:27
This is just tempfile.mkdtemp(). And then delete i
| |
26 tmpdir = "/tmp/simple-browser-controller-profile" | |
27 if real_os.path.exists(tmpdir): | |
28 ret = real_os.system("rm -rf -- %s" % tmpdir) | |
29 assert ret == 0 | |
30 self._port = DEFAULT_PORT | |
31 args = [self._executable, | |
32 "--no-first-run", | |
33 "--remote-debugging-port=%i" % self._port] | |
34 if not options.dont_override_profile: | |
35 args.append("--user-data-dir=%s" % tmpdir) | |
36 args.extend(options.extra_browser_args) | |
37 if options.hide_stdout: | |
38 self._devnull = open(real_os.devnull, 'w') | |
39 self._proc = real_subprocess.Popen( | |
40 args,stdout=self._devnull, stderr=self._devnull) | |
41 else: | |
42 self._devnull = None | |
43 self._proc = real_subprocess.Popen(args) | |
44 | |
45 try: | |
46 self._WaitForBrowserToComeUp() | |
47 except: | |
48 self.Close() | |
49 raise | |
50 | |
51 def _WaitForBrowserToComeUp(self): | |
52 while True: | |
53 try: | |
54 self._ListTabs() | |
55 break | |
56 except urllib2.URLError, ex: | |
57 pass | |
58 | |
59 def _ListTabs(self): | |
60 req = urllib2.urlopen("http://localhost:%i/json" % self._port) | |
61 data = req.read() | |
62 return json.loads(data) | |
63 | |
64 @property | |
65 def num_tabs(self): | |
66 return len(self._ListTabs()) | |
67 | |
68 def GetNthTabURL(self, index): | |
69 return self._ListTabs()[index]["url"] | |
70 | |
71 def ConnectToNthTab(self, index): | |
72 ib = inspector_backend.InspectorBackend(self, self._ListTabs()[index]) | |
73 return tab.Tab(ib) | |
74 | |
75 def Close(self): | |
76 self._proc.terminate() | |
77 self._proc.wait() | |
78 self._proc = None | |
79 | |
80 if self._devnull: | |
81 self._devnull.close() | |
OLD | NEW |