mobile/android/components/DirectoryProvider.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #filter substitution
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cu = Components.utils;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/FileUtils.jsm");
michael@0 12 Cu.import("resource://gre/modules/Services.jsm");
michael@0 13 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 14
michael@0 15 // -----------------------------------------------------------------------
michael@0 16 // Directory Provider for special browser folders and files
michael@0 17 // -----------------------------------------------------------------------
michael@0 18
michael@0 19 const NS_APP_CACHE_PARENT_DIR = "cachePDir";
michael@0 20 const NS_APP_SEARCH_DIR = "SrchPlugns";
michael@0 21 const NS_APP_SEARCH_DIR_LIST = "SrchPluginsDL";
michael@0 22 const NS_APP_USER_SEARCH_DIR = "UsrSrchPlugns";
michael@0 23 const NS_XPCOM_CURRENT_PROCESS_DIR = "XCurProcD";
michael@0 24 const XRE_APP_DISTRIBUTION_DIR = "XREAppDist";
michael@0 25 const XRE_UPDATE_ROOT_DIR = "UpdRootD";
michael@0 26 const ENVVAR_UPDATE_DIR = "UPDATES_DIRECTORY";
michael@0 27 const WEBAPPS_DIR = "webappsDir";
michael@0 28 const DOWNLOAD_DIR = "DfltDwnld"
michael@0 29
michael@0 30 const SYSTEM_DIST_PATH = "/system/@ANDROID_PACKAGE_NAME@/distribution";
michael@0 31
michael@0 32 function DirectoryProvider() {}
michael@0 33
michael@0 34 DirectoryProvider.prototype = {
michael@0 35 classID: Components.ID("{ef0f7a87-c1ee-45a8-8d67-26f586e46a4b}"),
michael@0 36
michael@0 37 QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider,
michael@0 38 Ci.nsIDirectoryServiceProvider2]),
michael@0 39
michael@0 40 getFile: function(prop, persistent) {
michael@0 41 if (prop == NS_APP_CACHE_PARENT_DIR) {
michael@0 42 let dirsvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
michael@0 43 let profile = dirsvc.get("ProfD", Ci.nsIFile);
michael@0 44 return profile;
michael@0 45 } else if (prop == WEBAPPS_DIR) {
michael@0 46 // returns the folder that should hold the webapps database file
michael@0 47 // For fennec we will store that in the root profile folder so that all
michael@0 48 // webapps can easily access it
michael@0 49 let dirsvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
michael@0 50 let profile = dirsvc.get("ProfD", Ci.nsIFile);
michael@0 51 return profile.parent;
michael@0 52 } else if (prop == XRE_APP_DISTRIBUTION_DIR) {
michael@0 53 // First, check to see if there's a distribution in the data directory.
michael@0 54 let dataDist = FileUtils.getDir(NS_XPCOM_CURRENT_PROCESS_DIR, ["distribution"], false);
michael@0 55 if (!dataDist.exists()) {
michael@0 56 // Then check to see if there's distribution in the system directory.
michael@0 57 let systemDist = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
michael@0 58 systemDist.initWithPath(SYSTEM_DIST_PATH);
michael@0 59 // Only return the system distribution location if it exists.
michael@0 60 if (systemDist.exists()) {
michael@0 61 return systemDist;
michael@0 62 }
michael@0 63 }
michael@0 64 return dataDist;
michael@0 65 } else if (prop == XRE_UPDATE_ROOT_DIR) {
michael@0 66 let env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
michael@0 67 if (env.exists(ENVVAR_UPDATE_DIR)) {
michael@0 68 let path = env.get(ENVVAR_UPDATE_DIR);
michael@0 69 if (path) {
michael@0 70 let localFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
michael@0 71 localFile.initWithPath(path);
michael@0 72 return localFile;
michael@0 73 }
michael@0 74 }
michael@0 75 let dm = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
michael@0 76 return dm.defaultDownloadsDirectory;
michael@0 77 } else if (prop == DOWNLOAD_DIR) {
michael@0 78 let dm = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
michael@0 79 return dm.defaultDownloadsDirectory;
michael@0 80 }
michael@0 81
michael@0 82 // We are retuning null to show failure instead for throwing an error. The
michael@0 83 // interface is called quite a bit and throwing an error is noisy. Returning
michael@0 84 // null works with the way the interface is called [see bug 529077]
michael@0 85 return null;
michael@0 86 },
michael@0 87
michael@0 88 /**
michael@0 89 * Appends the distribution-specific search engine directories to the array.
michael@0 90 * The distribution directory structure is as follows:
michael@0 91 *
michael@0 92 * \- distribution/
michael@0 93 * \- searchplugins/
michael@0 94 * |- common/
michael@0 95 * \- locale/
michael@0 96 * |- <locale 1>/
michael@0 97 * ...
michael@0 98 * \- <locale N>/
michael@0 99 *
michael@0 100 * Common engines are loaded for all locales. If there is no locale directory for
michael@0 101 * the current locale, there is a pref: "distribution.searchplugins.defaultLocale",
michael@0 102 * which specifies a default locale to use.
michael@0 103 */
michael@0 104 _appendDistroSearchDirs: function(array) {
michael@0 105 let distro = this.getFile(XRE_APP_DISTRIBUTION_DIR);
michael@0 106 if (!distro.exists())
michael@0 107 return;
michael@0 108
michael@0 109 let searchPlugins = distro.clone();
michael@0 110 searchPlugins.append("searchplugins");
michael@0 111 if (!searchPlugins.exists())
michael@0 112 return;
michael@0 113
michael@0 114 let commonPlugins = searchPlugins.clone();
michael@0 115 commonPlugins.append("common");
michael@0 116 if (commonPlugins.exists())
michael@0 117 array.push(commonPlugins);
michael@0 118
michael@0 119 let localePlugins = searchPlugins.clone();
michael@0 120 localePlugins.append("locale");
michael@0 121 if (!localePlugins.exists())
michael@0 122 return;
michael@0 123
michael@0 124 let curLocale = Services.prefs.getCharPref("general.useragent.locale");
michael@0 125 let curLocalePlugins = localePlugins.clone();
michael@0 126 curLocalePlugins.append(curLocale);
michael@0 127 if (curLocalePlugins.exists()) {
michael@0 128 array.push(curLocalePlugins);
michael@0 129 return;
michael@0 130 }
michael@0 131
michael@0 132 // We didn't append the locale dir - try the default one.
michael@0 133 let defLocale = Services.prefs.getCharPref("distribution.searchplugins.defaultLocale");
michael@0 134 let defLocalePlugins = localePlugins.clone();
michael@0 135 if (defLocalePlugins.exists())
michael@0 136 array.push(defLocalePlugins);
michael@0 137 },
michael@0 138
michael@0 139 getFiles: function(prop) {
michael@0 140 if (prop != NS_APP_SEARCH_DIR_LIST)
michael@0 141 return;
michael@0 142
michael@0 143 let result = [];
michael@0 144
michael@0 145 /**
michael@0 146 * We want to preserve the following order, since the search service loads
michael@0 147 * engines in first-loaded-wins order.
michael@0 148 * - distro search plugin locations
michael@0 149 * - user search plugin locations (profile)
michael@0 150 * - app search plugin location (shipped engines)
michael@0 151 */
michael@0 152 this._appendDistroSearchDirs(result);
michael@0 153
michael@0 154 let appUserSearchDir = FileUtils.getDir(NS_APP_USER_SEARCH_DIR, [], false);
michael@0 155 if (appUserSearchDir.exists())
michael@0 156 result.push(appUserSearchDir);
michael@0 157
michael@0 158 let appSearchDir = FileUtils.getDir(NS_APP_SEARCH_DIR, [], false);
michael@0 159 if (appSearchDir.exists())
michael@0 160 result.push(appSearchDir);
michael@0 161
michael@0 162 return {
michael@0 163 QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]),
michael@0 164 hasMoreElements: function() {
michael@0 165 return result.length > 0;
michael@0 166 },
michael@0 167 getNext: function() {
michael@0 168 return result.shift();
michael@0 169 }
michael@0 170 };
michael@0 171 }
michael@0 172 };
michael@0 173
michael@0 174 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DirectoryProvider]);
michael@0 175

mercurial