Bumping manifests a=b2g-bump
[gecko.git] / services / mobileid / MobileIdentitySmsVerificationFlow.jsm
blob2e1826a56d9e890edc58d38fb69a752824a1a821
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 this.EXPORTED_SYMBOLS = ["MobileIdentitySmsVerificationFlow"];
9 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
11 Cu.import("resource://gre/modules/MobileIdentityCommon.jsm");
12 Cu.import("resource://gre/modules/MobileIdentityVerificationFlow.jsm");
13 Cu.import("resource://gre/modules/Promise.jsm");
14 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
16 #ifdef MOZ_B2G_RIL
17 XPCOMUtils.defineLazyServiceGetter(this, "smsService",
18                                    "@mozilla.org/sms/smsservice;1",
19                                    "nsISmsService");
20 #endif
22 this.MobileIdentitySmsVerificationFlow = function(aVerificationOptions,
23                                                   aUI,
24                                                   aClient,
25                                                   aVerifyStrategy) {
27   // SMS MT or SMS MO+MT specific verify strategy.
28   this.smsVerifyStrategy = aVerifyStrategy;
30   log.debug("aVerificationOptions ${}", aVerificationOptions);
31   MobileIdentityVerificationFlow.call(this, aVerificationOptions, aUI, aClient,
32                                       this._verifyStrategy, this._cleanupStrategy);
35 this.MobileIdentitySmsVerificationFlow.prototype = {
37   __proto__: MobileIdentityVerificationFlow.prototype,
39   observedSilentNumber: null,
41   onSilentSms: null,
43   _verifyStrategy: function() {
44     if (!this.smsVerifyStrategy) {
45       return Promise.reject(ERROR_INTERNAL_UNEXPECTED);
46     }
48     // Even if the user selection is given to us as a possible external phone
49     // number, it is also possible that the phone number introduced by the
50     // user belongs to one of the SIMs inserted in the device which MSISDN
51     // is unknown for us, so we always observe for incoming messages coming
52     // from the given mtSender.
54 #ifdef MOZ_B2G_RIL
55     this.observedSilentNumber = this.verificationOptions.mtSender;
56     try {
57       smsService.addSilentNumber(this.observedSilentNumber);
58     } catch (e) {
59       log.warn("We are already listening for that number");
60     }
62     this.onSilentSms = (function(aSubject, aTopic, aData) {
63       log.debug("Got silent message " + aSubject.sender + " - " + aSubject.body);
64       // We might have observed a notification of an incoming silent message
65       // for other number. In that case, we just bail out.
66       if (aSubject.sender != this.observedSilentNumber) {
67         return;
68       }
70       // We got the SMS containing the verification code.
72       // If the phone number we are trying to verify is or can be an external
73       // phone number (meaning that it doesn't belong to any of the inserted
74       // SIMs) we will be receiving an human readable SMS containing a short
75       // verification code. In this case we need to parse the SMS body to
76       // extract the verification code.
77       // Otherwise, we just use the whole SMS body as it should contain a long
78       // verification code.
79       let verificationCode = aSubject.body;
80       if (this.verificationOptions.external) {
81         // We just take the numerical characters from the body.
82         verificationCode = aSubject.body.replace(/[^0-9]/g,'');
83       }
85       log.debug("Verification code: " + verificationCode);
87       this.verificationCodeDeferred.resolve(verificationCode);
88     }).bind(this);
90     Services.obs.addObserver(this.onSilentSms,
91                              SILENT_SMS_RECEIVED_TOPIC,
92                              false);
93     log.debug("Observing messages from " + this.observedSilentNumber);
94 #endif
96     return this.smsVerifyStrategy();
97   },
99   _cleanupStrategy: function() {
100 #ifdef MOZ_B2G_RIL
101     smsService.removeSilentNumber(this.observedSilentNumber);
102     Services.obs.removeObserver(this.onSilentSms,
103                                 SILENT_SMS_RECEIVED_TOPIC);
104     this.observedSilentNumber = null;
105     this.onSilentSms = null;
106 #endif
107   }