2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-12-24 05:41:24 +00:00
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
2013-04-25 20:31:50 +00:00
|
|
|
const Cc = Components.classes;
|
2015-06-25 04:56:23 +00:00
|
|
|
const Cr = Components.results;
|
2009-12-24 05:41:24 +00:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-07-13 14:36:09 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2014-02-11 17:16:00 +00:00
|
|
|
Cu.import("resource://gre/modules/Messaging.jsm");
|
2013-11-05 00:29:07 +00:00
|
|
|
|
2009-12-24 05:41:24 +00:00
|
|
|
function ContentDispatchChooser() {}
|
|
|
|
|
|
|
|
ContentDispatchChooser.prototype =
|
|
|
|
{
|
|
|
|
classID: Components.ID("5a072a22-1e66-4100-afc1-07aed8b62fc5"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentDispatchChooser]),
|
|
|
|
|
2013-04-25 20:31:50 +00:00
|
|
|
get protoSvc() {
|
2014-07-29 07:27:00 +00:00
|
|
|
if (!this._protoSvc) {
|
|
|
|
this._protoSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"].getService(Ci.nsIExternalProtocolService);
|
|
|
|
}
|
|
|
|
return this._protoSvc;
|
2013-04-25 20:31:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_getChromeWin: function getChromeWin() {
|
|
|
|
try {
|
|
|
|
return Services.wm.getMostRecentWindow("navigator:browser");
|
|
|
|
} catch (e) {
|
|
|
|
throw Cr.NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-12-24 05:41:24 +00:00
|
|
|
ask: function ask(aHandler, aWindowContext, aURI, aReason) {
|
|
|
|
let window = null;
|
|
|
|
try {
|
|
|
|
if (aWindowContext)
|
|
|
|
window = aWindowContext.getInterface(Ci.nsIDOMWindow);
|
|
|
|
} catch (e) { /* it's OK to not have a window */ }
|
|
|
|
|
2013-04-25 20:31:50 +00:00
|
|
|
// The current list is based purely on the scheme. Redo the query using the url to get more
|
|
|
|
// specific results.
|
|
|
|
aHandler = this.protoSvc.getProtocolHandlerInfoFromOS(aURI.spec, {});
|
2009-12-24 05:41:24 +00:00
|
|
|
|
2013-04-25 20:31:50 +00:00
|
|
|
// The first handler in the set is the Android Application Chooser (which will fall back to a default if one is set)
|
2013-04-25 20:35:05 +00:00
|
|
|
// If we have more than one option, let the OS handle showing a list (if needed).
|
|
|
|
if (aHandler.possibleApplicationHandlers.length > 1) {
|
2009-12-24 05:41:24 +00:00
|
|
|
aHandler.launchWithURI(aURI, aWindowContext);
|
2013-04-25 20:31:50 +00:00
|
|
|
} else {
|
2015-06-16 18:39:00 +00:00
|
|
|
// xpcshell tests do not have an Android Bridge but we require Android
|
|
|
|
// Bridge when using Messaging so we guard against this case. xpcshell
|
|
|
|
// tests also do not have a window, so we use this state to guard.
|
2013-04-25 20:31:50 +00:00
|
|
|
let win = this._getChromeWin();
|
2015-06-16 18:39:00 +00:00
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-05 00:29:07 +00:00
|
|
|
|
2015-06-16 18:39:00 +00:00
|
|
|
let msg = {
|
|
|
|
type: "Intent:OpenNoHandler",
|
|
|
|
uri: aURI.spec,
|
|
|
|
};
|
2013-11-05 00:29:07 +00:00
|
|
|
|
2015-08-07 23:19:23 +00:00
|
|
|
Messaging.sendRequestForResult(msg).then(() => {
|
|
|
|
// Java opens an app on success: take no action.
|
|
|
|
}, (uri) => {
|
2016-09-27 14:23:32 +00:00
|
|
|
// We couldn't open this. If this was from a click, it's likely that we just
|
|
|
|
// want this to fail silently. If the user entered this on the address bar, though,
|
|
|
|
// we want to show the neterror page.
|
|
|
|
|
|
|
|
let dwu = window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
let millis = dwu.millisSinceLastUserInput;
|
|
|
|
if (millis > 0 && millis >= 1000) {
|
|
|
|
window.location.href = uri;
|
|
|
|
}
|
2015-08-07 23:19:23 +00:00
|
|
|
});
|
2013-04-25 20:31:50 +00:00
|
|
|
}
|
2013-11-05 00:29:07 +00:00
|
|
|
},
|
2009-12-24 05:41:24 +00:00
|
|
|
};
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentDispatchChooser]);
|