| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 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 // | 18 // |
| 19 // No direct file system access, using WebSQL API | 19 // No direct file system access, using WebSQL API |
| 20 // | 20 // |
| 21 | 21 |
| 22 var IO = exports.IO = | 22 var IO = exports.IO = |
| 23 { | 23 { |
| 24 _db: null, | 24 _db: null, |
| 25 lineBreak: "\n", | 25 lineBreak: "\n", |
| 26 | 26 |
| 27 _transaction: function(callback) { | 27 _transaction: function(callback) |
| 28 { |
| 28 var dbCreated = false; | 29 var dbCreated = false; |
| 29 if (!this._db) | 30 if (!this._db) |
| 30 this._db = openDatabase("adblockplus", "1.0", "", 102400, function() { dbC
reated = true; }); | 31 this._db = openDatabase("adblockplus", "1.0", "", 102400, function() { dbC
reated = true; }); |
| 31 | 32 |
| 32 this._db.transaction(function(tx) { | 33 this._db.transaction(function(tx) |
| 34 { |
| 33 if (dbCreated) | 35 if (dbCreated) |
| 34 tx.executeSql("CREATE TABLE files (path unique, last_modified, content)"
); | 36 tx.executeSql("CREATE TABLE files (path unique, last_modified, content)"
); |
| 35 | 37 |
| 36 callback(tx); | 38 callback(tx); |
| 37 }); | 39 }); |
| 38 }, | 40 }, |
| 39 _getFilePath: function(file) { | 41 _getFilePath: function(file) |
| 42 { |
| 40 if (file instanceof FakeFile) | 43 if (file instanceof FakeFile) |
| 41 return file.path; | 44 return file.path; |
| 42 if ("spec" in file) | 45 if ("spec" in file) |
| 43 return file.spec; | 46 return file.spec; |
| 44 | 47 |
| 45 throw new Error("Unexpected file type"); | 48 throw new Error("Unexpected file type"); |
| 46 }, | 49 }, |
| 47 resolveFilePath: function(path) { | 50 resolveFilePath: function(path) |
| 51 { |
| 48 return new FakeFile(path); | 52 return new FakeFile(path); |
| 49 }, | 53 }, |
| 50 readFromFile: function(file, decode, listener, callback, timeLineID) { | 54 readFromFile: function(file, decode, listener, callback, timeLineID) |
| 51 if ("spec" in file && /^defaults\b/.test(file.spec)) { | 55 { |
| 56 if ("spec" in file && /^defaults\b/.test(file.spec)) |
| 57 { |
| 52 // Code attempts to read the default patterns.ini, we don't have that. | 58 // Code attempts to read the default patterns.ini, we don't have that. |
| 53 // Make sure to execute first-run actions instead. | 59 // Make sure to execute first-run actions instead. |
| 54 callback(null); | 60 callback(null); |
| 55 if (localStorage.currentVersion) | 61 if (localStorage.currentVersion) |
| 56 seenDataCorruption = true; | 62 seenDataCorruption = true; |
| 57 delete localStorage.currentVersion; | 63 delete localStorage.currentVersion; |
| 58 return; | 64 return; |
| 59 } | 65 } |
| 60 | 66 |
| 61 var path = this._getFilePath(file); | 67 var path = this._getFilePath(file); |
| 62 var runAsync = require("utils").Utils.runAsync; | 68 var runAsync = require("utils").Utils.runAsync; |
| 63 | 69 |
| 64 this._transaction(function(tx) { | 70 this._transaction(function(tx) |
| 71 { |
| 65 tx.executeSql( | 72 tx.executeSql( |
| 66 "SELECT content FROM files WHERE path = ?", | 73 "SELECT content FROM files WHERE path = ?", |
| 67 [path], | 74 [path], |
| 68 function(tx, results) { | 75 function(tx, results) |
| 69 if (results.rows.length == 0) { | 76 { |
| 77 if (results.rows.length == 0) |
| 78 { |
| 70 runAsync(callback, null, new Error("File doesn't exist")); | 79 runAsync(callback, null, new Error("File doesn't exist")); |
| 71 return; | 80 return; |
| 72 } | 81 } |
| 73 | 82 |
| 74 var lines = results.rows.item(0).content.split(/[\r\n]+/); | 83 var lines = results.rows.item(0).content.split(/[\r\n]+/); |
| 75 runAsync(function() { | 84 runAsync(function() |
| 85 { |
| 76 for (var i = 0; i < lines.length; i++) | 86 for (var i = 0; i < lines.length; i++) |
| 77 listener.process(lines[i]); | 87 listener.process(lines[i]); |
| 78 listener.process(null); | 88 listener.process(null); |
| 79 callback(null); | 89 callback(null); |
| 80 }); | 90 }); |
| 81 } | 91 } |
| 82 ); | 92 ); |
| 83 }); | 93 }); |
| 84 }, | 94 }, |
| 85 writeToFile: function(file, encode, data, callback, timeLineID) { | 95 writeToFile: function(file, encode, data, callback, timeLineID) |
| 96 { |
| 86 var path = this._getFilePath(file); | 97 var path = this._getFilePath(file); |
| 87 var lnbr = this.lineBreak; | 98 var lnbr = this.lineBreak; |
| 88 var runAsync = require("utils").Utils.runAsync; | 99 var runAsync = require("utils").Utils.runAsync; |
| 89 | 100 |
| 90 this._transaction(function(tx) { | 101 this._transaction(function(tx) |
| 102 { |
| 91 tx.executeSql( | 103 tx.executeSql( |
| 92 "INSERT OR REPLACE INTO files VALUES (?, ?, ?)", | 104 "INSERT OR REPLACE INTO files VALUES (?, ?, ?)", |
| 93 [path, Date.now(), data.join(lnbr) + lnbr], | 105 [path, Date.now(), data.join(lnbr) + lnbr], |
| 94 function() { runAsync(callback, null, null); } | 106 function() { runAsync(callback, null, null); } |
| 95 ); | 107 ); |
| 96 }); | 108 }); |
| 97 }, | 109 }, |
| 98 copyFile: function(fromFile, toFile, callback) { | 110 copyFile: function(fromFile, toFile, callback) |
| 111 { |
| 99 var fromPath = this._getFilePath(fromFile); | 112 var fromPath = this._getFilePath(fromFile); |
| 100 var toPath = this._getFilePath(toFile); | 113 var toPath = this._getFilePath(toFile); |
| 101 var runAsync = require("utils").Utils.runAsync; | 114 var runAsync = require("utils").Utils.runAsync; |
| 102 | 115 |
| 103 this._transaction(function(tx) { | 116 this._transaction(function(tx) |
| 117 { |
| 104 tx.executeSql( | 118 tx.executeSql( |
| 105 "INSERT OR REPLACE INTO files SELECT ?, ?, content FROM files WHERE path
= ?", | 119 "INSERT OR REPLACE INTO files SELECT ?, ?, content FROM files WHERE path
= ?", |
| 106 [toPath, Date.now(), fromPath], | 120 [toPath, Date.now(), fromPath], |
| 107 function(tx, results) { | 121 function(tx, results) |
| 122 { |
| 108 if (results.rowsAffected == 0) | 123 if (results.rowsAffected == 0) |
| 109 runAsync(callback, null, new Error("File doesn't exist")); | 124 runAsync(callback, null, new Error("File doesn't exist")); |
| 110 else | 125 else |
| 111 runAsync(callback, null, null); | 126 runAsync(callback, null, null); |
| 112 } | 127 } |
| 113 ); | 128 ); |
| 114 }); | 129 }); |
| 115 }, | 130 }, |
| 116 renameFile: function(fromFile, newName, callback) { | 131 renameFile: function(fromFile, newName, callback) |
| 132 { |
| 117 var path = this._getFilePath(fromFile); | 133 var path = this._getFilePath(fromFile); |
| 118 var runAsync = require("utils").Utils.runAsync; | 134 var runAsync = require("utils").Utils.runAsync; |
| 119 | 135 |
| 120 this._transaction(function(tx) { | 136 this._transaction(function(tx) |
| 137 { |
| 121 tx.executeSql( | 138 tx.executeSql( |
| 122 "UPDATE files SET path = ? WHERE path = ?", | 139 "UPDATE files SET path = ? WHERE path = ?", |
| 123 [newName, path], | 140 [newName, path], |
| 124 function(tx, results) { | 141 function(tx, results) |
| 142 { |
| 125 if (results.rowsAffected == 0) | 143 if (results.rowsAffected == 0) |
| 126 runAsync(callback, null, new Error("File doesn't exist")); | 144 runAsync(callback, null, new Error("File doesn't exist")); |
| 127 else | 145 else |
| 128 runAsync(callback, null, null); | 146 runAsync(callback, null, null); |
| 129 } | 147 } |
| 130 ); | 148 ); |
| 131 }); | 149 }); |
| 132 }, | 150 }, |
| 133 removeFile: function(file, callback) { | 151 removeFile: function(file, callback) |
| 152 { |
| 134 var path = this._getFilePath(file); | 153 var path = this._getFilePath(file); |
| 135 var runAsync = require("utils").Utils.runAsync; | 154 var runAsync = require("utils").Utils.runAsync; |
| 136 | 155 |
| 137 this._transaction(function(tx) { | 156 this._transaction(function(tx) |
| 157 { |
| 138 tx.executeSql( | 158 tx.executeSql( |
| 139 "DELETE FROM files WHERE path = ?", | 159 "DELETE FROM files WHERE path = ?", |
| 140 [path], | 160 [path], |
| 141 function() { runAsync(callback, null, null); } | 161 function() { runAsync(callback, null, null); } |
| 142 ); | 162 ); |
| 143 }); | 163 }); |
| 144 }, | 164 }, |
| 145 statFile: function(file, callback) { | 165 statFile: function(file, callback) |
| 166 { |
| 146 var path = this._getFilePath(file); | 167 var path = this._getFilePath(file); |
| 147 var runAsync = require("utils").Utils.runAsync; | 168 var runAsync = require("utils").Utils.runAsync; |
| 148 | 169 |
| 149 this._transaction(function(tx) { | 170 this._transaction(function(tx) |
| 171 { |
| 150 tx.executeSql( | 172 tx.executeSql( |
| 151 "SELECT last_modified FROM files WHERE path = ?", | 173 "SELECT last_modified FROM files WHERE path = ?", |
| 152 [path], | 174 [path], |
| 153 function(tx, results) { | 175 function(tx, results) |
| 176 { |
| 154 if (results.rows.length == 0) | 177 if (results.rows.length == 0) |
| 155 runAsync(callback, null, null, { | 178 runAsync(callback, null, null, { |
| 156 exists: false, | 179 exists: false, |
| 157 isDirectory: false, | 180 isDirectory: false, |
| 158 isFile: false, | 181 isFile: false, |
| 159 lastModified: 0 | 182 lastModified: 0 |
| 160 }); | 183 }); |
| 161 else | 184 else |
| 162 runAsync(callback, null, null, { | 185 runAsync(callback, null, null, { |
| 163 exists: true, | 186 exists: true, |
| 164 isDirectory: false, | 187 isDirectory: false, |
| 165 isFile: true, | 188 isFile: true, |
| 166 lastModified: results.rows.item(0).last_modified | 189 lastModified: results.rows.item(0).last_modified |
| 167 }); | 190 }); |
| 168 } | 191 } |
| 169 ); | 192 ); |
| 170 }); | 193 }); |
| 171 } | 194 } |
| 172 }; | 195 }; |
| LEFT | RIGHT |