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