OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | |
3 * Copyright (C) 2006-2013 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 // | |
19 // No direct file system access, using LocalStorage API | |
20 // | |
21 | |
22 var IO = exports.IO = | |
23 { | |
24 _getFilePath: function(file) | |
25 { | |
26 if (file instanceof FakeFile) | |
27 return file.path; | |
28 else if ("spec" in file) | |
29 return file.spec; | |
30 | |
31 throw new Error("Unexpected file type"); | |
32 }, | |
33 | |
34 _setFileContents: function(path, contents, lastModified) | |
35 { | |
36 window.localStorage[path] = contents; | |
37 window.localStorage[path + "/lastModified"] = lastModified || 0; | |
38 }, | |
39 | |
40 lineBreak: "\n", | |
41 | |
42 resolveFilePath: function(path) | |
43 { | |
44 return new FakeFile(path); | |
45 }, | |
46 | |
47 readFromFile: function(file, decode, listener, callback, timeLineID) | |
48 { | |
49 var Utils = require("utils").Utils; | |
50 Utils.runAsync(function() | |
51 { | |
52 if ("spec" in file && /^defaults\b/.test(file.spec)) | |
53 { | |
54 // Code attempts to read the default patterns.ini, we don't have that. | |
55 // Make sure to execute first-run actions instead. | |
56 if (localStorage.currentVersion) | |
57 seenDataCorruption = true; | |
58 delete localStorage.currentVersion; | |
59 callback(null); | |
60 return; | |
61 } | |
62 | |
63 var path = this._getFilePath(file); | |
64 if (!(path in window.localStorage)) | |
65 { | |
66 callback(new Error("File doesn't exist")) | |
67 return; | |
68 } | |
69 | |
70 var lines = window.localStorage[path].split(/[\r\n]+/); | |
71 for (var i = 0; i < lines.length; i++) | |
72 listener.process(lines[i]); | |
73 listener.process(null); | |
74 callback(null); | |
75 }.bind(this)); | |
76 }, | |
77 | |
78 writeToFile: function(file, encode, data, callback, timeLineID) | |
79 { | |
80 var path = this._getFilePath(file); | |
81 this._setFileContents(path, data.join(this.lineBreak) + this.lineBreak, Date
.now()); | |
82 | |
83 var Utils = require("utils").Utils; | |
84 Utils.runAsync(callback, null, null); | |
85 }, | |
86 | |
87 copyFile: function(fromFile, toFile, callback) | |
88 { | |
89 // Simply combine read and write operations | |
90 var data = []; | |
91 this.readFromFile(fromFile, false, { | |
92 process: function(line) | |
93 { | |
94 if (line !== null) | |
95 data.push(line); | |
96 } | |
97 }, function(e) | |
98 { | |
99 if (e) | |
100 callback(e); | |
101 else | |
102 this.writeToFile(toFile, false, data, callback); | |
103 }.bind(this)); | |
104 }, | |
105 | |
106 renameFile: function(fromFile, newName, callback) | |
107 { | |
108 var path = this._getFilePath(fromFile); | |
109 if (!(path in window.localStorage)) | |
110 { | |
111 callback(new Error("File doesn't exist")) | |
112 return; | |
113 } | |
114 | |
115 this._setFileContents(newName, window.localStorage[path], window.localStorag
e[path + "/lastModified"]); | |
116 this.removeFile(fromFile, callback); | |
117 }, | |
118 | |
119 removeFile: function(file, callback) | |
120 { | |
121 var path = this._getFilePath(file); | |
122 delete window.localStorage[path]; | |
123 delete window.localStorage[path + "/lastModified"]; | |
124 callback(null); | |
125 }, | |
126 | |
127 statFile: function(file, callback) | |
128 { | |
129 var path = this._getFilePath(file); | |
130 callback(null, { | |
131 exists: path in window.localStorage, | |
132 isDirectory: false, | |
133 isFile: true, | |
134 lastModified: parseInt(window.localStorage[path + "/lastModified"], 10) ||
0 | |
135 }); | |
136 } | |
137 }; | |
OLD | NEW |