downloadChrome function

Future<RevisionInfo> downloadChrome({
  1. int? revision,
  2. String? cachePath,
  3. void onDownloadProgress(
    1. int received,
    2. int total
    )?,
})

Downloads the chrome revision specified by revision to the cachePath directory.

await downloadChrome(
  revision: 1083080,
  cachePath: '.local-chromium',
  onDownloadProgress: (received, total) {
    print('downloaded $received of $total bytes');
  });

Implementation

Future<RevisionInfo> downloadChrome({
  int? revision,
  String? cachePath,
  void Function(int received, int total)? onDownloadProgress,
}) async {
  revision ??= _lastRevision;
  cachePath ??= '.local-chromium';

  var revisionDirectory = Directory(p.join(cachePath, '$revision'));
  if (!revisionDirectory.existsSync()) {
    revisionDirectory.createSync(recursive: true);
  }

  var exePath = getExecutablePath(revisionDirectory.path);

  var executableFile = File(exePath);

  if (!executableFile.existsSync()) {
    var url = _downloadUrl(revision);
    var zipPath = p.join(cachePath, '${revision}_${p.url.basename(url)}');
    await _downloadFile(url, zipPath, onDownloadProgress);
    _unzip(zipPath, revisionDirectory.path);
    File(zipPath).deleteSync();
  }

  if (!executableFile.existsSync()) {
    throw Exception("$exePath doesn't exist");
  }

  if (!Platform.isWindows) {
    await Process.run('chmod', ['+x', executableFile.absolute.path]);
  }

  if (Platform.isMacOS) {
    final chromeAppPath = executableFile.absolute.parent.parent.parent.path;

    await Process.run('xattr', ['-d', 'com.apple.quarantine', chromeAppPath]);
  }

  return RevisionInfo(
      folderPath: revisionDirectory.path,
      executablePath: executableFile.path,
      revision: revision);
}