• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5// Use the <code>chrome.downloads</code> API to programmatically initiate,
6// monitor, manipulate, and search for downloads.
7[permissions=downloads]
8namespace downloads {
9  [inline_doc] dictionary HeaderNameValuePair {
10    // Name of the HTTP header.
11    DOMString name;
12
13    // Value of the HTTP header.
14    DOMString value;
15  };
16
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>
20  //     <dt>overwrite</dt>
21  //     <dd>The existing file will be overwritten with the new file.</dd>
22  //     <dt>prompt</dt>
23  //     <dd>The user will be prompted with a file chooser dialog.</dd>
24  // </dl>
25  enum FilenameConflictAction {uniquify, overwrite, prompt};
26
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.
32    DOMString filename;
33
34    // The action to take if <code>filename</code> already exists.
35    FilenameConflictAction? conflictAction;
36  };
37
38  [inline_doc] enum HttpMethod {GET, POST};
39
40  enum InterruptReason {
41    FILE_FAILED,
42    FILE_ACCESS_DENIED,
43    FILE_NO_SPACE,
44    FILE_NAME_TOO_LONG,
45    FILE_TOO_LARGE,
46    FILE_VIRUS_INFECTED,
47    FILE_TRANSIENT_ERROR,
48    FILE_BLOCKED,
49    FILE_SECURITY_CHECK_FAILED,
50    FILE_TOO_SHORT,
51    NETWORK_FAILED,
52    NETWORK_TIMEOUT,
53    NETWORK_DISCONNECTED,
54    NETWORK_SERVER_DOWN,
55    NETWORK_INVALID_REQUEST,
56    SERVER_FAILED,
57    SERVER_NO_RANGE,
58    SERVER_PRECONDITION,
59    SERVER_BAD_CONTENT,
60    SERVER_UNAUTHORIZED,
61    SERVER_CERT_PROBLEM,
62    USER_CANCELED,
63    USER_SHUTDOWN,
64    CRASH};
65
66  [inline_doc] dictionary DownloadOptions {
67    // The URL to download.
68    DOMString url;
69
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.
75    DOMString? filename;
76
77    // The action to take if <code>filename</code> already exists.
78    FilenameConflictAction? conflictAction;
79
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.
82    boolean? saveAs;
83
84    // The HTTP method to use if the URL uses the HTTP[S] protocol.
85    HttpMethod? method;
86
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;
92
93    // Post body.
94    DOMString? body;
95  };
96
97  // <dl><dt>file</dt>
98  //     <dd>The download's filename is suspicious.</dd>
99  //     <dt>url</dt>
100  //     <dd>The download's URL is known to be malicious.</dd>
101  //     <dt>content</dt>
102  //     <dd>The downloaded file is known to be malicious.</dd>
103  //     <dt>uncommon</dt>
104  //     <dd>The download's URL is not commonly downloaded and could be
105  //     dangerous.</dd>
106  //     <dt>host</dt>
107  //     <dd>The download came from a host known to distribute malicious
108  //     binaries and is likely dangerous.</dd>
109  //     <dt>unwanted</dt>
110  //     <dd>The download is potentially unwanted or unsafe. E.g. it could make
111  //     changes to browser or computer settings.</dd>
112  //     <dt>safe</dt>
113  //     <dd>The download presents no known danger to the user's computer.</dd>
114  //     <dt>accepted</dt>
115  //     <dd>The user has accepted the dangerous download.</dd>
116  // </dl>
117  enum DangerType {
118    file, url, content, uncommon, host, unwanted, safe, accepted
119  };
120
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>
125  //     <dt>complete</dt>
126  //     <dd>The download completed successfully.</dd>
127  // </dl>
128  enum State {in_progress, interrupted, complete};
129
130  // The state of the process of downloading a file.
131  dictionary DownloadItem {
132    // An identifier that is persistent across browser sessions.
133    long id;
134
135    // Absolute URL.
136    DOMString url;
137
138    // Absolute URL.
139    DOMString referrer;
140
141    // Absolute local path.
142    DOMString filename;
143
144    // False if this download is recorded in the history, true if it is not
145    // recorded.
146    boolean incognito;
147
148    // Indication of whether this download is thought to be safe or known to be
149    // suspicious.
150    DangerType danger;
151
152    // The file's MIME type.
153    DOMString mime;
154
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>
159    DOMString startTime;
160
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>
165    DOMString? endTime;
166
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;
173
174    // Indicates whether the download is progressing, interrupted, or complete.
175    State state;
176
177    // True if the download has stopped reading data from the host, but kept the
178    // connection open.
179    boolean paused;
180
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.
183    boolean canResume;
184
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;
192
193    // Number of bytes received so far from the host, without considering file
194    // compression.
195    double bytesReceived;
196
197    // Number of bytes in the whole file, without considering file compression,
198    // or -1 if unknown.
199    double totalBytes;
200
201    // Number of bytes in the whole file post-decompression, or -1 if unknown.
202    double fileSize;
203
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.
213    boolean exists;
214
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;
218
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;
223  };
224
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.
230    DOMString[]? query;
231
232    // Limits results to $(ref:DownloadItem) that
233    // started before the given ms since the epoch.
234    DOMString? startedBefore;
235
236    // Limits results to $(ref:DownloadItem) that
237    // started after the given ms since the epoch.
238    DOMString? startedAfter;
239
240    // Limits results to $(ref:DownloadItem) that ended before the given ms since the
241    // epoch.
242    DOMString? endedBefore;
243
244    // Limits results to $(ref:DownloadItem) that ended after the given ms since the
245    // epoch.
246    DOMString? endedAfter;
247
248    // Limits results to $(ref:DownloadItem) whose
249    // <code>totalBytes</code> is greater than the given integer.
250    double? totalBytesGreater;
251
252    // Limits results to $(ref:DownloadItem) whose
253    // <code>totalBytes</code> is less than the given integer.
254    double? totalBytesLess;
255
256    // Limits results to $(ref:DownloadItem) whose
257    // <code>filename</code> matches the given regular expression.
258    DOMString? filenameRegex;
259
260    // Limits results to $(ref:DownloadItem) whose
261    // <code>url</code> matches the given regular expression.
262    DOMString? urlRegex;
263
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.
267    long? limit;
268
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;
275
276    // The <code>id</code> of the $(ref:DownloadItem) to query.
277    long? id;
278
279    // Absolute URL.
280    DOMString? url;
281
282    // Absolute local path.
283    DOMString? filename;
284
285    // Indication of whether this download is thought to be safe or known to be
286    // suspicious.
287    DangerType? danger;
288
289    // The file's MIME type.
290    DOMString? mime;
291
292    // The time when the download began in ISO 8601 format.
293    DOMString? startTime;
294
295    // The time when the download ended in ISO 8601 format.
296    DOMString? endTime;
297
298    // Indicates whether the download is progressing, interrupted, or complete.
299    State? state;
300
301    // True if the download has stopped reading data from the host, but kept the
302    // connection open.
303    boolean? paused;
304
305    // Why a download was interrupted.
306    InterruptReason? error;
307
308    // Number of bytes received so far from the host, without considering file
309    // compression.
310    double? bytesReceived;
311
312    // Number of bytes in the whole file, without considering file compression,
313    // or -1 if unknown.
314    double? totalBytes;
315
316    // Number of bytes in the whole file post-decompression, or -1 if unknown.
317    double? fileSize;
318
319    // Whether the downloaded file exists;
320    boolean? exists;
321  };
322
323  dictionary StringDelta {
324    DOMString? previous;
325    DOMString? current;
326  };
327
328  dictionary DoubleDelta {
329    double? previous;
330    double? current;
331  };
332
333  dictionary BooleanDelta {
334    boolean? previous;
335    boolean? current;
336  };
337
338  // Encapsulates a change in a DownloadItem.
339  [inline_doc] dictionary DownloadDelta {
340    // The <code>id</code> of the $(ref:DownloadItem)
341    // that changed.
342    long id;
343
344    // The change in <code>url</code>, if any.
345    StringDelta? url;
346
347    // The change in <code>filename</code>, if any.
348    StringDelta? filename;
349
350    // The change in <code>danger</code>, if any.
351    StringDelta? danger;
352
353    // The change in <code>mime</code>, if any.
354    StringDelta? mime;
355
356    // The change in <code>startTime</code>, if any.
357    StringDelta? startTime;
358
359    // The change in <code>endTime</code>, if any.
360    StringDelta? endTime;
361
362    // The change in <code>state</code>, if any.
363    StringDelta? state;
364
365    // The change in <code>canResume</code>, if any.
366    BooleanDelta? canResume;
367
368    // The change in <code>paused</code>, if any.
369    BooleanDelta? paused;
370
371    // The change in <code>error</code>, if any.
372    StringDelta? error;
373
374    // The change in <code>totalBytes</code>, if any.
375    DoubleDelta? totalBytes;
376
377    // The change in <code>fileSize</code>, if any.
378    DoubleDelta? fileSize;
379
380    // The change in <code>exists</code>, if any.
381    BooleanDelta? exists;
382  };
383
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
388    // any other size.
389    [legalValues=(16,32)] long? size;
390  };
391
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);
399
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);
416
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);
424
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);
431
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);
438
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);
444
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);
459
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);
467
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);
471
472    // Show the default Downloads folder in a file manager.
473    static void showDefaultFolder();
474
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);
480
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);
484
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);
495
496    // Initiate dragging the downloaded file to another application. Call in a
497    // javascript <code>ondragstart</code> handler.
498    static void drag(long downloadId);
499
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);
507  };
508
509  interface Events {
510    // This event fires with the $(ref:DownloadItem) object when a download
511    // begins.
512    static void onCreated(DownloadItem downloadItem);
513
514    // Fires with the <code>downloadId</code> when a download is erased from
515    // history.
516    // |downloadId|: The <code>id</code> of the $(ref:DownloadItem) that was
517    // erased.
518    static void onErased(long downloadId);
519
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);
525
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);
549  };
550};
551