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