From 7337b2f87304984cd92cc3cb9e9f7bc5d1391212 Mon Sep 17 00:00:00 2001 From: David Kettler Date: Fri, 11 Dec 2009 18:21:21 +1030 Subject: [PATCH] load_rc: allow chrome and file uris load_rc() will now accept a number of ways to specify the file to load. In particular this allows loading from chrome uris on the command line. The code tries to produce an nsILocalFile from whatever it's given so that, for instance, the directory processing works for chrome uris too. The subscript loader will refuse to load a non-local uri. A chrome uri is provided for the test suite, which allows usage like: conkeror -q -batch -l chrome://conkeror-test/content/simple Another usage is to load a module from the contrib area: load_rc("chrome://conkeror-contrib/content/mode-line-buttons.js"); --- chrome/chrome.manifest | 1 + modules/command-line.js | 2 +- modules/rc.js | 77 ++++++++++++++++++++++++++++++++++--------------- modules/utils.js | 6 ++++ 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/chrome/chrome.manifest b/chrome/chrome.manifest index fcd29b9..60f7bbc 100644 --- a/chrome/chrome.manifest +++ b/chrome/chrome.manifest @@ -6,6 +6,7 @@ locale conkeror-gui en-US file:../locale/en-US/ content conkeror-help file:../help/ content conkeror-contrib file:../contrib/modules/ +content conkeror-test file:../tests/ locale branding en-US file:../branding/ diff --git a/modules/command-line.js b/modules/command-line.js index 29cef6c..bc6b204 100644 --- a/modules/command-line.js +++ b/modules/command-line.js @@ -96,7 +96,7 @@ command_line_param_handler("f", true, function (command, ctx) { command_line_param_handler("l", false, function (path, ctx) { try { - load_rc(ctx.command_line.resolveFile(path).path); + load_rc(path, function(path) ctx.command_line.resolveFile(path)); } catch (e) { dump_error(e); } }); diff --git a/modules/rc.js b/modules/rc.js index fad1491..882d6e9 100644 --- a/modules/rc.js +++ b/modules/rc.js @@ -9,13 +9,16 @@ function load_rc_file (file) { try { - var name; + var prefix = "file://"; + var uri; if (typeof file == "string") - name = file; + uri = prefix + file; + else if (file instanceof Ci.nsIURI) + uri = file.spec; else - name = file.path; + uri = prefix + file.path; - subscript_loader.loadSubScript("file://" + name, conkeror); + subscript_loader.loadSubScript(uri, conkeror); } catch (e) { dump_error(e); } @@ -47,37 +50,65 @@ function load_rc_directory (file_o) { /* - * path_s: string path to load. may be a file, a directory, or null. - * if it is a file, that file will be loaded. if it is a directory, - * all `.js' files in that directory will be loaded. if it is null, - * the preference `conkeror.rcfile' will be read for the default. + * The file to load may be specified as an nsILocalFile, an nsIURI or + * a string uri or file path or null. If path specifies a directory + * all `.js' files in that directory will be loaded. The default, + * for a null path, is specified by the preference `conkeror.rcfile' + * if it is set, otherwise it is $HOME/.conkerorrc. A uri that is + * not of file or chrome scheme will fail. */ -function load_rc (path_s) { - if (! path_s) { +function load_rc (path, resolve) { + var file; + + if (typeof path == "object") + file = path; + if (typeof path == "string") { + try { + file = make_uri(path); + } catch (e) { + if (resolve) + file = resolve(path); + } + } + if (file instanceof Ci.nsIURI && file.schemeIs("chrome")) + try { + file = make_file_from_chrome(file); + } catch (e) { /* ignore */ } + if (file instanceof Ci.nsIURI && file.schemeIs("file")) + file = make_file(file.path); + + if (!path) { if (pref_has_user_value("conkeror.rcfile")) { var rcfile = get_pref("conkeror.rcfile"); if (rcfile.length) - path_s = rcfile; + path = rcfile; else return; } else { - var default_rc = get_home_directory(); - default_rc.appendRelativePath(".conkerorrc"); - if (default_rc.exists()) - path_s = default_rc.path; - else + file = get_home_directory(); + file.appendRelativePath(".conkerorrc"); + if (!file.exists()) return; } } - var file_o = Cc["@mozilla.org/file/local;1"] - .createInstance(Ci.nsILocalFile); - file_o.initWithPath(path_s); - if (file_o.isDirectory()) { - load_rc_directory(file_o); + if (!file) + file = make_file(path); + + if (file instanceof Ci.nsILocalFile) { + path = file.path; + if (!file.exists()) + throw interactive_error("File not found: " + path); + } + if (file instanceof Ci.nsIURI) + path = file.spec; + + if (file instanceof Ci.nsILocalFile && file.isDirectory()) { + load_rc_directory(file); } else { - load_rc_file(path_s); + load_rc_file(file); } - return file_o.path; + + return path; } diff --git a/modules/utils.js b/modules/utils.js index e2dd6d3..5e819c7 100644 --- a/modules/utils.js +++ b/modules/utils.js @@ -144,6 +144,12 @@ function make_uri (uri, charset, base_uri) { return io_service.newURI(uri, charset, base_uri); } +function make_file_from_chrome (url) { + var crs = Cc['@mozilla.org/chrome/chrome-registry;1'] + .getService(Ci.nsIChromeRegistry); + var file = crs.convertChromeURL(make_uri(url)); + return make_file(file.path); +} function get_document_content_disposition (document_o) { var content_disposition = null; -- 2.11.4.GIT