Bug 1000191 - [NFC] |NfcPeer.sendNDEF| fails on emulator. r=dimi
authorYoshi Huang <allstars.chh@mozilla.com>
Thu, 24 Apr 2014 17:05:26 +0800
changeset 180618 1778e4c2abd79f78f8caa257672b462901dbd7cc
parent 180617 9423e087c964c14ecd3b2c3a9231e331b0417f58
child 180619 a60aa2d95896057da9b3ec89af8ee51528a3d99d
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersdimi
bugs1000191
milestone31.0a1
Bug 1000191 - [NFC] |NfcPeer.sendNDEF| fails on emulator. r=dimi From 33a39097f4d5042ed678564f506a5505573a0a93 Mon Sep 17 00:00:00 2001 --- dom/system/gonk/Nfc.js | 52 +++++++++---------------------------------- dom/system/gonk/nfc_consts.js | 2 -- 2 files changed, 10 insertions(+), 44 deletions(-)
dom/system/gonk/Nfc.js
dom/system/gonk/nfc_consts.js
--- a/dom/system/gonk/Nfc.js
+++ b/dom/system/gonk/Nfc.js
@@ -76,19 +76,16 @@ XPCOMUtils.defineLazyServiceGetter(this,
                                    "@mozilla.org/parentprocessmessagemanager;1",
                                    "nsIMessageBroadcaster");
 XPCOMUtils.defineLazyServiceGetter(this, "gSystemMessenger",
                                    "@mozilla.org/system-message-internal;1",
                                    "nsISystemMessagesInternal");
 XPCOMUtils.defineLazyServiceGetter(this, "gSystemWorkerManager",
                                    "@mozilla.org/telephony/system-worker-manager;1",
                                    "nsISystemWorkerManager");
-XPCOMUtils.defineLazyServiceGetter(this, "gSettingsService",
-                                   "@mozilla.org/settingsService;1",
-                                   "nsISettingsService");
 XPCOMUtils.defineLazyServiceGetter(this, "UUIDGenerator",
                                     "@mozilla.org/uuid-generator;1",
                                     "nsIUUIDGenerator");
 XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
   return {
     QueryInterface: XPCOMUtils.generateQI([Ci.nsIMessageListener,
                                            Ci.nsIObserver]),
 
@@ -410,41 +407,37 @@ XPCOMUtils.defineLazyGetter(this, "gMess
 });
 
 function Nfc() {
   debug("Starting Worker");
   this.worker = new ChromeWorker("resource://gre/modules/nfc_worker.js");
   this.worker.onerror = this.onerror.bind(this);
   this.worker.onmessage = this.onmessage.bind(this);
 
-  Services.obs.addObserver(this, NFC.TOPIC_MOZSETTINGS_CHANGED, false);
+  gMessageManager.init(this);
 
-  gMessageManager.init(this);
-  let lock = gSettingsService.createLock();
-  lock.get(NFC.SETTING_NFC_ENABLED, this);
   // Maps sessionId (that are generated from nfcd) with a unique guid : 'SessionToken'
   this.sessionTokenMap = {};
   this.targetsByRequestId = {};
 
   gSystemWorkerManager.registerNfcWorker(this.worker);
 }
 
 Nfc.prototype = {
 
   classID:   NFC_CID,
   classInfo: XPCOMUtils.generateCI({classID: NFC_CID,
                                     classDescription: "Nfc",
                                     interfaces: [Ci.nsIWorkerHolder]}),
 
-  QueryInterface: XPCOMUtils.generateQI([Ci.nsIWorkerHolder,
-                                         Ci.nsIObserver,
-                                         Ci.nsISettingsServiceCallback]),
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIWorkerHolder]),
 
   _currentSessionId: null,
-  _enabled: false,
+
+  powerLevel: NFC.NFC_POWER_LEVEL_UNKNOWN,
 
   onerror: function onerror(event) {
     debug("Got an error: " + event.filename + ":" +
           event.lineno + ": " + event.message + "\n");
     event.preventDefault();
   },
 
   /**
@@ -521,16 +514,20 @@ Nfc.prototype = {
      case "ConfigResponse":
         let target = this.targetsByRequestId[message.requestId];
         if (!target) {
           debug("No target for requestId: " + message.requestId);
           return;
         }
         delete this.targetsByRequestId[message.requestId];
 
+        if (message.status == NFC.GECKO_NFC_ERROR_SUCCESS) {
+          this.powerLevel = message.powerLevel;
+        }
+
         target.sendAsyncMessage("NFC:ConfigResponse", message);
         break;
       case "ConnectResponse": // Fall through.
       case "CloseResponse":
       case "GetDetailsNDEFResponse":
       case "ReadNDEFResponse":
       case "MakeReadOnlyNDEFResponse":
       case "WriteNDEFResponse":
@@ -570,18 +567,18 @@ Nfc.prototype = {
       return null;
     } else if (message.name == "NFC:PowerOff") {
       this.targetsByRequestId[message.json.requestId] = message.target;
       this.setConfig({powerLevel: NFC.NFC_POWER_LEVEL_DISABLED,
                       requestId: message.json.requestId});
       return null;
     }
 
-    if (!this._enabled) {
-      debug("NFC is not enabled.");
+    if (this.powerLevel != NFC.NFC_POWER_LEVEL_ENABLED) {
+      debug("NFC is not enabled. current powerLevel:" + this.powerLevel);
       this.sendNfcErrorResponse(message);
       return null;
     }
 
     // Sanity check on sessionId
     if (message.json.sessionToken !== this.sessionTokenMap[this._currentSessionId]) {
       debug("Invalid Session Token: " + message.json.sessionToken +
             " Expected Session Token: " + this.sessionTokenMap[this._currentSessionId]);
@@ -625,45 +622,16 @@ Nfc.prototype = {
       default:
         debug("UnSupported : Message Name " + message.name);
         return null;
     }
 
     return null;
   },
 
-  /**
-   * nsISettingsServiceCallback
-   */
-
-  handle: function handle(aName, aResult) {
-    switch(aName) {
-      case NFC.SETTING_NFC_ENABLED:
-        debug("'nfc.enabled' is now " + aResult);
-        this._enabled = aResult;
-        break;
-    }
-  },
-
-  /**
-   * nsIObserver
-   */
-
-  observe: function observe(subject, topic, data) {
-    switch (topic) {
-      case NFC.TOPIC_MOZSETTINGS_CHANGED:
-        let setting = JSON.parse(data);
-        if (setting) {
-          let setting = JSON.parse(data);
-          this.handle(setting.key, setting.value);
-        }
-        break;
-    }
-  },
-
   setConfig: function setConfig(prop) {
     this.sendToWorker("config", prop);
   }
 };
 
 if (NFC_ENABLED) {
   this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Nfc]);
 }
--- a/dom/system/gonk/nfc_consts.js
+++ b/dom/system/gonk/nfc_consts.js
@@ -61,17 +61,15 @@ this.GECKO_NFC_ERROR_SUCCESS            
 this.GECKO_NFC_ERROR_GENERIC_FAILURE     = 1;
 
 // 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_ENABLED            = "nfc.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);