mobile/android/components/ContentDispatchChooser.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/components/ContentDispatchChooser.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Ci = Components.interfaces;
     1.9 +const Cu = Components.utils;
    1.10 +const Cc = Components.classes;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +Cu.import("resource://gre/modules/Messaging.jsm");
    1.15 +
    1.16 +function ContentDispatchChooser() {}
    1.17 +
    1.18 +ContentDispatchChooser.prototype =
    1.19 +{
    1.20 +  classID: Components.ID("5a072a22-1e66-4100-afc1-07aed8b62fc5"),
    1.21 +
    1.22 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentDispatchChooser]),
    1.23 +
    1.24 +  get protoSvc() {
    1.25 +    delete this.protoSvc;
    1.26 +    return this.protoSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"].getService(Ci.nsIExternalProtocolService);
    1.27 +  },
    1.28 +
    1.29 +  _getChromeWin: function getChromeWin() {
    1.30 +    try {
    1.31 +      return Services.wm.getMostRecentWindow("navigator:browser");
    1.32 +    } catch (e) {
    1.33 +      throw Cr.NS_ERROR_FAILURE;
    1.34 +    }
    1.35 +  },
    1.36 +
    1.37 +  ask: function ask(aHandler, aWindowContext, aURI, aReason) {
    1.38 +    let window = null;
    1.39 +    try {
    1.40 +      if (aWindowContext)
    1.41 +        window = aWindowContext.getInterface(Ci.nsIDOMWindow);
    1.42 +    } catch (e) { /* it's OK to not have a window */ }
    1.43 +
    1.44 +    // The current list is based purely on the scheme. Redo the query using the url to get more
    1.45 +    // specific results.
    1.46 +    aHandler = this.protoSvc.getProtocolHandlerInfoFromOS(aURI.spec, {});
    1.47 +
    1.48 +    // The first handler in the set is the Android Application Chooser (which will fall back to a default if one is set)
    1.49 +    // If we have more than one option, let the OS handle showing a list (if needed).
    1.50 +    if (aHandler.possibleApplicationHandlers.length > 1) {
    1.51 +      aHandler.launchWithURI(aURI, aWindowContext);
    1.52 +    } else {
    1.53 +      let win = this._getChromeWin();
    1.54 +      if (win && win.NativeWindow) {
    1.55 +        let bundle = Services.strings.createBundle("chrome://browser/locale/handling.properties");
    1.56 +        let failedText = bundle.GetStringFromName("protocol.failed");
    1.57 +        let searchText = bundle.GetStringFromName("protocol.toast.search");
    1.58 +
    1.59 +        win.NativeWindow.toast.show(failedText, "long", {
    1.60 +          button: {
    1.61 +            label: searchText,
    1.62 +            callback: function() {
    1.63 +              let message = {
    1.64 +                type: "Intent:Open",
    1.65 +                url: "market://search?q=" + aURI.scheme,
    1.66 +              };
    1.67 +
    1.68 +              sendMessageToJava(message);
    1.69 +            }
    1.70 +          }
    1.71 +        });
    1.72 +      }
    1.73 +    }
    1.74 +  },
    1.75 +};
    1.76 +
    1.77 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentDispatchChooser]);

mercurial