OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.runner.browser.content_shell; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import 'package:path/path.dart' as p; |
| 11 |
| 12 import '../../util/io.dart'; |
| 13 import 'browser.dart'; |
| 14 |
| 15 /// A class for running an instance of the Dartium content shell. |
| 16 /// |
| 17 /// Most of the communication with the browser is expected to happen via HTTP, |
| 18 /// so this exposes a bare-bones API. The browser starts as soon as the class is |
| 19 /// constructed, and is killed when [close] is called. |
| 20 /// |
| 21 /// Any errors starting or running the process are reported through [onExit]. |
| 22 class ContentShell implements Browser { |
| 23 /// The underlying process. |
| 24 Process _process; |
| 25 |
| 26 Future get onExit => _onExitCompleter.future; |
| 27 final _onExitCompleter = new Completer(); |
| 28 |
| 29 /// A future that completes when the browser process has started. |
| 30 /// |
| 31 /// This is used to ensure that [close] works regardless of when it's called. |
| 32 Future get _onProcessStarted => _onProcessStartedCompleter.future; |
| 33 final _onProcessStartedCompleter = new Completer(); |
| 34 |
| 35 /// Starts a new instance of content shell open to the given [url], which may |
| 36 /// be a [Uri] or a [String]. |
| 37 /// |
| 38 /// If [executable] is passed, it's used as the content shell executable. |
| 39 /// Otherwise the default executable name for the current OS will be used. |
| 40 ContentShell(url, {String executable}) { |
| 41 if (executable == null) executable = _defaultExecutable(); |
| 42 |
| 43 // Don't return a Future here because there's no need for the caller to wait |
| 44 // for the process to actually start. They should just wait for the HTTP |
| 45 // request instead. |
| 46 Process.start(executable, ["--dump-render-tree", url.toString()], |
| 47 environment: {"DART_FLAGS": "--checked"}).then((process) { |
| 48 _process = process; |
| 49 _onProcessStartedCompleter.complete(); |
| 50 return _process.exitCode; |
| 51 }).then((exitCode) { |
| 52 if (exitCode != 0) throw "Content shell failed with exit code $exitCode."; |
| 53 }).then(_onExitCompleter.complete) |
| 54 .catchError(_onExitCompleter.completeError); |
| 55 } |
| 56 |
| 57 Future close() { |
| 58 _onProcessStarted.then((_) => _process.kill()); |
| 59 |
| 60 // Swallow exceptions. The user should explicitly use [onExit] for these. |
| 61 return onExit.catchError((_) {}); |
| 62 } |
| 63 |
| 64 /// Return the default executable for the current operating system. |
| 65 String _defaultExecutable() => |
| 66 Platform.isWindows ? "content_shell.exe" : "content_shell"; |
| 67 } |
OLD | NEW |