michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/ctypes.jsm"); michael@0: michael@0: const RECOVERYSERVICE_CID = Components.ID("{b3caca5d-0bb0-48c6-912b-6be6cbf08832}"); michael@0: const RECOVERYSERVICE_CONTRACTID = "@mozilla.org/recovery-service;1"; michael@0: michael@0: function log(msg) { michael@0: dump("-*- RecoveryService: " + msg + "\n"); michael@0: } michael@0: michael@0: #ifdef MOZ_WIDGET_GONK michael@0: let librecovery = (function() { michael@0: let library; michael@0: try { michael@0: library = ctypes.open("librecovery.so"); michael@0: } catch (e) { michael@0: log("Unable to open librecovery.so"); michael@0: throw Cr.NS_ERROR_FAILURE; michael@0: } michael@0: let FotaUpdateStatus = new ctypes.StructType("FotaUpdateStatus", [ michael@0: { result: ctypes.int }, michael@0: { updatePath: ctypes.char.ptr } michael@0: ]); michael@0: michael@0: return { michael@0: factoryReset: library.declare("factoryReset", michael@0: ctypes.default_abi, michael@0: ctypes.int), michael@0: installFotaUpdate: library.declare("installFotaUpdate", michael@0: ctypes.default_abi, michael@0: ctypes.int, michael@0: ctypes.char.ptr, michael@0: ctypes.int), michael@0: michael@0: FotaUpdateStatus: FotaUpdateStatus, michael@0: getFotaUpdateStatus: library.declare("getFotaUpdateStatus", michael@0: ctypes.default_abi, michael@0: ctypes.int, michael@0: FotaUpdateStatus.ptr) michael@0: }; michael@0: })(); michael@0: #endif michael@0: michael@0: function RecoveryService() {} michael@0: michael@0: RecoveryService.prototype = { michael@0: classID: RECOVERYSERVICE_CID, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIRecoveryService]), michael@0: classInfo: XPCOMUtils.generateCI({ michael@0: classID: RECOVERYSERVICE_CID, michael@0: contractID: RECOVERYSERVICE_CONTRACTID, michael@0: interfaces: [Ci.nsIRecoveryService], michael@0: classDescription: "B2G Recovery Service" michael@0: }), michael@0: michael@0: factoryReset: function RS_factoryReset() { michael@0: #ifdef MOZ_WIDGET_GONK michael@0: // If this succeeds, then the device reboots and this never returns michael@0: if (librecovery.factoryReset() != 0) { michael@0: log("Error: Factory reset failed. Trying again after clearing cache."); michael@0: } michael@0: var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService); michael@0: cache.clear(); michael@0: if (librecovery.factoryReset() != 0) { michael@0: log("Error: Factory reset failed again"); michael@0: } michael@0: #endif michael@0: throw Cr.NS_ERROR_FAILURE; michael@0: }, michael@0: michael@0: installFotaUpdate: function RS_installFotaUpdate(updatePath) { michael@0: #ifdef MOZ_WIDGET_GONK michael@0: // If this succeeds, then the device reboots and this never returns michael@0: if (librecovery.installFotaUpdate(updatePath, updatePath.length) != 0) { michael@0: log("Error: FOTA install failed. Trying again after clearing cache."); michael@0: } michael@0: var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService); michael@0: cache.clear(); michael@0: if (librecovery.installFotaUpdate(updatePath, updatePath.length) != 0) { michael@0: log("Error: FOTA install failed again"); michael@0: } michael@0: #endif michael@0: throw Cr.NS_ERROR_FAILURE; michael@0: }, michael@0: michael@0: getFotaUpdateStatus: function RS_getFotaUpdateStatus() { michael@0: let status = Ci.nsIRecoveryService.FOTA_UPDATE_UNKNOWN; michael@0: #ifdef MOZ_WIDGET_GONK michael@0: let cStatus = librecovery.FotaUpdateStatus(); michael@0: michael@0: if (librecovery.getFotaUpdateStatus(cStatus.address()) == 0) { michael@0: status = cStatus.result; michael@0: } michael@0: michael@0: #endif michael@0: return status; michael@0: } michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RecoveryService]);