 Issue 29433625:
  Issue 5220 - Update the IO API  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluschrome/
    
  
    Issue 29433625:
  Issue 5220 - Update the IO API  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluschrome/| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 1 /* | 1 /* | 
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 
| 3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH | 
| 4 * | 4 * | 
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify | 
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as | 
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. | 
| 8 * | 8 * | 
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, | 
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. | 
| 13 * | 13 * | 
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License | 
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| 16 */ | 16 */ | 
| 17 | 17 | 
| 18 /* global FakeFile */ | |
| 19 | |
| 20 "use strict"; | 18 "use strict"; | 
| 21 | 19 | 
| 22 const keyPrefix = "file:"; | 20 const keyPrefix = "file:"; | 
| 23 | 21 | 
| 24 function fileToKey(file) | 22 function fileToKey(fileName) | 
| 25 { | 23 { | 
| 26 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); | 24 return keyPrefix + fileName; | 
| 
Wladimir Palant
2017/05/09 09:12:27
Does this even work? The parameters passed to the
 
hub
2017/05/10 16:08:51
Looking at the code it seems that "file" is a file
 | |
| 27 } | 25 } | 
| 28 | 26 | 
| 29 function loadFile(file) | 27 function loadFile(fileName) | 
| 30 { | 28 { | 
| 31 return new Promise((resolve, reject) => | 29 return new Promise((resolve, reject) => | 
| 32 { | 30 { | 
| 33 let key = fileToKey(file); | 31 let key = fileToKey(fileName); | 
| 34 | 32 | 
| 35 ext.storage.get([key], items => | 33 ext.storage.get(key, items => | 
| 36 { | 34 { | 
| 
Wladimir Palant
2017/05/09 09:12:27
This should really be checking chrome.runtime.last
 | |
| 37 let entry = items[key]; | 35 let entry = items[key]; | 
| 38 | 36 | 
| 39 if (entry) | 37 if (entry) | 
| 40 resolve(entry); | 38 resolve(entry); | 
| 41 else | 39 else | 
| 42 reject(new Error("File doesn't exist")); | 40 reject({type: "NoSuchFile"}); | 
| 43 }); | 41 }); | 
| 44 }); | 42 }); | 
| 45 } | 43 } | 
| 46 | 44 | 
| 47 function saveFile(file, data) | 45 function saveFile(fileName, data) | 
| 48 { | 46 { | 
| 49 return new Promise((resolve) => | 47 return new Promise((resolve, reject) => | 
| 
Wladimir Palant
2017/05/09 09:12:27
I don't think that it is a good idea to omit the r
 
hub
2017/05/10 16:08:51
I'll put it back wherever. I it probably a better
 
Sebastian Noack
2017/05/22 13:36:30
Personally, I think I would have omitted it too, b
 | |
| 50 { | 48 { | 
| 51 ext.storage.set( | 49 ext.storage.set( | 
| 52 fileToKey(file), | 50 fileToKey(fileName), | 
| 53 { | 51 { | 
| 54 content: Array.from(data), | 52 content: Array.from(data), | 
| 55 lastModified: Date.now() | 53 lastModified: Date.now() | 
| 56 }, | 54 }, | 
| 57 resolve | 55 resolve | 
| 58 ); | 56 ); | 
| 59 }); | 57 }); | 
| 60 } | 58 } | 
| 61 | 59 | 
| 60 function removeFile(fileName) | |
| 61 { | |
| 62 return new Promise((resolve, reject) => | |
| 63 { | |
| 64 ext.storage.remove(fileToKey(fileName), () => | |
| 65 { | |
| 66 if (chrome.runtime.lastError) | |
| 67 reject(chrome.runtime.lastError.message); | |
| 68 else | |
| 69 resolve(); | |
| 70 }); | |
| 71 }); | |
| 72 } | |
| 73 | |
| 62 exports.IO = | 74 exports.IO = | 
| 63 { | 75 { | 
| 64 resolveFilePath(path) { return new FakeFile(path); }, | 76 /** | 
| 
Wladimir Palant
2017/05/09 09:12:27
This method should be removed, along with the Fake
 
hub
2017/05/10 16:08:51
Done.
 | |
| 65 | 77 * Reads text lines from a file. | 
| 66 readFromFile(file, parser) | 78 * @param {string} fileName | 
| 
Wladimir Palant
2017/05/09 09:12:27
I suggest that you copy JSDoc comments from https:
 
hub
2017/05/10 16:08:51
I'll change the parameters to match. And this reso
 | |
| 79 * Name of the file to be read | |
| 80 * @param {TextSink} listener | |
| 81 * Function that will be called for each line in the file | |
| 82 * @return {Promise} | |
| 83 * Promise to be resolved or rejected once the operation is completed | |
| 84 */ | |
| 85 readFromFile(fileName, listener) | |
| 67 { | 86 { | 
| 68 return loadFile(file).then(entry => | 87 return loadFile(fileName).then(entry => | 
| 69 { | 88 { | 
| 70 for (let line of entry.content) | 89 for (let line of entry.content) | 
| 71 parser(line); | 90 listener(line); | 
| 72 | |
| 73 parser(null); | |
| 
Wladimir Palant
2017/05/09 09:12:27
Please remove this call, it is no longer necessary
 
hub
2017/05/10 16:08:51
Done.
 | |
| 74 }); | 91 }); | 
| 75 }, | 92 }, | 
| 76 | 93 | 
| 77 writeToFile(file, data) | 94 /** | 
| 95 * Writes text lines to a file. | |
| 96 * @param {string} fileName | |
| 97 * Name of the file to be written | |
| 98 * @param {Iterable.<string>} data | |
| 99 * An array-like or iterable object containing the lines (without line | |
| 100 * endings) | |
| 101 * @return {Promise} | |
| 102 * Promise to be resolved or rejected once the operation is completed | |
| 103 */ | |
| 104 writeToFile(fileName, data) | |
| 78 { | 105 { | 
| 79 return saveFile(file, data); | 106 return saveFile(fileName, data); | 
| 80 }, | 107 }, | 
| 81 | 108 | 
| 109 /** | |
| 110 * Copies a file. | |
| 111 * @param {string} fromFile | |
| 112 * Name of the file to be copied | |
| 113 * @param {string} toFile | |
| 114 * Name of the file to be written, will be overwritten if exists | |
| 115 * @return {Promise} | |
| 116 * Promise to be resolved or rejected once the operation is completed | |
| 117 */ | |
| 82 copyFile(fromFile, toFile) | 118 copyFile(fromFile, toFile) | 
| 83 { | 119 { | 
| 84 return new Promise((resolve, reject) => | 120 return loadFile(fromFile).then(entry => saveFile(toFile, entry.content)); | 
| 85 { | |
| 86 loadFile(fromFile).then(entry => | |
| 87 { | |
| 88 saveFile(toFile, entry.content).then(resolve()); | |
| 89 }); | |
| 90 }); | |
| 
Wladimir Palant
2017/05/09 09:12:27
You don't need to create the promise explicitly. T
 
hub
2017/05/10 16:08:51
Acknowledged.
 | |
| 91 }, | 121 }, | 
| 92 | 122 | 
| 123 /** | |
| 124 * Renames a file. | |
| 125 * @param {string} fromFile | |
| 126 * Name of the file to be renamed | |
| 127 * @param {string} newName | |
| 128 * New file name, will be overwritten if exists | |
| 129 * @return {Promise} | |
| 130 * Promise to be resolved or rejected once the operation is completed | |
| 131 */ | |
| 93 renameFile(fromFile, newName) | 132 renameFile(fromFile, newName) | 
| 94 { | 133 { | 
| 95 return new Promise(resolve => | 134 return loadFile(fromFile).then(entry => | 
| 96 { | 135 { | 
| 97 loadFile(fromFile).then(entry => | 136 return new Promise((resolve, reject) => | 
| 98 { | 137 { | 
| 99 ext.storage.remove(fileToKey(fromFile), () => | 138 ext.storage.set(fileToKey(newName), entry, () => | 
| 100 { | 139 { | 
| 101 ext.storage.set(keyPrefix + newName, entry, resolve); | 140 if (chrome.runtime.lastError) | 
| 141 reject(chrome.runtime.lastError.message); | |
| 142 else | |
| 143 resolve(); | |
| 102 }); | 144 }); | 
| 103 }); | 145 }); | 
| 104 }); | 146 }).then(() => removeFile(fromFile)); | 
| 
Wladimir Palant
2017/05/09 09:12:27
It's better to create the promises where they are
 
hub
2017/05/10 16:08:51
Make sense.
 | |
| 105 }, | 147 }, | 
| 106 | 148 | 
| 107 removeFile(file) | 149 /** | 
| 150 * Removes a file. | |
| 151 * @param {string} fileName | |
| 152 * Name of the file to be removed | |
| 153 * @return {Promise} | |
| 154 * Promise to be resolved or rejected once the operation is completed | |
| 155 */ | |
| 156 removeFile(fileName) | |
| 108 { | 157 { | 
| 109 return new Promise(resolve => | 158 return removeFile(fileName); | 
| 110 { | |
| 111 ext.storage.remove(fileToKey(file), resolve); | |
| 112 }); | |
| 113 }, | 159 }, | 
| 114 | 160 | 
| 115 statFile(file) | 161 /** | 
| 162 * Retrieves file metadata. | |
| 163 * @param {string} fileName | |
| 164 * Name of the file to be looked up | |
| 165 * @return {Promise.<StatData>} | |
| 166 * Promise to be resolved with file metadata once the operation is | |
| 167 * completed | |
| 168 */ | |
| 169 statFile(fileName) | |
| 116 { | 170 { | 
| 117 return loadFile(file).then((entry) => | 171 return loadFile(fileName).then(entry => | 
| 
Wladimir Palant
2017/05/09 09:12:27
Nit: you are somewhat inconsistent, are you puttin
 
hub
2017/05/10 16:08:51
Acknowledged.
 | |
| 118 { | 172 { | 
| 119 return { | 173 return { | 
| 120 exists: true, | 174 exists: true, | 
| 121 lastModified: entry.lastModified | 175 lastModified: entry.lastModified | 
| 122 }; | 176 }; | 
| 177 }).catch(error => | |
| 178 { | |
| 179 if (error.type == "NoSuchFile") | |
| 180 return {exists: false}; | |
| 181 throw error; | |
| 123 }); | 182 }); | 
| 
Wladimir Palant
2017/05/09 09:12:27
This promise will currently be rejected if the ent
 
hub
2017/05/10 16:08:51
Done.
 | |
| 124 } | 183 } | 
| 125 }; | 184 }; | 
| LEFT | RIGHT |