toJson method
Creates a map like {'browserName: 'firefox', 'firefox_profile': 'xxxxx'}
where xxxxx
is the zipped and base64 encoded content of the files in
profileDirectory if one was pased.
The files prefs.js
and user.js
are generated from the content of
prefs
and userPrefs
.
It can be uses like
var desired = Capabilities.firefox..addAll(firefoxProfile.toJson()}
Implementation
Map<String, dynamic> toJson() {
final archive = Archive();
final dir = profileDirectory;
if (dir != null) {
final files = dir.listSync(recursive: true).whereType<io.File>();
for (var file in files) {
final name = path.relative(file.path, from: dir.path);
if (name == 'prefs.js' || name == 'user.js') {
continue;
}
archive.addFile(ArchiveFile(name, file.readAsBytesSync()));
}
}
final prefsJsContent =
prefs.map((option) => option.asPrefString).join('\n').codeUnits;
archive.addFile(ArchiveFile('prefs.js', prefsJsContent));
final userJsContent =
userPrefs.map((option) => option.asPrefString).join('\n').codeUnits;
archive.addFile(ArchiveFile('user.js', userJsContent));
final zipData = ZipEncoder.encode(archive);
return {'firefox_profile': base64.encode(zipData)};
}