Bug 890909 - [Contacts API] migration code problem when matching using the substringMatching pref (eg in Brazil). r=gwagner

This commit is contained in:
Michael Henretty 2013-07-18 16:17:41 -04:00
parent d74a2f4a72
commit be57d7fb57
9 changed files with 84 additions and 58 deletions

View File

@ -421,7 +421,6 @@ pref("services.push.udp.port", 2442);
// NetworkStats
#ifdef MOZ_B2G_RIL
pref("dom.mozNetworkStats.enabled", true);
pref("ril.lastKnownMcc", "724");
pref("ril.cellbroadcast.disabled", false);
#endif

View File

@ -18,7 +18,7 @@ Cu.import("resource://gre/modules/IndexedDBHelper.jsm");
Cu.import("resource://gre/modules/PhoneNumberUtils.jsm");
const DB_NAME = "contacts";
const DB_VERSION = 12;
const DB_VERSION = 13;
const STORE_NAME = "contacts";
const SAVED_GETALL_STORE_NAME = "getallcache";
const CHUNK_SIZE = 20;
@ -456,6 +456,42 @@ ContactDB.prototype = {
next();
}
};
},
function upgrade12to13() {
if (DEBUG) debug("Add phone substring to the search index if appropriate for country");
if (this.substringMatching) {
if (!objectStore) {
objectStore = aTransaction.objectStore(STORE_NAME);
}
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
if (cursor.value.properties.tel) {
cursor.value.search.parsedTel = cursor.value.search.parsedTel || [];
cursor.value.properties.tel.forEach(
function(tel) {
let normalized = PhoneNumberUtils.normalize(tel.value.toString());
if (normalized) {
if (this.substringMatching && normalized.length > this.substringMatching) {
let sub = normalized.slice(-this.substringMatching);
if (cursor.value.search.parsedTel.indexOf(sub) === -1) {
if (DEBUG) debug("Adding substring index: " + tel + ", " + sub);
cursor.value.search.parsedTel.push(sub);
}
}
}
}.bind(this)
);
cursor.update(cursor.value);
}
cursor.continue();
} else {
next();
}
}.bind(this);
} else {
next();
}
}
];
@ -472,7 +508,7 @@ ContactDB.prototype = {
try {
var i = index++;
if (DEBUG) debug("Upgrade step: " + i + "\n");
steps[i]();
steps[i].call(outer);
} catch(ex) {
dump("Caught exception" + ex);
aTransaction.abort();
@ -1001,9 +1037,15 @@ ContactDB.prototype = {
// Enable special phone number substring matching. Does not update existing DB entries.
enableSubstringMatching: function enableSubstringMatching(aDigits) {
if (DEBUG) debug("MCC enabling substring matching " + aDigits);
this.substringMatching = aDigits;
},
disableSubstringMatching: function disableSubstringMatching() {
if (DEBUG) debug("MCC disabling substring matching");
delete this.substringMatching;
},
init: function init(aGlobal) {
this.initDBHelper(DB_NAME, DB_VERSION, [STORE_NAME, SAVED_GETALL_STORE_NAME, REVISION_STORE], aGlobal);
}

View File

@ -43,17 +43,10 @@ let ContactService = {
this._db = new ContactDB(myGlobal);
this._db.init(myGlobal);
let countryName = PhoneNumberUtils.getCountryName();
if (Services.prefs.getPrefType("dom.phonenumber.substringmatching." + countryName) == Ci.nsIPrefBranch.PREF_INT) {
if (DEBUG) debug("Enable Substring Matching for Phone Numbers: " + countryName);
let val = Services.prefs.getIntPref("dom.phonenumber.substringmatching." + countryName);
if (val && val > 0) {
this._db.enableSubstringMatching(val);
}
}
this.configureSubstringMatching();
Services.obs.addObserver(this, "profile-before-change", false);
Services.prefs.addObserver("dom.phonenumber.substringmatching", this, false);
Services.prefs.addObserver("ril.lastKnownSimMcc", this, false);
},
observe: function(aSubject, aTopic, aData) {
@ -71,16 +64,23 @@ let ContactService = {
this._db = null;
this._children = null;
this._cursors = null;
} else if (aTopic === 'nsPref:changed' && aData.contains("dom.phonenumber.substringmatching")) {
// We don't fully support changing substringMatching during runtime. This is mostly for testing.
let countryName = PhoneNumberUtils.getCountryName();
if (Services.prefs.getPrefType("dom.phonenumber.substringmatching." + countryName) == Ci.nsIPrefBranch.PREF_INT) {
let val = Services.prefs.getIntPref("dom.phonenumber.substringmatching." + countryName);
if (val && val > 0) {
this._db.enableSubstringMatching(val);
}
} else if (aTopic === 'nsPref:changed' && aData === "ril.lastKnownSimMcc") {
this.configureSubstringMatching();
}
},
configureSubstringMatching: function() {
let countryName = PhoneNumberUtils.getCountryName();
if (Services.prefs.getPrefType("dom.phonenumber.substringmatching." + countryName) == Ci.nsIPrefBranch.PREF_INT) {
let val = Services.prefs.getIntPref("dom.phonenumber.substringmatching." + countryName);
if (val) {
this._db.enableSubstringMatching(val);
return;
}
}
// if we got here, we dont have a substring setting
// for this country, so disable substring matching
this._db.disableSubstringMatching();
},
assertPermission: function(aMessage, aPerm) {

View File

@ -27,6 +27,7 @@ if (SpecialPowers.isMainProcess()) {
var substringLength = 8;
SpecialPowers.setIntPref("dom.phonenumber.substringmatching.BR", substringLength);
SpecialPowers.setCharPref("ril.lastKnownSimMcc", "724");
SpecialPowers.addPermission("contacts-write", true, document);
SpecialPowers.addPermission("contacts-read", true, document);
@ -258,4 +259,4 @@ addLoadEvent(next);
</script>
</pre>
</body>
</html>
</html>

View File

@ -297,7 +297,7 @@ interface nsIDOMMozMobileConnection : nsIDOMEventTarget
[implicit_jscontext] attribute jsval oncfstatechange;
};
[scriptable, uuid(c9d9ff61-a2f0-41cd-b478-9cefa7b31f31)]
[scriptable, uuid(49706beb-a160-40b7-b745-50f62e389a2c)]
interface nsIDOMMozMobileConnectionInfo : nsISupports
{
/**
@ -332,11 +332,6 @@ interface nsIDOMMozMobileConnectionInfo : nsISupports
*/
readonly attribute nsIDOMMozMobileNetworkInfo network;
/**
* Mobile Country Code (MCC) of last known network operator.
*/
readonly attribute DOMString lastKnownMcc;
/**
* Type of connection.
*

View File

@ -36,7 +36,6 @@ function testConnectionInfo() {
is(voice.emergencyCallsOnly, false);
is(voice.roaming, false);
isAndroidNetwork(voice.network);
is(voice.lastKnownMcc, "310");
let data = connection.data;
// data.connected = true means there's an active data call which we
@ -45,7 +44,6 @@ function testConnectionInfo() {
is(data.emergencyCallsOnly, false);
is(data.roaming, false);
isAndroidNetwork(data.network);
is(data.lastKnownMcc, null);
testGetNetworks();
}

View File

@ -24,7 +24,7 @@ XPCOMUtils.defineLazyServiceGetter(this, "mobileConnection",
this.PhoneNumberUtils = {
// 1. See whether we have a network mcc
// 2. If we don't have that, look for the simcard mcc
// 3. TODO: If we don't have that or its 0 (not activated), pick up the last used mcc
// 3. If we don't have that or its 0 (not activated), pick up the last used mcc
// 4. If we don't have, default to some mcc
// mcc for Brasil
@ -36,19 +36,22 @@ this.PhoneNumberUtils = {
#ifdef MOZ_B2G_RIL
// Get network mcc
if (mobileConnection.voiceConnectionInfo &&
mobileConnection.voiceConnectionInfo.network) {
mcc = mobileConnection.voiceConnectionInfo.network.mcc;
let voice = mobileConnection.voiceConnectionInfo;
if (voice && voice.network && voice.network.mcc) {
mcc = voice.network.mcc;
}
// Get SIM mcc
if (!mcc) {
mcc = mobileConnection.iccInfo.mcc;
let iccInfo = mobileConnection.iccInfo;
if (!mcc && iccInfo.mcc) {
mcc = iccInfo.mcc;
}
// Get previous mcc
if (!mcc && mobileConnection.voiceConnectionInfo) {
mcc = mobileConnection.voiceConnectionInfo.lastKnownMcc;
// Attempt to grab last known sim mcc from prefs
if (!mcc) {
try {
mcc = Services.prefs.getCharPref("ril.lastKnownSimMcc");
} catch (e) {}
}
// Set to default mcc

View File

@ -181,7 +181,6 @@ MobileConnectionInfo.prototype = {
emergencyCallsOnly: false,
roaming: false,
network: null,
lastKnownMcc: null,
cell: null,
type: null,
signalStrength: null,

View File

@ -650,7 +650,6 @@ function RadioInterface(options) {
emergencyCallsOnly: false,
roaming: false,
network: null,
lastKnownMcc: null,
cell: null,
type: null,
signalStrength: null,
@ -659,18 +658,12 @@ function RadioInterface(options) {
emergencyCallsOnly: false,
roaming: false,
network: null,
lastKnownMcc: null,
cell: null,
type: null,
signalStrength: null,
relSignalStrength: null},
};
try {
this.rilContext.voice.lastKnownMcc =
Services.prefs.getCharPref("ril.lastKnownMcc");
} catch (e) {}
this.voicemailInfo = {
number: null,
displayName: null
@ -1346,18 +1339,6 @@ RadioInterface.prototype = {
let data = this.rilContext.data;
if (this.networkChanged(message, voice.network)) {
// Update lastKnownMcc.
if (message.mcc) {
voice.lastKnownMcc = message.mcc;
// Update pref if mcc is changed.
// !voice.network is in case voice.network is still null.
if (!voice.network || voice.network.mcc != message.mcc) {
try {
Services.prefs.setCharPref("ril.lastKnownMcc", message.mcc);
} catch (e) {}
}
}
// Update lastKnownNetwork
if (message.mcc && message.mnc) {
try {
@ -2171,6 +2152,14 @@ RadioInterface.prototype = {
gMessageManager.sendIccMessage("RIL:IccInfoChanged",
this.clientId, message);
// Update lastKnownSimMcc.
if (message.mcc) {
try {
Services.prefs.setCharPref("ril.lastKnownSimMcc",
message.mcc.toString());
} catch (e) {}
}
// Update lastKnownHomeNetwork.
if (message.mcc && message.mnc) {
try {