ChromeDownloads class
class ChromeDownloads extends ChromeApi { static final JsObject _downloads = chrome['downloads']; ChromeDownloads._(); bool get available => _downloads != null; /** * Download a URL. If the URL uses the HTTP[S] protocol, then the request will * include all cookies currently set for its hostname. If both `filename` and * `saveAs` are specified, then the Save As dialog will be displayed, * pre-populated with the specified `filename`. If the download started * successfully, `callback` will be called with the new [DownloadItem]'s * `downloadId`. If there was an error starting the download, then `callback` * will be called with `downloadId=undefined` and [runtime.lastError] will * contain a descriptive string. The error strings are not guaranteed to * remain backwards compatible between releases. Extensions must not parse it. * [options]: What to download and how. * [callback]: Called with the id of the new [DownloadItem]. */ Future<int> download(DownloadOptions options) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter<int>.oneArg(); _downloads.callMethod('download', [jsify(options), completer.callback]); return completer.future; } /** * Find [DownloadItem]. Set `query` to the empty object to get all * [DownloadItem]. To get a specific [DownloadItem], set only the `id` field. * To page through a large number of items, set `orderBy: ['-startTime']`, set * `limit` to the number of items per page, and set `startedAfter` to the * `startTime` of the last item from the last page. */ Future<List<DownloadItem>> search(DownloadQuery query) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter<List<DownloadItem>>.oneArg((e) => listify(e, _createDownloadItem)); _downloads.callMethod('search', [jsify(query), completer.callback]); return completer.future; } /** * Pause the download. If the request was successful the download is in a * paused state. Otherwise [runtime.lastError] contains an error message. The * request will fail if the download is not active. * [downloadId]: The id of the download to pause. * [callback]: Called when the pause request is completed. */ Future pause(int downloadId) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter.noArgs(); _downloads.callMethod('pause', [downloadId, completer.callback]); return completer.future; } /** * Resume a paused download. If the request was successful the download is in * progress and unpaused. Otherwise [runtime.lastError] contains an error * message. The request will fail if the download is not active. * [downloadId]: The id of the download to resume. * [callback]: Called when the resume request is completed. */ Future resume(int downloadId) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter.noArgs(); _downloads.callMethod('resume', [downloadId, completer.callback]); return completer.future; } /** * Cancel a download. When `callback` is run, the download is cancelled, * completed, interrupted or doesn't exist anymore. * [downloadId]: The id of the download to cancel. * [callback]: Called when the cancel request is completed. */ Future cancel(int downloadId) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter.noArgs(); _downloads.callMethod('cancel', [downloadId, completer.callback]); return completer.future; } /** * Retrieve an icon for the specified download. For new downloads, file icons * are available after the [onCreated] event has been received. The image * returned by this function while a download is in progress may be different * from the image returned after the download is complete. Icon retrieval is * done by querying the underlying operating system or toolkit depending on * the platform. The icon that is returned will therefore depend on a number * of factors including state of the download, platform, registered file types * and visual theme. If a file icon cannot be determined, [runtime.lastError] * will contain an error message. * [downloadId]: The identifier for the download. * [callback]: A URL to an image that represents the download. */ Future<String> getFileIcon(int downloadId, [GetFileIconOptions options]) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter<String>.oneArg(); _downloads.callMethod('getFileIcon', [downloadId, jsify(options), completer.callback]); return completer.future; } /** * Open the downloaded file now if the [DownloadItem] is complete; otherwise * returns an error through [runtime.lastError]. Requires the * `"downloads.open"` permission in addition to the `"downloads"` permission. * An [onChanged] event will fire when the item is opened for the first time. * [downloadId]: The identifier for the downloaded file. */ void open(int downloadId) { if (_downloads == null) _throwNotAvailable(); _downloads.callMethod('open', [downloadId]); } /** * Show the downloaded file in its folder in a file manager. * [downloadId]: The identifier for the downloaded file. */ void show(int downloadId) { if (_downloads == null) _throwNotAvailable(); _downloads.callMethod('show', [downloadId]); } /** * Show the default Downloads folder in a file manager. */ void showDefaultFolder() { if (_downloads == null) _throwNotAvailable(); _downloads.callMethod('showDefaultFolder'); } /** * Erase matching [DownloadItem] from history without deleting the downloaded * file. An [onErased] event will fire for each [DownloadItem] that matches * `query`, then `callback` will be called. */ Future<List<int>> erase(DownloadQuery query) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter<List<int>>.oneArg(listify); _downloads.callMethod('erase', [jsify(query), completer.callback]); return completer.future; } /** * Remove the downloaded file if it exists and the [DownloadItem] is complete; * otherwise return an error through [runtime.lastError]. */ Future removeFile(int downloadId) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter.noArgs(); _downloads.callMethod('removeFile', [downloadId, completer.callback]); return completer.future; } /** * Prompt the user to accept a dangerous download. Does not automatically * accept dangerous downloads. If the download is accepted, then an * [onChanged] event will fire, otherwise nothing will happen. When all the * data is fetched into a temporary file and either the download is not * dangerous or the danger has been accepted, then the temporary file is * renamed to the target filename, the [state] changes to 'complete', and * [onChanged] fires. * [downloadId]: The identifier for the [DownloadItem]. * [callback]: Called when the danger prompt dialog closes. */ Future acceptDanger(int downloadId) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter.noArgs(); _downloads.callMethod('acceptDanger', [downloadId, completer.callback]); return completer.future; } /** * Initiate dragging the downloaded file to another application. Call in a * javascript `ondragstart` handler. */ void drag(int downloadId) { if (_downloads == null) _throwNotAvailable(); _downloads.callMethod('drag', [downloadId]); } /** * Enable or disable the gray shelf at the bottom of every window associated * with the current browser profile. The shelf will be disabled as long as at * least one extension has disabled it. Enabling the shelf while at least one * other extension has disabled it will return an error through * [runtime.lastError]. Requires the `"downloads.shelf"` permission in * addition to the `"downloads"` permission. */ void setShelfEnabled(bool enabled) { if (_downloads == null) _throwNotAvailable(); _downloads.callMethod('setShelfEnabled', [enabled]); } Stream<DownloadItem> get onCreated => _onCreated.stream; final ChromeStreamController<DownloadItem> _onCreated = new ChromeStreamController<DownloadItem>.oneArg(_downloads, 'onCreated', _createDownloadItem); Stream<int> get onErased => _onErased.stream; final ChromeStreamController<int> _onErased = new ChromeStreamController<int>.oneArg(_downloads, 'onErased', selfConverter); Stream<DownloadDelta> get onChanged => _onChanged.stream; final ChromeStreamController<DownloadDelta> _onChanged = new ChromeStreamController<DownloadDelta>.oneArg(_downloads, 'onChanged', _createDownloadDelta); Stream<OnDeterminingFilenameEvent> get onDeterminingFilename => _onDeterminingFilename.stream; final ChromeStreamController<OnDeterminingFilenameEvent> _onDeterminingFilename = new ChromeStreamController<OnDeterminingFilenameEvent>.twoArgs(_downloads, 'onDeterminingFilename', _createOnDeterminingFilenameEvent); void _throwNotAvailable() { throw new UnsupportedError("'chrome.downloads' is not available"); } }
Extends
ChromeApi > ChromeDownloads
Properties
final bool available #
Returns true if the API is available. The common causes of an API not being avilable are:
- a permission is missing in the application's manifest.json file
- the API is defined on a newer version of Chrome then the current runtime
bool get available => _downloads != null;
final Stream<DownloadDelta> onChanged #
Stream<DownloadDelta> get onChanged => _onChanged.stream;
final Stream<DownloadItem> onCreated #
Stream<DownloadItem> get onCreated => _onCreated.stream;
final Stream<OnDeterminingFilenameEvent> onDeterminingFilename #
Stream<OnDeterminingFilenameEvent> get onDeterminingFilename => _onDeterminingFilename.stream;
final Stream<int> onErased #
Stream<int> get onErased => _onErased.stream;
Methods
Future acceptDanger(int downloadId) #
Prompt the user to accept a dangerous download. Does not automatically
accept dangerous downloads. If the download is accepted, then an
onChanged event will fire, otherwise nothing will happen. When all the
data is fetched into a temporary file and either the download is not
dangerous or the danger has been accepted, then the temporary file is
renamed to the target filename, the state
changes to 'complete', and
onChanged fires.
downloadId: The identifier for the DownloadItem.
callback
: Called when the danger prompt dialog closes.
Future acceptDanger(int downloadId) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter.noArgs(); _downloads.callMethod('acceptDanger', [downloadId, completer.callback]); return completer.future; }
Future cancel(int downloadId) #
Cancel a download. When callback
is run, the download is cancelled,
completed, interrupted or doesn't exist anymore.
downloadId: The id of the download to cancel.
callback
: Called when the cancel request is completed.
Future cancel(int downloadId) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter.noArgs(); _downloads.callMethod('cancel', [downloadId, completer.callback]); return completer.future; }
Future<int> download(DownloadOptions options) #
Download a URL. If the URL uses the HTTPS
protocol, then the request will
include all cookies currently set for its hostname. If both filename
and
saveAs
are specified, then the Save As dialog will be displayed,
pre-populated with the specified filename
. If the download started
successfully, callback
will be called with the new DownloadItem's
downloadId
. If there was an error starting the download, then callback
will be called with downloadId=undefined
and runtime.lastError
will
contain a descriptive string. The error strings are not guaranteed to
remain backwards compatible between releases. Extensions must not parse it.
options: What to download and how.
callback
: Called with the id of the new DownloadItem.
Future<int> download(DownloadOptions options) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter<int>.oneArg(); _downloads.callMethod('download', [jsify(options), completer.callback]); return completer.future; }
void drag(int downloadId) #
Initiate dragging the downloaded file to another application. Call in a
javascript ondragstart
handler.
void drag(int downloadId) { if (_downloads == null) _throwNotAvailable(); _downloads.callMethod('drag', [downloadId]); }
Future<List<int>> erase(DownloadQuery query) #
Erase matching DownloadItem from history without deleting the downloaded
file. An onErased event will fire for each DownloadItem that matches
query
, then callback
will be called.
Future<List<int>> erase(DownloadQuery query) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter<List<int>>.oneArg(listify); _downloads.callMethod('erase', [jsify(query), completer.callback]); return completer.future; }
Future<String> getFileIcon(int downloadId, [GetFileIconOptions options]) #
Retrieve an icon for the specified download. For new downloads, file icons
are available after the onCreated event has been received. The image
returned by this function while a download is in progress may be different
from the image returned after the download is complete. Icon retrieval is
done by querying the underlying operating system or toolkit depending on
the platform. The icon that is returned will therefore depend on a number
of factors including state of the download, platform, registered file types
and visual theme. If a file icon cannot be determined, runtime.lastError
will contain an error message.
downloadId: The identifier for the download.
callback
: A URL to an image that represents the download.
Future<String> getFileIcon(int downloadId, [GetFileIconOptions options]) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter<String>.oneArg(); _downloads.callMethod('getFileIcon', [downloadId, jsify(options), completer.callback]); return completer.future; }
void open(int downloadId) #
Open the downloaded file now if the DownloadItem is complete; otherwise
returns an error through runtime.lastError
. Requires the
"downloads.open"
permission in addition to the "downloads"
permission.
An onChanged event will fire when the item is opened for the first time.
downloadId: The identifier for the downloaded file.
void open(int downloadId) { if (_downloads == null) _throwNotAvailable(); _downloads.callMethod('open', [downloadId]); }
Future pause(int downloadId) #
Pause the download. If the request was successful the download is in a
paused state. Otherwise runtime.lastError
contains an error message. The
request will fail if the download is not active.
downloadId: The id of the download to pause.
callback
: Called when the pause request is completed.
Future pause(int downloadId) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter.noArgs(); _downloads.callMethod('pause', [downloadId, completer.callback]); return completer.future; }
Future removeFile(int downloadId) #
Remove the downloaded file if it exists and the DownloadItem is complete;
otherwise return an error through runtime.lastError
.
Future removeFile(int downloadId) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter.noArgs(); _downloads.callMethod('removeFile', [downloadId, completer.callback]); return completer.future; }
Future resume(int downloadId) #
Resume a paused download. If the request was successful the download is in
progress and unpaused. Otherwise runtime.lastError
contains an error
message. The request will fail if the download is not active.
downloadId: The id of the download to resume.
callback
: Called when the resume request is completed.
Future resume(int downloadId) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter.noArgs(); _downloads.callMethod('resume', [downloadId, completer.callback]); return completer.future; }
Future<List<DownloadItem>> search(DownloadQuery query) #
Find DownloadItem. Set query
to the empty object to get all
DownloadItem. To get a specific DownloadItem, set only the id
field.
To page through a large number of items, set orderBy: ['-startTime']
, set
limit
to the number of items per page, and set startedAfter
to the
startTime
of the last item from the last page.
Future<List<DownloadItem>> search(DownloadQuery query) { if (_downloads == null) _throwNotAvailable(); var completer = new ChromeCompleter<List<DownloadItem>>.oneArg((e) => listify(e, _createDownloadItem)); _downloads.callMethod('search', [jsify(query), completer.callback]); return completer.future; }
void setShelfEnabled(bool enabled) #
Enable or disable the gray shelf at the bottom of every window associated
with the current browser profile. The shelf will be disabled as long as at
least one extension has disabled it. Enabling the shelf while at least one
other extension has disabled it will return an error through
runtime.lastError
. Requires the "downloads.shelf"
permission in
addition to the "downloads"
permission.
void setShelfEnabled(bool enabled) { if (_downloads == null) _throwNotAvailable(); _downloads.callMethod('setShelfEnabled', [enabled]); }
void show(int downloadId) #
Show the downloaded file in its folder in a file manager. downloadId: The identifier for the downloaded file.
void show(int downloadId) { if (_downloads == null) _throwNotAvailable(); _downloads.callMethod('show', [downloadId]); }
void showDefaultFolder() #
Show the default Downloads folder in a file manager.
void showDefaultFolder() { if (_downloads == null) _throwNotAvailable(); _downloads.callMethod('showDefaultFolder'); }