1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-22 06:47:58 +01:00

Yay XPCSafeJSObjectWrapper.

This commit is contained in:
Kris Maglione
2009-11-03 06:11:53 -05:00
parent 1ce498401e
commit d4b818b7d7
3 changed files with 25 additions and 31 deletions

View File

@@ -476,43 +476,37 @@ function IO() //{{{
*/ */
function File(path, checkPWD) function File(path, checkPWD)
{ {
let self = { __proto__: File.prototype }
if (arguments.length < 2) if (arguments.length < 2)
checkPWD = true; checkPWD = true;
self.file = services.create("file"); let file = services.create("file");
if (path instanceof Ci.nsIFile) if (path instanceof Ci.nsIFile)
self.file = path; file = path;
else if (/file:\/\//.test(path)) else if (/file:\/\//.test(path))
self.file = services.create("file:").getFileFromURLSpec(path); file = services.create("file:").getFileFromURLSpec(path);
else else
{ {
let expandedPath = io.expandPath(path); let expandedPath = io.expandPath(path);
if (!isAbsolutePath(expandedPath) && checkPWD) if (!isAbsolutePath(expandedPath) && checkPWD)
self = joinPaths(io.getCurrentDirectory().path, expandedPath); file = joinPaths(io.getCurrentDirectory().path, expandedPath);
else else
self.file.initWithPath(expandedPath); file.initWithPath(expandedPath);
} }
self.wrappedNative = self.file; file = XPCSafeJSObjectWrapper(file);
return self; file.__proto__ = File.prototype;
return file;
} }
File.prototype = { File.prototype = {
__noSuchMethod__: function (meth, args)
{
return this.wrappedNative[meth].apply(this.wrappedNative,
args.map(function (a) a instanceof File ? a.wrappedNative : a));
},
/** /**
* Iterates over the objects in this directory. * Iterates over the objects in this directory.
*/ */
iterDirectory: function () iterDirectory: function ()
{ {
if (!this.file.isDirectory()) if (!this.isDirectory())
throw Error("Not a directory"); throw Error("Not a directory");
let entries = this.file.directoryEntries; let entries = this.directoryEntries;
while (entries.hasMoreElements()) while (entries.hasMoreElements())
yield File(entries.getNext().QueryInterface(Ci.nsIFile)); yield File(entries.getNext().QueryInterface(Ci.nsIFile));
}, },
@@ -525,7 +519,7 @@ function IO() //{{{
*/ */
readDirectory: function (sort) readDirectory: function (sort)
{ {
if (!this.file.isDirectory()) if (!this.isDirectory())
throw Error("Not a directory"); throw Error("Not a directory");
let array = [e for (e in this.iterDirectory())]; let array = [e for (e in this.iterDirectory())];
@@ -550,7 +544,7 @@ function IO() //{{{
if (!encoding) if (!encoding)
encoding = options["fileencoding"]; encoding = options["fileencoding"];
ifstream.init(this.file, -1, 0, 0); ifstream.init(this, -1, 0, 0);
icstream.init(ifstream, encoding, 4096, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); // 4096 bytes buffering icstream.init(ifstream, encoding, 4096, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); // 4096 bytes buffering
let buffer = []; let buffer = [];
@@ -608,7 +602,7 @@ function IO() //{{{
if (!perms) if (!perms)
perms = 0644; perms = 0644;
ofstream.init(this.file, mode, perms, 0); ofstream.init(this, mode, perms, 0);
let ocstream = getStream(0); let ocstream = getStream(0);
try try
{ {
@@ -638,13 +632,6 @@ function IO() //{{{
return true; return true;
}, },
}; };
/* It would be nice if there were a simpler way to do this. */
("leafName nativeLeafName permissions permissionsOfLink lastModifiedTime " +
"lastModifiedTimeOfLink fileSize fileSizeOfLink target nativeTarget path " +
"nativePath parent directoryEntries").split(" ").forEach(function (p) {
File.prototype.__defineGetter__(p, function () this.file[p]);
File.prototype.__defineSetter__(p, function (val) this.file[p] = val);
});
/////////////////////////////////////////////////////////////////////////////}}} /////////////////////////////////////////////////////////////////////////////}}}
@@ -904,7 +891,7 @@ lookup:
let process = services.create("process"); let process = services.create("process");
process.init(file.wrappedNative); process.init(file);
process.run(blocking, args.map(String), args.length); process.run(blocking, args.map(String), args.length);
return process.exitValue; return process.exitValue;
@@ -989,7 +976,7 @@ lookup:
liberator.echomsg("sourcing \"" + filename + "\"", 2); liberator.echomsg("sourcing \"" + filename + "\"", 2);
let str = file.read(); let str = file.read();
let uri = services.get("io").newFileURI(file.wrappedNative); let uri = services.get("io").newFileURI(file);
// handle pure JavaScript files specially // handle pure JavaScript files specially
if (/\.js$/.test(filename)) if (/\.js$/.test(filename))

View File

@@ -412,7 +412,7 @@ const liberator = (function () //{{{
let file = io.File(args[0]); let file = io.File(args[0]);
if (file.exists() && file.isReadable() && file.isFile()) if (file.exists() && file.isReadable() && file.isFile())
services.get("extensionManager").installItemFromFile(file.file, "app-profile"); services.get("extensionManager").installItemFromFile(file, "app-profile");
else else
{ {
if (file.exists() && file.isDirectory()) if (file.exists() && file.isDirectory())
@@ -1877,6 +1877,8 @@ const liberator = (function () //{{{
// this function is called when the chrome is ready // this function is called when the chrome is ready
startup: function () startup: function ()
{
try
{ {
let start = Date.now(); let start = Date.now();
liberator.log("Initializing liberator object...", 0); liberator.log("Initializing liberator object...", 0);
@@ -2033,6 +2035,11 @@ const liberator = (function () //{{{
liberator.dump("loaded in " + (Date.now() - start) + " ms"); liberator.dump("loaded in " + (Date.now() - start) + " ms");
liberator.log(config.name + " fully initialized", 0); liberator.log(config.name + " fully initialized", 0);
}
catch (e)
{
liberator.reportError(e);
}
}, },
shutdown: function () shutdown: function ()

View File

@@ -325,7 +325,7 @@ const util = { //{{{
const TIME = Date.now(); const TIME = Date.now();
let zip = services.create("zipWriter"); let zip = services.create("zipWriter");
zip.open(FILE.file, io.MODE_CREATE | io.MODE_WRONLY | io.MODE_TRUNCATE); zip.open(FILE, io.MODE_CREATE | io.MODE_WRONLY | io.MODE_TRUNCATE);
function addURIEntry(file, uri) function addURIEntry(file, uri)
zip.addEntryChannel(PATH + file, TIME, 9, zip.addEntryChannel(PATH + file, TIME, 9,
services.get("io").newChannel(uri, null, null), false); services.get("io").newChannel(uri, null, null), false);
@@ -791,7 +791,7 @@ const util = { //{{{
// Try to find a matching file. // Try to find a matching file.
let file = io.File(url); let file = io.File(url);
if (file.exists() && file.isReadable()) if (file.exists() && file.isReadable())
return services.get("io").newFileURI(file.wrappedNative).spec; return services.get("io").newFileURI(file).spec;
} }
catch (e) {} catch (e) {}