michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const Cc = Components.classes; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/Messaging.jsm"); michael@0: michael@0: function ContentDispatchChooser() {} michael@0: michael@0: ContentDispatchChooser.prototype = michael@0: { michael@0: classID: Components.ID("5a072a22-1e66-4100-afc1-07aed8b62fc5"), michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentDispatchChooser]), michael@0: michael@0: get protoSvc() { michael@0: delete this.protoSvc; michael@0: return this.protoSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"].getService(Ci.nsIExternalProtocolService); michael@0: }, michael@0: michael@0: _getChromeWin: function getChromeWin() { michael@0: try { michael@0: return Services.wm.getMostRecentWindow("navigator:browser"); michael@0: } catch (e) { michael@0: throw Cr.NS_ERROR_FAILURE; michael@0: } michael@0: }, michael@0: michael@0: ask: function ask(aHandler, aWindowContext, aURI, aReason) { michael@0: let window = null; michael@0: try { michael@0: if (aWindowContext) michael@0: window = aWindowContext.getInterface(Ci.nsIDOMWindow); michael@0: } catch (e) { /* it's OK to not have a window */ } michael@0: michael@0: // The current list is based purely on the scheme. Redo the query using the url to get more michael@0: // specific results. michael@0: aHandler = this.protoSvc.getProtocolHandlerInfoFromOS(aURI.spec, {}); michael@0: michael@0: // The first handler in the set is the Android Application Chooser (which will fall back to a default if one is set) michael@0: // If we have more than one option, let the OS handle showing a list (if needed). michael@0: if (aHandler.possibleApplicationHandlers.length > 1) { michael@0: aHandler.launchWithURI(aURI, aWindowContext); michael@0: } else { michael@0: let win = this._getChromeWin(); michael@0: if (win && win.NativeWindow) { michael@0: let bundle = Services.strings.createBundle("chrome://browser/locale/handling.properties"); michael@0: let failedText = bundle.GetStringFromName("protocol.failed"); michael@0: let searchText = bundle.GetStringFromName("protocol.toast.search"); michael@0: michael@0: win.NativeWindow.toast.show(failedText, "long", { michael@0: button: { michael@0: label: searchText, michael@0: callback: function() { michael@0: let message = { michael@0: type: "Intent:Open", michael@0: url: "market://search?q=" + aURI.scheme, michael@0: }; michael@0: michael@0: sendMessageToJava(message); michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: } michael@0: }, michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentDispatchChooser]);