Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/compat.js

Issue 9846017: Make JavaScript sources compile into the library; convert JavaScript files on the fly (Closed)
Patch Set: Review comments addressed Created March 15, 2013, 3:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/adblockplus.js ('k') | lib/elemHideHitRegistration.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of the Adblock Plus extension, 2 * This file is part of the Adblock Plus extension,
3 * Copyright (C) 2006-2012 Eyeo GmbH 3 * Copyright (C) 2006-2012 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 // TODO: These need to be defined properly
19 function XMLHttpRequest() {};
20 var window = {
21 setTimeout: function(){},
22 requestFileSystem: function(){}
23 };
24
18 // 25 //
19 // Module framework stuff 26 // Module framework stuff
20 // 27 //
21 28
22 function require(module) 29 function require(module)
23 { 30 {
24 return require.scopes[module]; 31 return require.scopes[module];
25 } 32 }
26 require.scopes = {__proto__: null}; 33 require.scopes = {__proto__: null};
27 34
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 const Ci = Components.interfaces; 95 const Ci = Components.interfaces;
89 const Cr = Components.results; 96 const Cr = Components.results;
90 const Cu = Components.utils; 97 const Cu = Components.utils;
91 98
92 var XPCOMUtils = 99 var XPCOMUtils =
93 { 100 {
94 generateQI: function() {} 101 generateQI: function() {}
95 }; 102 };
96 103
97 // 104 //
98 // Info pseudo-module
99 //
100
101 require.scopes.info =
102 {
103 get addonID()
104 {
105 return chrome.i18n.getMessage("@@extension_id");
106 },
107 addonVersion: "2.1", // Hardcoded for now
108 addonRoot: "",
109 get addonName()
110 {
111 return chrome.i18n.getMessage("name");
112 },
113 application: "chrome"
114 };
115
116 //
117 // IO module: no direct file system access, using FileSystem API
118 //
119
120 require.scopes.io =
121 {
122 IO: {
123 _getFileEntry: function(file, create, successCallback, errorCallback)
124 {
125 if (file instanceof FakeFile)
126 file = file.path;
127 else if ("spec" in file)
128 file = file.spec;
129
130 // Remove directory path - we operate on a single directory in Chrome
131 file = file.replace(/^.*[\/\\]/, "");
132
133 // We request a gigabyte of space, just in case
134 (window.requestFileSystem || window.webkitRequestFileSystem)(window.PERSIS TENT, 1024*1024*1024, function(fs)
135 {
136 fs.root.getFile(file, {create: create}, function(fileEntry)
137 {
138 successCallback(fs, fileEntry);
139 }, errorCallback);
140 }, errorCallback);
141 },
142
143 lineBreak: "\n",
144
145 resolveFilePath: function(path)
146 {
147 return new FakeFile(path);
148 },
149
150 readFromFile: function(file, decode, listener, callback, timeLineID)
151 {
152 if ("spec" in file && /^defaults\b/.test(file.spec))
153 {
154 // Code attempts to read the default patterns.ini, we don't have that.
155 // Make sure to execute first-run actions instead.
156 callback(null);
157 if (localStorage.currentVersion)
158 seenDataCorruption = true;
159 delete localStorage.currentVersion;
160 return;
161 }
162
163 this._getFileEntry(file, false, function(fs, fileEntry)
164 {
165 fileEntry.file(function(file)
166 {
167 var reader = new FileReader();
168 reader.onloadend = function()
169 {
170 if (reader.error)
171 callback(reader.error);
172 else
173 {
174 var lines = reader.result.split(/[\r\n]+/);
175 for (var i = 0; i < lines.length; i++)
176 listener.process(lines[i]);
177 listener.process(null);
178 callback(null);
179 }
180 };
181 reader.readAsText(file);
182 }, callback);
183 }, callback);
184 },
185
186 writeToFile: function(file, encode, data, callback, timeLineID)
187 {
188 this._getFileEntry(file, true, function(fs, fileEntry)
189 {
190 fileEntry.createWriter(function(writer)
191 {
192 var executeWriteOperation = function(op, nextOperation)
193 {
194 writer.onwriteend = function()
195 {
196 if (writer.error)
197 callback(writer.error);
198 else
199 nextOperation();
200 }.bind(this);
201
202 op();
203 }.bind(this);
204
205 executeWriteOperation(writer.truncate.bind(writer, 0), function()
206 {
207 var blob;
208 try
209 {
210 blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {typ e: "text/plain"});
211 }
212 catch (e)
213 {
214 if (!(e instanceof TypeError))
215 throw e;
216
217 // Blob wasn't a constructor before Chrome 20
218 var builder = new (window.BlobBuilder || window.WebKitBlobBuilder) ;
219 builder.append(data.join(this.lineBreak) + this.lineBreak);
220 blob = builder.getBlob("text/plain");
221 }
222 executeWriteOperation(writer.write.bind(writer, blob), callback.bind (null, null));
223 }.bind(this));
224 }.bind(this), callback);
225 }.bind(this), callback);
226 },
227
228 copyFile: function(fromFile, toFile, callback)
229 {
230 // Simply combine read and write operations
231 var data = [];
232 this.readFromFile(fromFile, false, {
233 process: function(line)
234 {
235 if (line !== null)
236 data.push(line);
237 }
238 }, function(e)
239 {
240 if (e)
241 callback(e);
242 else
243 this.writeToFile(toFile, false, data, callback);
244 }.bind(this));
245 },
246
247 renameFile: function(fromFile, newName, callback)
248 {
249 this._getFileEntry(fromFile, false, function(fs, fileEntry)
250 {
251 fileEntry.moveTo(fs.root, newName, function()
252 {
253 callback(null);
254 }, callback);
255 }, callback);
256 },
257
258 removeFile: function(file, callback)
259 {
260 this._getFileEntry(file, false, function(fs, fileEntry)
261 {
262 fileEntry.remove(function()
263 {
264 callback(null);
265 }, callback);
266 }, callback);
267 },
268
269 statFile: function(file, callback)
270 {
271 this._getFileEntry(file, false, function(fs, fileEntry)
272 {
273 fileEntry.getMetadata(function(metadata)
274 {
275 callback(null, {
276 exists: true,
277 isDirectory: fileEntry.isDirectory,
278 isFile: fileEntry.isFile,
279 lastModified: metadata.modificationTime.getTime()
280 });
281 }, callback);
282 }, callback);
283 }
284 }
285 };
286
287 //
288 // Fake nsIFile implementation for our I/O 105 // Fake nsIFile implementation for our I/O
289 // 106 //
290 107
291 function FakeFile(path) 108 function FakeFile(path)
292 { 109 {
293 this.path = path; 110 this.path = path;
294 } 111 }
295 FakeFile.prototype = 112 FakeFile.prototype =
296 { 113 {
297 get leafName() 114 get leafName()
(...skipping 13 matching lines...) Expand all
311 return new FakeFile(this.path); 128 return new FakeFile(this.path);
312 }, 129 },
313 get parent() 130 get parent()
314 { 131 {
315 return {create: function() {}}; 132 return {create: function() {}};
316 }, 133 },
317 normalize: function() {} 134 normalize: function() {}
318 }; 135 };
319 136
320 // 137 //
321 // Prefs module: the values are hardcoded for now.
322 //
323
324 require.scopes.prefs = {
325 Prefs: {
326 enabled: true,
327 patternsfile: "patterns.ini",
328 patternsbackups: 5,
329 patternsbackupinterval: 24,
330 data_directory: "",
331 savestats: false,
332 privateBrowsing: false,
333 subscriptions_fallbackerrors: 5,
334 subscriptions_fallbackurl: "https://adblockplus.org/getSubscription?version= %VERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHAN NELSTATUS%&responseStatus=%RESPONSESTATUS%",
335 subscriptions_autoupdate: true,
336 subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/exc eptionrules.txt",
337 documentation_link: "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG %",
338 addListener: function() {}
339 }
340 };
341
342 //
343 // Utils module
344 //
345
346 require.scopes.utils =
347 {
348 Utils: {
349 systemPrincipal: null,
350 getString: function(id)
351 {
352 return id;
353 },
354 runAsync: function(callback, thisPtr)
355 {
356 var params = Array.prototype.slice.call(arguments, 2);
357 window.setTimeout(function()
358 {
359 callback.apply(thisPtr, params);
360 }, 0);
361 },
362 get appLocale()
363 {
364 var locale = chrome.i18n.getMessage("@@ui_locale").replace(/_/g, "-");
365 this.__defineGetter__("appLocale", function() {return locale});
366 return this.appLocale;
367 },
368 generateChecksum: function(lines)
369 {
370 // We cannot calculate MD5 checksums yet :-(
371 return null;
372 },
373 makeURI: function(url)
374 {
375 return Services.io.newURI(url);
376 },
377
378 checkLocalePrefixMatch: function(prefixes)
379 {
380 if (!prefixes)
381 return null;
382
383 var list = prefixes.split(",");
384 for (var i = 0; i < list.length; i++)
385 if (new RegExp("^" + list[i] + "\\b").test(this.appLocale))
386 return list[i];
387
388 return null;
389 },
390
391 chooseFilterSubscription: function(subscriptions)
392 {
393 var selectedItem = null;
394 var selectedPrefix = null;
395 var matchCount = 0;
396 for (var i = 0; i < subscriptions.length; i++)
397 {
398 var subscription = subscriptions[i];
399 if (!selectedItem)
400 selectedItem = subscription;
401
402 var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription. getAttribute("prefixes"));
403 if (prefix)
404 {
405 if (!selectedPrefix || selectedPrefix.length < prefix.length)
406 {
407 selectedItem = subscription;
408 selectedPrefix = prefix;
409 matchCount = 1;
410 }
411 else if (selectedPrefix && selectedPrefix.length == prefix.length)
412 {
413 matchCount++;
414
415 // If multiple items have a matching prefix of the same length:
416 // Select one of the items randomly, probability should be the same
417 // for all items. So we replace the previous match here with
418 // probability 1/N (N being the number of matches).
419 if (Math.random() * matchCount < 1)
420 {
421 selectedItem = subscription;
422 selectedPrefix = prefix;
423 }
424 }
425 }
426 }
427 return selectedItem;
428 }
429 }
430 };
431
432 //
433 // ElemHideHitRegistration dummy implementation
434 //
435
436 require.scopes.elemHideHitRegistration =
437 {
438 AboutHandler: {}
439 };
440
441 //
442 // Services.jsm module emulation 138 // Services.jsm module emulation
443 // 139 //
444 140
445 var Services = 141 var Services =
446 { 142 {
447 io: { 143 io: {
448 newURI: function(uri) 144 newURI: function(uri)
449 { 145 {
450 if (!uri.length || uri[0] == "~") 146 if (!uri.length || uri[0] == "~")
451 throw new Error("Invalid URI"); 147 throw new Error("Invalid URI");
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 status: -1, 288 status: -1,
593 notificationCallbacks: {}, 289 notificationCallbacks: {},
594 loadFlags: 0, 290 loadFlags: 0,
595 INHIBIT_CACHING: 0, 291 INHIBIT_CACHING: 0,
596 VALIDATE_ALWAYS: 0, 292 VALIDATE_ALWAYS: 0,
597 QueryInterface: function() 293 QueryInterface: function()
598 { 294 {
599 return this; 295 return this;
600 } 296 }
601 }; 297 };
OLDNEW
« no previous file with comments | « lib/adblockplus.js ('k') | lib/elemHideHitRegistration.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld