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