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
,
64 [inline_doc
] dictionary DownloadOptions
{
65 // The URL to download.
68 // A file path relative to the Downloads directory to contain the downloaded
69 // file, possibly containing subdirectories. Absolute paths, empty paths,
70 // and paths containing back-references ".." will cause an error.
71 // $(ref:onDeterminingFilename) allows suggesting a filename after the file's
72 // MIME type and a tentative filename have been determined.
75 // The action to take if <code>filename</code> already exists.
76 FilenameConflictAction? conflictAction
;
78 // Use a file-chooser to allow the user to select a filename regardless of
79 // whether <code>filename</code> is set or already exists.
82 // The HTTP method to use if the URL uses the HTTP[S] protocol.
85 // Extra HTTP headers to send with the request if the URL uses the HTTP[s]
86 // protocol. Each header is represented as a dictionary containing the keys
87 // <code>name</code> and either <code>value</code> or
88 // <code>binaryValue</code>, restricted to those allowed by XMLHttpRequest.
89 HeaderNameValuePair
[]? headers
;
96 // <dd>The download's filename is suspicious.</dd>
98 // <dd>The download's URL is known to be malicious.</dd>
100 // <dd>The downloaded file is known to be malicious.</dd>
102 // <dd>The download's URL is not commonly downloaded and could be
105 // <dd>The download came from a host known to distribute malicious
106 // binaries and is likely dangerous.</dd>
108 // <dd>The download is potentially unwanted or unsafe. E.g. it could make
109 // changes to browser or computer settings.</dd>
111 // <dd>The download presents no known danger to the user's computer.</dd>
113 // <dd>The user has accepted the dangerous download.</dd>
116 file
, url
, content
, uncommon
, host
, unwanted
, safe
, accepted
119 // <dl><dt>in_progress</dt>
120 // <dd>The download is currently receiving data from the server.</dd>
121 // <dt>interrupted</dt>
122 // <dd>An error broke the connection with the file host.</dd>
124 // <dd>The download completed successfully.</dd>
126 enum State
{in_progress
, interrupted
, complete
};
128 // The state of the process of downloading a file.
129 dictionary DownloadItem
{
130 // An identifier that is persistent across browser sessions.
139 // Absolute local path.
142 // False if this download is recorded in the history, true if it is not
146 // Indication of whether this download is thought to be safe or known to be
150 // The file's MIME type.
153 // The time when the download began in ISO 8601 format. May be passed
154 // directly to the Date constructor: <code>chrome.downloads.search({},
155 // function(items){items.forEach(function(item){console.log(new
156 // Date(item.startTime))})})</code>
159 // The time when the download ended in ISO 8601 format. May be passed
160 // directly to the Date constructor: <code>chrome.downloads.search({},
161 // function(items){items.forEach(function(item){if (item.endTime)
162 // console.log(new Date(item.endTime))})})</code>
165 // Estimated time when the download will complete in ISO 8601 format. May be
166 // passed directly to the Date constructor:
167 // <code>chrome.downloads.search({},
168 // function(items){items.forEach(function(item){if (item.estimatedEndTime)
169 // console.log(new Date(item.estimatedEndTime))})})</code>
170 DOMString? estimatedEndTime
;
172 // Indicates whether the download is progressing, interrupted, or complete.
175 // True if the download has stopped reading data from the host, but kept the
179 // True if the download is in progress and paused, or else if it is
180 // interrupted and can be resumed starting from where it was interrupted.
183 // Why the download was interrupted. Several kinds of HTTP errors may be
184 // grouped under one of the errors beginning with <code>SERVER_</code>.
185 // Errors relating to the network begin with <code>NETWORK_</code>, errors
186 // relating to the process of writing the file to the file system begin with
187 // <code>FILE_</code>, and interruptions initiated by the user begin with
188 // <code>USER_</code>.
189 InterruptReason? error
;
191 // Number of bytes received so far from the host, without considering file
193 double bytesReceived
;
195 // Number of bytes in the whole file, without considering file compression,
199 // Number of bytes in the whole file post-decompression, or -1 if unknown.
202 // Whether the downloaded file still exists. This information may be out of
203 // date because Chrome does not automatically watch for file removal. Call
204 // $(ref:search)() in order to trigger the check for file existence. When the
205 // existence check completes, if the file has been deleted, then an
206 // $(ref:onChanged) event will fire. Note that $(ref:search)() does not wait
207 // for the existence check to finish before returning, so results from
208 // $(ref:search)() may not accurately reflect the file system. Also,
209 // $(ref:search)() may be called as often as necessary, but will not check for
210 // file existence any more frequently than once every 10 seconds.
213 // The identifier for the extension that initiated this download if this
214 // download was initiated by an extension. Does not change once it is set.
215 DOMString? byExtensionId
;
217 // The localized name of the extension that initiated this download if this
218 // download was initiated by an extension. May change if the extension
219 // changes its name or if the user changes their locale.
220 DOMString? byExtensionName
;
223 [inline_doc
] dictionary DownloadQuery
{
224 // This array of search terms limits results to $(ref:DownloadItem) whose
225 // <code>filename</code> or <code>url</code> contain all of the search terms
226 // that do not begin with a dash '-' and none of the search terms that do
227 // begin with a dash.
230 // Limits results to $(ref:DownloadItem) that
231 // started before the given ms since the epoch.
232 DOMString? startedBefore
;
234 // Limits results to $(ref:DownloadItem) that
235 // started after the given ms since the epoch.
236 DOMString? startedAfter
;
238 // Limits results to $(ref:DownloadItem) that ended before the given ms since the
240 DOMString? endedBefore
;
242 // Limits results to $(ref:DownloadItem) that ended after the given ms since the
244 DOMString? endedAfter
;
246 // Limits results to $(ref:DownloadItem) whose
247 // <code>totalBytes</code> is greater than the given integer.
248 double? totalBytesGreater
;
250 // Limits results to $(ref:DownloadItem) whose
251 // <code>totalBytes</code> is less than the given integer.
252 double? totalBytesLess
;
254 // Limits results to $(ref:DownloadItem) whose
255 // <code>filename</code> matches the given regular expression.
256 DOMString? filenameRegex
;
258 // Limits results to $(ref:DownloadItem) whose
259 // <code>url</code> matches the given regular expression.
262 // The maximum number of matching $(ref:DownloadItem) returned. Defaults to
263 // 1000. Set to 0 in order to return all matching $(ref:DownloadItem). See
264 // $(ref:search) for how to page through results.
267 // Set elements of this array to $(ref:DownloadItem) properties in order to
268 // sort search results. For example, setting
269 // <code>orderBy=['startTime']</code> sorts the $(ref:DownloadItem) by their
270 // start time in ascending order. To specify descending order, prefix with a
271 // hyphen: '-startTime'.
272 DOMString
[]? orderBy
;
274 // The <code>id</code> of the $(ref:DownloadItem) to query.
280 // Absolute local path.
283 // Indication of whether this download is thought to be safe or known to be
287 // The file's MIME type.
290 // The time when the download began in ISO 8601 format.
291 DOMString? startTime
;
293 // The time when the download ended in ISO 8601 format.
296 // Indicates whether the download is progressing, interrupted, or complete.
299 // True if the download has stopped reading data from the host, but kept the
303 // Why a download was interrupted.
304 InterruptReason? error
;
306 // Number of bytes received so far from the host, without considering file
308 double? bytesReceived
;
310 // Number of bytes in the whole file, without considering file compression,
314 // Number of bytes in the whole file post-decompression, or -1 if unknown.
317 // Whether the downloaded file exists;
321 dictionary StringDelta
{
326 dictionary DoubleDelta
{
331 dictionary BooleanDelta
{
336 // Encapsulates a change in a DownloadItem.
337 [inline_doc
] dictionary DownloadDelta
{
338 // The <code>id</code> of the $(ref:DownloadItem)
342 // The change in <code>url</code>, if any.
345 // The change in <code>filename</code>, if any.
346 StringDelta? filename
;
348 // The change in <code>danger</code>, if any.
351 // The change in <code>mime</code>, if any.
354 // The change in <code>startTime</code>, if any.
355 StringDelta? startTime
;
357 // The change in <code>endTime</code>, if any.
358 StringDelta? endTime
;
360 // The change in <code>state</code>, if any.
363 // The change in <code>canResume</code>, if any.
364 BooleanDelta? canResume
;
366 // The change in <code>paused</code>, if any.
367 BooleanDelta? paused
;
369 // The change in <code>error</code>, if any.
372 // The change in <code>totalBytes</code>, if any.
373 DoubleDelta? totalBytes
;
375 // The change in <code>fileSize</code>, if any.
376 DoubleDelta? fileSize
;
378 // The change in <code>exists</code>, if any.
379 BooleanDelta? exists
;
382 [inline_doc
] dictionary GetFileIconOptions
{
383 // The size of the returned icon. The icon will be square with dimensions
384 // size * size pixels. The default and largest size for the icon is 32x32
385 // pixels. The only supported sizes are 16 and 32. It is an error to specify
387 [legalValues
=(16,32)] long? size
;
390 callback DownloadCallback
= void(long downloadId
);
391 callback SearchCallback
= void(DownloadItem
[] results
);
392 callback EraseCallback
= void(long[] erasedIds
);
393 callback NullCallback
= void();
394 callback GetFileIconCallback
= void(optional DOMString iconURL
);
395 callback SuggestFilenameCallback
= void(
396 optional FilenameSuggestion suggestion
);
398 interface Functions
{
399 // Download a URL. If the URL uses the HTTP[S] protocol, then the request
400 // will include all cookies currently set for its hostname. If both
401 // <code>filename</code> and <code>saveAs</code> are specified, then the
402 // Save As dialog will be displayed, pre-populated with the specified
403 // <code>filename</code>. If the download started successfully,
404 // <code>callback</code> will be called with the new $(ref:DownloadItem)'s
405 // <code>downloadId</code>. If there was an error starting the download,
406 // then <code>callback</code> will be called with
407 // <code>downloadId=undefined</code> and $(ref:runtime.lastError) will contain
408 // a descriptive string. The error strings are not guaranteed to remain
409 // backwards compatible between releases. Extensions must not parse it.
410 // |options|: What to download and how.
411 // |callback|: Called with the id of the new $(ref:DownloadItem).
412 static
void download
(DownloadOptions options
,
413 optional DownloadCallback
callback);
415 // Find $(ref:DownloadItem). Set <code>query</code> to the empty object to get
416 // all $(ref:DownloadItem). To get a specific $(ref:DownloadItem), set only the
417 // <code>id</code> field. To page through a large number of items, set
418 // <code>orderBy: ['-startTime']</code>, set <code>limit</code> to the
419 // number of items per page, and set <code>startedAfter</code> to the
420 // <code>startTime</code> of the last item from the last page.
421 static
void search
(DownloadQuery query
, SearchCallback
callback);
423 // Pause the download. If the request was successful the download is in a
424 // paused state. Otherwise $(ref:runtime.lastError) contains an error message.
425 // The request will fail if the download is not active.
426 // |downloadId|: The id of the download to pause.
427 // |callback|: Called when the pause request is completed.
428 static
void pause
(long downloadId
, optional NullCallback
callback);
430 // Resume a paused download. If the request was successful the download is
431 // in progress and unpaused. Otherwise $(ref:runtime.lastError) contains an
432 // error message. The request will fail if the download is not active.
433 // |downloadId|: The id of the download to resume.
434 // |callback|: Called when the resume request is completed.
435 static
void resume
(long downloadId
, optional NullCallback
callback);
437 // Cancel a download. When <code>callback</code> is run, the download is
438 // cancelled, completed, interrupted or doesn't exist anymore.
439 // |downloadId|: The id of the download to cancel.
440 // |callback|: Called when the cancel request is completed.
441 static
void cancel
(long downloadId
, optional NullCallback
callback);
443 // Retrieve an icon for the specified download. For new downloads, file
444 // icons are available after the $(ref:onCreated) event has been received. The
445 // image returned by this function while a download is in progress may be
446 // different from the image returned after the download is complete. Icon
447 // retrieval is done by querying the underlying operating system or toolkit
448 // depending on the platform. The icon that is returned will therefore
449 // depend on a number of factors including state of the download, platform,
450 // registered file types and visual theme. If a file icon cannot be
451 // determined, $(ref:runtime.lastError) will contain an error message.
452 // |downloadId|: The identifier for the download.
453 // |callback|: A URL to an image that represents the download.
454 static
void getFileIcon
(long downloadId
,
455 optional GetFileIconOptions options
,
456 GetFileIconCallback
callback);
458 // Open the downloaded file now if the $(ref:DownloadItem) is complete;
459 // otherwise returns an error through $(ref:runtime.lastError). Requires the
460 // <code>"downloads.open"</code> permission in addition to the
461 // <code>"downloads"</code> permission. An $(ref:onChanged) event will fire
462 // when the item is opened for the first time.
463 // |downloadId|: The identifier for the downloaded file.
464 static
void open
(long downloadId
);
466 // Show the downloaded file in its folder in a file manager.
467 // |downloadId|: The identifier for the downloaded file.
468 static
void show
(long downloadId
);
470 // Show the default Downloads folder in a file manager.
471 static
void showDefaultFolder
();
473 // Erase matching $(ref:DownloadItem) from history without deleting the
474 // downloaded file. An $(ref:onErased) event will fire for each
475 // $(ref:DownloadItem) that matches <code>query</code>, then
476 // <code>callback</code> will be called.
477 static
void erase
(DownloadQuery query
, optional EraseCallback
callback);
479 // Remove the downloaded file if it exists and the $(ref:DownloadItem) is
480 // complete; otherwise return an error through $(ref:runtime.lastError).
481 static
void removeFile
(long downloadId
, optional NullCallback
callback);
483 // Prompt the user to accept a dangerous download. Does not automatically
484 // accept dangerous downloads. If the download is accepted, then an
485 // $(ref:onChanged) event will fire, otherwise nothing will happen. When all
486 // the data is fetched into a temporary file and either the download is not
487 // dangerous or the danger has been accepted, then the temporary file is
488 // renamed to the target filename, the |state| changes to 'complete', and
489 // $(ref:onChanged) fires.
490 // |downloadId|: The identifier for the $(ref:DownloadItem).
491 // |callback|: Called when the danger prompt dialog closes.
492 static
void acceptDanger
(long downloadId
, optional NullCallback
callback);
494 // Initiate dragging the downloaded file to another application. Call in a
495 // javascript <code>ondragstart</code> handler.
496 static
void drag
(long downloadId
);
498 // Enable or disable the gray shelf at the bottom of every window associated
499 // with the current browser profile. The shelf will be disabled as long as
500 // at least one extension has disabled it. Enabling the shelf while at least
501 // one other extension has disabled it will return an error through
502 // $(ref:runtime.lastError). Requires the <code>"downloads.shelf"</code>
503 // permission in addition to the <code>"downloads"</code> permission.
504 static
void setShelfEnabled
(boolean enabled
);
508 // This event fires with the $(ref:DownloadItem) object when a download
510 static
void onCreated
(DownloadItem downloadItem
);
512 // Fires with the <code>downloadId</code> when a download is erased from
514 // |downloadId|: The <code>id</code> of the $(ref:DownloadItem) that was
516 static
void onErased
(long downloadId
);
518 // When any of a $(ref:DownloadItem)'s properties except
519 // <code>bytesReceived</code> and <code>estimatedEndTime</code> changes,
520 // this event fires with the <code>downloadId</code> and an object
521 // containing the properties that changed.
522 static
void onChanged
(DownloadDelta downloadDelta
);
524 // During the filename determination process, extensions will be given the
525 // opportunity to override the target $(ref:DownloadItem.filename). Each
526 // extension may not register more than one listener for this event. Each
527 // listener must call <code>suggest</code> exactly once, either
528 // synchronously or asynchronously. If the listener calls
529 // <code>suggest</code> asynchronously, then it must return
530 // <code>true</code>. If the listener neither calls <code>suggest</code>
531 // synchronously nor returns <code>true</code>, then <code>suggest</code>
532 // will be called automatically. The $(ref:DownloadItem) will not complete
533 // until all listeners have called <code>suggest</code>. Listeners may call
534 // <code>suggest</code> without any arguments in order to allow the download
535 // to use <code>downloadItem.filename</code> for its filename, or pass a
536 // <code>suggestion</code> object to <code>suggest</code> in order to
537 // override the target filename. If more than one extension overrides the
538 // filename, then the last extension installed whose listener passes a
539 // <code>suggestion</code> object to <code>suggest</code> wins. In order to
540 // avoid confusion regarding which extension will win, users should not
541 // install extensions that may conflict. If the download is initiated by
542 // $(ref:download) and the target filename is known before the MIME type and
543 // tentative filename have been determined, pass <code>filename</code> to
544 // $(ref:download) instead.
545 [maxListeners
=1] static
void onDeterminingFilename
(
546 DownloadItem downloadItem
, SuggestFilenameCallback suggest
);