Bug 1072808 - Part 1: Add TelephonyMessenger as a Wrapper for TelephonyService-Related System Messages. r=echen
authorBevis Tseng <btseng@mozilla.com>
Mon, 20 Oct 2014 16:50:57 +0800
changeset 241689 10ef609563a76c56a0f7e59d5eb47da7148d3485
parent 241688 19c38d9ed8df0c347ad606fc4c18f3e56082854b
child 241690 f69c551148903c638888a890452df7d464a97971
push id4311
push userraliiev@mozilla.com
push dateMon, 12 Jan 2015 19:37:41 +0000
treeherdermozilla-beta@150c9fed433b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersechen
bugs1072808
milestone36.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 1072808 - Part 1: Add TelephonyMessenger as a Wrapper for TelephonyService-Related System Messages. r=echen
b2g/installer/package-manifest.in
dom/system/gonk/RILSystemMessenger.jsm
dom/system/gonk/RILSystemMessengerHelper.js
dom/system/gonk/RILSystemMessengerHelper.manifest
dom/system/gonk/moz.build
dom/system/gonk/tests/test_ril_system_messenger.js
dom/system/gonk/tests/xpcshell.ini
dom/telephony/gonk/TelephonyService.js
dom/telephony/moz.build
dom/telephony/nsITelephonyMessenger.idl
--- a/b2g/installer/package-manifest.in
+++ b/b2g/installer/package-manifest.in
@@ -444,16 +444,18 @@
 @BINPATH@/components/MmsService.manifest
 @BINPATH@/components/MobileConnectionService.js
 @BINPATH@/components/MobileConnectionService.manifest
 @BINPATH@/components/MobileMessageDatabaseService.js
 @BINPATH@/components/MobileMessageDatabaseService.manifest
 @BINPATH@/components/RadioInterfaceLayer.js
 @BINPATH@/components/RadioInterfaceLayer.manifest
 @BINPATH@/components/RILContentHelper.js
+@BINPATH@/components/RILSystemMessengerHelper.js
+@BINPATH@/components/RILSystemMessengerHelper.manifest
 @BINPATH@/components/TelephonyAudioService.js
 @BINPATH@/components/TelephonyAudioService.manifest
 @BINPATH@/components/TelephonyService.js
 @BINPATH@/components/TelephonyService.manifest
 @BINPATH@/components/VoicemailService.js
 @BINPATH@/components/VoicemailService.manifest
 #endif // MOZ_WIDGET_GONK && MOZ_B2G_RIL
 
new file mode 100644
--- /dev/null
+++ b/dom/system/gonk/RILSystemMessenger.jsm
@@ -0,0 +1,56 @@
+/* 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";
+
+/**
+ * RILSystemMessenger
+ */
+this.RILSystemMessenger = function() {};
+RILSystemMessenger.prototype = {
+
+  /**
+   * Hook of Broadcast function
+   *
+   * @param aType
+   *        The type of the message to be sent.
+   * @param aMessage
+   *        The message object to be broadcasted.
+   */
+  broadcastMessage: function(aType, aMessage) {
+    // Function stub to be replaced by the owner of this messenger.
+  },
+
+  /**
+   * Wrapper to send "telephony-new-call" system message.
+   */
+  notifyNewCall: function() {
+    this.broadcastMessage("telephony-new-call", {});
+  },
+
+  /**
+   * Wrapper to send "telephony-call-ended" system message.
+   */
+  notifyCallEnded: function(aServiceId, aNumber, aCdmaWaitingNumber, aEmergency,
+                            aDuration, aOutgoing, aHangUpLocal) {
+    let data = {
+      serviceId: aServiceId,
+      number: aNumber,
+      emergency: aEmergency,
+      duration: aDuration,
+      direction: aOutgoing ? "outgoing" : "incoming",
+      hangUpLocal: aHangUpLocal
+    };
+
+    if (aCdmaWaitingNumber != null) {
+      data.secondNumber = aCdmaWaitingNumber;
+    }
+
+    this.broadcastMessage("telephony-call-ended", data);
+  }
+};
+
+this.EXPORTED_SYMBOLS = [
+  'RILSystemMessenger'
+];
new file mode 100644
--- /dev/null
+++ b/dom/system/gonk/RILSystemMessengerHelper.js
@@ -0,0 +1,73 @@
+/* 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 {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
+
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+let RSM = {};
+Cu.import("resource://gre/modules/RILSystemMessenger.jsm", RSM);
+
+const RILSYSTEMMESSENGERHELPER_CONTRACTID =
+  "@mozilla.org/ril/system-messenger-helper;1";
+const RILSYSTEMMESSENGERHELPER_CID =
+  Components.ID("{19d9a4ea-580d-11e4-8f6c-37ababfaaea9}");
+
+XPCOMUtils.defineLazyServiceGetter(this, "gSystemMessenger",
+                                   "@mozilla.org/system-message-internal;1",
+                                   "nsISystemMessagesInternal");
+
+let DEBUG = false;
+function debug(s) {
+  dump("-@- RILSystemMessenger: " + s + "\n");
+};
+
+// Read debug setting from pref.
+try {
+  let debugPref = Services.prefs.getBoolPref("ril.debugging.enabled");
+  DEBUG = DEBUG || debugPref;
+} catch (e) {}
+
+/**
+ * RILSystemMessengerHelper
+ */
+function RILSystemMessengerHelper() {
+  this.messenger = new RSM.RILSystemMessenger();
+  this.messenger.broadcastMessage = (aType, aMessage) => {
+    if (DEBUG) {
+      debug("broadcastMessage: aType: " + aType +
+            ", aMessage: "+ JSON.stringify(aMessage));
+    }
+
+    gSystemMessenger.broadcastMessage(aType, aMessage);
+  };
+}
+RILSystemMessengerHelper.prototype = {
+
+  classID: RILSYSTEMMESSENGERHELPER_CID,
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsITelephonyMessenger]),
+
+  /**
+   * RILSystemMessenger instance.
+   */
+  messenger: null,
+
+  /**
+   * nsITelephonyMessenger API
+   */
+  notifyNewCall: function() {
+    this.messenger.notifyNewCall();
+  },
+
+  notifyCallEnded: function(aServiceId, aNumber, aCdmaWaitingNumber, aEmergency,
+                            aDuration, aOutgoing, aHangUpLocal) {
+    this.messenger.notifyCallEnded(aServiceId, aNumber, aCdmaWaitingNumber, aEmergency,
+                                   aDuration, aOutgoing, aHangUpLocal);
+  }
+};
+
+this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RILSystemMessengerHelper]);
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/dom/system/gonk/RILSystemMessengerHelper.manifest
@@ -0,0 +1,6 @@
+# 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/.
+
+component {19d9a4ea-580d-11e4-8f6c-37ababfaaea9} RILSystemMessengerHelper.js
+contract @mozilla.org/ril/system-messenger-helper;1 {19d9a4ea-580d-11e4-8f6c-37ababfaaea9}
\ No newline at end of file
--- a/dom/system/gonk/moz.build
+++ b/dom/system/gonk/moz.build
@@ -85,20 +85,23 @@ EXTRA_JS_MODULES += [
 if CONFIG['MOZ_B2G_RIL']:
     XPIDL_SOURCES += [
         'nsIRadioInterfaceLayer.idl',
     ]
     EXTRA_COMPONENTS += [
         'RadioInterfaceLayer.js',
         'RadioInterfaceLayer.manifest',
         'RILContentHelper.js',
+        'RILSystemMessengerHelper.js',
+        'RILSystemMessengerHelper.manifest',
     ]
     EXTRA_JS_MODULES += [
         'ril_consts.js',
         'ril_worker.js',
+        'RILSystemMessenger.jsm',
     ]
 
 FAIL_ON_WARNINGS = True
 
 include('/ipc/chromium/chromium-config.mozbuild')
 
 LOCAL_INCLUDES += [
     '/dom/base',
new file mode 100644
--- /dev/null
+++ b/dom/system/gonk/tests/test_ril_system_messenger.js
@@ -0,0 +1,111 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+/**
+ * Name space for RILSystemMessenger.jsm. Only initialized after first call to
+ * newRILSystemMessenger.
+ */
+let RSM;
+
+let gReceivedMsgType = null;
+let gReceivedMessage = null;
+
+/**
+ * Create a new RILSystemMessenger instance.
+ *
+ * @return a RILSystemMessenger instance.
+ */
+function newRILSystemMessenger() {
+  if (!RSM) {
+    RSM = Cu.import("resource://gre/modules/RILSystemMessenger.jsm", {});
+    equal(typeof RSM.RILSystemMessenger, "function", "RSM.RILSystemMessenger");
+  }
+
+  let rsm = new RSM.RILSystemMessenger();
+  rsm.broadcastMessage = (aType, aMessage) => {
+    gReceivedMsgType = aType;
+    gReceivedMessage = aMessage;
+  };
+
+  return rsm;
+}
+
+function equal_received_system_message(aType, aMessage) {
+  equal(aType, gReceivedMsgType);
+  deepEqual(aMessage, gReceivedMessage);
+  gReceivedMsgType = null;
+  gReceivedMessage = null;
+}
+
+/**
+ * Verify that each nsIXxxMessenger could be retrieved.
+ */
+function run_test() {
+  let telephonyMessenger = Cc["@mozilla.org/ril/system-messenger-helper;1"]
+                           .getService(Ci.nsITelephonyMessenger);
+
+  ok(telephonyMessenger !== null, "Get TelephonyMessenger.");
+
+  run_next_test();
+}
+
+/**
+ * Verify RILSystemMessenger.notifyNewCall()
+ */
+add_test(function test_telephony_messenger_notify_new_call() {
+  let messenger = newRILSystemMessenger();
+
+  messenger.notifyNewCall();
+  equal_received_system_message("telephony-new-call", {});
+
+  run_next_test();
+});
+
+/**
+ * Verify RILSystemMessenger.notifyCallEnded()
+ */
+add_test(function test_telephony_messenger_notify_call_ended() {
+  let messenger = newRILSystemMessenger();
+
+  messenger.notifyCallEnded(1,
+                            "+0987654321",
+                            null,
+                            true,
+                            500,
+                            false,
+                            true);
+
+  equal_received_system_message("telephony-call-ended", {
+    serviceId: 1,
+    number: "+0987654321",
+    emergency: true,
+    duration: 500,
+    direction: "incoming",
+    hangUpLocal: true
+  });
+
+  // Verify 'optional' parameter of secondNumber.
+  messenger.notifyCallEnded(1,
+                            "+0987654321",
+                            "+1234567890",
+                            true,
+                            500,
+                            true,
+                            false);
+
+  equal_received_system_message("telephony-call-ended", {
+    serviceId: 1,
+    number: "+0987654321",
+    emergency: true,
+    duration: 500,
+    direction: "outgoing",
+    hangUpLocal: false,
+    secondNumber: "+1234567890"
+  });
+
+  run_next_test();
+});
--- a/dom/system/gonk/tests/xpcshell.ini
+++ b/dom/system/gonk/tests/xpcshell.ini
@@ -32,8 +32,11 @@ skip-if = true
 [test_ril_worker_clir.js]
 [test_ril_worker_clip.js]
 [test_ril_worker_ssn.js]
 [test_ril_worker_voiceprivacy.js]
 [test_ril_worker_ecm.js]
 [test_ril_worker_stk.js]
 [test_ril_worker_barring_password.js]
 [test_ril_worker_cdma_info_rec.js]
+[test_ril_system_messenger.js]
+# header_helpers.js is not needed for test_ril_system_messenger.js
+head =
--- a/dom/telephony/gonk/TelephonyService.js
+++ b/dom/telephony/gonk/TelephonyService.js
@@ -65,19 +65,19 @@ function debug(s) {
 XPCOMUtils.defineLazyServiceGetter(this, "gRadioInterfaceLayer",
                                    "@mozilla.org/ril;1",
                                    "nsIRadioInterfaceLayer");
 
 XPCOMUtils.defineLazyServiceGetter(this, "gPowerManagerService",
                                    "@mozilla.org/power/powermanagerservice;1",
                                    "nsIPowerManagerService");
 
-XPCOMUtils.defineLazyServiceGetter(this, "gSystemMessenger",
-                                   "@mozilla.org/system-message-internal;1",
-                                   "nsISystemMessagesInternal");
+XPCOMUtils.defineLazyServiceGetter(this, "gTelephonyMessenger",
+                                   "@mozilla.org/ril/system-messenger-helper;1",
+                                   "nsITelephonyMessenger");
 
 XPCOMUtils.defineLazyServiceGetter(this, "gAudioService",
                                    "@mozilla.org/telephony/audio-service;1",
                                    "nsITelephonyAudioService");
 
 XPCOMUtils.defineLazyServiceGetter(this, "gGonkMobileConnectionService",
                                    "@mozilla.org/mobileconnection/mobileconnectionservice;1",
                                    "nsIGonkMobileConnectionService");
@@ -1085,31 +1085,27 @@ TelephonyService.prototype = {
   notifyCallDisconnected: function(aClientId, aCall) {
     if (DEBUG) debug("handleCallDisconnected: " + JSON.stringify(aCall));
 
     aCall.clientId = aClientId;
     aCall.state = nsITelephonyService.CALL_STATE_DISCONNECTED;
     aCall.isEmergency = this._isEmergencyNumber(aCall.number);
     let duration = ("started" in aCall && typeof aCall.started == "number") ?
       new Date().getTime() - aCall.started : 0;
-    let data = {
-      number: aCall.number,
-      serviceId: aClientId,
-      emergency: aCall.isEmergency,
-      duration: duration,
-      direction: aCall.isOutgoing ? "outgoing" : "incoming",
-      hangUpLocal: aCall.hangUpLocal
-    };
 
-    if (this._cdmaCallWaitingNumber != null) {
-      data.secondNumber = this._cdmaCallWaitingNumber;
-      this._cdmaCallWaitingNumber = null;
-    }
+    gTelephonyMessenger.notifyCallEnded(aClientId,
+                                        aCall.number,
+                                        this._cdmaCallWaitingNumber,
+                                        aCall.isEmergency,
+                                        duration,
+                                        aCall.isOutgoing,
+                                        aCall.hangUpLocal);
 
-    gSystemMessenger.broadcastMessage("telephony-call-ended", data);
+    // Clear cache of this._cdmaCallWaitingNumber after call disconnected.
+    this._cdmaCallWaitingNumber = null;
 
     let manualConfStateChange = false;
     let childId = this._currentCalls[aClientId][aCall.callIndex].childId;
     if (childId) {
       // Child cannot live without parent.
       let childCall = this._currentCalls[aClientId][childId];
       this.notifyCallDisconnected(aClientId, childCall);
     } else {
@@ -1164,32 +1160,32 @@ TelephonyService.prototype = {
    * Not much is known about this call at this point, but it's enough
    * to start bringing up the Phone app already.
    */
   notifyCallRing: function() {
     // We need to acquire a CPU wake lock to avoid the system falling into
     // the sleep mode when the RIL handles the incoming call.
     this._acquireCallRingWakeLock();
 
-    gSystemMessenger.broadcastMessage("telephony-new-call", {});
+    gTelephonyMessenger.notifyNewCall();
   },
 
   /**
    * Handle call state changes by updating our current state and the audio
    * system.
    */
   notifyCallStateChanged: function(aClientId, aCall, aSkipStateConversion) {
     if (DEBUG) debug("handleCallStateChange: " + JSON.stringify(aCall));
 
     if (!aSkipStateConversion) {
       aCall.state = this._convertRILCallState(aCall.state);
     }
 
     if (aCall.state == nsITelephonyService.CALL_STATE_DIALING) {
-      gSystemMessenger.broadcastMessage("telephony-new-call", {});
+      gTelephonyMessenger.notifyNewCall();
     }
 
     aCall.clientId = aClientId;
     this._updateActiveCall(aCall);
 
     function pick(arg, defaultValue) {
       return typeof arg !== 'undefined' ? arg : defaultValue;
     }
--- a/dom/telephony/moz.build
+++ b/dom/telephony/moz.build
@@ -49,16 +49,17 @@ IPDL_SOURCES += [
     'ipc/TelephonyTypes.ipdlh'
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk' and CONFIG['MOZ_B2G_RIL']:
     XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell/xpcshell.ini']
     XPIDL_SOURCES += [
         'nsIGonkTelephonyService.idl',
         'nsITelephonyAudioService.idl',
+        'nsITelephonyMessenger.idl',
     ]
     EXTRA_COMPONENTS += [
         'gonk/TelephonyAudioService.js',
         'gonk/TelephonyAudioService.manifest',
         'gonk/TelephonyService.js',
         'gonk/TelephonyService.manifest',
     ]
 
new file mode 100644
--- /dev/null
+++ b/dom/telephony/nsITelephonyMessenger.idl
@@ -0,0 +1,40 @@
+/* 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/. */
+
+#include "nsISupports.idl"
+
+[scriptable, uuid(998a48b2-5b54-11e4-833e-6b17c1427d49)]
+interface nsITelephonyMessenger : nsISupports
+{
+  /**
+   * To broadcast 'telephony-new-call' system message
+   */
+  void notifyNewCall();
+
+  /**
+   * To broadcast 'telephony-call-ended' system message
+   *
+   * @param aServiceId
+   *        The ID of Service where this info is notified from.
+   * @param aNumber
+   *        The phone number of the call to be ended.
+   * @param aCdmaWaitingNumber
+   *        The CDMA call waiting number to be ended if available.
+   * @param aEmergency
+   *        true if it's a emergency number.
+   * @param aDuration
+   *        The duration of this call.
+   * @param aOutgoing
+   *        true if it's an outgoing call.
+   * @param aHangUpLocal
+   *        true if this call was hung up by the user.
+   */
+  void notifyCallEnded(in unsigned long aServiceId,
+                       in DOMString aNumber,
+                       in DOMString aCdmaWaitingNumber,
+                       in boolean aEmergency,
+                       in unsigned long aDuration,
+                       in boolean aOutgoing,
+                       in boolean aHangUpLocal);
+};
\ No newline at end of file