diff --git a/dom/phonenumberutils/PhoneNumber.jsm b/dom/phonenumberutils/PhoneNumber.jsm index 849fe639d2a8..e22941a72481 100644 --- a/dom/phonenumberutils/PhoneNumber.jsm +++ b/dom/phonenumberutils/PhoneNumber.jsm @@ -306,8 +306,9 @@ this.PhoneNumber = (function (dataBase) { // Remove formating characters and whitespace. number = PhoneNumberNormalizer.Normalize(number); - // If there is no defaultRegion, we can't parse international access codes. - if (!defaultRegion && number[0] !== '+') + // If there is no defaultRegion or the defaultRegion is the global region, + // we can't parse international access codes. + if ((!defaultRegion || defaultRegion === '001') && number[0] !== '+') return null; // Detect and strip leading '+'. @@ -317,6 +318,11 @@ this.PhoneNumber = (function (dataBase) { // Lookup the meta data for the given region. var md = FindMetaDataForRegion(defaultRegion.toUpperCase()); + if (!md) { + dump("Couldn't find Meta Data for region: " + defaultRegion + "\n"); + return null; + } + // See if the number starts with an international prefix, and if the // number resulting from stripping the code is valid, then remove the // prefix and flag the number as international. diff --git a/dom/phonenumberutils/PhoneNumberUtils.jsm b/dom/phonenumberutils/PhoneNumberUtils.jsm index 478e6f67905a..a3239a40ac10 100644 --- a/dom/phonenumberutils/PhoneNumberUtils.jsm +++ b/dom/phonenumberutils/PhoneNumberUtils.jsm @@ -29,8 +29,14 @@ XPCOMUtils.defineLazyServiceGetter(this, "gIccService", #endif this.PhoneNumberUtils = { - init: function() { + + initParent: function() { ppmm.addMessageListener(["PhoneNumberService:FuzzyMatch"], this); + this._countryNameCache = Object.create(null); + }, + + initChild: function () { + this._countryNameCache = Object.create(null); }, // 1. See whether we have a network mcc // 2. If we don't have that, look for the simcard mcc @@ -131,13 +137,31 @@ this.PhoneNumberUtils = { }, parseWithMCC: function(aNumber, aMCC) { + if (DEBUG) debug("parseWithMCC " + aNumber + ", " + aMCC); let countryName = MCC_ISO3166_TABLE[aMCC]; if (DEBUG) debug("found country name: " + countryName); return PhoneNumber.Parse(aNumber, countryName); }, - parseWithCountryName: function(aNumber, countryName) { - return PhoneNumber.Parse(aNumber, countryName); + parseWithCountryName: function(aNumber, aCountryName) { + if (this._countryNameCache[aCountryName]) { + return PhoneNumber.Parse(aNumber, aCountryName); + } + + if (Object.keys(this._countryNameCache).length === 0) { + // populate the cache + let keys = Object.keys(MCC_ISO3166_TABLE); + for (let i = 0; i < keys.length; i++) { + this._countryNameCache[MCC_ISO3166_TABLE[keys[i]]] = true; + } + } + + if (!this._countryNameCache[aCountryName]) { + dump("Couldn't find country name: " + aCountryName + "\n"); + return null; + } + + return PhoneNumber.Parse(aNumber, aCountryName); }, isPlainPhoneNumber: function isPlainPhoneNumber(aNumber) { @@ -205,6 +229,8 @@ if (inParent) { XPCOMUtils.defineLazyServiceGetter(this, "ppmm", "@mozilla.org/parentprocessmessagemanager;1", "nsIMessageListenerManager"); - PhoneNumberUtils.init(); + PhoneNumberUtils.initParent(); +} else { + PhoneNumberUtils.initChild(); } diff --git a/dom/phonenumberutils/tests/test_phonenumberutils.xul b/dom/phonenumberutils/tests/test_phonenumberutils.xul index 6303b60210b3..1ee69b7e693d 100644 --- a/dom/phonenumberutils/tests/test_phonenumberutils.xul +++ b/dom/phonenumberutils/tests/test_phonenumberutils.xul @@ -47,6 +47,26 @@ function ParseWithMcc(dial, mcc) { } } +function ParseWithCountryName(dial, countryName) { + var result = PhoneNumberUtils.parseWithCountryName(dial, countryName); + if (result) { + ok(true, "Parses!\n"); + } else { + ok(false, "Should Parse"); + dump("expected: parses"); + } +} + +function CantParseWithCountryName(dial, countryName) { + var result = PhoneNumberUtils.parseWithCountryName(dial, countryName); + if (result) { + ok(false, "Should not Parse"); + dump("expected: does not parse!\n"); + } else { + ok(true, "Expected Parsing error!\n"); + } +} + function FuzzyMatch(number1, number2, expect) { var result = PhoneNumberUtils.fuzzyMatch(number1, number2); is(result, expect, "FuzzyMatch OK!"); @@ -56,6 +76,12 @@ function FuzzyMatch(number1, number2, expect) { CantParseWithMcc("1234", 123); ParseWithMcc("4165555555", 302); +ParseWithCountryName("4155123456", "US"); +CantParseWithCountryName("4155123456", "001"); +CantParseWithCountryName("4155123456", "XXY"); +ParseWithCountryName("4155123456", "US"); + + is(PhoneNumberUtils.normalize("123abc", true), "123", "NumbersOnly"); is(PhoneNumberUtils.normalize("123abc", false), "123222", "NumbersOnly");