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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/adblockplus.js ('k') | lib/elemHideHitRegistration.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compat.js
===================================================================
rename from lib/adblockplus_compat.js
rename to lib/compat.js
--- a/lib/adblockplus_compat.js
+++ b/lib/compat.js
@@ -10,16 +10,23 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
+// TODO: These need to be defined properly
+function XMLHttpRequest() {};
+var window = {
+ setTimeout: function(){},
+ requestFileSystem: function(){}
+};
+
//
// Module framework stuff
//
function require(module)
{
return require.scopes[module];
}
@@ -90,206 +97,16 @@ const Cr = Components.results;
const Cu = Components.utils;
var XPCOMUtils =
{
generateQI: function() {}
};
//
-// Info pseudo-module
-//
-
-require.scopes.info =
-{
- get addonID()
- {
- return chrome.i18n.getMessage("@@extension_id");
- },
- addonVersion: "2.1", // Hardcoded for now
- addonRoot: "",
- get addonName()
- {
- return chrome.i18n.getMessage("name");
- },
- application: "chrome"
-};
-
-//
-// IO module: no direct file system access, using FileSystem API
-//
-
-require.scopes.io =
-{
- IO: {
- _getFileEntry: function(file, create, successCallback, errorCallback)
- {
- if (file instanceof FakeFile)
- file = file.path;
- else if ("spec" in file)
- file = file.spec;
-
- // Remove directory path - we operate on a single directory in Chrome
- file = file.replace(/^.*[\/\\]/, "");
-
- // We request a gigabyte of space, just in case
- (window.requestFileSystem || window.webkitRequestFileSystem)(window.PERSISTENT, 1024*1024*1024, function(fs)
- {
- fs.root.getFile(file, {create: create}, function(fileEntry)
- {
- successCallback(fs, fileEntry);
- }, errorCallback);
- }, errorCallback);
- },
-
- lineBreak: "\n",
-
- resolveFilePath: function(path)
- {
- return new FakeFile(path);
- },
-
- readFromFile: function(file, decode, listener, callback, timeLineID)
- {
- if ("spec" in file && /^defaults\b/.test(file.spec))
- {
- // Code attempts to read the default patterns.ini, we don't have that.
- // Make sure to execute first-run actions instead.
- callback(null);
- if (localStorage.currentVersion)
- seenDataCorruption = true;
- delete localStorage.currentVersion;
- return;
- }
-
- this._getFileEntry(file, false, function(fs, fileEntry)
- {
- fileEntry.file(function(file)
- {
- var reader = new FileReader();
- reader.onloadend = function()
- {
- if (reader.error)
- callback(reader.error);
- else
- {
- var lines = reader.result.split(/[\r\n]+/);
- for (var i = 0; i < lines.length; i++)
- listener.process(lines[i]);
- listener.process(null);
- callback(null);
- }
- };
- reader.readAsText(file);
- }, callback);
- }, callback);
- },
-
- writeToFile: function(file, encode, data, callback, timeLineID)
- {
- this._getFileEntry(file, true, function(fs, fileEntry)
- {
- fileEntry.createWriter(function(writer)
- {
- var executeWriteOperation = function(op, nextOperation)
- {
- writer.onwriteend = function()
- {
- if (writer.error)
- callback(writer.error);
- else
- nextOperation();
- }.bind(this);
-
- op();
- }.bind(this);
-
- executeWriteOperation(writer.truncate.bind(writer, 0), function()
- {
- var blob;
- try
- {
- blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type: "text/plain"});
- }
- catch (e)
- {
- if (!(e instanceof TypeError))
- throw e;
-
- // Blob wasn't a constructor before Chrome 20
- var builder = new (window.BlobBuilder || window.WebKitBlobBuilder);
- builder.append(data.join(this.lineBreak) + this.lineBreak);
- blob = builder.getBlob("text/plain");
- }
- executeWriteOperation(writer.write.bind(writer, blob), callback.bind(null, null));
- }.bind(this));
- }.bind(this), callback);
- }.bind(this), callback);
- },
-
- copyFile: function(fromFile, toFile, callback)
- {
- // Simply combine read and write operations
- var data = [];
- this.readFromFile(fromFile, false, {
- process: function(line)
- {
- if (line !== null)
- data.push(line);
- }
- }, function(e)
- {
- if (e)
- callback(e);
- else
- this.writeToFile(toFile, false, data, callback);
- }.bind(this));
- },
-
- renameFile: function(fromFile, newName, callback)
- {
- this._getFileEntry(fromFile, false, function(fs, fileEntry)
- {
- fileEntry.moveTo(fs.root, newName, function()
- {
- callback(null);
- }, callback);
- }, callback);
- },
-
- removeFile: function(file, callback)
- {
- this._getFileEntry(file, false, function(fs, fileEntry)
- {
- fileEntry.remove(function()
- {
- callback(null);
- }, callback);
- }, callback);
- },
-
- statFile: function(file, callback)
- {
- this._getFileEntry(file, false, function(fs, fileEntry)
- {
- fileEntry.getMetadata(function(metadata)
- {
- callback(null, {
- exists: true,
- isDirectory: fileEntry.isDirectory,
- isFile: fileEntry.isFile,
- lastModified: metadata.modificationTime.getTime()
- });
- }, callback);
- }, callback);
- }
- }
-};
-
-//
// Fake nsIFile implementation for our I/O
//
function FakeFile(path)
{
this.path = path;
}
FakeFile.prototype =
@@ -313,137 +130,16 @@ FakeFile.prototype =
get parent()
{
return {create: function() {}};
},
normalize: function() {}
};
//
-// Prefs module: the values are hardcoded for now.
-//
-
-require.scopes.prefs = {
- Prefs: {
- enabled: true,
- patternsfile: "patterns.ini",
- patternsbackups: 5,
- patternsbackupinterval: 24,
- data_directory: "",
- savestats: false,
- privateBrowsing: false,
- subscriptions_fallbackerrors: 5,
- subscriptions_fallbackurl: "https://adblockplus.org/getSubscription?version=%VERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHANNELSTATUS%&responseStatus=%RESPONSESTATUS%",
- subscriptions_autoupdate: true,
- subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/exceptionrules.txt",
- documentation_link: "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG%",
- addListener: function() {}
- }
-};
-
-//
-// Utils module
-//
-
-require.scopes.utils =
-{
- Utils: {
- systemPrincipal: null,
- getString: function(id)
- {
- return id;
- },
- runAsync: function(callback, thisPtr)
- {
- var params = Array.prototype.slice.call(arguments, 2);
- window.setTimeout(function()
- {
- callback.apply(thisPtr, params);
- }, 0);
- },
- get appLocale()
- {
- var locale = chrome.i18n.getMessage("@@ui_locale").replace(/_/g, "-");
- this.__defineGetter__("appLocale", function() {return locale});
- return this.appLocale;
- },
- generateChecksum: function(lines)
- {
- // We cannot calculate MD5 checksums yet :-(
- return null;
- },
- makeURI: function(url)
- {
- return Services.io.newURI(url);
- },
-
- checkLocalePrefixMatch: function(prefixes)
- {
- if (!prefixes)
- return null;
-
- var list = prefixes.split(",");
- for (var i = 0; i < list.length; i++)
- if (new RegExp("^" + list[i] + "\\b").test(this.appLocale))
- return list[i];
-
- return null;
- },
-
- chooseFilterSubscription: function(subscriptions)
- {
- var selectedItem = null;
- var selectedPrefix = null;
- var matchCount = 0;
- for (var i = 0; i < subscriptions.length; i++)
- {
- var subscription = subscriptions[i];
- if (!selectedItem)
- selectedItem = subscription;
-
- var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.getAttribute("prefixes"));
- if (prefix)
- {
- if (!selectedPrefix || selectedPrefix.length < prefix.length)
- {
- selectedItem = subscription;
- selectedPrefix = prefix;
- matchCount = 1;
- }
- else if (selectedPrefix && selectedPrefix.length == prefix.length)
- {
- matchCount++;
-
- // If multiple items have a matching prefix of the same length:
- // Select one of the items randomly, probability should be the same
- // for all items. So we replace the previous match here with
- // probability 1/N (N being the number of matches).
- if (Math.random() * matchCount < 1)
- {
- selectedItem = subscription;
- selectedPrefix = prefix;
- }
- }
- }
- }
- return selectedItem;
- }
- }
-};
-
-//
-// ElemHideHitRegistration dummy implementation
-//
-
-require.scopes.elemHideHitRegistration =
-{
- AboutHandler: {}
-};
-
-//
// Services.jsm module emulation
//
var Services =
{
io: {
newURI: function(uri)
{
« 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