b2g/components/RecoveryService.js

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

mercurial