Bug 863263 part A - Modify some code in nsReadableUtils to support fallible and infal...
[gecko.git] / b2g / components / RecoveryService.js
bloba30a1fac78fd743899ac4785ed4c583611d7e1f8
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";
8 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
11 Cu.import("resource://gre/modules/ctypes.jsm");
13 const RECOVERYSERVICE_CID = Components.ID("{b3caca5d-0bb0-48c6-912b-6be6cbf08832}");
14 const RECOVERYSERVICE_CONTRACTID = "@mozilla.org/recovery-service;1";
16 function log(msg) {
17   dump("-*- RecoveryService: " + msg + "\n");
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                                               ]);
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),
44     FotaUpdateStatus:    FotaUpdateStatus,
45     getFotaUpdateStatus: library.declare("getFotaUpdateStatus",
46                                          ctypes.default_abi,
47                                          ctypes.int,
48                                          FotaUpdateStatus.ptr)
49   };
50 })();
51 #endif
53 function RecoveryService() {}
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   }),
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");
70     }
71 #endif
72     throw Cr.NS_ERROR_FAILURE;
73   },
75   installFotaUpdate: function RS_installFotaUpdate(updatePath) {
76 #ifdef MOZ_WIDGET_GONK
77     // If this succeeds, then the device reboots and this never returns
78     if (librecovery.installFotaUpdate(updatePath, updatePath.length) != 0) {
79       log("Error: FOTA install failed");
80     }
81 #endif
82     throw Cr.NS_ERROR_FAILURE;
83   },
85   getFotaUpdateStatus: function RS_getFotaUpdateStatus() {
86     let status =  Ci.nsIRecoveryService.FOTA_UPDATE_UNKNOWN;
87 #ifdef MOZ_WIDGET_GONK
88     let cStatus = librecovery.FotaUpdateStatus();
90     if (librecovery.getFotaUpdateStatus(cStatus.address()) == 0) {
91       status = cStatus.result;
92     }
94 #endif
95     return status;
96   }
99 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RecoveryService]);