Fix web-based protocol handlers not working from Send Link and certain other situations (bug 394483), r=biesi, r=gavin (browser portion), sr=sicking, a=beta blocker

This commit is contained in:
dmose@mozilla.org 2007-10-23 11:20:56 -07:00
Родитель 2117c3b9f9
Коммит 35beadc609
5 изменённых файлов: 90 добавлений и 32 удалений

Просмотреть файл

@ -3941,6 +3941,10 @@ nsBrowserAccess.prototype =
return null;
}
if (!gPrefService)
gPrefService = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch2);
var loadflags = isExternal ?
Ci.nsIWebNavigation.LOAD_FLAGS_FROM_EXTERNAL :
Ci.nsIWebNavigation.LOAD_FLAGS_NONE;

Просмотреть файл

@ -114,12 +114,9 @@ interface nsIHandlerInfo : nsISupports {
*
* @param aWindowContext
* The window to parent the dialog against, and, if a web handler
* is chosen, it is loaded in this window as well. This parameter
* may be ultimately passed nsIURILoader.openURI in the case of a
* web handler, and aWindowContext is null or not present, web
* handlers will fail. We need to do better than that; bug 394483
* filed in order to track.
*
* is chosen, it is loaded in this window as well. See
* nsIHandlerApp.launchWithURI for more details.
*
* @throw NS_ERROR_INVALID_ARG if preferredAction is not valid for this
* call. Other exceptions may be thrown.
*/
@ -279,9 +276,16 @@ interface nsIHandlerApp : nsISupports {
* The URI to launch this application with
*
* @param aWindowContext
* Required for web handlers; is passed through to
* nsIURILoader.openURI, but see bug 394483 for info on
* how this needs to evolve.
*
* Currently only relevant to web-handler apps. If given, this
* represents the docshell to load the handler in and is passed
* through to nsIURILoader.openURI. If this parameter is null or
* not present, the web handler app implementation will attempt to
* find/create a place to load the handler and do so. As of this
* writing, it tries to load the web handler in a new window using
* nsIBrowserDOMWindow.openURI. In the future, it may attempt to
* have a more comprehensive strategy which could include handing
* off to the system default browser (bug 394479).
*/
void launchWithURI(in nsIURI aURI,
[optional] in nsIInterfaceRequestor aWindowContext);

Просмотреть файл

@ -44,6 +44,7 @@ Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const Ci = Components.interfaces;
const Cr = Components.results;
const Cc = Components.classes;
////////////////////////////////////////////////////////////////////////////////
//// nsWebHandler class
@ -95,25 +96,62 @@ nsWebHandlerApp.prototype = {
// encode the URI to be handled
var escapedUriSpecToHandle = encodeURIComponent(aURI.spec);
// insert the encoded URI
var uriToSend = this.uriTemplate.replace("%s", escapedUriSpecToHandle);
// insert the encoded URI and create the object version
var uriSpecToSend = this.uriTemplate.replace("%s", escapedUriSpecToHandle);
var ioService = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
var uriToSend = ioService.newURI(uriSpecToSend, null, null);
// if we have a window context, use the URI loader to load there
if (aWindowContext) {
// create a channel from this URI
var ioService = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService);
var channel = ioService.newChannel(uriToSend, null, null);
channel.loadFlags = Components.interfaces.nsIChannel.LOAD_DOCUMENT_URI;
// create a channel from this URI
var channel = ioService.newChannelFromURI(uriToSend);
channel.loadFlags = Ci.nsIChannel.LOAD_DOCUMENT_URI;
// load the channel
var uriLoader = Components.classes["@mozilla.org/uriloader;1"].
getService(Components.interfaces.nsIURILoader);
// XXX ideally, aIsContentPreferred (the second param) should really be
// passed in from above. Practically, true is probably a reasonable
// default since browsers don't care much, and link click is likely to be
// the more interesting case for non-browser apps. See
// <https://bugzilla.mozilla.org/show_bug.cgi?id=392957#c9> for details.
uriLoader.openURI(channel, true, aWindowContext);
// load the channel
var uriLoader = Cc["@mozilla.org/uriloader;1"].
getService(Ci.nsIURILoader);
// XXX ideally, aIsContentPreferred (the second param) should really be
// passed in from above. Practically, true is probably a reasonable
// default since browsers don't care much, and link click is likely to be
// the more interesting case for non-browser apps. See
// <https://bugzilla.mozilla.org/show_bug.cgi?id=392957#c9> for details.
uriLoader.openURI(channel, true, aWindowContext);
return;
}
// since we don't have a window context, hand it off to a browser
var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
// get browser dom window
var browserDOMWin = windowMediator.getMostRecentWindow("navigator:browser")
.QueryInterface(Ci.nsIDOMChromeWindow)
.browserDOMWindow;
// if we got an exception, there are several possible reasons why:
// a) this gecko embedding doesn't provide an nsIBrowserDOMWindow
// implementation (i.e. doesn't support browser-style functionality),
// so we need to kick the URL out to the OS default browser. This is
// the subject of bug 394479.
// b) this embedding does provide an nsIBrowserDOMWindow impl, but
// there doesn't happen to be a browser window open at the moment; one
// should be opened. It's not clear whether this situation will really
// ever occur in real life. If it does, the only API that I can find
// that seems reasonably likely to work for most embedders is the
// command line handler.
// c) something else went wrong
//
// it's not clear how one would differentiate between the three cases
// above, so for now we don't catch the exception
// openURI
browserDOMWin.openURI(uriToSend,
null, // no window.opener
Ci.nsIBrowserDOMWindow.OPEN_DEFAULT_WINDOW,
Ci.nsIBrowserDOMWindow.OPEN_NEW);
return;
},

Просмотреть файл

@ -12,11 +12,17 @@ Pseudo Web Handler App
<![CDATA[
function onLoad() {
var originalUriToHandle = encodeURIComponent(window.opener.testURI);
window.opener.is(location.search, "?uri=" + originalUriToHandle,
"uri passed to web-handler app");
window.opener.SimpleTest.finish()
// if we have a window.opener, this must be the windowContext
// instance of this test. check that we got the URI right and clean up.
if (window.opener) {
window.opener.is(location.search,
"?uri=" + encodeURIComponent(window.opener.testURI),
"uri passed to web-handler app");
window.opener.SimpleTest.finish();
}
// ensure that we actually have the privs to close non-script-opened windows
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
window.close();
}
]]>

Просмотреть файл

@ -65,9 +65,15 @@ function test() {
webHandler.launchWithURI(uri, windowContext);
// if we get this far without an exception, we've passed
ok(true, "webHandler launchWithURI test");
// if we get this far without an exception, we've at least partly passed
// (remaining check in handlerApp.xhtml)
ok(true, "webHandler launchWithURI (existing window/tab) started");
// make the web browser launch in its own window/tab
webHandler.launchWithURI(uri);
// if we get this far without an exception, we've passed
ok(true, "webHandler launchWithURI (new window/tab) test started");
// set up the local handler object
var localHandler =