Unverified Commit d6614dba authored by Devon Carew's avatar Devon Carew Committed by GitHub

save and restore the chrome session local storage information (#53030)

save and restore the chrome session local storage information
parent c9323bdc
...@@ -15,6 +15,7 @@ import '../base/io.dart'; ...@@ -15,6 +15,7 @@ import '../base/io.dart';
import '../base/logger.dart'; import '../base/logger.dart';
import '../base/os.dart'; import '../base/os.dart';
import '../convert.dart'; import '../convert.dart';
import '../globals.dart' as globals;
/// An environment variable used to override the location of chrome. /// An environment variable used to override the location of chrome.
const String kChromeEnvironment = 'CHROME_EXECUTABLE'; const String kChromeEnvironment = 'CHROME_EXECUTABLE';
...@@ -115,27 +116,17 @@ class ChromeLauncher { ...@@ -115,27 +116,17 @@ class ChromeLauncher {
/// port is picked automatically. /// port is picked automatically.
/// ///
/// `skipCheck` does not attempt to make a devtools connection before returning. /// `skipCheck` does not attempt to make a devtools connection before returning.
Future<Chrome> launch(String url, { bool headless = false, int debugPort, bool skipCheck = false, Directory dataDir }) async { Future<Chrome> launch(String url, { bool headless = false, int debugPort, bool skipCheck = false, Directory cacheDir }) async {
if (_currentCompleter.isCompleted) { if (_currentCompleter.isCompleted) {
throwToolExit('Only one instance of chrome can be started.'); throwToolExit('Only one instance of chrome can be started.');
} }
// This is a JSON file which contains configuration from the
// browser session, such as window position. It is located
// under the Chrome data-dir folder.
final String preferencesPath = _fileSystem.path.join('Default', 'preferences');
final String chromeExecutable = findChromeExecutable(_platform, _fileSystem); final String chromeExecutable = findChromeExecutable(_platform, _fileSystem);
final Directory activeDataDir = _fileSystem.systemTempDirectory.createTempSync('flutter_tool.'); final Directory userDataDir = _fileSystem.systemTempDirectory.createTempSync('flutter_tools_chrome_device.');
// Seed data dir with previous state.
if (cacheDir != null) {
final File savedPreferencesFile = _fileSystem.file(_fileSystem.path.join(dataDir?.path ?? '', preferencesPath)); // Seed data dir with previous state.
final File destinationFile = _fileSystem.file(_fileSystem.path.join(activeDataDir.path, preferencesPath)); _restoreUserSessionInformation(cacheDir, userDataDir);
if (dataDir != null) {
if (savedPreferencesFile.existsSync()) {
destinationFile.parent.createSync(recursive: true);
savedPreferencesFile.copySync(destinationFile.path);
}
} }
final int port = debugPort ?? await _operatingSystemUtils.findFreePort(); final int port = debugPort ?? await _operatingSystemUtils.findFreePort();
...@@ -143,7 +134,7 @@ class ChromeLauncher { ...@@ -143,7 +134,7 @@ class ChromeLauncher {
chromeExecutable, chromeExecutable,
// Using a tmp directory ensures that a new instance of chrome launches // Using a tmp directory ensures that a new instance of chrome launches
// allowing for the remote debug port to be enabled. // allowing for the remote debug port to be enabled.
'--user-data-dir=${activeDataDir.path}', '--user-data-dir=${userDataDir.path}',
'--remote-debugging-port=$port', '--remote-debugging-port=$port',
// When the DevTools has focus we don't want to slow down the application. // When the DevTools has focus we don't want to slow down the application.
'--disable-background-timer-throttling', '--disable-background-timer-throttling',
...@@ -163,18 +154,10 @@ class ChromeLauncher { ...@@ -163,18 +154,10 @@ class ChromeLauncher {
final Process process = await _processManager.start(args); final Process process = await _processManager.start(args);
// When the process exits, copy the user settings back to the provided // When the process exits, copy the user settings back to the provided data-dir.
// data-dir. if (cacheDir != null) {
if (dataDir != null) {
unawaited(process.exitCode.whenComplete(() { unawaited(process.exitCode.whenComplete(() {
if (destinationFile.existsSync()) { _cacheUserSessionInformation(userDataDir, cacheDir);
savedPreferencesFile.parent.createSync(recursive: true);
// If the file contains a crash string, remove it to hide
// the popup on next run.
final String contents = destinationFile.readAsStringSync();
savedPreferencesFile.writeAsStringSync(contents
.replaceFirst('"exit_type":"Crashed"', '"exit_type":"Normal"'));
}
})); }));
} }
...@@ -206,6 +189,57 @@ class ChromeLauncher { ...@@ -206,6 +189,57 @@ class ChromeLauncher {
), skipCheck); ), skipCheck);
} }
// This is a JSON file which contains configuration from the browser session,
// such as window position. It is located under the Chrome data-dir folder.
String get _preferencesPath => _fileSystem.path.join('Default', 'preferences');
// The directory that Chrome uses to store local storage information for web apps.
String get _localStoragePath => _fileSystem.path.join('Default', 'Local Storage');
/// Copy Chrome user information from a Chrome session into a per-project
/// cache.
///
/// Note: more detailed docs of the Chrome user preferences store exists here:
/// https://www.chromium.org/developers/design-documents/preferences.
void _cacheUserSessionInformation(Directory userDataDir, Directory cacheDir) {
final File targetPreferencesFile = _fileSystem.file(_fileSystem.path.join(cacheDir?.path ?? '', _preferencesPath));
final File sourcePreferencesFile = _fileSystem.file(_fileSystem.path.join(userDataDir.path, _preferencesPath));
final Directory targetLocalStorageDir = _fileSystem.directory(_fileSystem.path.join(cacheDir?.path ?? '', _localStoragePath));
final Directory sourceLocalStorageDir = _fileSystem.directory(_fileSystem.path.join(userDataDir.path, _localStoragePath));
if (sourcePreferencesFile.existsSync()) {
targetPreferencesFile.parent.createSync(recursive: true);
// If the file contains a crash string, remove it to hide the popup on next run.
final String contents = sourcePreferencesFile.readAsStringSync();
targetPreferencesFile.writeAsStringSync(contents
.replaceFirst('"exit_type":"Crashed"', '"exit_type":"Normal"'));
}
if (sourceLocalStorageDir.existsSync()) {
targetLocalStorageDir.createSync(recursive: true);
globals.fsUtils.copyDirectorySync(sourceLocalStorageDir, targetLocalStorageDir);
}
}
/// Restore Chrome user information from a per-project cache into Chrome's
/// user data directory.
void _restoreUserSessionInformation(Directory cacheDir, Directory userDataDir) {
final File sourcePreferencesFile = _fileSystem.file(_fileSystem.path.join(cacheDir.path ?? '', _preferencesPath));
final File targetPreferencesFile = _fileSystem.file(_fileSystem.path.join(userDataDir.path, _preferencesPath));
final Directory sourceLocalStorageDir = _fileSystem.directory(_fileSystem.path.join(cacheDir.path ?? '', _localStoragePath));
final Directory targetLocalStorageDir = _fileSystem.directory(_fileSystem.path.join(userDataDir.path, _localStoragePath));
if (sourcePreferencesFile.existsSync()) {
targetPreferencesFile.parent.createSync(recursive: true);
sourcePreferencesFile.copySync(targetPreferencesFile.path);
}
if (sourceLocalStorageDir.existsSync()) {
targetLocalStorageDir.createSync(recursive: true);
globals.fsUtils.copyDirectorySync(sourceLocalStorageDir, targetLocalStorageDir);
}
}
static Future<Chrome> _connect(Chrome chrome, bool skipCheck) async { static Future<Chrome> _connect(Chrome chrome, bool skipCheck) async {
// The connection is lazy. Try a simple call to make sure the provided // The connection is lazy. Try a simple call to make sure the provided
// connection is valid. // connection is valid.
......
...@@ -138,7 +138,7 @@ class ChromeDevice extends Device { ...@@ -138,7 +138,7 @@ class ChromeDevice extends Device {
if (launchChrome) { if (launchChrome) {
_chrome = await globals.chromeLauncher.launch( _chrome = await globals.chromeLauncher.launch(
url, url,
dataDir: globals.fs.currentDirectory cacheDir: globals.fs.currentDirectory
.childDirectory('.dart_tool') .childDirectory('.dart_tool')
.childDirectory('chrome-device'), .childDirectory('chrome-device'),
headless: debuggingOptions.webRunHeadless, headless: debuggingOptions.webRunHeadless,
......
...@@ -63,14 +63,14 @@ void main() { ...@@ -63,14 +63,14 @@ void main() {
}); });
test('can launch chrome and connect to the devtools', () async { test('can launch chrome and connect to the devtools', () async {
await testLaunchChrome('/.tmp_rand0/flutter_tool.rand0', processManager, chromeLauncher); await testLaunchChrome('/.tmp_rand0/flutter_tools_chrome_device.rand0', processManager, chromeLauncher);
}); });
test('cannot have two concurrent instances of chrome', () async { test('cannot have two concurrent instances of chrome', () async {
await testLaunchChrome('/.tmp_rand0/flutter_tool.rand0', processManager, chromeLauncher); await testLaunchChrome('/.tmp_rand0/flutter_tools_chrome_device.rand0', processManager, chromeLauncher);
bool pass = false; bool pass = false;
try { try {
await testLaunchChrome('/.tmp_rand0/flutter_tool.rand1', processManager, chromeLauncher); await testLaunchChrome('/.tmp_rand0/flutter_tools_chrome_device.rand1', processManager, chromeLauncher);
} on ToolExit catch (_) { } on ToolExit catch (_) {
pass = true; pass = true;
} }
...@@ -78,16 +78,16 @@ void main() { ...@@ -78,16 +78,16 @@ void main() {
}); });
test('can launch new chrome after stopping a previous chrome', () async { test('can launch new chrome after stopping a previous chrome', () async {
final Chrome chrome = await testLaunchChrome('/.tmp_rand0/flutter_tool.rand0', processManager, chromeLauncher); final Chrome chrome = await testLaunchChrome('/.tmp_rand0/flutter_tools_chrome_device.rand0', processManager, chromeLauncher);
await chrome.close(); await chrome.close();
await testLaunchChrome('/.tmp_rand0/flutter_tool.rand1', processManager, chromeLauncher); await testLaunchChrome('/.tmp_rand0/flutter_tools_chrome_device.rand1', processManager, chromeLauncher);
}); });
test('can launch chrome with a custom debug port', () async { test('can launch chrome with a custom debug port', () async {
processManager.addCommand(const FakeCommand( processManager.addCommand(const FakeCommand(
command: <String>[ command: <String>[
'example_chrome', 'example_chrome',
'--user-data-dir=/.tmp_rand1/flutter_tool.rand1', '--user-data-dir=/.tmp_rand1/flutter_tools_chrome_device.rand1',
'--remote-debugging-port=10000', '--remote-debugging-port=10000',
..._kChromeArgs, ..._kChromeArgs,
'example_url', 'example_url',
...@@ -106,7 +106,7 @@ void main() { ...@@ -106,7 +106,7 @@ void main() {
processManager.addCommand(const FakeCommand( processManager.addCommand(const FakeCommand(
command: <String>[ command: <String>[
'example_chrome', 'example_chrome',
'--user-data-dir=/.tmp_rand1/flutter_tool.rand1', '--user-data-dir=/.tmp_rand1/flutter_tools_chrome_device.rand1',
'--remote-debugging-port=1234', '--remote-debugging-port=1234',
..._kChromeArgs, ..._kChromeArgs,
'--headless', '--headless',
...@@ -125,9 +125,10 @@ void main() { ...@@ -125,9 +125,10 @@ void main() {
); );
}); });
test('can seed chrome temp directory with existing preferences', () async { test('can seed chrome temp directory with existing session data', () async {
final Completer<void> exitCompleter = Completer<void>.sync(); final Completer<void> exitCompleter = Completer<void>.sync();
final Directory dataDir = fileSystem.directory('chrome-stuff'); final Directory dataDir = fileSystem.directory('chrome-stuff');
final File preferencesFile = dataDir final File preferencesFile = dataDir
.childDirectory('Default') .childDirectory('Default')
.childFile('preferences'); .childFile('preferences');
...@@ -135,9 +136,17 @@ void main() { ...@@ -135,9 +136,17 @@ void main() {
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync('example'); ..writeAsStringSync('example');
final Directory localStorageContentsDirectory = dataDir
.childDirectory('Default')
.childDirectory('Local Storage')
.childDirectory('leveldb');
localStorageContentsDirectory.createSync(recursive: true);
localStorageContentsDirectory.childFile('LOCK').writeAsBytesSync(<int>[]);
localStorageContentsDirectory.childFile('LOG').writeAsStringSync('contents');
processManager.addCommand(FakeCommand(command: const <String>[ processManager.addCommand(FakeCommand(command: const <String>[
'example_chrome', 'example_chrome',
'--user-data-dir=/.tmp_rand1/flutter_tool.rand1', '--user-data-dir=/.tmp_rand1/flutter_tools_chrome_device.rand1',
'--remote-debugging-port=1234', '--remote-debugging-port=1234',
..._kChromeArgs, ..._kChromeArgs,
'example_url', 'example_url',
...@@ -146,11 +155,12 @@ void main() { ...@@ -146,11 +155,12 @@ void main() {
await chromeLauncher.launch( await chromeLauncher.launch(
'example_url', 'example_url',
skipCheck: true, skipCheck: true,
dataDir: dataDir, cacheDir: dataDir,
); );
// validate preferences
final File tempFile = fileSystem final File tempFile = fileSystem
.directory('.tmp_rand1/flutter_tool.rand1') .directory('.tmp_rand1/flutter_tools_chrome_device.rand1')
.childDirectory('Default') .childDirectory('Default')
.childFile('preferences'); .childFile('preferences');
...@@ -163,6 +173,21 @@ void main() { ...@@ -163,6 +173,21 @@ void main() {
// writes non-crash back to dart_tool // writes non-crash back to dart_tool
expect(preferencesFile.readAsStringSync(), '"exit_type":"Normal"'); expect(preferencesFile.readAsStringSync(), '"exit_type":"Normal"');
// validate local storage
final Directory storageDir = fileSystem
.directory('.tmp_rand1/flutter_tools_chrome_device.rand1')
.childDirectory('Default')
.childDirectory('Local Storage')
.childDirectory('leveldb');
expect(storageDir.existsSync(), true);
expect(storageDir.childFile('LOCK').existsSync(), true);
expect(storageDir.childFile('LOCK').readAsBytesSync(), hasLength(0));
expect(storageDir.childFile('LOG').existsSync(), true);
expect(storageDir.childFile('LOG').readAsStringSync(), 'contents');
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment