| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('downloads', function() { | 5 cr.define('downloads', function() { |
| 6 /** | 6 /** |
| 7 * @param {string} chromeSendName | 7 * @param {string} chromeSendName |
| 8 * @return {function(string):void} A chrome.send() callback with curried name. | 8 * @return {function(string):void} A chrome.send() callback with curried name. |
| 9 */ | 9 */ |
| 10 function chromeSendWithId(chromeSendName) { | 10 function chromeSendWithId(chromeSendName) { |
| 11 return function(id) { chrome.send(chromeSendName, [id]); }; | 11 return function(id) { chrome.send(chromeSendName, [id]); }; |
| 12 } | 12 } |
| 13 | 13 |
| 14 /** @constructor */ | 14 /** @constructor */ |
| 15 function ActionService() {} | 15 function ActionService() {} |
| 16 | 16 |
| 17 /** |
| 18 * @param {string} s |
| 19 * @return {string} |s| without whitespace at the beginning or end. |
| 20 */ |
| 21 function trim(s) { return s.trim(); } |
| 22 |
| 23 /** |
| 24 * @param {string|undefined} value |
| 25 * @return {boolean} Whether |value| is truthy. |
| 26 */ |
| 27 function truthy(value) { return !!value; } |
| 28 |
| 29 /** |
| 30 * @param {string} searchText Input typed by the user into a search box. |
| 31 * @return {Array<string>} A list of terms extracted from |searchText|. |
| 32 */ |
| 33 ActionService.splitTerms = function(searchText) { |
| 34 // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']). |
| 35 return searchText.split(/"([^"]*)"/).map(trim).filter(truthy); |
| 36 }; |
| 37 |
| 17 ActionService.prototype = { | 38 ActionService.prototype = { |
| 18 /** @param {string} id ID of the download to cancel. */ | 39 /** @param {string} id ID of the download to cancel. */ |
| 19 cancel: chromeSendWithId('cancel'), | 40 cancel: chromeSendWithId('cancel'), |
| 20 | 41 |
| 21 /** Instructs the browser to clear all finished downloads. */ | 42 /** Instructs the browser to clear all finished downloads. */ |
| 22 clearAll: function() { | 43 clearAll: function() { |
| 23 if (loadTimeData.getBoolean('allowDeletingHistory')) { | 44 if (loadTimeData.getBoolean('allowDeletingHistory')) { |
| 24 chrome.send('clearAll'); | 45 chrome.send('clearAll'); |
| 25 this.search(''); | 46 this.search(''); |
| 26 } | 47 } |
| 27 }, | 48 }, |
| 28 | 49 |
| 29 /** @param {string} id ID of the dangerous download to discard. */ | 50 /** @param {string} id ID of the dangerous download to discard. */ |
| 30 discardDangerous: chromeSendWithId('discardDangerous'), | 51 discardDangerous: chromeSendWithId('discardDangerous'), |
| 31 | 52 |
| 32 /** @param {string} url URL of a file to download. */ | 53 /** @param {string} url URL of a file to download. */ |
| 33 download: function(url) { | 54 download: function(url) { |
| 34 var a = document.createElement('a'); | 55 var a = document.createElement('a'); |
| 35 a.href = url; | 56 a.href = url; |
| 36 a.setAttribute('download', ''); | 57 a.setAttribute('download', ''); |
| 37 a.click(); | 58 a.click(); |
| 38 }, | 59 }, |
| 39 | 60 |
| 40 /** @param {string} id ID of the download that the user started dragging. */ | 61 /** @param {string} id ID of the download that the user started dragging. */ |
| 41 drag: chromeSendWithId('drag'), | 62 drag: chromeSendWithId('drag'), |
| 42 | 63 |
| 64 /** @private {boolean} */ |
| 65 isSearching_: false, |
| 66 |
| 43 /** | 67 /** |
| 44 * @return {boolean} Whether the user is currently searching for downloads | 68 * @return {boolean} Whether the user is currently searching for downloads |
| 45 * (i.e. has a non-empty search term). | 69 * (i.e. has a non-empty search term). |
| 46 */ | 70 */ |
| 47 isSearching: function() { | 71 isSearching: function() { |
| 48 return this.searchText_.length > 0; | 72 return this.isSearching_; |
| 49 }, | 73 }, |
| 50 | 74 |
| 51 /** Opens the current local destination for downloads. */ | 75 /** Opens the current local destination for downloads. */ |
| 52 openDownloadsFolder: chrome.send.bind(chrome, 'openDownloadsFolder'), | 76 openDownloadsFolder: chrome.send.bind(chrome, 'openDownloadsFolder'), |
| 53 | 77 |
| 54 /** | 78 /** |
| 55 * @param {string} id ID of the download to run locally on the user's box. | 79 * @param {string} id ID of the download to run locally on the user's box. |
| 56 */ | 80 */ |
| 57 openFile: chromeSendWithId('openFile'), | 81 openFile: chromeSendWithId('openFile'), |
| 58 | 82 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 71 */ | 95 */ |
| 72 saveDangerous: chromeSendWithId('saveDangerous'), | 96 saveDangerous: chromeSendWithId('saveDangerous'), |
| 73 | 97 |
| 74 /** @param {string} searchText What to search for. */ | 98 /** @param {string} searchText What to search for. */ |
| 75 search: function(searchText) { | 99 search: function(searchText) { |
| 76 if (this.searchText_ == searchText) | 100 if (this.searchText_ == searchText) |
| 77 return; | 101 return; |
| 78 | 102 |
| 79 this.searchText_ = searchText; | 103 this.searchText_ = searchText; |
| 80 | 104 |
| 81 // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']). | 105 var terms = ActionService.splitTerms(searchText); |
| 82 function trim(s) { return s.trim(); } | 106 this.isSearching_ = terms.length > 0; |
| 83 chrome.send('getDownloads', searchText.split(/"([^"]*)"/).map(trim)); | 107 |
| 108 chrome.send('getDownloads', terms); |
| 84 }, | 109 }, |
| 85 | 110 |
| 86 /** | 111 /** |
| 87 * Shows the local folder a finished download resides in. | 112 * Shows the local folder a finished download resides in. |
| 88 * @param {string} id ID of the download to show. | 113 * @param {string} id ID of the download to show. |
| 89 */ | 114 */ |
| 90 show: chromeSendWithId('show'), | 115 show: chromeSendWithId('show'), |
| 91 | 116 |
| 92 /** Undo download removal. */ | 117 /** Undo download removal. */ |
| 93 undo: chrome.send.bind(chrome, 'undo'), | 118 undo: chrome.send.bind(chrome, 'undo'), |
| 94 }; | 119 }; |
| 95 | 120 |
| 96 cr.addSingletonGetter(ActionService); | 121 cr.addSingletonGetter(ActionService); |
| 97 | 122 |
| 98 return {ActionService: ActionService}; | 123 return {ActionService: ActionService}; |
| 99 }); | 124 }); |
| OLD | NEW |