diff --git a/CHANGELOG.md b/CHANGELOG.md index 7922d36..4a1b460 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.5 + +- Add a parameter to use a specified user-data-dir instead of a system temp. + ## 0.1.4 - Start Chrome maximized. diff --git a/lib/src/chrome.dart b/lib/src/chrome.dart index ea1a144..2a8b3de 100644 --- a/lib/src/chrome.dart +++ b/lib/src/chrome.dart @@ -41,18 +41,16 @@ String get _executable { /// Manager for an instance of Chrome. class Chrome { - Chrome._( - this.debugPort, - this.chromeConnection, { - Process process, - Directory dataDir, - }) : _process = process, + Chrome._(this.debugPort, this.chromeConnection, + {Process process, Directory dataDir, this.deleteDataDir = false}) + : _process = process, _dataDir = dataDir; final int debugPort; final ChromeConnection chromeConnection; final Process _process; final Directory _dataDir; + final bool deleteDataDir; /// Connects to an instance of Chrome with an open debug port. static Future fromExisting(int port) async => @@ -61,12 +59,14 @@ class Chrome { /// Starts Chrome with the given arguments and a specific port. /// /// Each url in [urls] will be loaded in a separate tab. - static Future startWithDebugPort( - List urls, { - int debugPort, - bool headless = false, - }) async { - final dataDir = Directory.systemTemp.createTempSync(); + static Future startWithDebugPort(List urls, + {int debugPort, bool headless = false, String userDataDir}) async { + Directory dataDir; + if (userDataDir == null) { + dataDir = Directory.systemTemp.createTempSync(); + } else { + dataDir = Directory(userDataDir); + } final port = debugPort == null || debugPort == 0 ? await findUnusedPort() : debugPort; @@ -108,6 +108,7 @@ class Chrome { ChromeConnection('localhost', port), process: process, dataDir: dataDir, + deleteDataDir: userDataDir = null, )); } @@ -150,8 +151,10 @@ class Chrome { // Chrome starts another process as soon as it dies that modifies the // profile information. Give it some time before attempting to delete // the directory. - await Future.delayed(Duration(milliseconds: 500)); - await _dataDir?.delete(recursive: true); + if (deleteDataDir) { + await Future.delayed(Duration(milliseconds: 500)); + await _dataDir?.delete(recursive: true); + } } catch (_) { // Silently fail if we can't clean up the profile information. // It is a system tmp directory so it should get cleaned up eventually. diff --git a/pubspec.yaml b/pubspec.yaml index 9a3ffc6..59c3d46 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: browser_launcher description: Provides a standardized way to launch web browsers for testing and tools. -version: 0.1.4 +version: 0.1.5 author: Dart Team homepage: https://github.com/dart-lang/browser_launcher