Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(525)

Side by Side Diff: lib/src/runner/browser/chrome.dart

Issue 1175163003: Factor out some common logic from the launchers. (Closed) Base URL: git@github.com:dart-lang/test@master
Patch Set: fix test Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/runner/browser/browser.dart ('k') | lib/src/runner/browser/content_shell.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library test.runner.browser.chrome; 5 library test.runner.browser.chrome;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert';
9 import 'dart:io'; 8 import 'dart:io';
10 9
11 import 'package:path/path.dart' as p; 10 import 'package:path/path.dart' as p;
12 import 'package:stack_trace/stack_trace.dart';
13 11
14 import '../../util/io.dart'; 12 import '../../util/io.dart';
15 import '../../utils.dart';
16 import '../application_exception.dart';
17 import 'browser.dart'; 13 import 'browser.dart';
18 14
19 // TODO(nweiz): move this into its own package? 15 // TODO(nweiz): move this into its own package?
20 // TODO(nweiz): support other browsers.
21 /// A class for running an instance of Chrome. 16 /// A class for running an instance of Chrome.
22 /// 17 ///
23 /// Most of the communication with the browser is expected to happen via HTTP, 18 /// Most of the communication with the browser is expected to happen via HTTP,
24 /// so this exposes a bare-bones API. The browser starts as soon as the class is 19 /// so this exposes a bare-bones API. The browser starts as soon as the class is
25 /// constructed, and is killed when [close] is called. 20 /// constructed, and is killed when [close] is called.
26 /// 21 ///
27 /// Any errors starting or running the process are reported through [onExit]. 22 /// Any errors starting or running the process are reported through [onExit].
28 class Chrome implements Browser { 23 class Chrome extends Browser {
29 /// The underlying process. 24 final name = "Chrome";
30 Process _process;
31
32 Future get onExit => _onExitCompleter.future;
33 final _onExitCompleter = new Completer();
34
35 /// A future that completes when the browser process has started.
36 ///
37 /// This is used to ensure that [close] works regardless of when it's called.
38 Future get _onProcessStarted => _onProcessStartedCompleter.future;
39 final _onProcessStartedCompleter = new Completer();
40 25
41 /// Starts a new instance of Chrome open to the given [url], which may be a 26 /// Starts a new instance of Chrome open to the given [url], which may be a
42 /// [Uri] or a [String]. 27 /// [Uri] or a [String].
43 /// 28 ///
44 /// If [executable] is passed, it's used as the Chrome executable. Otherwise 29 /// If [executable] is passed, it's used as the Chrome executable. Otherwise
45 /// the default executable name for the current OS will be used. 30 /// the default executable name for the current OS will be used.
46 Chrome(url, {String executable}) { 31 Chrome(url, {String executable})
32 : super(() => _startBrowser(url, executable));
33
34 static Future<Process> _startBrowser(url, [String executable]) async {
47 if (executable == null) executable = _defaultExecutable(); 35 if (executable == null) executable = _defaultExecutable();
48 36
49 // Don't return a Future here because there's no need for the caller to wait 37 var dir = createTempDir();
50 // for the process to actually start. They should just wait for the HTTP 38 var process = await Process.start(executable, [
51 // request instead. 39 "--user-data-dir=$dir",
52 invoke(() async { 40 url.toString(),
53 try { 41 "--disable-extensions",
54 var exitCode = await withTempDir((dir) async { 42 "--disable-popup-blocking",
55 var process = await Process.start(executable, [ 43 "--bwsi",
56 "--user-data-dir=$dir", 44 "--no-first-run",
57 url.toString(), 45 "--no-default-browser-check",
58 "--disable-extensions", 46 "--disable-default-apps",
59 "--disable-popup-blocking", 47 "--disable-translate"
60 "--bwsi", 48 ]);
61 "--no-first-run",
62 "--no-default-browser-check",
63 "--disable-default-apps",
64 "--disable-translate"
65 ]);
66 49
67 _process = process; 50 process.exitCode
68 _onProcessStartedCompleter.complete(); 51 .then((_) => new Directory(dir).deleteSync(recursive: true));
69 52
70 // TODO(nweiz): the browser's standard output is almost always useless 53 return process;
71 // noise, but we should allow the user to opt in to seeing it.
72 return await _process.exitCode;
73 });
74
75 if (exitCode != 0) {
76 var error = await UTF8.decodeStream(_process.stderr);
77 throw new ApplicationException(
78 "Chrome failed with exit code $exitCode:\n$error");
79 }
80
81 _onExitCompleter.complete();
82 } catch (error, stackTrace) {
83 if (stackTrace == null) stackTrace = new Trace.current();
84 _onExitCompleter.completeError(
85 new ApplicationException(
86 "Failed to start Chrome: ${getErrorMessage(error)}."),
87 stackTrace);
88 }
89 });
90 }
91
92 Future close() {
93 _onProcessStarted.then((_) => _process.kill());
94
95 // Swallow exceptions. The user should explicitly use [onExit] for these.
96 return onExit.catchError((_) {});
97 } 54 }
98 55
99 /// Return the default executable for the current operating system. 56 /// Return the default executable for the current operating system.
100 String _defaultExecutable() { 57 static String _defaultExecutable() {
101 if (Platform.isMacOS) { 58 if (Platform.isMacOS) {
102 return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; 59 return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
103 } 60 }
104 if (!Platform.isWindows) return 'google-chrome'; 61 if (!Platform.isWindows) return 'google-chrome';
105 62
106 // Chrome could be installed in several places on Windows. The only way to 63 // Chrome could be installed in several places on Windows. The only way to
107 // find it is to check. 64 // find it is to check.
108 var prefixes = [ 65 var prefixes = [
109 Platform.environment['LOCALAPPDATA'], 66 Platform.environment['LOCALAPPDATA'],
110 Platform.environment['PROGRAMFILES'], 67 Platform.environment['PROGRAMFILES'],
111 Platform.environment['PROGRAMFILES(X86)'] 68 Platform.environment['PROGRAMFILES(X86)']
112 ]; 69 ];
113 var suffix = r'Google\Chrome\Application\chrome.exe'; 70 var suffix = r'Google\Chrome\Application\chrome.exe';
114 71
115 for (var prefix in prefixes) { 72 for (var prefix in prefixes) {
116 if (prefix == null) continue; 73 if (prefix == null) continue;
117 74
118 var path = p.join(prefix, suffix); 75 var path = p.join(prefix, suffix);
119 if (new File(p.join(prefix, suffix)).existsSync()) return path; 76 if (new File(p.join(prefix, suffix)).existsSync()) return path;
120 } 77 }
121 78
122 // Fall back on looking it up on the path. This probably won't work, but at 79 // Fall back on looking it up on the path. This probably won't work, but at
123 // least it will fail with a useful error message. 80 // least it will fail with a useful error message.
124 return "chrome.exe"; 81 return "chrome.exe";
125 } 82 }
126 } 83 }
OLDNEW
« no previous file with comments | « lib/src/runner/browser/browser.dart ('k') | lib/src/runner/browser/content_shell.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698