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

Powered by Google App Engine
This is Rietveld