Bug 1058440 - B2G NFC: enable debug when NFC debug is enabled. r=allstars.chh
authorDimi Lee <dlee@mozilla.com>
Tue, 14 Oct 2014 16:37:50 +0800
changeset 210284 ce264541d9982eee1e40665f0eec1cad45400092
parent 210283 12ea66bf1f593c0c919e22d56ce8185bc6f39ca6
child 210285 921718b07c58770add51b49d9c17009ba362d723
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersallstars.chh
bugs1058440
milestone36.0a1
Bug 1058440 - B2G NFC: enable debug when NFC debug is enabled. r=allstars.chh
dom/nfc/NfcContentHelper.js
dom/nfc/gonk/Nfc.js
dom/nfc/gonk/nfc_consts.js
dom/nfc/nsINfcContentHelper.idl
dom/nfc/nsNfc.js
--- a/dom/nfc/NfcContentHelper.js
+++ b/dom/nfc/NfcContentHelper.js
@@ -31,23 +31,26 @@ XPCOMUtils.defineLazyGetter(this, "NFC",
 
 Cu.import("resource://gre/modules/systemlibs.js");
 const NFC_ENABLED = libcutils.property_get("ro.moz.nfc.enabled", "false") === "true";
 
 // set to true to in nfc_consts.js to see debug messages
 let DEBUG = NFC.DEBUG_CONTENT_HELPER;
 
 let debug;
-if (DEBUG) {
-  debug = function (s) {
-    dump("-*- NfcContentHelper: " + s + "\n");
-  };
-} else {
-  debug = function (s) {};
-}
+function updateDebug() {
+  if (DEBUG) {
+    debug = function (s) {
+      dump("-*- NfcContentHelper: " + s + "\n");
+    };
+  } else {
+    debug = function (s) {};
+  }
+};
+updateDebug();
 
 const NFCCONTENTHELPER_CID =
   Components.ID("{4d72c120-da5f-11e1-9b23-0800200c9a66}");
 
 const NFC_IPC_MSG_NAMES = [
   "NFC:ReadNDEFResponse",
   "NFC:WriteNDEFResponse",
   "NFC:MakeReadOnlyNDEFResponse",
@@ -60,16 +63,18 @@ const NFC_IPC_MSG_NAMES = [
 ];
 
 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
                                    "@mozilla.org/childprocessmessagemanager;1",
                                    "nsISyncMessageSender");
 
 function NfcContentHelper() {
   this.initDOMRequestHelper(/* aWindow */ null, NFC_IPC_MSG_NAMES);
+
+  Services.obs.addObserver(this, NFC.TOPIC_MOZSETTINGS_CHANGED, false);
   Services.obs.addObserver(this, "xpcom-shutdown", false);
 
   this._requestMap = [];
 }
 
 NfcContentHelper.prototype = {
   __proto__: DOMRequestIpcHelper.prototype,
 
@@ -81,16 +86,27 @@ NfcContentHelper.prototype = {
     classID:          NFCCONTENTHELPER_CID,
     classDescription: "NfcContentHelper",
     interfaces:       [Ci.nsINfcContentHelper]
   }),
 
   _requestMap: null,
   eventTarget: null,
 
+  init: function init(aWindow) {
+    if (aWindow && aWindow.navigator.mozSettings) {
+      let lock = aWindow.navigator.mozSettings.createLock();
+      var nfcDebug = lock.get(NFC.SETTING_NFC_DEBUG);
+      nfcDebug.onsuccess = function _nfcDebug() {
+        DEBUG = nfcDebug.result[NFC.SETTING_NFC_DEBUG];
+        updateDebug();
+      };
+    }
+  },
+
   encodeNDEFRecords: function encodeNDEFRecords(records) {
     let encodedRecords = [];
     for (let i = 0; i < records.length; i++) {
       let record = records[i];
       encodedRecords.push({
         tnf: record.tnf,
         type: record.type || undefined,
         id: record.id || undefined,
@@ -323,18 +339,26 @@ NfcContentHelper.prototype = {
                           {requestId: requestId});
     return request;
   },
 
   // nsIObserver
   observe: function observe(subject, topic, data) {
     if (topic == "xpcom-shutdown") {
       this.destroyDOMRequestHelper();
+      Services.obs.removeObserver(this, NFC.TOPIC_MOZSETTINGS_CHANGED);
       Services.obs.removeObserver(this, "xpcom-shutdown");
       cpmm = null;
+    } else if (topic == NFC.TOPIC_MOZSETTINGS_CHANGED) {
+      if ("wrappedJSObject" in subject) {
+        subject = subject.wrappedJSObject;
+      }
+      if (subject) {
+        this.handle(subject.key, subject.value);
+      }
     }
   },
 
   // nsIMessageListener
 
   fireRequestSuccess: function fireRequestSuccess(requestId, result) {
     let request = this.takeRequest(requestId);
     if (!request) {
@@ -390,16 +414,25 @@ NfcContentHelper.prototype = {
           case NFC.NFC_PEER_EVENT_LOST:
             this.eventTarget.notifyPeerLost(result.sessionToken);
             break;
         }
         break;
     }
   },
 
+  handle: function handle(name, result) {
+    switch (name) {
+      case NFC.SETTING_NFC_DEBUG:
+        DEBUG = result;
+        updateDebug();
+        break;
+    }
+  },
+
   handleReadNDEFResponse: function handleReadNDEFResponse(result) {
     let requester = this._requestMap[result.requestId];
     if (!requester) {
       debug("Response Invalid requestId=" + result.requestId);
       return;
     }
     delete this._requestMap[result.requestId];
 
--- a/dom/nfc/gonk/Nfc.js
+++ b/dom/nfc/gonk/Nfc.js
@@ -17,36 +17,43 @@
 
 "use strict";
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
+XPCOMUtils.defineLazyServiceGetter(this, "gSettingsService",
+                                   "@mozilla.org/settingsService;1",
+                                   "nsISettingsService");
+
 XPCOMUtils.defineLazyGetter(this, "NFC", function () {
   let obj = {};
   Cu.import("resource://gre/modules/nfc_consts.js", obj);
   return obj;
 });
 
 Cu.import("resource://gre/modules/systemlibs.js");
 const NFC_ENABLED = libcutils.property_get("ro.moz.nfc.enabled", "false") === "true";
 
 // set to true in nfc_consts.js to see debug messages
 let DEBUG = NFC.DEBUG_NFC;
 
 let debug;
-if (DEBUG) {
-  debug = function (s) {
-    dump("-*- Nfc: " + s + "\n");
-  };
-} else {
-  debug = function (s) {};
-}
+function updateDebug() {
+  if (DEBUG) {
+    debug = function (s) {
+      dump("-*- Nfc: " + s + "\n");
+    };
+  } else {
+    debug = function (s) {};
+  }
+};
+updateDebug();
 
 const NFC_CONTRACTID = "@mozilla.org/nfc;1";
 const NFC_CID =
   Components.ID("{2ff24790-5e74-11e1-b86c-0800200c9a66}");
 
 const NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES = [
   "NFC:AddEventTarget"
 ];
@@ -98,24 +105,29 @@ XPCOMUtils.defineLazyGetter(this, "gMess
     peerTargets: {},
     currentPeer: null,
 
     eventTargets: [],
 
     init: function init(nfc) {
       this.nfc = nfc;
 
+      let lock = gSettingsService.createLock();
+      lock.get(NFC.SETTING_NFC_DEBUG, this.nfc);
+
+      Services.obs.addObserver(this, NFC.TOPIC_MOZSETTINGS_CHANGED, false);
       Services.obs.addObserver(this, NFC.TOPIC_XPCOM_SHUTDOWN, false);
       this._registerMessageListeners();
     },
 
     _shutdown: function _shutdown() {
       this.nfc.shutdown();
       this.nfc = null;
 
+      Services.obs.removeObserver(this, NFC.TOPIC_MOZSETTINGS_CHANGED);
       Services.obs.removeObserver(this, NFC.TOPIC_XPCOM_SHUTDOWN);
       this._unregisterMessageListeners();
     },
 
     _registerMessageListeners: function _registerMessageListeners() {
       ppmm.addMessageListener("child-process-shutdown", this);
 
       for (let message of NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES) {
@@ -330,16 +342,24 @@ XPCOMUtils.defineLazyGetter(this, "gMess
     },
 
     /**
      * nsIObserver interface methods.
      */
 
     observe: function observe(subject, topic, data) {
       switch (topic) {
+        case NFC.TOPIC_MOZSETTINGS_CHANGED:
+          if ("wrappedJSObject" in subject) {
+            subject = subject.wrappedJSObject;
+          }
+          if (subject) {
+            this.nfc.handle(subject.key, subject.value);
+          }
+          break;
         case NFC.TOPIC_XPCOM_SHUTDOWN:
           this._shutdown();
           break;
       }
     },
   };
 });
 
@@ -645,16 +665,28 @@ Nfc.prototype = {
     Object.keys(this.targetsByRequestId).forEach((requestId) => {
       if (this.targetsByRequestId[requestId] === target) {
         delete this.targetsByRequestId[requestId];
       }
     });
   },
 
   /**
+   * nsISettingsServiceCallback
+   */
+  handle: function handle(name, result) {
+    switch (name) {
+      case NFC.SETTING_NFC_DEBUG:
+        DEBUG = result;
+        updateDebug();
+        break;
+    }
+  },
+
+  /**
    * nsIObserver interface methods.
    */
   observe: function(subject, topic, data) {
     if (topic != "profile-after-change") {
       debug("Should receive 'profile-after-change' only, received " + topic);
     }
   },
 
--- a/dom/nfc/gonk/nfc_consts.js
+++ b/dom/nfc/gonk/nfc_consts.js
@@ -100,15 +100,18 @@ this.NFC_ERROR_MSG[this.NFC_GECKO_ERROR_
 this.NFC_ERROR_MSG[this.NFC_GECKO_ERROR_SEND_FILE_FAILED] = "NfcSendFileFailed";
 
 // NFC powerlevels must match config PDUs.
 this.NFC_POWER_LEVEL_UNKNOWN        = -1;
 this.NFC_POWER_LEVEL_DISABLED       = 0;
 this.NFC_POWER_LEVEL_LOW            = 1;
 this.NFC_POWER_LEVEL_ENABLED        = 2;
 
+this.TOPIC_MOZSETTINGS_CHANGED      = "mozsettings-changed";
 this.TOPIC_XPCOM_SHUTDOWN           = "xpcom-shutdown";
 
+this.SETTING_NFC_DEBUG = "nfc.debugging.enabled";
+
 this.NFC_PEER_EVENT_READY = 0x01;
 this.NFC_PEER_EVENT_LOST  = 0x02;
 
 // Allow this file to be imported via Components.utils.import().
 this.EXPORTED_SYMBOLS = Object.keys(this);
--- a/dom/nfc/nsINfcContentHelper.idl
+++ b/dom/nfc/nsINfcContentHelper.idl
@@ -22,22 +22,24 @@ interface nsINfcDOMEventTarget : nsISupp
    * Callback function used to notify peerlost.
    *
    * @param sessionToken
    *        SessionToken received from Chrome process
    */
    void notifyPeerLost(in DOMString sessionToken);
 };
 
-[scriptable, uuid(d3f1bdc1-048f-44a8-abe2-bc386edce40b)]
+[scriptable, uuid(cb9c934d-a7fa-422b-bcc1-4ac39741e6ec)]
 interface nsINfcContentHelper : nsISupports
 {
   const long NFC_EVENT_PEER_READY = 0x01;
   const long NFC_EVENT_PEER_LOST  = 0x02;
 
+  void init(in nsIDOMWindow window);
+
   boolean checkSessionToken(in DOMString sessionToken);
 
   nsIDOMDOMRequest readNDEF(in nsIDOMWindow window, in DOMString sessionToken);
   nsIDOMDOMRequest writeNDEF(in nsIDOMWindow window, in nsIVariant records, in DOMString sessionToken);
   nsIDOMDOMRequest makeReadOnlyNDEF(in nsIDOMWindow window, in DOMString sessionToken);
 
   nsIDOMDOMRequest connect(in nsIDOMWindow window, in unsigned long techType, in DOMString sessionToken);
   nsIDOMDOMRequest close(in nsIDOMWindow window, in DOMString sessionToken);
--- a/dom/nfc/nsNfc.js
+++ b/dom/nfc/nsNfc.js
@@ -143,16 +143,19 @@ function mozNfc() {
 mozNfc.prototype = {
   _nfcContentHelper: null,
   _window: null,
   nfcObject: null,
 
   init: function init(aWindow) {
     debug("mozNfc init called");
     this._window = aWindow;
+    if (this._nfcContentHelper) {
+      this._nfcContentHelper.init(aWindow);
+    }
   },
 
   // Only apps which have nfc-manager permission can call the following interfaces
   // 'checkP2PRegistration' , 'notifyUserAcceptedP2P' , 'notifySendFileStatus',
   // 'startPoll', 'stopPoll', and 'powerOff'.
   checkP2PRegistration: function checkP2PRegistration(manifestUrl) {
     // Get the AppID and pass it to ContentHelper
     let appID = appsService.getAppLocalIdByManifestURL(manifestUrl);