1 // Copyright 2015 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 cr.define('downloads', function() {
7 * @param {string} chromeSendName
8 * @return {function(string):void} A chrome.send() callback with curried name.
10 function chromeSendWithId(chromeSendName) {
11 return function(id) { chrome.send(chromeSendName, [id]); };
15 function ActionService() {}
17 ActionService.prototype = {
18 /** @param {string} id ID of the download to cancel. */
19 cancel: chromeSendWithId('cancel'),
21 /** Instructs the browser to clear all finished downloads. */
22 clearAll: function() {
23 if (loadTimeData.getBoolean('allowDeletingHistory')) {
24 chrome.send('clearAll');
29 /** @param {string} id ID of the dangerous download to discard. */
30 discardDangerous: chromeSendWithId('discardDangerous'),
32 /** @param {string} id ID of the download that the user started dragging. */
33 drag: chromeSendWithId('drag'),
35 /** @param {string} url URL of a file to download. */
36 download: function(url) {
37 var a = document.createElement('a');
39 a.setAttribute('download', '');
43 /** Opens the current local destination for downloads. */
44 openDownloadsFolder: chrome.send.bind(chrome, 'openDownloadsFolder'),
47 * @param {string} id ID of the download to run locally on the user's box.
49 openFile: chromeSendWithId('openFile'),
51 /** @param {string} id ID the of the progressing download to pause. */
52 pause: chromeSendWithId('pause'),
54 /** @param {string} id ID of the finished download to remove. */
55 remove: chromeSendWithId('remove'),
57 /** @param {string} id ID of the paused download to resume. */
58 resume: chromeSendWithId('resume'),
61 * @param {string} id ID of the dangerous download to save despite
64 saveDangerous: chromeSendWithId('saveDangerous'),
66 /** @param {string} searchText What to search for. */
67 search: function(searchText) {
68 if (this.searchText_ == searchText)
71 this.searchText_ = searchText;
73 // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']).
74 function trim(s) { return s.trim(); }
75 chrome.send('getDownloads', searchText.split(/"([^"]*)"/).map(trim));
79 * Shows the local folder a finished download resides in.
80 * @param {string} id ID of the download to show.
82 show: chromeSendWithId('show'),
84 /** Undo download removal. */
85 undo: chrome.send.bind(chrome, 'undo'),
88 return {ActionService: ActionService};