Contents

Downloads

This guide describes how to manage file downloads, track a download progress, get a notification when the download has been completed, etc.

Overview

To get a list of all downloads, associated with the specific Profile, including already completed downloads during this application session and the active downloads, use the following code:

auto downloads = profile->downloads();
for (auto download : downloads) {
  // TBD: CODE: check if it's -> or .
  std::cout << "Download from: " << download->target().url << std::endl;
}

Accepting a download

To accept or reject a download, use the onStartDownload delegate:

browser->onStartDownload = [](const StartDownloadArgs& args, 
                              StartDownloadAction action) {
  action.download("path/to/the/target/file");
};

Make sure that the Chromium has sufficient permissions to save the file. If the target file already exists, Chromium will overwrite it. We recommend checking the permissions before accepting the file download.

By default, all download requests are canceled.

To show the Chromium’s file chooser dialog, use this code:

browser->onStartDownload = [](const StartDownloadArgs& args, 
                              StartDownloadAction action) {
  auto download = args.download;
  action.prompt();
};

Controlling ongoing downloads

Pausing a download

To pause the download use the Download::pause() method:

download->pause();

Resuming a download

To resume the paused download use the Download::resume() method:

download->resume();

Canceling a download

You can cancel the download anytime unless the download is in the terminated state. This includes the completed downloads, canceled downloads, and interrupted downloads that cannot be resumed.

To cancel the download use the Download::cancel() method:

bool canceled = download->isCanceled();
bool finished = download->isFinished();
bool interrupted = download->isInterrupted();
if (!(canceled || finished || interrupted)) {
    download->cancel();
}

Download events

You can track the download progress, get notifications when the download has been canceled, paused, interrupted, or finished.

Download canceled

To get notifications when the Download has been canceled use the onDownloadCanceled method:

download->onDownloadCanceled += [](const DownloadCanceled& event) {};

Download finished

To get notifications when the Download has been finished use the onDownloadFinished method:

download->onDownloadFinished += [](const DownloadFinished& event) {};

Download paused

To get notifications when the Download has been paused use the onDownloadPaused method:

download->onDownloadPaused += [](const DownloadPaused& event) {};

Download updated

To track the download progress use the onDownloadUpdated method:

download->onDownloadUpdated += [](const DownloadUpdated& event) {
  int64_t current_speed = event.current_speed;
  int64_t received_bytes = event.received_bytes;
  auto download = event.download;
};

Download interrupted

To get notifications when the Download has been interrupted for some reason use the onDownloadInterrupted method:

download->onDownloadInterrupted += [](const DownloadInterrupted& event) {
  if (event.reason == DownloadInterruptReason::kFileNoSpace) {
    // ...
  }
};
On this page
Top