Persist Chrome Default Directory (#69921)

This commit is contained in:
Gary Roumanis
2020-11-06 10:54:04 -08:00
committed by GitHub
parent a2fd69cdbf
commit 16daed86c3
2 changed files with 33 additions and 48 deletions

View File

@@ -296,60 +296,55 @@ class ChromiumLauncher {
}
}
// This is a directory which Chrome uses to store cookies, preferences and
// other session data.
String get _chromeDefaultPath => _fileSystem.path.join('Default');
// 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));
final Directory targetChromeDefault = _fileSystem.directory(_fileSystem.path.join(cacheDir?.path ?? '', _chromeDefaultPath));
final Directory sourceChromeDefault = _fileSystem.directory(_fileSystem.path.join(userDataDir.path, _chromeDefaultPath));
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);
if (sourceChromeDefault.existsSync()) {
targetChromeDefault.createSync(recursive: true);
try {
_fileSystemUtils.copyDirectorySync(sourceLocalStorageDir, targetLocalStorageDir);
_fileSystemUtils.copyDirectorySync(sourceChromeDefault, targetChromeDefault);
} on FileSystemException catch (err) {
// This is a best-effort update. Display the message in case the failure is relevant.
// one possible example is a file lock due to multiple running chrome instances.
_logger.printError('Failed to save Chrome preferences: $err');
}
}
final File targetPreferencesFile = _fileSystem.file(_fileSystem.path.join(cacheDir?.path ?? '', _preferencesPath));
final File sourcePreferencesFile = _fileSystem.file(_fileSystem.path.join(userDataDir.path, _preferencesPath));
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"'));
}
}
/// 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));
final Directory sourceChromeDefault = _fileSystem.directory(_fileSystem.path.join(cacheDir.path ?? '', _chromeDefaultPath));
final Directory targetChromeDefault = _fileSystem.directory(_fileSystem.path.join(userDataDir.path, _chromeDefaultPath));
if (sourcePreferencesFile.existsSync()) {
targetPreferencesFile.parent.createSync(recursive: true);
sourcePreferencesFile.copySync(targetPreferencesFile.path);
}
if (sourceLocalStorageDir.existsSync()) {
targetLocalStorageDir.createSync(recursive: true);
_fileSystemUtils.copyDirectorySync(sourceLocalStorageDir, targetLocalStorageDir);
if (sourceChromeDefault.existsSync()) {
targetChromeDefault.createSync(recursive: true);
_fileSystemUtils.copyDirectorySync(sourceChromeDefault, targetChromeDefault);
}
}