Bug 965143 - Dispatching an error event instead of throwing an exception in network selection api. r=hsinyi

This commit is contained in:
Edgar Chen 2014-06-09 18:59:49 +08:00
parent 0fc7d66e4a
commit 04f363ddcd
4 changed files with 86 additions and 58 deletions

View File

@ -105,12 +105,19 @@ function testSelectNetworkErrors(aNetworkToSelect, aAnotherNetwork) {
isnot(aNetworkToSelect.longName, mobileConnection.voice.network.longName,
"aNetworkToSelect.longName");
let promise = selectNetworkAndWait(aNetworkToSelect);
// attempt to selectNetwork while one request has already been sent
throwsException(() => mobileConnection.selectNetwork(aAnotherNetwork));
let promises = [];
promises.push(selectNetworkAndWait(aNetworkToSelect));
// attempt to selectNetwork while one request has already been sent, we except
// to get an error here.
promises.push(selectNetwork(aAnotherNetwork)
.then(function resolve() {
ok(false, "should not success");
}, function reject(aError) {
is(aError.name, "AlreadySelectingANetwork", "got an error");
}));
return promise;
return Promise.all(promises);
}
function testSelectExistingNetworkManual(aNetwork) {

View File

@ -441,7 +441,6 @@ function RILContentHelper() {
this.initDOMRequestHelper(/* aWindow */ null, RIL_IPC_MSG_NAMES);
this._windowsMap = [];
this._selectingNetworks = [];
this._mobileConnectionListeners = [];
this._cellBroadcastListeners = [];
this._voicemailListeners = [];
@ -671,12 +670,6 @@ RILContentHelper.prototype = {
})[0];
},
/**
* The networks that are currently trying to be selected (or "automatic").
* This helps ensure that only one network per client is selected at a time.
*/
_selectingNetworks: null,
getNetworks: function(clientId, window) {
if (window == null) {
throw Components.Exception("Can't get window object",
@ -704,36 +697,23 @@ RILContentHelper.prototype = {
Cr.NS_ERROR_UNEXPECTED);
}
if (this._selectingNetworks[clientId]) {
throw new Error("Already selecting a network: " + this._selectingNetworks[clientId]);
}
if (!network) {
throw new Error("Invalid network provided: " + network);
}
if (isNaN(parseInt(network.mnc, 10))) {
throw new Error("Invalid network MNC: " + network.mnc);
}
if (isNaN(parseInt(network.mcc, 10))) {
throw new Error("Invalid network MCC: " + network.mcc);
}
let request = Services.DOMRequest.createRequest(window);
let requestId = this.getRequestId(request);
if (!network ||
isNaN(parseInt(network.mcc, 10)) || isNaN(parseInt(network.mnc, 10))) {
this.dispatchFireRequestError(RIL.GECKO_ERROR_INVALID_PARAMETER);
return request;
}
if (this.rilContexts[clientId].networkSelectionMode == RIL.GECKO_NETWORK_SELECTION_MANUAL &&
this.rilContexts[clientId].voiceConnectionInfo.network === network) {
// Already manually selected this network, so schedule
// onsuccess to be fired on the next tick
this.dispatchFireRequestSuccess(requestId, null);
return request;
}
this._selectingNetworks[clientId] = network;
cpmm.sendAsyncMessage("RIL:SelectNetwork", {
clientId: clientId,
data: {
@ -747,16 +727,11 @@ RILContentHelper.prototype = {
},
selectNetworkAutomatically: function(clientId, window) {
if (window == null) {
throw Components.Exception("Can't get window object",
Cr.NS_ERROR_UNEXPECTED);
}
if (this._selectingNetworks[clientId]) {
throw new Error("Already selecting a network: " + this._selectingNetworks[clientId]);
}
let request = Services.DOMRequest.createRequest(window);
let requestId = this.getRequestId(request);
@ -767,7 +742,6 @@ RILContentHelper.prototype = {
return request;
}
this._selectingNetworks[clientId] = "automatic";
cpmm.sendAsyncMessage("RIL:SelectNetworkAuto", {
clientId: clientId,
data: {
@ -1726,12 +1700,10 @@ RILContentHelper.prototype = {
this.rilContexts[clientId].networkSelectionMode = data.mode;
break;
case "RIL:SelectNetwork":
this.handleSelectNetwork(clientId, data,
RIL.GECKO_NETWORK_SELECTION_MANUAL);
this.handleSimpleRequest(data.requestId, data.errorMsg, null);
break;
case "RIL:SelectNetworkAuto":
this.handleSelectNetwork(clientId, data,
RIL.GECKO_NETWORK_SELECTION_AUTOMATIC);
this.handleSimpleRequest(data.requestId, data.errorMsg, null);
break;
case "RIL:SetPreferredNetworkType":
this.handleSimpleRequest(data.requestId, data.errorMsg, null);
@ -1937,17 +1909,6 @@ RILContentHelper.prototype = {
this.fireRequestSuccess(message.requestId, networks);
},
handleSelectNetwork: function(clientId, message, mode) {
this._selectingNetworks[clientId] = null;
this.rilContexts[clientId].networkSelectionMode = mode;
if (message.errorMsg) {
this.fireRequestError(message.requestId, message.errorMsg);
} else {
this.fireRequestSuccess(message.requestId, null);
}
},
handleIccExchangeAPDU: function(message) {
if (message.errorMsg) {
this.fireRequestError(message.requestId, message.errorMsg);

View File

@ -1941,10 +1941,10 @@ RadioInterface.prototype = {
this.workerMessenger.sendWithIPCMessage(msg, "getAvailableNetworks");
break;
case "RIL:SelectNetwork":
this.workerMessenger.sendWithIPCMessage(msg, "selectNetwork");
this.selectNetwork(msg.target, msg.json.data);
break;
case "RIL:SelectNetworkAuto":
this.workerMessenger.sendWithIPCMessage(msg, "selectNetworkAuto");
this.selectNetworkAuto(msg.target, msg.json.data);
break;
case "RIL:SetPreferredNetworkType":
this.setPreferredNetworkType(msg.target, msg.json.data);
@ -2446,6 +2446,53 @@ RadioInterface.prototype = {
}).bind(this));
},
/**
* The network that is currently trying to be selected (or "automatic").
* This helps ensure that only one network per client is selected at a time.
*/
_selectingNetwork: null,
selectNetwork: function(target, message) {
if (this._selectingNetwork) {
message.errorMsg = "AlreadySelectingANetwork";
target.sendAsyncMessage("RIL:SelectNetwork", {
clientId: this.clientId,
data: message
});
return;
}
this._selectingNetwork = message;
this.workerMessenger.send("selectNetwork", message, (function(response) {
this._selectingNetwork = null;
target.sendAsyncMessage("RIL:SelectNetwork", {
clientId: this.clientId,
data: response
});
return false;
}).bind(this));
},
selectNetworkAuto: function(target, message) {
if (this._selectingNetwork) {
message.errorMsg = "AlreadySelectingANetwork";
target.sendAsyncMessage("RIL:SelectNetworkAuto", {
clientId: this.clientId,
data: message
});
return;
}
this._selectingNetwork = "automatic";
this.workerMessenger.send("selectNetworkAuto", message, (function(response) {
this._selectingNetwork = null;
target.sendAsyncMessage("RIL:SelectNetworkAuto", {
clientId: this.clientId,
data: response
});
}).bind(this));
},
setCellBroadcastSearchList: function(newSearchList) {
if ((newSearchList == this._cellBroadcastSearchList) ||
(newSearchList && this._cellBroadcastSearchList &&

View File

@ -4424,6 +4424,19 @@ RilObject.prototype = {
this.sendChromeMessage(message);
},
_updateNetworkSelectionMode: function(mode) {
if (this.networkSelectionMode === mode) {
return;
}
let options = {
rilMessageType: "networkselectionmodechange",
mode: mode
};
this.networkSelectionMode = mode;
this._sendNetworkInfoMessage(NETWORK_INFO_NETWORK_SELECTION_MODE, options);
},
_processNetworks: function() {
let strings = this.context.Buf.readStringList();
let networks = [];
@ -6145,15 +6158,13 @@ RilObject.prototype[REQUEST_QUERY_NETWORK_SELECTION_MODE] = function REQUEST_QUE
break;
}
if (this.networkSelectionMode != selectionMode) {
this.networkSelectionMode = options.mode = selectionMode;
options.rilMessageType = "networkselectionmodechange";
this._sendNetworkInfoMessage(NETWORK_INFO_NETWORK_SELECTION_MODE, options);
}
this._updateNetworkSelectionMode(selectionMode);
};
RilObject.prototype[REQUEST_SET_NETWORK_SELECTION_AUTOMATIC] = function REQUEST_SET_NETWORK_SELECTION_AUTOMATIC(length, options) {
if (options.rilRequestError) {
options.errorMsg = RIL_ERROR_TO_GECKO_ERROR[options.rilRequestError];
} else {
this._updateNetworkSelectionMode(GECKO_NETWORK_SELECTION_AUTOMATIC);
}
this.sendChromeMessage(options);
@ -6161,6 +6172,8 @@ RilObject.prototype[REQUEST_SET_NETWORK_SELECTION_AUTOMATIC] = function REQUEST_
RilObject.prototype[REQUEST_SET_NETWORK_SELECTION_MANUAL] = function REQUEST_SET_NETWORK_SELECTION_MANUAL(length, options) {
if (options.rilRequestError) {
options.errorMsg = RIL_ERROR_TO_GECKO_ERROR[options.rilRequestError];
} else {
this._updateNetworkSelectionMode(GECKO_NETWORK_SELECTION_MANUAL);
}
this.sendChromeMessage(options);