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} url URL of a file to download. */
33 download: function(url) {
34 var a = document.createElement('a');
36 a.setAttribute('download', '');
40 /** @param {string} id ID of the download that the user started dragging. */
41 drag: chromeSendWithId('drag'),
44 * @return {boolean} Whether the user is currently searching for downloads
45 * (i.e. has a non-empty search term).
47 isSearching: function() {
48 return this.searchText_.length > 0;
51 /** Opens the current local destination for downloads. */
52 openDownloadsFolder: chrome.send.bind(chrome, 'openDownloadsFolder'),
55 * @param {string} id ID of the download to run locally on the user's box.
57 openFile: chromeSendWithId('openFile'),
59 /** @param {string} id ID the of the progressing download to pause. */
60 pause: chromeSendWithId('pause'),
62 /** @param {string} id ID of the finished download to remove. */
63 remove: chromeSendWithId('remove'),
65 /** @param {string} id ID of the paused download to resume. */
66 resume: chromeSendWithId('resume'),
69 * @param {string} id ID of the dangerous download to save despite
72 saveDangerous: chromeSendWithId('saveDangerous'),
74 /** @param {string} searchText What to search for. */
75 search: function(searchText) {
76 if (this.searchText_ == searchText)
79 this.searchText_ = searchText;
81 // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']).
82 function trim(s) { return s.trim(); }
83 chrome.send('getDownloads', searchText.split(/"([^"]*)"/).map(trim));
87 * Shows the local folder a finished download resides in.
88 * @param {string} id ID of the download to show.
90 show: chromeSendWithId('show'),
92 /** Undo download removal. */
93 undo: chrome.send.bind(chrome, 'undo'),
96 return {ActionService: ActionService};