OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 const keyPrefix = "file:"; |
| 19 |
| 20 function fileToKey(file) |
| 21 { |
| 22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); |
| 23 } |
| 24 |
| 25 function loadFile(file, successCallback, errorCallback) |
| 26 { |
| 27 let key = fileToKey(file); |
| 28 |
| 29 ext.storage.get([key], function(items) |
| 30 { |
| 31 let entry = items[key]; |
| 32 |
| 33 if (entry) |
| 34 successCallback(entry); |
| 35 else |
| 36 errorCallback(new Error("File doesn't exist")); |
| 37 }); |
| 38 } |
| 39 |
| 40 function saveFile(file, data, callback) |
| 41 { |
| 42 ext.storage.set( |
| 43 fileToKey(file), |
| 44 { |
| 45 content: data, |
| 46 lastModified: Date.now() |
| 47 }, |
| 48 callback |
| 49 ); |
| 50 } |
| 51 |
| 52 exports.IO = |
| 53 { |
| 54 resolveFilePath: function(path) |
| 55 { |
| 56 return new FakeFile(path); |
| 57 }, |
| 58 |
| 59 readFromFile: function(file, listener, callback) |
| 60 { |
| 61 runWhenMigrated(function() |
| 62 { |
| 63 function onLoaded(entry) |
| 64 { |
| 65 for (let line of entry.content) |
| 66 listener.process(line); |
| 67 |
| 68 listener.process(null); |
| 69 callback(null); |
| 70 } |
| 71 |
| 72 loadFile(file, onLoaded, callback); |
| 73 }); |
| 74 }, |
| 75 |
| 76 writeToFile: function(file, data, callback) |
| 77 { |
| 78 runWhenMigrated(function() |
| 79 { |
| 80 saveFile(file, data, callback); |
| 81 }); |
| 82 }, |
| 83 |
| 84 copyFile: function(fromFile, toFile, callback) |
| 85 { |
| 86 runWhenMigrated(function() |
| 87 { |
| 88 function onLoaded(entry) |
| 89 { |
| 90 saveFile(toFile, entry.content, callback); |
| 91 } |
| 92 |
| 93 loadFile(fromFile, onLoaded, callback); |
| 94 }); |
| 95 }, |
| 96 |
| 97 renameFile: function(fromFile, newName, callback) |
| 98 { |
| 99 runWhenMigrated(function() |
| 100 { |
| 101 function onLoaded() |
| 102 { |
| 103 ext.storage.remove(fileToKey(fromFile), function() |
| 104 { |
| 105 ext.storage.set(keyPrefix + newName, entry, callback); |
| 106 }); |
| 107 } |
| 108 |
| 109 loadFile(fromFile, onLoaded, callback); |
| 110 }); |
| 111 }, |
| 112 |
| 113 removeFile: function(file, callback) |
| 114 { |
| 115 runWhenMigrated(function() |
| 116 { |
| 117 ext.storage.remove(fileToKey(file), callback); |
| 118 }); |
| 119 }, |
| 120 |
| 121 statFile: function(file, callback) |
| 122 { |
| 123 runWhenMigrated(function() |
| 124 { |
| 125 function onLoaded(entry) |
| 126 { |
| 127 callback(null, { |
| 128 exists: true, |
| 129 lastModified: entry.lastModified |
| 130 }); |
| 131 } |
| 132 |
| 133 loadFile(file, onLoaded, callback); |
| 134 }); |
| 135 } |
| 136 }; |
| 137 |
| 138 // Migrate files for users updating from old versions. |
| 139 // Defer IO operations until migration is complete. |
| 140 // TODO: Remove the migration code after a few releases. |
| 141 let migrated = false; |
| 142 let deferred = []; |
| 143 |
| 144 function runWhenMigrated(callback) |
| 145 { |
| 146 if (migrated) |
| 147 callback(); |
| 148 else |
| 149 deferred.push(callback); |
| 150 } |
| 151 |
| 152 ext.storage.migrateFiles(function() |
| 153 { |
| 154 migrated = true; |
| 155 |
| 156 while (deferred.length > 0) |
| 157 deferred.shift()(); |
| 158 }); |
OLD | NEW |