OLD | NEW |
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.dartium; | 5 library test.runner.browser.dartium; |
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 /// A class for running an instance of Dartium. | 15 /// A class for running an instance of Dartium. |
20 /// | 16 /// |
21 /// Most of the communication with the browser is expected to happen via HTTP, | 17 /// Most of the communication with the browser is expected to happen via HTTP, |
22 /// so this exposes a bare-bones API. The browser starts as soon as the class is | 18 /// so this exposes a bare-bones API. The browser starts as soon as the class is |
23 /// constructed, and is killed when [close] is called. | 19 /// constructed, and is killed when [close] is called. |
24 /// | 20 /// |
25 /// Any errors starting or running the process are reported through [onExit]. | 21 /// Any errors starting or running the process are reported through [onExit]. |
26 class Dartium implements Browser { | 22 class Dartium extends Browser { |
27 /// The underlying process. | 23 final name = "Dartium"; |
28 Process _process; | |
29 | 24 |
30 Future get onExit => _onExitCompleter.future; | 25 Dartium(url, {String executable}) |
31 final _onExitCompleter = new Completer(); | 26 : super(() => _startBrowser(url, executable)); |
32 | |
33 /// A future that completes when the browser process has started. | |
34 /// | |
35 /// This is used to ensure that [close] works regardless of when it's called. | |
36 Future get _onProcessStarted => _onProcessStartedCompleter.future; | |
37 final _onProcessStartedCompleter = new Completer(); | |
38 | 27 |
39 /// Starts a new instance of Dartium open to the given [url], which may be a | 28 /// Starts a new instance of Dartium open to the given [url], which may be a |
40 /// [Uri] or a [String]. | 29 /// [Uri] or a [String]. |
41 /// | 30 /// |
42 /// If [executable] is passed, it's used as the Dartium executable. Otherwise | 31 /// If [executable] is passed, it's used as the Dartium executable. Otherwise |
43 /// the default executable name for the current OS will be used. | 32 /// the default executable name for the current OS will be used. |
44 Dartium(url, {String executable}) { | 33 static Future<Process> _startBrowser(url, [String executable]) async { |
45 if (executable == null) executable = _defaultExecutable(); | 34 if (executable == null) executable = _defaultExecutable(); |
46 | 35 |
47 // Don't return a Future here because there's no need for the caller to wait | 36 var dir = createTempDir(); |
48 // for the process to actually start. They should just wait for the HTTP | 37 var process = await Process.start(executable, [ |
49 // request instead. | 38 "--user-data-dir=$dir", |
50 invoke(() async { | 39 url.toString(), |
51 try { | 40 "--disable-extensions", |
52 var exitCode = await withTempDir((dir) async { | 41 "--disable-popup-blocking", |
53 var process = await Process.start(executable, [ | 42 "--bwsi", |
54 "--user-data-dir=$dir", | 43 "--no-first-run", |
55 url.toString(), | 44 "--no-default-browser-check", |
56 "--disable-extensions", | 45 "--disable-default-apps", |
57 "--disable-popup-blocking", | 46 "--disable-translate" |
58 "--bwsi", | 47 ], environment: {"DART_FLAGS": "--checked"}); |
59 "--no-first-run", | |
60 "--no-default-browser-check", | |
61 "--disable-default-apps", | |
62 "--disable-translate" | |
63 ], environment: {"DART_FLAGS": "--checked"}); | |
64 | 48 |
65 _process = process; | 49 process.exitCode |
66 _onProcessStartedCompleter.complete(); | 50 .then((_) => new Directory(dir).deleteSync(recursive: true)); |
67 | 51 |
68 // TODO(nweiz): the browser's standard output is almost always useless | 52 return process; |
69 // noise, but we should allow the user to opt in to seeing it. | |
70 return await _process.exitCode; | |
71 }); | |
72 | |
73 if (exitCode != 0) { | |
74 var error = await UTF8.decodeStream(_process.stderr); | |
75 throw new ApplicationException( | |
76 "Dartium failed with exit code $exitCode:\n$error"); | |
77 } | |
78 | |
79 _onExitCompleter.complete(); | |
80 } catch (error, stackTrace) { | |
81 if (stackTrace == null) stackTrace = new Trace.current(); | |
82 _onExitCompleter.completeError( | |
83 new ApplicationException( | |
84 "Failed to start Dartium: ${getErrorMessage(error)}."), | |
85 stackTrace); | |
86 } | |
87 }); | |
88 } | |
89 | |
90 Future close() { | |
91 _onProcessStarted.then((_) => _process.kill()); | |
92 | |
93 // Swallow exceptions. The user should explicitly use [onExit] for these. | |
94 return onExit.catchError((_) {}); | |
95 } | 53 } |
96 | 54 |
97 /// Return the default executable for the current operating system. | 55 /// Return the default executable for the current operating system. |
98 String _defaultExecutable() { | 56 static String _defaultExecutable() { |
99 var dartium = _executableInEditor(); | 57 var dartium = _executableInEditor(); |
100 if (dartium != null) return dartium; | 58 if (dartium != null) return dartium; |
101 return Platform.isWindows ? "dartium.exe" : "dartium"; | 59 return Platform.isWindows ? "dartium.exe" : "dartium"; |
102 } | 60 } |
103 | 61 |
104 String _executableInEditor() { | 62 static String _executableInEditor() { |
105 var dir = p.dirname(sdkDir); | 63 var dir = p.dirname(sdkDir); |
106 | 64 |
107 if (Platform.isWindows) { | 65 if (Platform.isWindows) { |
108 if (!new File(p.join(dir, "DartEditor.exe")).existsSync()) return null; | 66 if (!new File(p.join(dir, "DartEditor.exe")).existsSync()) return null; |
109 | 67 |
110 var dartium = p.join(dir, "chromium\\chrome.exe"); | 68 var dartium = p.join(dir, "chromium\\chrome.exe"); |
111 return new File(dartium).existsSync() ? dartium : null; | 69 return new File(dartium).existsSync() ? dartium : null; |
112 } | 70 } |
113 | 71 |
114 if (Platform.isMacOS) { | 72 if (Platform.isMacOS) { |
115 if (!new File(p.join(dir, "DartEditor.app/Contents/MacOS/DartEditor")) | 73 if (!new File(p.join(dir, "DartEditor.app/Contents/MacOS/DartEditor")) |
116 .existsSync()) { | 74 .existsSync()) { |
117 return null; | 75 return null; |
118 } | 76 } |
119 | 77 |
120 var dartium = p.join( | 78 var dartium = p.join( |
121 dir, "chromium/Chromium.app/Contents/MacOS/Chromium"); | 79 dir, "chromium/Chromium.app/Contents/MacOS/Chromium"); |
122 return new File(dartium).existsSync() ? dartium : null; | 80 return new File(dartium).existsSync() ? dartium : null; |
123 } | 81 } |
124 | 82 |
125 assert(Platform.isLinux); | 83 assert(Platform.isLinux); |
126 if (!new File(p.join(dir, "DartEditor")).existsSync()) return null; | 84 if (!new File(p.join(dir, "DartEditor")).existsSync()) return null; |
127 | 85 |
128 var dartium = p.join(dir, "chromium", "chrome"); | 86 var dartium = p.join(dir, "chromium", "chrome"); |
129 return new File(dartium).existsSync() ? dartium : null; | 87 return new File(dartium).existsSync() ? dartium : null; |
130 } | 88 } |
131 } | 89 } |
OLD | NEW |