Bug 1058440 - B2G NFC: enable debug when NFC debug is enabled. r=allstars.chh

This commit is contained in:
Dimi Lee 2014-08-29 19:13:51 +08:00
parent 9def110412
commit aaa8026ad0
3 changed files with 74 additions and 14 deletions

View File

@ -23,6 +23,10 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/DOMRequestHelper.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);
@ -36,13 +40,16 @@ const NFC_ENABLED = libcutils.property_get("ro.moz.nfc.enabled", "false") === "t
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}");
@ -77,6 +84,11 @@ GetDetailsNDEFResponse.prototype = {
function NfcContentHelper() {
this.initDOMRequestHelper(/* aWindow */ null, NFC_IPC_MSG_NAMES);
let lock = gSettingsService.createLock();
lock.get(NFC.SETTING_NFC_DEBUG, this);
Services.obs.addObserver(this, NFC.TOPIC_MOZSETTINGS_CHANGED, false);
Services.obs.addObserver(this, "xpcom-shutdown", false);
this._requestMap = [];
@ -355,8 +367,14 @@ NfcContentHelper.prototype = {
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) {
let setting = JSON.parse(data);
if (setting) {
this.handle(setting.key, setting.value);
}
}
},
@ -427,6 +445,15 @@ NfcContentHelper.prototype = {
}
},
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) {

View File

@ -22,6 +22,10 @@ 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);
@ -35,13 +39,16 @@ const NFC_ENABLED = libcutils.property_get("ro.moz.nfc.enabled", "false") === "t
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 =
@ -98,6 +105,10 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
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();
},
@ -106,6 +117,7 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
this.nfc.shutdown();
this.nfc = null;
Services.obs.removeObserver(this, NFC.TOPIC_MOZSETTINGS_CHANGED);
Services.obs.removeObserver(this, NFC.TOPIC_XPCOM_SHUTDOWN);
this._unregisterMessageListeners();
},
@ -318,6 +330,12 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
observe: function observe(subject, topic, data) {
switch (topic) {
case NFC.TOPIC_MOZSETTINGS_CHANGED:
let setting = JSON.parse(data);
if (setting) {
this.nfc.handle(setting.key, setting.value);
}
break;
case NFC.TOPIC_XPCOM_SHUTDOWN:
this._shutdown();
break;
@ -581,6 +599,18 @@ Nfc.prototype = {
return null;
},
/**
* nsISettingsServiceCallback
*/
handle: function handle(name, result) {
switch (name) {
case NFC.SETTING_NFC_DEBUG:
DEBUG = result;
updateDebug();
break;
}
},
/**
* nsIObserver interface methods.
*/

View File

@ -105,8 +105,11 @@ 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;