Bug 988469 - MSISDN verification API for privileged apps. Part 3: B2G UI Glue. r=vingtetun
authorFernando Jiménez <ferjmoreno@gmail.com>
Sat, 07 Jun 2014 19:30:19 +0200
changeset 206647 8a38d3372bf2c525802e46ccd368abb60f04dc2f
parent 206646 fe1432e7b89554319c974ebe09ae528f3169b57a
child 206648 e3dc24f425734635f3f6d663d2339c4234027d86
push id3741
push userasasaki@mozilla.com
push dateMon, 21 Jul 2014 20:25:18 +0000
treeherdermozilla-beta@4d6f46f5af68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvingtetun
bugs988469
milestone32.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 988469 - MSISDN verification API for privileged apps. Part 3: B2G UI Glue. r=vingtetun
b2g/components/B2GComponents.manifest
b2g/components/ContentRequestHelper.jsm
b2g/components/FxAccountsUIGlue.js
b2g/components/MobileIdentityUIGlue.js
b2g/components/moz.build
b2g/installer/package-manifest.in
--- a/b2g/components/B2GComponents.manifest
+++ b/b2g/components/B2GComponents.manifest
@@ -90,8 +90,11 @@ component {c83c02c0-5d43-4e3e-987f-9173b
 contract @mozilla.org/simulator-screen;1 {c83c02c0-5d43-4e3e-987f-9173b313e880}
 category profile-after-change SimulatorScreen @mozilla.org/simulator-screen;1
 
 component {e30b0e13-2d12-4cb0-bc4c-4e617a1bf76e} OopCommandLine.js
 contract @mozilla.org/commandlinehandler/general-startup;1?type=b2goop {e30b0e13-2d12-4cb0-bc4c-4e617a1bf76e}
 category command-line-handler m-b2goop @mozilla.org/commandlinehandler/general-startup;1?type=b2goop
 #endif
 
+# MobileIdentityUIGlue.js
+component {83dbe26a-81f3-4a75-9541-3d0b7ca496b5} MobileIdentityUIGlue.js
+contract @mozilla.org/services/mobileid-ui-glue;1 {83dbe26a-81f3-4a75-9541-3d0b7ca496b5}
new file mode 100644
--- /dev/null
+++ b/b2g/components/ContentRequestHelper.jsm
@@ -0,0 +1,68 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+this.EXPORTED_SYMBOLS = ["ContentRequestHelper"];
+
+const { interfaces: Ci, utils: Cu } = Components;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Promise.jsm");
+
+XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
+                                   "@mozilla.org/uuid-generator;1",
+                                   "nsIUUIDGenerator");
+
+XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
+                                  "resource://gre/modules/SystemAppProxy.jsm");
+
+function debug(msg) {
+  // dump("ContentRequestHelper ** " + msg + "\n");
+}
+
+this.ContentRequestHelper = function() {
+}
+
+ContentRequestHelper.prototype = {
+
+  contentRequest: function(aContentEventName, aChromeEventName,
+                           aInternalEventName, aData) {
+    let deferred = Promise.defer();
+
+    let id = uuidgen.generateUUID().toString();
+
+    SystemAppProxy.addEventListener(aContentEventName,
+                                    function onContentEvent(result) {
+      SystemAppProxy.removeEventListener(aContentEventName,
+                                         onContentEvent);
+      let msg = result.detail;
+      if (!msg || !msg.id || msg.id != id) {
+        deferred.reject("InternalErrorWrongContentEvent " +
+                        JSON.stringify(msg));
+        SystemAppProxy.removeEventListener(aContentEventName,
+                                           onContentEvent);
+        return;
+      }
+
+      debug("Got content event " + JSON.stringify(msg));
+
+      if (msg.error) {
+        deferred.reject(msg.error);
+      } else {
+        deferred.resolve(msg.result);
+      }
+    });
+
+    let detail = {
+       eventName: aInternalEventName,
+       id: id,
+       data: aData
+    };
+    debug("Send chrome event " + JSON.stringify(detail));
+    SystemAppProxy._sendCustomEvent(aChromeEventName, detail);
+
+    return deferred.promise;
+  }
+};
--- a/b2g/components/FxAccountsUIGlue.js
+++ b/b2g/components/FxAccountsUIGlue.js
@@ -1,76 +1,36 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict"
 
 const { interfaces: Ci, utils: Cu } = Components;
 
-Cu.import("resource://gre/modules/XPCOMUtils.jsm");
-Cu.import("resource://gre/modules/Services.jsm");
-Cu.import("resource://gre/modules/Promise.jsm");
 Cu.import("resource://gre/modules/FxAccountsCommon.js");
-
-XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
-                                   "@mozilla.org/uuid-generator;1",
-                                   "nsIUUIDGenerator");
-
-XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
-                                  "resource://gre/modules/SystemAppProxy.jsm");
+Cu.import("resource://gre/modules/ContentRequestHelper.jsm");
 
 function FxAccountsUIGlue() {
 }
 
 FxAccountsUIGlue.prototype = {
 
-  _contentRequest: function(aEventName, aData) {
-    let deferred = Promise.defer();
-
-    let id = uuidgen.generateUUID().toString();
-
-    SystemAppProxy.addEventListener("mozFxAccountsRPContentEvent",
-                                    function onContentEvent(result) {
-      let msg = result.detail;
-      if (!msg || !msg.id || msg.id != id) {
-        deferred.reject("InternalErrorWrongContentEvent");
-        SystemAppProxy.removeEventListener("mozFxAccountsRPContentEvent",
-                                           onContentEvent);
-        return;
-      }
-
-      log.debug("UIGlue got content event " + JSON.stringify(msg));
-
-      if (msg.error) {
-        deferred.reject(msg);
-      } else {
-        deferred.resolve(msg.result);
-      }
-      SystemAppProxy.removeEventListener("mozFxAccountsRPContentEvent",
-                                         onContentEvent);
-    });
-
-    let detail = {
-       eventName: aEventName,
-       id: id,
-       data: aData
-    };
-    log.debug("Send chrome event " + JSON.stringify(detail));
-    SystemAppProxy._sendCustomEvent("mozFxAccountsUnsolChromeEvent", detail);
-
-    return deferred.promise;
-  },
+  __proto__: ContentRequestHelper.prototype,
 
   signInFlow: function() {
-    return this._contentRequest("openFlow");
+    return this.contentRequest("mozFxAccountsRPContentEvent",
+                               "mozFxAccountsUnsolChromeEvent",
+                               "openFlow");
   },
 
   refreshAuthentication: function(aEmail) {
-    return this._contentRequest("refreshAuthentication", {
+    return this.contentRequest("mozFxAccountsRPContentEvent",
+                               "mozFxAccountsUnsolChromeEvent",
+                               "refreshAuthentication", {
       email: aEmail
     });
   },
 
   classID: Components.ID("{51875c14-91d7-4b8c-b65d-3549e101228c}"),
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIFxAccountsUIGlue])
 };
new file mode 100644
--- /dev/null
+++ b/b2g/components/MobileIdentityUIGlue.js
@@ -0,0 +1,161 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict"
+
+const { interfaces: Ci, utils: Cu, classes: Cc } = Components;
+
+Cu.import("resource://gre/modules/ContentRequestHelper.jsm");
+Cu.import("resource://gre/modules/MobileIdentityCommon.jsm");
+Cu.import("resource://gre/modules/MobileIdentityUIGlueCommon.jsm");
+Cu.import("resource://gre/modules/Promise.jsm");
+
+XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
+                                   "@mozilla.org/uuid-generator;1",
+                                   "nsIUUIDGenerator");
+
+XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
+                                  "resource://gre/modules/SystemAppProxy.jsm");
+
+const CHROME_EVENT = "mozMobileIdChromeEvent";
+const CONTENT_EVENT = "mozMobileIdContentEvent";
+const UNSOLICITED_CONTENT_EVENT = "mozMobileIdUnsolContentEvent";
+
+function MobileIdentityUIGlue() {
+  SystemAppProxy.addEventListener(UNSOLICITED_CONTENT_EVENT, this);
+}
+
+MobileIdentityUIGlue.prototype = {
+
+  __proto__: ContentRequestHelper.prototype,
+
+  _sendChromeEvent: function(aEventName, aData) {
+    SystemAppProxy._sendCustomEvent(CHROME_EVENT, {
+      eventName: aEventName,
+      id: uuidgen.generateUUID().toString(),
+      data: aData
+    });
+  },
+
+  _oncancel: null,
+
+  get oncancel() {
+    return this._oncancel;
+  },
+
+  set oncancel(aCallback) {
+    this._oncancel = aCallback;
+  },
+
+  _onresendcode: null,
+
+  get onresendcode() {
+    return this._onresendcode;
+  },
+
+  set onresendcode(aCallback) {
+    this._onresendcode = aCallback;
+  },
+
+  startFlow: function(aManifestURL, aIccInfo) {
+    let phoneNumberInfo;
+    if (aIccInfo) {
+      phoneNumberInfo = [];
+      for (var i = 0; i < aIccInfo.length; i++) {
+        let iccInfo = aIccInfo[i];
+        phoneNumberInfo.push({
+          primary: iccInfo.primary,
+          msisdn: iccInfo.msisdn,
+          operator: iccInfo.operator,
+          external: iccInfo.external,
+          serviceId: iccInfo.serviceId,
+          mcc: iccInfo.mcc
+        });
+      }
+    }
+
+    return this.contentRequest(CONTENT_EVENT,
+                               CHROME_EVENT,
+                               "onpermissionrequest",
+                               { phoneNumberInfo: phoneNumberInfo || [],
+                                 manifestURL: aManifestURL })
+    .then(
+      (result) => {
+        if (!result || !result.phoneNumber && !result.serviceId) {
+          return Promise.reject(ERROR_INVALID_PROMPT_RESULT);
+        }
+
+        let promptResult = new MobileIdentityUIGluePromptResult(
+          result.phoneNumber || null,
+          result.prefix || null,
+          result.mcc || null,
+          result.serviceId || null
+        );
+        return promptResult;
+      }
+    );
+  },
+
+  verificationCodePrompt: function(aRetriesLeft, aTimeout, aTimeLeft) {
+    return this.contentRequest(CONTENT_EVENT,
+                               CHROME_EVENT,
+                               "onverificationcode",
+                               { retriesLeft: aRetriesLeft,
+                                 verificationTimeout: aTimeout,
+                                 verificationTimeoutLeft: aTimeLeft })
+    .then(
+      (result) => {
+        if (!result || !result.verificationCode) {
+          return Promise.reject(ERROR_INVALID_VERIFICATION_CODE);
+        }
+
+        return result.verificationCode;
+      }
+    );
+  },
+
+  error: function(aError) {
+    log.error("UI error " + aError);
+    this._sendChromeEvent("onerror", {
+      error: aError
+    });
+  },
+
+  verify: function() {
+    this._sendChromeEvent("verify");
+  },
+
+  verified: function(aVerifiedPhoneNumber) {
+    this._sendChromeEvent("onverified", {
+      verifiedPhoneNumber: aVerifiedPhoneNumber
+    });
+  },
+
+  handleEvent: function(aEvent) {
+    let msg = aEvent.detail;
+    if (!msg) {
+      log.warning("Got invalid event");
+      return;
+    }
+    log.debug("Got content event ${}", msg);
+
+    switch(msg.eventName) {
+      case 'cancel':
+        this.oncancel();
+        break;
+      case 'resendcode':
+        this.onresendcode();
+        break;
+      default:
+        log.warning("Invalid event name");
+        break;
+    }
+  },
+
+  classID: Components.ID("{83dbe26a-81f3-4a75-9541-3d0b7ca496b5}"),
+
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIMobileIdentityUIGlue])
+};
+
+this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MobileIdentityUIGlue]);
--- a/b2g/components/moz.build
+++ b/b2g/components/moz.build
@@ -10,16 +10,17 @@ EXTRA_COMPONENTS += [
     'ActivitiesGlue.js',
     'AlertsService.js',
     'B2GAboutRedirector.js',
     'ContentPermissionPrompt.js',
     'FilePicker.js',
     'HelperAppDialog.js',
     'InterAppCommUIGlue.js',
     'MailtoProtocolHandler.js',
+    'MobileIdentityUIGlue.js',
     'PaymentGlue.js',
     'ProcessGlobal.js',
     'SmsProtocolHandler.js',
     'SystemMessageGlue.js',
     'TelProtocolHandler.js',
     'WebappsUpdateTimer.js',
     'YoutubeProtocolHandler.js',
 ]
@@ -38,16 +39,17 @@ EXTRA_PP_COMPONENTS += [
 
 if CONFIG['MOZ_UPDATER']:
     EXTRA_PP_COMPONENTS += [
         'UpdatePrompt.js',
     ]
 
 EXTRA_JS_MODULES += [
     'AlertsHelper.jsm',
+    'ContentRequestHelper.jsm',
     'ErrorPage.jsm',
     'SignInToWebsite.jsm',
     'SystemAppProxy.jsm',
     'TelURIParser.jsm',
     'WebappsUpdater.jsm',
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] != 'gonk':
--- a/b2g/installer/package-manifest.in
+++ b/b2g/installer/package-manifest.in
@@ -826,16 +826,17 @@ bin/components/@DLL_PREFIX@nkgnomevfs@DL
 #endif
 
 @BINPATH@/components/DataStore.manifest
 @BINPATH@/components/DataStoreImpl.js
 @BINPATH@/components/dom_datastore.xpt
 
 @BINPATH@/components/MobileIdentity.manifest
 @BINPATH@/components/MobileIdentity.js
+@BINPATH@/components/MobileIdentityUIGlue.js
 @BINPATH@/components/dom_mobileidentity.xpt
 
 #ifdef MOZ_WEBSPEECH
 @BINPATH@/components/dom_webspeechsynth.xpt
 #endif
 
 #ifdef XP_MACOSX
 @BINPATH@/@DLL_PREFIX@plugin_child_interpose@DLL_SUFFIX@