mobile/android/components/DirectoryProvider.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/components/DirectoryProvider.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,175 @@
     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 +#filter substitution
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cu = Components.utils;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/FileUtils.jsm");
    1.15 +Cu.import("resource://gre/modules/Services.jsm");
    1.16 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.17 +
    1.18 +// -----------------------------------------------------------------------
    1.19 +// Directory Provider for special browser folders and files
    1.20 +// -----------------------------------------------------------------------
    1.21 +
    1.22 +const NS_APP_CACHE_PARENT_DIR = "cachePDir";
    1.23 +const NS_APP_SEARCH_DIR       = "SrchPlugns";
    1.24 +const NS_APP_SEARCH_DIR_LIST  = "SrchPluginsDL";
    1.25 +const NS_APP_USER_SEARCH_DIR  = "UsrSrchPlugns";
    1.26 +const NS_XPCOM_CURRENT_PROCESS_DIR = "XCurProcD";
    1.27 +const XRE_APP_DISTRIBUTION_DIR = "XREAppDist";
    1.28 +const XRE_UPDATE_ROOT_DIR     = "UpdRootD";
    1.29 +const ENVVAR_UPDATE_DIR       = "UPDATES_DIRECTORY";
    1.30 +const WEBAPPS_DIR             = "webappsDir";
    1.31 +const DOWNLOAD_DIR            = "DfltDwnld"
    1.32 +
    1.33 +const SYSTEM_DIST_PATH = "/system/@ANDROID_PACKAGE_NAME@/distribution";
    1.34 +
    1.35 +function DirectoryProvider() {}
    1.36 +
    1.37 +DirectoryProvider.prototype = {
    1.38 +  classID: Components.ID("{ef0f7a87-c1ee-45a8-8d67-26f586e46a4b}"),
    1.39 +  
    1.40 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider,
    1.41 +                                         Ci.nsIDirectoryServiceProvider2]),
    1.42 +
    1.43 +  getFile: function(prop, persistent) {
    1.44 +    if (prop == NS_APP_CACHE_PARENT_DIR) {
    1.45 +      let dirsvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
    1.46 +      let profile = dirsvc.get("ProfD", Ci.nsIFile);
    1.47 +      return profile;
    1.48 +    } else if (prop == WEBAPPS_DIR) {
    1.49 +      // returns the folder that should hold the webapps database file
    1.50 +      // For fennec we will store that in the root profile folder so that all
    1.51 +      // webapps can easily access it
    1.52 +      let dirsvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
    1.53 +      let profile = dirsvc.get("ProfD", Ci.nsIFile);
    1.54 +      return profile.parent;
    1.55 +    } else if (prop == XRE_APP_DISTRIBUTION_DIR) {
    1.56 +      // First, check to see if there's a distribution in the data directory.
    1.57 +      let dataDist = FileUtils.getDir(NS_XPCOM_CURRENT_PROCESS_DIR, ["distribution"], false);
    1.58 +      if (!dataDist.exists()) {
    1.59 +        // Then check to see if there's distribution in the system directory.
    1.60 +        let systemDist = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
    1.61 +        systemDist.initWithPath(SYSTEM_DIST_PATH);
    1.62 +        // Only return the system distribution location if it exists.
    1.63 +        if (systemDist.exists()) {
    1.64 +          return systemDist;
    1.65 +        }
    1.66 +      }
    1.67 +      return dataDist;
    1.68 +    } else if (prop == XRE_UPDATE_ROOT_DIR) {
    1.69 +      let env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
    1.70 +      if (env.exists(ENVVAR_UPDATE_DIR)) {
    1.71 +        let path = env.get(ENVVAR_UPDATE_DIR);
    1.72 +        if (path) {
    1.73 +          let localFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
    1.74 +          localFile.initWithPath(path);
    1.75 +          return localFile;
    1.76 +        }
    1.77 +      }
    1.78 +      let dm = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
    1.79 +      return dm.defaultDownloadsDirectory;
    1.80 +    } else if (prop == DOWNLOAD_DIR) {
    1.81 +      let dm = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
    1.82 +      return dm.defaultDownloadsDirectory;
    1.83 +    }
    1.84 +    
    1.85 +    // We are retuning null to show failure instead for throwing an error. The
    1.86 +    // interface is called quite a bit and throwing an error is noisy. Returning
    1.87 +    // null works with the way the interface is called [see bug 529077]
    1.88 +    return null;
    1.89 +  },
    1.90 +
    1.91 +  /**
    1.92 +   * Appends the distribution-specific search engine directories to the array.
    1.93 +   * The distribution directory structure is as follows:
    1.94 +   *
    1.95 +   * \- distribution/
    1.96 +   *    \- searchplugins/
    1.97 +   *       |- common/
    1.98 +   *       \- locale/
    1.99 +   *          |- <locale 1>/
   1.100 +   *          ...
   1.101 +   *          \- <locale N>/
   1.102 +   *
   1.103 +   * Common engines are loaded for all locales. If there is no locale directory for
   1.104 +   * the current locale, there is a pref: "distribution.searchplugins.defaultLocale",
   1.105 +   * which specifies a default locale to use.
   1.106 +   */
   1.107 +  _appendDistroSearchDirs: function(array) {
   1.108 +    let distro = this.getFile(XRE_APP_DISTRIBUTION_DIR);
   1.109 +    if (!distro.exists())
   1.110 +      return;
   1.111 +
   1.112 +    let searchPlugins = distro.clone();
   1.113 +    searchPlugins.append("searchplugins");
   1.114 +    if (!searchPlugins.exists())
   1.115 +      return;
   1.116 +
   1.117 +    let commonPlugins = searchPlugins.clone();
   1.118 +    commonPlugins.append("common");
   1.119 +    if (commonPlugins.exists())
   1.120 +      array.push(commonPlugins);
   1.121 +
   1.122 +    let localePlugins = searchPlugins.clone();
   1.123 +    localePlugins.append("locale");
   1.124 +    if (!localePlugins.exists())
   1.125 +      return;
   1.126 +
   1.127 +    let curLocale = Services.prefs.getCharPref("general.useragent.locale");
   1.128 +    let curLocalePlugins = localePlugins.clone();
   1.129 +    curLocalePlugins.append(curLocale);
   1.130 +    if (curLocalePlugins.exists()) {
   1.131 +      array.push(curLocalePlugins);
   1.132 +      return;
   1.133 +    }
   1.134 +
   1.135 +    // We didn't append the locale dir - try the default one.
   1.136 +    let defLocale = Services.prefs.getCharPref("distribution.searchplugins.defaultLocale");
   1.137 +    let defLocalePlugins = localePlugins.clone();
   1.138 +    if (defLocalePlugins.exists())
   1.139 +      array.push(defLocalePlugins);
   1.140 +  },
   1.141 +
   1.142 +  getFiles: function(prop) {
   1.143 +    if (prop != NS_APP_SEARCH_DIR_LIST)
   1.144 +      return;
   1.145 +
   1.146 +    let result = [];
   1.147 +
   1.148 +    /**
   1.149 +     * We want to preserve the following order, since the search service loads
   1.150 +     * engines in first-loaded-wins order.
   1.151 +     *   - distro search plugin locations
   1.152 +     *   - user search plugin locations (profile)
   1.153 +     *   - app search plugin location (shipped engines)
   1.154 +     */
   1.155 +    this._appendDistroSearchDirs(result);
   1.156 +
   1.157 +    let appUserSearchDir = FileUtils.getDir(NS_APP_USER_SEARCH_DIR, [], false);
   1.158 +    if (appUserSearchDir.exists())
   1.159 +      result.push(appUserSearchDir);
   1.160 +
   1.161 +    let appSearchDir = FileUtils.getDir(NS_APP_SEARCH_DIR, [], false);
   1.162 +    if (appSearchDir.exists())
   1.163 +      result.push(appSearchDir);
   1.164 +
   1.165 +    return {
   1.166 +      QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]),
   1.167 +      hasMoreElements: function() {
   1.168 +        return result.length > 0;
   1.169 +      },
   1.170 +      getNext: function() {
   1.171 +        return result.shift();
   1.172 +      }
   1.173 +    };
   1.174 +  }
   1.175 +};
   1.176 +
   1.177 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DirectoryProvider]);
   1.178 +

mercurial