Bug 1165841 - Part 2: move window-related code to nsNfc.js. r=dimi

This commit is contained in:
Yoshi Huang 2015-05-20 17:06:38 +08:00
parent badd3e8271
commit 7cb626527c
3 changed files with 63 additions and 59 deletions

View File

@ -71,6 +71,7 @@ function NfcContentHelper() {
Services.obs.addObserver(this, "xpcom-shutdown", false);
this._requestMap = [];
this.initDOMRequestHelper(/* window */ null, NFC_IPC_MSG_NAMES);
}
NfcContentHelper.prototype = {
@ -88,56 +89,13 @@ NfcContentHelper.prototype = {
Ci.nsINfcBrowserAPI]
}),
_window: null,
_requestMap: null,
_rfState: null,
_tabId: null,
eventListener: null,
init: function init(aWindow) {
if (aWindow == null) {
throw Components.Exception("Can't get window object",
Cr.NS_ERROR_UNEXPECTED);
}
this._window = aWindow;
this.initDOMRequestHelper(this._window, NFC_IPC_MSG_NAMES);
if (!NFC.DEBUG_CONTENT_HELPER && this._window.navigator.mozSettings) {
let lock = this._window.navigator.mozSettings.createLock();
var nfcDebug = lock.get(NFC.SETTING_NFC_DEBUG);
nfcDebug.onsuccess = function _nfcDebug() {
DEBUG = nfcDebug.result[NFC.SETTING_NFC_DEBUG];
updateDebug();
};
}
let info = cpmm.sendSyncMessage("NFC:QueryInfo")[0];
this._rfState = info.rfState;
// For now, we assume app will run in oop mode so we can get
// tab id for each app. Fix bug 1116449 if we are going to
// support in-process mode.
let docShell = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell);
try {
this._tabId = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsITabChild)
.tabId;
} catch(e) {
// Only parent process does not have tab id, so in this case
// NfcContentHelper is used by system app. Use -1(tabId) to
// indicate its system app.
let inParent = Cc["@mozilla.org/xre/app-info;1"]
.getService(Ci.nsIXULRuntime)
.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
if (inParent) {
this._tabId = Ci.nsINfcBrowserAPI.SYSTEM_APP_ID;
} else {
throw Components.Exception("Can't get tab id in child process",
Cr.NS_ERROR_UNEXPECTED);
}
}
},
queryRFState: function queryRFState() {
@ -224,9 +182,21 @@ NfcContentHelper.prototype = {
});
},
addEventListener: function addEventListener(listener) {
addEventListener: function addEventListener(listener, tabId) {
let _window = listener.window;
// TODO Bug 1166210 - enable NFC debug for child process.
if (!NFC.DEBUG_CONTENT_HELPER && _window.navigator.mozSettings) {
let lock = _window.navigator.mozSettings.createLock();
var nfcDebug = lock.get(NFC.SETTING_NFC_DEBUG);
nfcDebug.onsuccess = function _nfcDebug() {
DEBUG = nfcDebug.result[NFC.SETTING_NFC_DEBUG];
updateDebug();
};
}
this.eventListener = listener;
cpmm.sendAsyncMessage("NFC:AddEventListener", { tabId: this._tabId });
cpmm.sendAsyncMessage("NFC:AddEventListener", { tabId: tabId });
},
registerTargetForPeerReady: function registerTargetForPeerReady(appId) {
@ -399,16 +369,7 @@ NfcContentHelper.prototype = {
return;
}
let ndefMsg = new this._window.Array();
let records = result.records;
for (let i = 0; i < records.length; i++) {
let record = records[i];
ndefMsg.push(new this._window.MozNDEFRecord({tnf: record.tnf,
type: record.type,
id: record.id,
payload: record.payload}));
}
callback.notifySuccessWithNDEFRecords(ndefMsg);
callback.notifySuccessWithNDEFRecords(result.records);
},
handleCheckP2PRegistrationResponse: function handleCheckP2PRegistrationResponse(result) {

View File

@ -124,7 +124,7 @@ interface nsINfcBrowserAPI : nsISupports
in boolean isFocus);
};
[scriptable, uuid(9cf841c9-0347-4564-99e5-18c0f74eca4d)]
[scriptable, uuid(080a84f1-f039-46ed-9dc6-f34ce3aa9638)]
interface nsINfcContentHelper : nsISupports
{
void init(in nsIDOMWindow window);
@ -233,8 +233,10 @@ interface nsINfcContentHelper : nsISupports
*
* @param listener
* An instance of the nsINfcEventListener.
* @param tabId
* The tab ID of the listener.
*/
void addEventListener(in nsINfcEventListener listener);
void addEventListener(in nsINfcEventListener listener, in uint64_t tabId);
/**
* Register the given application id with parent process

View File

@ -73,7 +73,16 @@ NfcCallback.prototype = {
debug("can not find promise resolver for id: " + this._requestId);
return;
}
resolver.resolve(aRecords);
let records = new this._window.Array();
for (let i = 0; i < aRecords.length; i++) {
let record = aRecords[i];
records.push(new this._window.MozNDEFRecord({tnf: record.tnf,
type: record.type,
id: record.id,
payload: record.payload}));
}
resolver.resolve(records);
},
notifySuccessWithByteArray: function notifySuccessWithByteArray(aArray) {
@ -335,6 +344,7 @@ function MozNFCImpl() {
MozNFCImpl.prototype = {
_nfcContentHelper: null,
window: null,
_tabId: null,
_rfState: null,
_contentObj: null,
nfcPeer: null,
@ -352,11 +362,42 @@ MozNFCImpl.prototype = {
if (this._nfcContentHelper) {
this._nfcContentHelper.init(aWindow);
this._nfcContentHelper.addEventListener(this);
this._tabId = this.getTabId(aWindow);
this._nfcContentHelper.addEventListener(this, this._tabId);
this._rfState = this._nfcContentHelper.queryRFState();
}
},
getTabId: function getTabId(aWindow) {
let tabId;
// For now, we assume app will run in oop mode so we can get
// tab id for each app. Fix bug 1116449 if we are going to
// support in-process mode.
let docShell = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell);
try {
tabId = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsITabChild)
.tabId;
} catch(e) {
// Only parent process does not have tab id, so in this case
// NfcContentHelper is used by system app. Use -1(tabId) to
// indicate its system app.
let inParent = Cc["@mozilla.org/xre/app-info;1"]
.getService(Ci.nsIXULRuntime)
.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
if (inParent) {
tabId = Ci.nsINfcBrowserAPI.SYSTEM_APP_ID;
} else {
throw Components.Exception("Can't get tab id in child process",
Cr.NS_ERROR_UNEXPECTED);
}
}
return tabId;
},
// Only apps which have nfc-manager permission can call the following interfaces
// 'checkP2PRegistration' , 'notifyUserAcceptedP2P' , 'notifySendFileStatus',
// 'startPoll', 'stopPoll', and 'powerOff'.