b2g/components/DirectoryProvider.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/components/DirectoryProvider.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,270 @@
     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 file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Cc = Components.classes;
     1.9 +const Ci = Components.interfaces;
    1.10 +const Cu = Components.utils;
    1.11 +const Cr = Components.results;
    1.12 +
    1.13 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.14 +Cu.import("resource://gre/modules/Services.jsm");
    1.15 +
    1.16 +const XRE_OS_UPDATE_APPLY_TO_DIR = "OSUpdApplyToD"
    1.17 +const UPDATE_ARCHIVE_DIR = "UpdArchD"
    1.18 +const LOCAL_DIR = "/data/local";
    1.19 +const UPDATES_DIR = "updates/0";
    1.20 +const FOTA_DIR = "updates/fota";
    1.21 +
    1.22 +XPCOMUtils.defineLazyServiceGetter(Services, "env",
    1.23 +                                   "@mozilla.org/process/environment;1",
    1.24 +                                   "nsIEnvironment");
    1.25 +
    1.26 +XPCOMUtils.defineLazyServiceGetter(Services, "um",
    1.27 +                                   "@mozilla.org/updates/update-manager;1",
    1.28 +                                   "nsIUpdateManager");
    1.29 +
    1.30 +XPCOMUtils.defineLazyServiceGetter(Services, "volumeService",
    1.31 +                                   "@mozilla.org/telephony/volume-service;1",
    1.32 +                                   "nsIVolumeService");
    1.33 +
    1.34 +XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
    1.35 +                                   "@mozilla.org/childprocessmessagemanager;1",
    1.36 +                                   "nsISyncMessageSender");
    1.37 +
    1.38 +XPCOMUtils.defineLazyGetter(this, "gExtStorage", function dp_gExtStorage() {
    1.39 +    return Services.env.get("EXTERNAL_STORAGE");
    1.40 +});
    1.41 +
    1.42 +// This exists to mark the affected code for bug 828858.
    1.43 +const gUseSDCard = true;
    1.44 +
    1.45 +const VERBOSE = 1;
    1.46 +let log =
    1.47 +  VERBOSE ?
    1.48 +  function log_dump(msg) { dump("DirectoryProvider: " + msg + "\n"); } :
    1.49 +  function log_noop(msg) { };
    1.50 +
    1.51 +function DirectoryProvider() {
    1.52 +}
    1.53 +
    1.54 +DirectoryProvider.prototype = {
    1.55 +  classID: Components.ID("{9181eb7c-6f87-11e1-90b1-4f59d80dd2e5}"),
    1.56 +
    1.57 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider]),
    1.58 +  _xpcom_factory: XPCOMUtils.generateSingletonFactory(DirectoryProvider),
    1.59 +
    1.60 +  _profD: null,
    1.61 +
    1.62 +  getFile: function dp_getFile(prop, persistent) {
    1.63 +#ifdef MOZ_WIDGET_GONK
    1.64 +    let localProps = ["cachePDir", "webappsDir", "PrefD", "indexedDBPDir",
    1.65 +                      "permissionDBPDir", "UpdRootD"];
    1.66 +    if (localProps.indexOf(prop) != -1) {
    1.67 +      let file = Cc["@mozilla.org/file/local;1"]
    1.68 +                   .createInstance(Ci.nsILocalFile)
    1.69 +      file.initWithPath(LOCAL_DIR);
    1.70 +      persistent.value = true;
    1.71 +      return file;
    1.72 +    }
    1.73 +    if (prop == "ProfD") {
    1.74 +      let dir = Cc["@mozilla.org/file/local;1"]
    1.75 +                  .createInstance(Ci.nsILocalFile);
    1.76 +      dir.initWithPath(LOCAL_DIR+"/tests/profile");
    1.77 +      if (dir.exists()) {
    1.78 +        persistent.value = true;
    1.79 +        return dir;
    1.80 +      }
    1.81 +    }
    1.82 +    if (prop == "coreAppsDir") {
    1.83 +      let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile)
    1.84 +      file.initWithPath("/system/b2g");
    1.85 +      persistent.value = true;
    1.86 +      return file;
    1.87 +    }
    1.88 +    if (prop == UPDATE_ARCHIVE_DIR) {
    1.89 +      // getUpdateDir will set persistent to false since it may toggle between
    1.90 +      // /data/local/ and /mnt/sdcard based on free space and/or availability
    1.91 +      // of the sdcard.
    1.92 +      // before download, check if free space is 2.1 times of update.mar
    1.93 +      return this.getUpdateDir(persistent, UPDATES_DIR, 2.1);
    1.94 +    }
    1.95 +    if (prop == XRE_OS_UPDATE_APPLY_TO_DIR) {
    1.96 +      // getUpdateDir will set persistent to false since it may toggle between
    1.97 +      // /data/local/ and /mnt/sdcard based on free space and/or availability
    1.98 +      // of the sdcard.
    1.99 +      // before apply, check if free space is 1.1 times of update.mar
   1.100 +      return this.getUpdateDir(persistent, FOTA_DIR, 1.1);
   1.101 +    }
   1.102 +#else
   1.103 +    // In desktop builds, coreAppsDir is the same as the profile directory.
   1.104 +    // We just need to get the path from the parent, and it is then used to
   1.105 +    // build jar:remoteopenfile:// uris.
   1.106 +    if (prop == "coreAppsDir") {
   1.107 +      let appsDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
   1.108 +      appsDir.append("webapps");
   1.109 +      persistent.value = true;
   1.110 +      return appsDir;
   1.111 +    } else if (prop == "ProfD") {
   1.112 +      let inParent = Cc["@mozilla.org/xre/app-info;1"]
   1.113 +                       .getService(Ci.nsIXULRuntime)
   1.114 +                       .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
   1.115 +      if (inParent) {
   1.116 +        // Just bail out to use the default from toolkit.
   1.117 +        return null;
   1.118 +      }
   1.119 +      if (!this._profD) {
   1.120 +        this._profD = cpmm.sendSyncMessage("getProfD", {})[0];
   1.121 +      }
   1.122 +      let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
   1.123 +      file.initWithPath(this._profD);
   1.124 +      persistent.value = true;
   1.125 +      return file;
   1.126 +    }
   1.127 +#endif
   1.128 +    return null;
   1.129 +  },
   1.130 +
   1.131 +  // The VolumeService only exists on the device, and not on desktop
   1.132 +  volumeHasFreeSpace: function dp_volumeHasFreeSpace(volumePath, requiredSpace) {
   1.133 +    if (!volumePath) {
   1.134 +      return false;
   1.135 +    }
   1.136 +    if (!Services.volumeService) {
   1.137 +      return false;
   1.138 +    }
   1.139 +    let volume = Services.volumeService.createOrGetVolumeByPath(volumePath);
   1.140 +    if (!volume || volume.state !== Ci.nsIVolume.STATE_MOUNTED) {
   1.141 +      return false;
   1.142 +    }
   1.143 +    let stat = volume.getStats();
   1.144 +    if (!stat) {
   1.145 +      return false;
   1.146 +    }
   1.147 +    return requiredSpace <= stat.freeBytes;
   1.148 +  },
   1.149 +
   1.150 +  findUpdateDirWithFreeSpace: function dp_findUpdateDirWithFreeSpace(requiredSpace, subdir) {
   1.151 +    if (!Services.volumeService) {
   1.152 +      return this.createUpdatesDir(LOCAL_DIR, subdir);
   1.153 +    }
   1.154 +
   1.155 +    let activeUpdate = Services.um.activeUpdate;
   1.156 +    if (gUseSDCard) {
   1.157 +      if (this.volumeHasFreeSpace(gExtStorage, requiredSpace)) {
   1.158 +        let extUpdateDir = this.createUpdatesDir(gExtStorage, subdir);
   1.159 +        if (extUpdateDir !== null) {
   1.160 +          return extUpdateDir;
   1.161 +        }
   1.162 +        log("Warning: " + gExtStorage + " has enough free space for update " +
   1.163 +            activeUpdate.name + ", but is not writable");
   1.164 +      }
   1.165 +    }
   1.166 +
   1.167 +    if (this.volumeHasFreeSpace(LOCAL_DIR, requiredSpace)) {
   1.168 +      let localUpdateDir = this.createUpdatesDir(LOCAL_DIR, subdir);
   1.169 +      if (localUpdateDir !== null) {
   1.170 +        return localUpdateDir;
   1.171 +      }
   1.172 +      log("Warning: " + LOCAL_DIR + " has enough free space for update " +
   1.173 +          activeUpdate.name + ", but is not writable");
   1.174 +    }
   1.175 +
   1.176 +    return null;
   1.177 +  },
   1.178 +
   1.179 +  getUpdateDir: function dp_getUpdateDir(persistent, subdir, multiple) {
   1.180 +    let defaultUpdateDir = this.getDefaultUpdateDir();
   1.181 +    persistent.value = false;
   1.182 +
   1.183 +    let activeUpdate = Services.um.activeUpdate;
   1.184 +    if (!activeUpdate) {
   1.185 +      log("Warning: No active update found, using default update dir: " +
   1.186 +          defaultUpdateDir);
   1.187 +      return defaultUpdateDir;
   1.188 +    }
   1.189 +
   1.190 +    let selectedPatch = activeUpdate.selectedPatch;
   1.191 +    if (!selectedPatch) {
   1.192 +      log("Warning: No selected patch, using default update dir: " +
   1.193 +          defaultUpdateDir);
   1.194 +      return defaultUpdateDir;
   1.195 +    }
   1.196 +
   1.197 +    let requiredSpace = selectedPatch.size * multiple;
   1.198 +    let updateDir = this.findUpdateDirWithFreeSpace(requiredSpace, subdir);
   1.199 +    if (updateDir) {
   1.200 +      return updateDir;
   1.201 +    }
   1.202 +
   1.203 +    // If we've gotten this far, there isn't enough free space to download the patch
   1.204 +    // on either external storage or /data/local. All we can do is report the
   1.205 +    // error and let upstream code handle it more gracefully.
   1.206 +    log("Error: No volume found with " + requiredSpace + " bytes for downloading"+
   1.207 +        " update " + activeUpdate.name);
   1.208 +    activeUpdate.errorCode = Cr.NS_ERROR_FILE_TOO_BIG;
   1.209 +    return null;
   1.210 +  },
   1.211 +
   1.212 +  createUpdatesDir: function dp_createUpdatesDir(root, subdir) {
   1.213 +      let dir = Cc["@mozilla.org/file/local;1"]
   1.214 +                   .createInstance(Ci.nsILocalFile);
   1.215 +      dir.initWithPath(root);
   1.216 +      if (!dir.isWritable()) {
   1.217 +        log("Error: " + dir.path + " isn't writable");
   1.218 +        return null;
   1.219 +      }
   1.220 +      dir.appendRelativePath(subdir);
   1.221 +      if (dir.exists()) {
   1.222 +        if (dir.isDirectory() && dir.isWritable()) {
   1.223 +          return dir;
   1.224 +        }
   1.225 +        // subdir is either a file or isn't writable. In either case we
   1.226 +        // can't use it.
   1.227 +        log("Error: " + dir.path + " is a file or isn't writable");
   1.228 +        return null;
   1.229 +      }
   1.230 +      // subdir doesn't exist, and the parent is writable, so try to
   1.231 +      // create it. This can fail if a file named updates exists.
   1.232 +      try {
   1.233 +        dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0770);
   1.234 +      } catch (e) {
   1.235 +        // The create failed for some reason. We can't use it.
   1.236 +        log("Error: " + dir.path + " unable to create directory");
   1.237 +        return null;
   1.238 +      }
   1.239 +      return dir;
   1.240 +  },
   1.241 +
   1.242 +  getDefaultUpdateDir: function dp_getDefaultUpdateDir() {
   1.243 +    let path = gExtStorage;
   1.244 +    if (!path) {
   1.245 +      path = LOCAL_DIR;
   1.246 +    }
   1.247 +
   1.248 +    if (Services.volumeService) {
   1.249 +      let extVolume = Services.volumeService.createOrGetVolumeByPath(path);
   1.250 +      if (!extVolume) {
   1.251 +        path = LOCAL_DIR;
   1.252 +      }
   1.253 +    }
   1.254 +
   1.255 +    let dir = Cc["@mozilla.org/file/local;1"]
   1.256 +                 .createInstance(Ci.nsILocalFile)
   1.257 +    dir.initWithPath(path);
   1.258 +
   1.259 +    if (!dir.exists() && path != LOCAL_DIR) {
   1.260 +      // Fallback to LOCAL_DIR if we didn't fallback earlier
   1.261 +      dir.initWithPath(LOCAL_DIR);
   1.262 +
   1.263 +      if (!dir.exists()) {
   1.264 +        throw Cr.NS_ERROR_FILE_NOT_FOUND;
   1.265 +      }
   1.266 +    }
   1.267 +
   1.268 +    dir.appendRelativePath("updates");
   1.269 +    return dir;
   1.270 +  }
   1.271 +};
   1.272 +
   1.273 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DirectoryProvider]);

mercurial