Bug 1043186 - Part 2: onpeerlost should be fired to correct target. r=dimi

This commit is contained in:
Yoshi Huang 2014-07-24 18:11:42 +08:00
Родитель 8b0b298d5b
Коммит b5aed2d79f
5 изменённых файлов: 78 добавлений и 100 удалений

Просмотреть файл

@ -223,26 +223,22 @@ mozNfc.prototype = {
eventListenerWasAdded: function(evt) {
let eventType = this.getEventType(evt);
if (eventType == -1)
if (eventType != NFC_PEER_EVENT_READY) {
return;
this.registerTarget(eventType);
}
let appId = this._window.document.nodePrincipal.appId;
this._nfcContentHelper.registerTargetForPeerReady(this._window, appId);
},
eventListenerWasRemoved: function(evt) {
let eventType = this.getEventType(evt);
if (eventType == -1)
if (eventType != NFC_PEER_EVENT_READY) {
return;
this.unregisterTarget(eventType);
},
}
registerTarget: function registerTarget(event) {
let appId = this._window.document.nodePrincipal.appId;
this._nfcContentHelper.registerTargetForPeerEvent(this._window, appId, event);
},
unregisterTarget: function unregisterTarget(event) {
let appId = this._window.document.nodePrincipal.appId;
this._nfcContentHelper.unregisterTargetForPeerEvent(this._window, appId, event);
this._nfcContentHelper.unregisterTargetForPeerReady(this._window, appId);
},
notifyPeerReady: function notifyPeerReady(sessionToken) {

Просмотреть файл

@ -12,8 +12,6 @@ function peerReadyCb(evt) {
let peer = nfc.getNFCPeer(evt.detail);
ok(peer instanceof MozNFCPeer, "Should get a NFCPeer object.");
// reset callback.
nfc.onpeerready = null;
NCI.deactivate();
}
@ -21,6 +19,9 @@ function peerLostCb(evt) {
log("peerLostCb called");
ok(evt.detail === undefined, "evt.detail should be undefined");
ok(true);
// reset callback.
nfc.onpeerready = null;
nfc.onpeerlost = null;
toggleNFC(false).then(runNextTest);
}
@ -94,10 +95,25 @@ function testCheckNfcPeerObjForInvalidToken() {
toggleNFC(false).then(runNextTest);
}
function testPeerLostShouldNotBeCalled() {
nfc.onpeerlost = function () {
ok(false, "onpeerlost shouldn't be called");
};
toggleNFC(true)
.then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0))
.then(NCI.deactivate)
.then(() => toggleNFC(false));
nfc.onpeerlost = null;
runNextTest();
}
let tests = [
testPeerReady,
testCheckP2PRegFailure,
testCheckNfcPeerObjForInvalidToken
testCheckNfcPeerObjForInvalidToken,
testPeerLostShouldNotBeCalled
];
SpecialPowers.pushPermissions(

Просмотреть файл

@ -62,8 +62,8 @@ const NFC_IPC_WRITE_PERM_MSG_NAMES = [
"NFC:WriteNDEF",
"NFC:MakeReadOnlyNDEF",
"NFC:SendFile",
"NFC:RegisterPeerTarget",
"NFC:UnregisterPeerTarget"
"NFC:RegisterPeerReadyTarget",
"NFC:UnregisterPeerReadyTarget"
];
const NFC_IPC_MANAGER_PERM_MSG_NAMES = [
@ -224,52 +224,28 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
}
},
registerPeerTarget: function registerPeerTarget(msg) {
registerPeerReadyTarget: function registerPeerReadyTarget(msg) {
let appInfo = msg.json;
// Sanity check on PeerEvent
if (!this.isValidPeerEvent(appInfo.event)) {
return;
}
let targets = this.peerTargetsMap;
let targetInfo = targets[appInfo.appId];
// If the application Id is already registered
if (targetInfo) {
// If the event is not registered
if (targetInfo.event !== appInfo.event) {
// Update the event field ONLY
targetInfo.event |= appInfo.event;
}
// Otherwise event is already registered, return!
return;
}
// Target not registered yet! Add to the target map
// Registered targetInfo target consists of 2 fields (values)
// target : Target to notify the right content for peer notifications
// event : NFC_PEER_EVENT_READY (0x01) Or NFC_PEER_EVENT_LOST (0x02)
// Target not registered yet! Add to the target map
let newTargetInfo = { target : msg.target,
event : appInfo.event };
isPeerReadyCalled: false };
targets[appInfo.appId] = newTargetInfo;
},
unregisterPeerTarget: function unregisterPeerTarget(msg) {
unregisterPeerReadyTarget: function unregisterPeerReadyTarget(msg) {
let appInfo = msg.json;
// Sanity check on PeerEvent
if (!this.isValidPeerEvent(appInfo.event)) {
return;
}
let targets = this.peerTargetsMap;
let targetInfo = targets[appInfo.appId];
if (targetInfo) {
// Application Id registered and the event exactly matches.
if (targetInfo.event === appInfo.event) {
// Remove the target from the list of registered targets
delete targets[appInfo.appId]
}
else {
// Otherwise, update the event field ONLY, by removing the event flag
targetInfo.event &= ~appInfo.event;
}
// Remove the target from the list of registered targets
delete targets[appInfo.appId]
}
},
@ -284,39 +260,29 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
});
},
isRegisteredP2PTarget: function isRegisteredP2PTarget(appId, event) {
isPeerReadyTarget: function isPeerReadyTarget(appId) {
let targetInfo = this.peerTargetsMap[appId];
// Check if it is a registered target for the 'event'
return ((targetInfo != null) && ((targetInfo.event & event) !== 0));
return (targetInfo != null);
},
isPeerReadyCalled: function isPeerReadyCalled(appId) {
let targetInfo = this.peerTargetsMap[appId];
return targetInfo.IsPeerReadyCalled;
},
notifyPeerEvent: function notifyPeerEvent(appId, event) {
let targetInfo = this.peerTargetsMap[appId];
// Check if the application id is a registeredP2PTarget
if (this.isRegisteredP2PTarget(appId, event)) {
targetInfo.target.sendAsyncMessage("NFC:PeerEvent", {
event: event,
sessionToken: this.nfc.sessionTokenMap[this.nfc._currentSessionId]
});
return;
}
debug("Application ID : " + appId + " is not a registered target" +
"for the event " + event + " notification");
},
isValidPeerEvent: function isValidPeerEvent(event) {
// Valid values : 0x01, 0x02 Or 0x03
return ((event === NFC.NFC_PEER_EVENT_READY) ||
(event === NFC.NFC_PEER_EVENT_LOST) ||
(event === (NFC.NFC_PEER_EVENT_READY | NFC.NFC_PEER_EVENT_LOST)));
targetInfo.target.sendAsyncMessage("NFC:PeerEvent", {
event: event,
sessionToken: this.nfc.sessionTokenMap[this.nfc._currentSessionId]
});
},
checkP2PRegistration: function checkP2PRegistration(msg) {
// Check if the session and application id yeild a valid registered
// target. It should have registered for NFC_PEER_EVENT_READY
let isValid = !!this.nfc.sessionTokenMap[this.nfc._currentSessionId] &&
this.isRegisteredP2PTarget(msg.json.appId,
NFC.NFC_PEER_EVENT_READY);
this.isPeerReadyTarget(msg.json.appId);
// Remember the current AppId if registered.
this.currentPeerAppId = (isValid) ? msg.json.appId : null;
@ -378,17 +344,24 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
debug("Registering target for this SessionToken : " +
this.nfc.sessionTokenMap[this.nfc._currentSessionId]);
return NFC.NFC_SUCCESS;
case "NFC:RegisterPeerTarget":
this.registerPeerTarget(msg);
case "NFC:RegisterPeerReadyTarget":
this.registerPeerReadyTarget(msg);
return null;
case "NFC:UnregisterPeerTarget":
this.unregisterPeerTarget(msg);
case "NFC:UnregisterPeerReadyTarget":
this.unregisterPeerReadyTarget(msg);
return null;
case "NFC:CheckP2PRegistration":
this.checkP2PRegistration(msg);
return null;
case "NFC:NotifyUserAcceptedP2P":
// Notify the 'NFC_PEER_EVENT_READY' since user has acknowledged
if (!this.isPeerReadyTarget(msg.json.appId)) {
debug("Application ID : " + appId + " is not a registered PeerReadytarget");
return null;
}
let targetInfo = this.peerTargetsMap[msg.json.appId];
targetInfo.IsPeerReadyCalled = true;
this.notifyPeerEvent(msg.json.appId, NFC.NFC_PEER_EVENT_READY);
return null;
case "NFC:NotifySendFileStatus":
@ -530,8 +503,15 @@ Nfc.prototype = {
delete message.sessionId;
gSystemMessenger.broadcastMessage("nfc-manager-tech-lost", message);
// Notify 'PeerLost' to appropriate registered target, if any
gMessageManager.notifyPeerEvent(gMessageManager.currentPeerAppId, NFC.NFC_PEER_EVENT_LOST);
let appId = gMessageManager.currentPeerAppId;
// For peerlost, the message is delievered to the target which
// registered onpeerready and onpeerready has been called before.
if (gMessageManager.isPeerReadyTarget(appId) && gMessageManager.isPeerReadyCalled(appId)) {
gMessageManager.notifyPeerEvent(appId, NFC.NFC_PEER_EVENT_LOST);
}
delete this.sessionTokenMap[this._currentSessionId];
this._currentSessionId = null;
this.currentPeerAppId = null;

Просмотреть файл

@ -261,30 +261,22 @@ NfcContentHelper.prototype = {
this.peerEventListener = listener;
},
registerTargetForPeerEvent:
function registerTargetForPeerEvent(window, appId, event) {
registerTargetForPeerReady: function registerTargetForPeerReady(window, appId) {
if (window == null) {
throw Components.Exception("Can't get window object",
Cr.NS_ERROR_UNEXPECTED);
}
cpmm.sendAsyncMessage("NFC:RegisterPeerTarget", {
appId: appId,
event: event
});
cpmm.sendAsyncMessage("NFC:RegisterPeerReadyTarget", { appId: appId });
},
unregisterTargetForPeerEvent:
function unregisterTargetForPeerEvent(window, appId, event) {
unregisterTargetForPeerReady: function unregisterTargetForPeerReady(window, appId) {
if (window == null) {
throw Components.Exception("Can't get window object",
Cr.NS_ERROR_UNEXPECTED);
}
cpmm.sendAsyncMessage("NFC:UnregisterPeerTarget", {
appId: appId,
event: event
});
cpmm.sendAsyncMessage("NFC:UnregisterPeerReadyTarget", { appId: appId });
},
checkP2PRegistration: function checkP2PRegistration(window, appId) {

Просмотреть файл

@ -27,7 +27,7 @@ interface nsINfcPeerEventListener : nsISupports
void notifyPeerLost(in DOMString sessionToken);
};
[scriptable, uuid(21559f98-526c-44c8-94fb-2095a112c47c)]
[scriptable, uuid(5bea28d3-67ee-4916-8c8e-4f35e3666e61)]
interface nsINfcContentHelper : nsISupports
{
const long NFC_EVENT_PEER_READY = 0x01;
@ -79,13 +79,10 @@ interface nsINfcContentHelper : nsISupports
*
* @param appId
* Application ID to be registered
*
* @param event
* Event to be registered. Either NFC_EVENT_PEER_READY or NFC_EVENT_PEER_LOST
*/
void registerTargetForPeerEvent(in nsIDOMWindow window,
in unsigned long appId,
in octet event);
void registerTargetForPeerReady(in nsIDOMWindow window,
in unsigned long appId);
/**
* Unregister the given application id with Chrome process
*
@ -94,13 +91,10 @@ interface nsINfcContentHelper : nsISupports
*
* @param appId
* Application ID to be registered
*
* @param event
* Event to be unregistered. Either NFC_EVENT_PEER_READY or NFC_EVENT_PEER_LOST
*/
void unregisterTargetForPeerEvent(in nsIDOMWindow window,
in unsigned long appId,
in octet event);
void unregisterTargetForPeerReady(in nsIDOMWindow window,
in unsigned long appId);
/**
* Checks if the given application's id is a registered peer target (with the Chrome process)
*