Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | "use strict"; |
michael@0 | 7 | |
michael@0 | 8 | const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
michael@0 | 9 | |
michael@0 | 10 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/ctypes.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | const RECOVERYSERVICE_CID = Components.ID("{b3caca5d-0bb0-48c6-912b-6be6cbf08832}"); |
michael@0 | 14 | const RECOVERYSERVICE_CONTRACTID = "@mozilla.org/recovery-service;1"; |
michael@0 | 15 | |
michael@0 | 16 | function log(msg) { |
michael@0 | 17 | dump("-*- RecoveryService: " + msg + "\n"); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 21 | let librecovery = (function() { |
michael@0 | 22 | let library; |
michael@0 | 23 | try { |
michael@0 | 24 | library = ctypes.open("librecovery.so"); |
michael@0 | 25 | } catch (e) { |
michael@0 | 26 | log("Unable to open librecovery.so"); |
michael@0 | 27 | throw Cr.NS_ERROR_FAILURE; |
michael@0 | 28 | } |
michael@0 | 29 | let FotaUpdateStatus = new ctypes.StructType("FotaUpdateStatus", [ |
michael@0 | 30 | { result: ctypes.int }, |
michael@0 | 31 | { updatePath: ctypes.char.ptr } |
michael@0 | 32 | ]); |
michael@0 | 33 | |
michael@0 | 34 | return { |
michael@0 | 35 | factoryReset: library.declare("factoryReset", |
michael@0 | 36 | ctypes.default_abi, |
michael@0 | 37 | ctypes.int), |
michael@0 | 38 | installFotaUpdate: library.declare("installFotaUpdate", |
michael@0 | 39 | ctypes.default_abi, |
michael@0 | 40 | ctypes.int, |
michael@0 | 41 | ctypes.char.ptr, |
michael@0 | 42 | ctypes.int), |
michael@0 | 43 | |
michael@0 | 44 | FotaUpdateStatus: FotaUpdateStatus, |
michael@0 | 45 | getFotaUpdateStatus: library.declare("getFotaUpdateStatus", |
michael@0 | 46 | ctypes.default_abi, |
michael@0 | 47 | ctypes.int, |
michael@0 | 48 | FotaUpdateStatus.ptr) |
michael@0 | 49 | }; |
michael@0 | 50 | })(); |
michael@0 | 51 | #endif |
michael@0 | 52 | |
michael@0 | 53 | function RecoveryService() {} |
michael@0 | 54 | |
michael@0 | 55 | RecoveryService.prototype = { |
michael@0 | 56 | classID: RECOVERYSERVICE_CID, |
michael@0 | 57 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIRecoveryService]), |
michael@0 | 58 | classInfo: XPCOMUtils.generateCI({ |
michael@0 | 59 | classID: RECOVERYSERVICE_CID, |
michael@0 | 60 | contractID: RECOVERYSERVICE_CONTRACTID, |
michael@0 | 61 | interfaces: [Ci.nsIRecoveryService], |
michael@0 | 62 | classDescription: "B2G Recovery Service" |
michael@0 | 63 | }), |
michael@0 | 64 | |
michael@0 | 65 | factoryReset: function RS_factoryReset() { |
michael@0 | 66 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 67 | // If this succeeds, then the device reboots and this never returns |
michael@0 | 68 | if (librecovery.factoryReset() != 0) { |
michael@0 | 69 | log("Error: Factory reset failed. Trying again after clearing cache."); |
michael@0 | 70 | } |
michael@0 | 71 | var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService); |
michael@0 | 72 | cache.clear(); |
michael@0 | 73 | if (librecovery.factoryReset() != 0) { |
michael@0 | 74 | log("Error: Factory reset failed again"); |
michael@0 | 75 | } |
michael@0 | 76 | #endif |
michael@0 | 77 | throw Cr.NS_ERROR_FAILURE; |
michael@0 | 78 | }, |
michael@0 | 79 | |
michael@0 | 80 | installFotaUpdate: function RS_installFotaUpdate(updatePath) { |
michael@0 | 81 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 82 | // If this succeeds, then the device reboots and this never returns |
michael@0 | 83 | if (librecovery.installFotaUpdate(updatePath, updatePath.length) != 0) { |
michael@0 | 84 | log("Error: FOTA install failed. Trying again after clearing cache."); |
michael@0 | 85 | } |
michael@0 | 86 | var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService); |
michael@0 | 87 | cache.clear(); |
michael@0 | 88 | if (librecovery.installFotaUpdate(updatePath, updatePath.length) != 0) { |
michael@0 | 89 | log("Error: FOTA install failed again"); |
michael@0 | 90 | } |
michael@0 | 91 | #endif |
michael@0 | 92 | throw Cr.NS_ERROR_FAILURE; |
michael@0 | 93 | }, |
michael@0 | 94 | |
michael@0 | 95 | getFotaUpdateStatus: function RS_getFotaUpdateStatus() { |
michael@0 | 96 | let status = Ci.nsIRecoveryService.FOTA_UPDATE_UNKNOWN; |
michael@0 | 97 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 98 | let cStatus = librecovery.FotaUpdateStatus(); |
michael@0 | 99 | |
michael@0 | 100 | if (librecovery.getFotaUpdateStatus(cStatus.address()) == 0) { |
michael@0 | 101 | status = cStatus.result; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | #endif |
michael@0 | 105 | return status; |
michael@0 | 106 | } |
michael@0 | 107 | }; |
michael@0 | 108 | |
michael@0 | 109 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RecoveryService]); |