mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
save and restore the chrome session local storage information (#53030)
save and restore the chrome session local storage information
This commit is contained in:
parent
c9323bdccd
commit
d6614dbafd
@ -15,6 +15,7 @@ import '../base/io.dart';
|
||||
import '../base/logger.dart';
|
||||
import '../base/os.dart';
|
||||
import '../convert.dart';
|
||||
import '../globals.dart' as globals;
|
||||
|
||||
/// An environment variable used to override the location of chrome.
|
||||
const String kChromeEnvironment = 'CHROME_EXECUTABLE';
|
||||
@ -115,27 +116,17 @@ class ChromeLauncher {
|
||||
/// port is picked automatically.
|
||||
///
|
||||
/// `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) {
|
||||
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 Directory activeDataDir = _fileSystem.systemTempDirectory.createTempSync('flutter_tool.');
|
||||
// Seed data dir with previous state.
|
||||
final Directory userDataDir = _fileSystem.systemTempDirectory.createTempSync('flutter_tools_chrome_device.');
|
||||
|
||||
final File savedPreferencesFile = _fileSystem.file(_fileSystem.path.join(dataDir?.path ?? '', preferencesPath));
|
||||
final File destinationFile = _fileSystem.file(_fileSystem.path.join(activeDataDir.path, preferencesPath));
|
||||
if (dataDir != null) {
|
||||
if (savedPreferencesFile.existsSync()) {
|
||||
destinationFile.parent.createSync(recursive: true);
|
||||
savedPreferencesFile.copySync(destinationFile.path);
|
||||
}
|
||||
if (cacheDir != null) {
|
||||
// Seed data dir with previous state.
|
||||
_restoreUserSessionInformation(cacheDir, userDataDir);
|
||||
}
|
||||
|
||||
final int port = debugPort ?? await _operatingSystemUtils.findFreePort();
|
||||
@ -143,7 +134,7 @@ class ChromeLauncher {
|
||||
chromeExecutable,
|
||||
// Using a tmp directory ensures that a new instance of chrome launches
|
||||
// allowing for the remote debug port to be enabled.
|
||||
'--user-data-dir=${activeDataDir.path}',
|
||||
'--user-data-dir=${userDataDir.path}',
|
||||
'--remote-debugging-port=$port',
|
||||
// When the DevTools has focus we don't want to slow down the application.
|
||||
'--disable-background-timer-throttling',
|
||||
@ -163,18 +154,10 @@ class ChromeLauncher {
|
||||
|
||||
final Process process = await _processManager.start(args);
|
||||
|
||||
// When the process exits, copy the user settings back to the provided
|
||||
// data-dir.
|
||||
if (dataDir != null) {
|
||||
// When the process exits, copy the user settings back to the provided data-dir.
|
||||
if (cacheDir != null) {
|
||||
unawaited(process.exitCode.whenComplete(() {
|
||||
if (destinationFile.existsSync()) {
|
||||
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"'));
|
||||
}
|
||||
_cacheUserSessionInformation(userDataDir, cacheDir);
|
||||
}));
|
||||
}
|
||||
|
||||
@ -206,6 +189,57 @@ class ChromeLauncher {
|
||||
), 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 {
|
||||
// The connection is lazy. Try a simple call to make sure the provided
|
||||
// connection is valid.
|
||||
|
@ -138,7 +138,7 @@ class ChromeDevice extends Device {
|
||||
if (launchChrome) {
|
||||
_chrome = await globals.chromeLauncher.launch(
|
||||
url,
|
||||
dataDir: globals.fs.currentDirectory
|
||||
cacheDir: globals.fs.currentDirectory
|
||||
.childDirectory('.dart_tool')
|
||||
.childDirectory('chrome-device'),
|
||||
headless: debuggingOptions.webRunHeadless,
|
||||
|
@ -63,14 +63,14 @@ void main() {
|
||||
});
|
||||
|
||||
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 {
|
||||
await testLaunchChrome('/.tmp_rand0/flutter_tool.rand0', processManager, chromeLauncher);
|
||||
await testLaunchChrome('/.tmp_rand0/flutter_tools_chrome_device.rand0', processManager, chromeLauncher);
|
||||
bool pass = false;
|
||||
try {
|
||||
await testLaunchChrome('/.tmp_rand0/flutter_tool.rand1', processManager, chromeLauncher);
|
||||
await testLaunchChrome('/.tmp_rand0/flutter_tools_chrome_device.rand1', processManager, chromeLauncher);
|
||||
} on ToolExit catch (_) {
|
||||
pass = true;
|
||||
}
|
||||
@ -78,16 +78,16 @@ void main() {
|
||||
});
|
||||
|
||||
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 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 {
|
||||
processManager.addCommand(const FakeCommand(
|
||||
command: <String>[
|
||||
'example_chrome',
|
||||
'--user-data-dir=/.tmp_rand1/flutter_tool.rand1',
|
||||
'--user-data-dir=/.tmp_rand1/flutter_tools_chrome_device.rand1',
|
||||
'--remote-debugging-port=10000',
|
||||
..._kChromeArgs,
|
||||
'example_url',
|
||||
@ -106,7 +106,7 @@ void main() {
|
||||
processManager.addCommand(const FakeCommand(
|
||||
command: <String>[
|
||||
'example_chrome',
|
||||
'--user-data-dir=/.tmp_rand1/flutter_tool.rand1',
|
||||
'--user-data-dir=/.tmp_rand1/flutter_tools_chrome_device.rand1',
|
||||
'--remote-debugging-port=1234',
|
||||
..._kChromeArgs,
|
||||
'--headless',
|
||||
@ -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 Directory dataDir = fileSystem.directory('chrome-stuff');
|
||||
|
||||
final File preferencesFile = dataDir
|
||||
.childDirectory('Default')
|
||||
.childFile('preferences');
|
||||
@ -135,9 +136,17 @@ void main() {
|
||||
..createSync(recursive: true)
|
||||
..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>[
|
||||
'example_chrome',
|
||||
'--user-data-dir=/.tmp_rand1/flutter_tool.rand1',
|
||||
'--user-data-dir=/.tmp_rand1/flutter_tools_chrome_device.rand1',
|
||||
'--remote-debugging-port=1234',
|
||||
..._kChromeArgs,
|
||||
'example_url',
|
||||
@ -146,11 +155,12 @@ void main() {
|
||||
await chromeLauncher.launch(
|
||||
'example_url',
|
||||
skipCheck: true,
|
||||
dataDir: dataDir,
|
||||
cacheDir: dataDir,
|
||||
);
|
||||
|
||||
// validate preferences
|
||||
final File tempFile = fileSystem
|
||||
.directory('.tmp_rand1/flutter_tool.rand1')
|
||||
.directory('.tmp_rand1/flutter_tools_chrome_device.rand1')
|
||||
.childDirectory('Default')
|
||||
.childFile('preferences');
|
||||
|
||||
@ -163,6 +173,21 @@ void main() {
|
||||
|
||||
// writes non-crash back to dart_tool
|
||||
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');
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user