1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Use the <code>chrome.downloads</code> API to programmatically initiate,
6 // monitor, manipulate, and search for downloads.
7 [permissions
=downloads
]
9 [inline_doc
] dictionary HeaderNameValuePair
{
10 // Name of the HTTP header.
13 // Value of the HTTP header.
17 // <dl><dt>uniquify</dt>
18 // <dd>To avoid duplication, the <code>filename</code> is changed to
19 // include a counter before the filename extension.</dd>
21 // <dd>The existing file will be overwritten with the new file.</dd>
23 // <dd>The user will be prompted with a file chooser dialog.</dd>
25 enum FilenameConflictAction
{uniquify
, overwrite
, prompt
};
27 [inline_doc
] dictionary FilenameSuggestion
{
28 // The $(ref:DownloadItem)'s new target $(ref:DownloadItem.filename), as a path
29 // relative to the user's default Downloads directory, possibly containing
30 // subdirectories. Absolute paths, empty paths, and paths containing
31 // back-references ".." will be ignored.
34 // The action to take if <code>filename</code> already exists.
35 FilenameConflictAction? conflictAction
;
38 [inline_doc
] enum HttpMethod
{GET
, POST
};
40 enum InterruptReason
{
49 FILE_SECURITY_CHECK_FAILED
,
55 NETWORK_INVALID_REQUEST
,
66 [inline_doc
] dictionary DownloadOptions
{
67 // The URL to download.
70 // A file path relative to the Downloads directory to contain the downloaded
71 // file, possibly containing subdirectories. Absolute paths, empty paths,
72 // and paths containing back-references ".." will cause an error.
73 // $(ref:onDeterminingFilename) allows suggesting a filename after the file's
74 // MIME type and a tentative filename have been determined.
77 // The action to take if <code>filename</code> already exists.
78 FilenameConflictAction? conflictAction
;
80 // Use a file-chooser to allow the user to select a filename regardless of
81 // whether <code>filename</code> is set or already exists.
84 // The HTTP method to use if the URL uses the HTTP[S] protocol.
87 // Extra HTTP headers to send with the request if the URL uses the HTTP[s]
88 // protocol. Each header is represented as a dictionary containing the keys
89 // <code>name</code> and either <code>value</code> or
90 // <code>binaryValue</code>, restricted to those allowed by XMLHttpRequest.
91 HeaderNameValuePair
[]? headers
;
98 // <dd>The download's filename is suspicious.</dd>
100 // <dd>The download's URL is known to be malicious.</dd>
102 // <dd>The downloaded file is known to be malicious.</dd>
104 // <dd>The download's URL is not commonly downloaded and could be
107 // <dd>The download came from a host known to distribute malicious
108 // binaries and is likely dangerous.</dd>
110 // <dd>The download is potentially unwanted or unsafe. E.g. it could make
111 // changes to browser or computer settings.</dd>
113 // <dd>The download presents no known danger to the user's computer.</dd>
115 // <dd>The user has accepted the dangerous download.</dd>
118 file
, url
, content
, uncommon
, host
, unwanted
, safe
, accepted
121 // <dl><dt>in_progress</dt>
122 // <dd>The download is currently receiving data from the server.</dd>
123 // <dt>interrupted</dt>
124 // <dd>An error broke the connection with the file host.</dd>
126 // <dd>The download completed successfully.</dd>
128 enum State
{in_progress
, interrupted
, complete
};
130 // The state of the process of downloading a file.
131 dictionary DownloadItem
{
132 // An identifier that is persistent across browser sessions.
141 // Absolute local path.
144 // False if this download is recorded in the history, true if it is not
148 // Indication of whether this download is thought to be safe or known to be
152 // The file's MIME type.
155 // The time when the download began in ISO 8601 format. May be passed
156 // directly to the Date constructor: <code>chrome.downloads.search({},
157 // function(items){items.forEach(function(item){console.log(new
158 // Date(item.startTime))})})</code>
161 // The time when the download ended in ISO 8601 format. May be passed
162 // directly to the Date constructor: <code>chrome.downloads.search({},
163 // function(items){items.forEach(function(item){if (item.endTime)
164 // console.log(new Date(item.endTime))})})</code>
167 // Estimated time when the download will complete in ISO 8601 format. May be
168 // passed directly to the Date constructor:
169 // <code>chrome.downloads.search({},
170 // function(items){items.forEach(function(item){if (item.estimatedEndTime)
171 // console.log(new Date(item.estimatedEndTime))})})</code>
172 DOMString? estimatedEndTime
;
174 // Indicates whether the download is progressing, interrupted, or complete.
177 // True if the download has stopped reading data from the host, but kept the
181 // True if the download is in progress and paused, or else if it is
182 // interrupted and can be resumed starting from where it was interrupted.
185 // Why the download was interrupted. Several kinds of HTTP errors may be
186 // grouped under one of the errors beginning with <code>SERVER_</code>.
187 // Errors relating to the network begin with <code>NETWORK_</code>, errors
188 // relating to the process of writing the file to the file system begin with
189 // <code>FILE_</code>, and interruptions initiated by the user begin with
190 // <code>USER_</code>.
191 InterruptReason? error
;
193 // Number of bytes received so far from the host, without considering file
195 double bytesReceived
;
197 // Number of bytes in the whole file, without considering file compression,
201 // Number of bytes in the whole file post-decompression, or -1 if unknown.
204 // Whether the downloaded file still exists. This information may be out of
205 // date because Chrome does not automatically watch for file removal. Call
206 // $(ref:search)() in order to trigger the check for file existence. When the
207 // existence check completes, if the file has been deleted, then an
208 // $(ref:onChanged) event will fire. Note that $(ref:search)() does not wait
209 // for the existence check to finish before returning, so results from
210 // $(ref:search)() may not accurately reflect the file system. Also,
211 // $(ref:search)() may be called as often as necessary, but will not check for
212 // file existence any more frequently than once every 10 seconds.
215 // The identifier for the extension that initiated this download if this
216 // download was initiated by an extension. Does not change once it is set.
217 DOMString? byExtensionId
;
219 // The localized name of the extension that initiated this download if this
220 // download was initiated by an extension. May change if the extension
221 // changes its name or if the user changes their locale.
222 DOMString? byExtensionName
;
225 [inline_doc
] dictionary DownloadQuery
{
226 // This array of search terms limits results to $(ref:DownloadItem) whose
227 // <code>filename</code> or <code>url</code> contain all of the search terms
228 // that do not begin with a dash '-' and none of the search terms that do
229 // begin with a dash.
232 // Limits results to $(ref:DownloadItem) that
233 // started before the given ms since the epoch.
234 DOMString? startedBefore
;
236 // Limits results to $(ref:DownloadItem) that
237 // started after the given ms since the epoch.
238 DOMString? startedAfter
;
240 // Limits results to $(ref:DownloadItem) that ended before the given ms since the
242 DOMString? endedBefore
;
244 // Limits results to $(ref:DownloadItem) that ended after the given ms since the
246 DOMString? endedAfter
;
248 // Limits results to $(ref:DownloadItem) whose
249 // <code>totalBytes</code> is greater than the given integer.
250 double? totalBytesGreater
;
252 // Limits results to $(ref:DownloadItem) whose
253 // <code>totalBytes</code> is less than the given integer.
254 double? totalBytesLess
;
256 // Limits results to $(ref:DownloadItem) whose
257 // <code>filename</code> matches the given regular expression.
258 DOMString? filenameRegex
;
260 // Limits results to $(ref:DownloadItem) whose
261 // <code>url</code> matches the given regular expression.
264 // The maximum number of matching $(ref:DownloadItem) returned. Defaults to
265 // 1000. Set to 0 in order to return all matching $(ref:DownloadItem). See
266 // $(ref:search) for how to page through results.
269 // Set elements of this array to $(ref:DownloadItem) properties in order to
270 // sort search results. For example, setting
271 // <code>orderBy=['startTime']</code> sorts the $(ref:DownloadItem) by their
272 // start time in ascending order. To specify descending order, prefix with a
273 // hyphen: '-startTime'.
274 DOMString
[]? orderBy
;
276 // The <code>id</code> of the $(ref:DownloadItem) to query.
282 // Absolute local path.
285 // Indication of whether this download is thought to be safe or known to be
289 // The file's MIME type.
292 // The time when the download began in ISO 8601 format.
293 DOMString? startTime
;
295 // The time when the download ended in ISO 8601 format.
298 // Indicates whether the download is progressing, interrupted, or complete.
301 // True if the download has stopped reading data from the host, but kept the
305 // Why a download was interrupted.
306 InterruptReason? error
;
308 // Number of bytes received so far from the host, without considering file
310 double? bytesReceived
;
312 // Number of bytes in the whole file, without considering file compression,
316 // Number of bytes in the whole file post-decompression, or -1 if unknown.
319 // Whether the downloaded file exists;
323 dictionary StringDelta
{
328 dictionary DoubleDelta
{
333 dictionary BooleanDelta
{
338 // Encapsulates a change in a DownloadItem.
339 [inline_doc
] dictionary DownloadDelta
{
340 // The <code>id</code> of the $(ref:DownloadItem)
344 // The change in <code>url</code>, if any.
347 // The change in <code>filename</code>, if any.
348 StringDelta? filename
;
350 // The change in <code>danger</code>, if any.
353 // The change in <code>mime</code>, if any.
356 // The change in <code>startTime</code>, if any.
357 StringDelta? startTime
;
359 // The change in <code>endTime</code>, if any.
360 StringDelta? endTime
;
362 // The change in <code>state</code>, if any.
365 // The change in <code>canResume</code>, if any.
366 BooleanDelta? canResume
;
368 // The change in <code>paused</code>, if any.
369 BooleanDelta? paused
;
371 // The change in <code>error</code>, if any.
374 // The change in <code>totalBytes</code>, if any.
375 DoubleDelta? totalBytes
;
377 // The change in <code>fileSize</code>, if any.
378 DoubleDelta? fileSize
;
380 // The change in <code>exists</code>, if any.
381 BooleanDelta? exists
;
384 [inline_doc
] dictionary GetFileIconOptions
{
385 // The size of the returned icon. The icon will be square with dimensions
386 // size * size pixels. The default and largest size for the icon is 32x32
387 // pixels. The only supported sizes are 16 and 32. It is an error to specify
389 [legalValues
=(16,32)] long? size
;
392 callback DownloadCallback
= void(long downloadId
);
393 callback SearchCallback
= void(DownloadItem
[] results
);
394 callback EraseCallback
= void(long[] erasedIds
);
395 callback NullCallback
= void();
396 callback GetFileIconCallback
= void(optional DOMString iconURL
);
397 callback SuggestFilenameCallback
= void(
398 optional FilenameSuggestion suggestion
);
400 interface Functions
{
401 // Download a URL. If the URL uses the HTTP[S] protocol, then the request
402 // will include all cookies currently set for its hostname. If both
403 // <code>filename</code> and <code>saveAs</code> are specified, then the
404 // Save As dialog will be displayed, pre-populated with the specified
405 // <code>filename</code>. If the download started successfully,
406 // <code>callback</code> will be called with the new $(ref:DownloadItem)'s
407 // <code>downloadId</code>. If there was an error starting the download,
408 // then <code>callback</code> will be called with
409 // <code>downloadId=undefined</code> and $(ref:runtime.lastError) will contain
410 // a descriptive string. The error strings are not guaranteed to remain
411 // backwards compatible between releases. Extensions must not parse it.
412 // |options|: What to download and how.
413 // |callback|: Called with the id of the new $(ref:DownloadItem).
414 static
void download
(DownloadOptions options
,
415 optional DownloadCallback
callback);
417 // Find $(ref:DownloadItem). Set <code>query</code> to the empty object to get
418 // all $(ref:DownloadItem). To get a specific $(ref:DownloadItem), set only the
419 // <code>id</code> field. To page through a large number of items, set
420 // <code>orderBy: ['-startTime']</code>, set <code>limit</code> to the
421 // number of items per page, and set <code>startedAfter</code> to the
422 // <code>startTime</code> of the last item from the last page.
423 static
void search
(DownloadQuery query
, SearchCallback
callback);
425 // Pause the download. If the request was successful the download is in a
426 // paused state. Otherwise $(ref:runtime.lastError) contains an error message.
427 // The request will fail if the download is not active.
428 // |downloadId|: The id of the download to pause.
429 // |callback|: Called when the pause request is completed.
430 static
void pause
(long downloadId
, optional NullCallback
callback);
432 // Resume a paused download. If the request was successful the download is
433 // in progress and unpaused. Otherwise $(ref:runtime.lastError) contains an
434 // error message. The request will fail if the download is not active.
435 // |downloadId|: The id of the download to resume.
436 // |callback|: Called when the resume request is completed.
437 static
void resume
(long downloadId
, optional NullCallback
callback);
439 // Cancel a download. When <code>callback</code> is run, the download is
440 // cancelled, completed, interrupted or doesn't exist anymore.
441 // |downloadId|: The id of the download to cancel.
442 // |callback|: Called when the cancel request is completed.
443 static
void cancel
(long downloadId
, optional NullCallback
callback);
445 // Retrieve an icon for the specified download. For new downloads, file
446 // icons are available after the $(ref:onCreated) event has been received. The
447 // image returned by this function while a download is in progress may be
448 // different from the image returned after the download is complete. Icon
449 // retrieval is done by querying the underlying operating system or toolkit
450 // depending on the platform. The icon that is returned will therefore
451 // depend on a number of factors including state of the download, platform,
452 // registered file types and visual theme. If a file icon cannot be
453 // determined, $(ref:runtime.lastError) will contain an error message.
454 // |downloadId|: The identifier for the download.
455 // |callback|: A URL to an image that represents the download.
456 static
void getFileIcon
(long downloadId
,
457 optional GetFileIconOptions options
,
458 GetFileIconCallback
callback);
460 // Open the downloaded file now if the $(ref:DownloadItem) is complete;
461 // otherwise returns an error through $(ref:runtime.lastError). Requires the
462 // <code>"downloads.open"</code> permission in addition to the
463 // <code>"downloads"</code> permission. An $(ref:onChanged) event will fire
464 // when the item is opened for the first time.
465 // |downloadId|: The identifier for the downloaded file.
466 static
void open
(long downloadId
);
468 // Show the downloaded file in its folder in a file manager.
469 // |downloadId|: The identifier for the downloaded file.
470 static
void show
(long downloadId
);
472 // Show the default Downloads folder in a file manager.
473 static
void showDefaultFolder
();
475 // Erase matching $(ref:DownloadItem) from history without deleting the
476 // downloaded file. An $(ref:onErased) event will fire for each
477 // $(ref:DownloadItem) that matches <code>query</code>, then
478 // <code>callback</code> will be called.
479 static
void erase
(DownloadQuery query
, optional EraseCallback
callback);
481 // Remove the downloaded file if it exists and the $(ref:DownloadItem) is
482 // complete; otherwise return an error through $(ref:runtime.lastError).
483 static
void removeFile
(long downloadId
, optional NullCallback
callback);
485 // Prompt the user to accept a dangerous download. Does not automatically
486 // accept dangerous downloads. If the download is accepted, then an
487 // $(ref:onChanged) event will fire, otherwise nothing will happen. When all
488 // the data is fetched into a temporary file and either the download is not
489 // dangerous or the danger has been accepted, then the temporary file is
490 // renamed to the target filename, the |state| changes to 'complete', and
491 // $(ref:onChanged) fires.
492 // |downloadId|: The identifier for the $(ref:DownloadItem).
493 // |callback|: Called when the danger prompt dialog closes.
494 static
void acceptDanger
(long downloadId
, optional NullCallback
callback);
496 // Initiate dragging the downloaded file to another application. Call in a
497 // javascript <code>ondragstart</code> handler.
498 static
void drag
(long downloadId
);
500 // Enable or disable the gray shelf at the bottom of every window associated
501 // with the current browser profile. The shelf will be disabled as long as
502 // at least one extension has disabled it. Enabling the shelf while at least
503 // one other extension has disabled it will return an error through
504 // $(ref:runtime.lastError). Requires the <code>"downloads.shelf"</code>
505 // permission in addition to the <code>"downloads"</code> permission.
506 static
void setShelfEnabled
(boolean enabled
);
510 // This event fires with the $(ref:DownloadItem) object when a download
512 static
void onCreated
(DownloadItem downloadItem
);
514 // Fires with the <code>downloadId</code> when a download is erased from
516 // |downloadId|: The <code>id</code> of the $(ref:DownloadItem) that was
518 static
void onErased
(long downloadId
);
520 // When any of a $(ref:DownloadItem)'s properties except
521 // <code>bytesReceived</code> and <code>estimatedEndTime</code> changes,
522 // this event fires with the <code>downloadId</code> and an object
523 // containing the properties that changed.
524 static
void onChanged
(DownloadDelta downloadDelta
);
526 // During the filename determination process, extensions will be given the
527 // opportunity to override the target $(ref:DownloadItem.filename). Each
528 // extension may not register more than one listener for this event. Each
529 // listener must call <code>suggest</code> exactly once, either
530 // synchronously or asynchronously. If the listener calls
531 // <code>suggest</code> asynchronously, then it must return
532 // <code>true</code>. If the listener neither calls <code>suggest</code>
533 // synchronously nor returns <code>true</code>, then <code>suggest</code>
534 // will be called automatically. The $(ref:DownloadItem) will not complete
535 // until all listeners have called <code>suggest</code>. Listeners may call
536 // <code>suggest</code> without any arguments in order to allow the download
537 // to use <code>downloadItem.filename</code> for its filename, or pass a
538 // <code>suggestion</code> object to <code>suggest</code> in order to
539 // override the target filename. If more than one extension overrides the
540 // filename, then the last extension installed whose listener passes a
541 // <code>suggestion</code> object to <code>suggest</code> wins. In order to
542 // avoid confusion regarding which extension will win, users should not
543 // install extensions that may conflict. If the download is initiated by
544 // $(ref:download) and the target filename is known before the MIME type and
545 // tentative filename have been determined, pass <code>filename</code> to
546 // $(ref:download) instead.
547 [maxListeners
=1] static
void onDeterminingFilename
(
548 DownloadItem downloadItem
, SuggestFilenameCallback suggest
);