Search code examples
xulrunner

XULrunner permissions for loading remote XUL


I would like to use just PHP (via local AMPPS server) to generate all of my XUL. It appears that XULrunner will not allow one to load a "remote" URL (ie. http://mylocaldomain.com).

I have done a fair amount of searching and I see replies that look like an answer but they are vague or quite old, and I am unable to get them to work. What I think is a relevant answer is to use nsIPermissionManager, there is an example at https://developer.mozilla.org/en-US/docs/Using_Remote_XUL but I am unable to get it to work

Components.classes["@mozilla.org/permissionmanager;1"]
    .getService(Components.interfaces.nsIPermissionManager)
    .add('http://www.mylocaldomain.com/', 'allowXULXBL', Components.interfaces.nsIPermissionManager.ALLOW_ACTION);

I have tried loading it in the pref.js as well as at the beginning of my main.js and neither works. If I place it in my main.js file the Error Console in XULrunner gives this error

Error: NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument arg 0 [nsIPermissionManager.add]

and If I run it in Firefox I get this error

TypeError: Components.classes is undefined

I also found this post here, Is it possible to reference remote content from chrome.manifest? (XULRunner) which basically looks to have you place the application.ini and the defaults\preferences\prefs.js on your server and run them from there. So if I run my app with "...\xulrunner.exe" -app http://www.mylocaldomain.com/application.ini -jsconsole XULrunner just says it can't parse the application.ini.

So the question how does one setup in XULrunner to allow the use of a remote URL?

Edit: Round 2 (well more like round 50 but who's counting)

It is amazing that there isn't a single tutorial on remote XUL. I bought a book Introductory XUL not word in the whole book about remote XUL. Yea I could have bought a a different book, but as age goes, the next one on the list was from 2007, before Mozilla mutilated the XULrunner.

With the book idea having failed I though there's an Add-on, Remote XUL Manager. Let just dissect it. Out of its almost 1000 lines of code it looks like it takes 2 lines to set a permission for a remote URL.

In permissions.js at lines 97 you have

Services.perms.add(uri, ALLOW_REMOTE_XUL, ALLOW);

and line 143 you have

Services.io.newURI(aDomainString, null, null);

Put together and variables replaced you get

var uri = Services.io.newURI('http://www.mylocaldomain.com', null, null);
Services.perms.add(uri, "allowXULXBL", 1);

To run it, it looks like you need to add this before it

Components.utils.import("resource://gre/modules/Services.jsm");

But upon running that in XULrunner I get this error

TypeError: Components.utils is undefined

same as before. So a bit of googling and I found I need this

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

so I added it to the top of my code and got this error

TypeError: netscape.security.PrivilegeManager is undefined

And if you google for xulrunner "netscape.security.PrivilegeManager is undefined" you get 4 results (with this post I guess it will now be 5). Fortunately, if remove xulrunner you get more results and a bit of reading between the lines it appears that netscape.security.PrivilegeManager is obsolete (obsolete reference).

On one of my searches xulrunner "Components.utils is undefined" (again 4 results) turned up a post here on SO, Firefox Add-on SDK and js-ctypes. Based on that I tried this

const {Cu} = require('chrome');
Cu.import("resource://gre/modules/Services.jsm");

var uri = Services.io.newURI('http://www.mylocaldomain.com', null, null);
Services.perms.add(uri, "allowXULXBL", 1);

Which gives this error

ReferenceError: require is not defined

I appears that doing this is possible as there is an Add-on doing it, and looks like its just a couple lines but what those line are appears to be a secret more closely guarded that nuclear launch codes.

So back to hunting down every last little rabbit trail and discovering more dead-ends, at least till someone is willing to spill the beans.


Solution

  • Well, this is an Oh duh moment. But as I have seen a lot of these posts go unanswered I am going to post the answer.

    <?xml version="1.0"?>
    
    <?xml-stylesheet href="main.css" type="text/css"?>
    
    <window id="main" title="My App" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    
        <vbox flex="1">
            <browser id="mainbrowser" src="http://www.mylocaldomain.com/main.xul" flex="1" />
        </vbox>
    
    </window>
    

    It truly is that simple. The browser element is very powerful and can run XUL from any URL with no extra configuration needed. Note there is no javascript above, and as for my pref.js, it is just the one preference needed to start my local main.xul, and then the 5 extra options that help with debugging. I would even remove the stylesheet but, at the moment, it looks like there is a bug that forces XULrunner into fullscreen mode when I remove it.

    I have been looking at this issue and building work arounds for the last 2+ months. I can't believe all the time I wasted on something that had such a simple solution.