downloadChrome function

Future<DownloadedBrowserInfo> downloadChrome(
  1. {String? version,
  2. String? cachePath,
  3. void onDownloadProgress(
    1. int received,
    2. int total
    )?,
  4. BrowserPlatform? platform}
)

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

await downloadChrome(
  version: '112.0.5615.121',
  cachePath: '.local-chrome',
  onDownloadProgress: (received, total) {
    print('downloaded $received of $total bytes');
  });

Implementation

Future<DownloadedBrowserInfo> downloadChrome({
  String? version,
  String? cachePath,
  void Function(int received, int total)? onDownloadProgress,
  BrowserPlatform? platform,
}) async {
  version ??= _lastVersion;
  cachePath ??= '.local-chrome';
  platform ??= BrowserPlatform.current;

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

  var exePath = p.join(revisionDirectory.path, getExecutablePath(platform));

  var executableFile = File(exePath);

  if (!executableFile.existsSync()) {
    var url = _downloadUrl(platform, version);
    var zipPath = p.join(revisionDirectory.path, 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 DownloadedBrowserInfo(
      folderPath: revisionDirectory.path,
      executablePath: executableFile.path,
      version: version);
}