toolkit/components/urlformatter/nsURLFormatter.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/urlformatter/nsURLFormatter.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,148 @@
     1.4 +#filter substitution
     1.5 +
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 + /**
    1.11 + * @class nsURLFormatterService
    1.12 + *
    1.13 + * nsURLFormatterService exposes methods to substitute variables in URL formats.
    1.14 + *
    1.15 + * Mozilla Applications linking to Mozilla websites are strongly encouraged to use
    1.16 + * URLs of the following format:
    1.17 + *
    1.18 + *   http[s]://%SERVICE%.mozilla.[com|org]/%LOCALE%/
    1.19 + */
    1.20 +
    1.21 +const Cc = Components.classes;
    1.22 +const Ci = Components.interfaces;
    1.23 +const Cu = Components.utils;
    1.24 +
    1.25 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.26 +Cu.import("resource://gre/modules/Services.jsm");
    1.27 +
    1.28 +const PREF_APP_DISTRIBUTION           = "distribution.id";
    1.29 +const PREF_APP_DISTRIBUTION_VERSION   = "distribution.version";
    1.30 +
    1.31 +XPCOMUtils.defineLazyModuleGetter(this, "UpdateChannel",
    1.32 +                                  "resource://gre/modules/UpdateChannel.jsm");
    1.33 +
    1.34 +function nsURLFormatterService() {
    1.35 +  XPCOMUtils.defineLazyGetter(this, "appInfo", function UFS_appInfo() {
    1.36 +    return Cc["@mozilla.org/xre/app-info;1"].
    1.37 +           getService(Ci.nsIXULAppInfo).
    1.38 +           QueryInterface(Ci.nsIXULRuntime);
    1.39 +  });
    1.40 +
    1.41 +  XPCOMUtils.defineLazyGetter(this, "ABI", function UFS_ABI() {
    1.42 +    let ABI = "default";
    1.43 +    try {
    1.44 +      ABI = this.appInfo.XPCOMABI;
    1.45 +
    1.46 +      if ("@mozilla.org/xpcom/mac-utils;1" in Cc) {
    1.47 +        // Mac universal build should report a different ABI than either macppc
    1.48 +        // or mactel.
    1.49 +        let macutils = Cc["@mozilla.org/xpcom/mac-utils;1"]
    1.50 +                         .getService(Ci.nsIMacUtils);
    1.51 +        if (macutils && macutils.isUniversalBinary) {
    1.52 +          ABI = "Universal-gcc3";
    1.53 +        }
    1.54 +      }
    1.55 +    } catch (e) {}
    1.56 +
    1.57 +    return ABI;
    1.58 +  });
    1.59 +
    1.60 +  XPCOMUtils.defineLazyGetter(this, "OSVersion", function UFS_OSVersion() {
    1.61 +    let OSVersion = "default";
    1.62 +    let sysInfo = Cc["@mozilla.org/system-info;1"].
    1.63 +                  getService(Ci.nsIPropertyBag2);
    1.64 +    try {
    1.65 +      OSVersion = sysInfo.getProperty("name") + " " +
    1.66 +                  sysInfo.getProperty("version");
    1.67 +      OSVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
    1.68 +    } catch (e) {}
    1.69 +
    1.70 +    return encodeURIComponent(OSVersion);
    1.71 +  });
    1.72 +
    1.73 +  XPCOMUtils.defineLazyGetter(this, "distribution", function UFS_distribution() {
    1.74 +    let distribution = { id: "default", version: "default" };
    1.75 +
    1.76 +    let defaults = Services.prefs.getDefaultBranch(null);
    1.77 +    try {
    1.78 +      distribution.id = defaults.getCharPref(PREF_APP_DISTRIBUTION);
    1.79 +    } catch (e) {}
    1.80 +    try {
    1.81 +      distribution.version = defaults.getCharPref(PREF_APP_DISTRIBUTION_VERSION);
    1.82 +    } catch (e) {}
    1.83 +
    1.84 +    return distribution;
    1.85 +  });
    1.86 +}
    1.87 +
    1.88 +nsURLFormatterService.prototype = {
    1.89 +  classID: Components.ID("{e6156350-2be8-11db-a98b-0800200c9a66}"),
    1.90 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIURLFormatter]),
    1.91 +
    1.92 +  _defaults: {
    1.93 +    LOCALE:           function() Cc["@mozilla.org/chrome/chrome-registry;1"].
    1.94 +                                 getService(Ci.nsIXULChromeRegistry).
    1.95 +                                 getSelectedLocale('global'),
    1.96 +    VENDOR:           function() this.appInfo.vendor,
    1.97 +    NAME:             function() this.appInfo.name,
    1.98 +    ID:               function() this.appInfo.ID,
    1.99 +    VERSION:          function() this.appInfo.version,
   1.100 +    APPBUILDID:       function() this.appInfo.appBuildID,
   1.101 +    PLATFORMVERSION:  function() this.appInfo.platformVersion,
   1.102 +    PLATFORMBUILDID:  function() this.appInfo.platformBuildID,
   1.103 +    APP:              function() this.appInfo.name.toLowerCase().replace(/ /, ""),
   1.104 +    OS:               function() this.appInfo.OS,
   1.105 +    XPCOMABI:         function() this.ABI,
   1.106 +    BUILD_TARGET:     function() this.appInfo.OS + "_" + this.ABI,
   1.107 +    OS_VERSION:       function() this.OSVersion,
   1.108 +    CHANNEL:          function() UpdateChannel.get(),
   1.109 +    MOZILLA_API_KEY:   function() "@MOZ_MOZILLA_API_KEY@",
   1.110 +    GOOGLE_API_KEY:   function() "@MOZ_GOOGLE_API_KEY@",
   1.111 +    DISTRIBUTION:     function() this.distribution.id,
   1.112 +    DISTRIBUTION_VERSION: function() this.distribution.version
   1.113 +  },
   1.114 +
   1.115 +  formatURL: function uf_formatURL(aFormat) {
   1.116 +    var _this = this;
   1.117 +    var replacementCallback = function(aMatch, aKey) {
   1.118 +      if (aKey in _this._defaults) {
   1.119 +        return _this._defaults[aKey].call(_this);
   1.120 +      }
   1.121 +      Cu.reportError("formatURL: Couldn't find value for key: " + aKey);
   1.122 +      return aMatch;
   1.123 +    }
   1.124 +    return aFormat.replace(/%([A-Z_]+)%/g, replacementCallback);
   1.125 +  },
   1.126 +
   1.127 +  formatURLPref: function uf_formatURLPref(aPref) {
   1.128 +    var format = null;
   1.129 +    var PS = Cc['@mozilla.org/preferences-service;1'].
   1.130 +             getService(Ci.nsIPrefBranch);
   1.131 +
   1.132 +    try {
   1.133 +      format = PS.getComplexValue(aPref, Ci.nsISupportsString).data;
   1.134 +    } catch(ex) {
   1.135 +      Cu.reportError("formatURLPref: Couldn't get pref: " + aPref);
   1.136 +      return "about:blank";
   1.137 +    }
   1.138 +
   1.139 +    if (!PS.prefHasUserValue(aPref) &&
   1.140 +        /^(data:text\/plain,.+=.+|chrome:\/\/.+\/locale\/.+\.properties)$/.test(format)) {
   1.141 +      // This looks as if it might be a localised preference
   1.142 +      try {
   1.143 +        format = PS.getComplexValue(aPref, Ci.nsIPrefLocalizedString).data;
   1.144 +      } catch(ex) {}
   1.145 +    }
   1.146 +
   1.147 +    return this.formatURL(format);
   1.148 +  }
   1.149 +};
   1.150 +
   1.151 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsURLFormatterService]);

mercurial