b2g/components/RecoveryService.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/components/RecoveryService.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,109 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
     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 file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +"use strict";
    1.10 +
    1.11 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    1.12 +
    1.13 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.14 +Cu.import("resource://gre/modules/ctypes.jsm");
    1.15 +
    1.16 +const RECOVERYSERVICE_CID = Components.ID("{b3caca5d-0bb0-48c6-912b-6be6cbf08832}");
    1.17 +const RECOVERYSERVICE_CONTRACTID = "@mozilla.org/recovery-service;1";
    1.18 +
    1.19 +function log(msg) {
    1.20 +  dump("-*- RecoveryService: " + msg + "\n");
    1.21 +}
    1.22 +
    1.23 +#ifdef MOZ_WIDGET_GONK
    1.24 +let librecovery = (function() {
    1.25 +  let library;
    1.26 +  try {
    1.27 +    library = ctypes.open("librecovery.so");
    1.28 +  } catch (e) {
    1.29 +    log("Unable to open librecovery.so");
    1.30 +    throw Cr.NS_ERROR_FAILURE;
    1.31 +  }
    1.32 +  let FotaUpdateStatus = new ctypes.StructType("FotaUpdateStatus", [
    1.33 +                                                { result: ctypes.int },
    1.34 +                                                { updatePath: ctypes.char.ptr }
    1.35 +                                              ]);
    1.36 +
    1.37 +  return {
    1.38 +    factoryReset:        library.declare("factoryReset",
    1.39 +                                         ctypes.default_abi,
    1.40 +                                         ctypes.int),
    1.41 +    installFotaUpdate:   library.declare("installFotaUpdate",
    1.42 +                                         ctypes.default_abi,
    1.43 +                                         ctypes.int,
    1.44 +                                         ctypes.char.ptr,
    1.45 +                                         ctypes.int),
    1.46 +
    1.47 +    FotaUpdateStatus:    FotaUpdateStatus,
    1.48 +    getFotaUpdateStatus: library.declare("getFotaUpdateStatus",
    1.49 +                                         ctypes.default_abi,
    1.50 +                                         ctypes.int,
    1.51 +                                         FotaUpdateStatus.ptr)
    1.52 +  };
    1.53 +})();
    1.54 +#endif
    1.55 +
    1.56 +function RecoveryService() {}
    1.57 +
    1.58 +RecoveryService.prototype = {
    1.59 +  classID: RECOVERYSERVICE_CID,
    1.60 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIRecoveryService]),
    1.61 +  classInfo: XPCOMUtils.generateCI({
    1.62 +    classID: RECOVERYSERVICE_CID,
    1.63 +    contractID: RECOVERYSERVICE_CONTRACTID,
    1.64 +    interfaces: [Ci.nsIRecoveryService],
    1.65 +    classDescription: "B2G Recovery Service"
    1.66 +  }),
    1.67 +
    1.68 +  factoryReset: function RS_factoryReset() {
    1.69 +#ifdef MOZ_WIDGET_GONK
    1.70 +    // If this succeeds, then the device reboots and this never returns
    1.71 +    if (librecovery.factoryReset() != 0) {
    1.72 +      log("Error: Factory reset failed. Trying again after clearing cache.");
    1.73 +    }
    1.74 +    var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService);
    1.75 +    cache.clear();
    1.76 +    if (librecovery.factoryReset() != 0) {
    1.77 +      log("Error: Factory reset failed again");
    1.78 +    }
    1.79 +#endif
    1.80 +    throw Cr.NS_ERROR_FAILURE;
    1.81 +  },
    1.82 +
    1.83 +  installFotaUpdate: function RS_installFotaUpdate(updatePath) {
    1.84 +#ifdef MOZ_WIDGET_GONK
    1.85 +    // If this succeeds, then the device reboots and this never returns
    1.86 +    if (librecovery.installFotaUpdate(updatePath, updatePath.length) != 0) {
    1.87 +      log("Error: FOTA install failed. Trying again after clearing cache.");
    1.88 +    }
    1.89 +    var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService);
    1.90 +    cache.clear();
    1.91 +    if (librecovery.installFotaUpdate(updatePath, updatePath.length) != 0) {
    1.92 +      log("Error: FOTA install failed again");
    1.93 +    }
    1.94 +#endif
    1.95 +    throw Cr.NS_ERROR_FAILURE;
    1.96 +  },
    1.97 +
    1.98 +  getFotaUpdateStatus: function RS_getFotaUpdateStatus() {
    1.99 +    let status =  Ci.nsIRecoveryService.FOTA_UPDATE_UNKNOWN;
   1.100 +#ifdef MOZ_WIDGET_GONK
   1.101 +    let cStatus = librecovery.FotaUpdateStatus();
   1.102 +
   1.103 +    if (librecovery.getFotaUpdateStatus(cStatus.address()) == 0) {
   1.104 +      status = cStatus.result;
   1.105 +    }
   1.106 +
   1.107 +#endif
   1.108 +    return status;
   1.109 +  }
   1.110 +};
   1.111 +
   1.112 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RecoveryService]);

mercurial