dom/phonenumberutils/PhoneNumberUtils.jsm

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
michael@0 3
michael@0 4 "use strict";
michael@0 5
michael@0 6 this.EXPORTED_SYMBOLS = ["PhoneNumberUtils"];
michael@0 7
michael@0 8 const DEBUG = false;
michael@0 9 function debug(s) { if(DEBUG) dump("-*- PhoneNumberutils: " + s + "\n"); }
michael@0 10
michael@0 11 const Cu = Components.utils;
michael@0 12 const Cc = Components.classes;
michael@0 13 const Ci = Components.interfaces;
michael@0 14
michael@0 15 Cu.import("resource://gre/modules/Services.jsm");
michael@0 16 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
michael@0 17 Cu.import("resource://gre/modules/PhoneNumberNormalizer.jsm");
michael@0 18 Cu.import("resource://gre/modules/mcc_iso3166_table.jsm");
michael@0 19
michael@0 20 #ifdef MOZ_B2G_RIL
michael@0 21 XPCOMUtils.defineLazyServiceGetter(this, "mobileConnection",
michael@0 22 "@mozilla.org/ril/content-helper;1",
michael@0 23 "nsIMobileConnectionProvider");
michael@0 24 XPCOMUtils.defineLazyServiceGetter(this, "icc",
michael@0 25 "@mozilla.org/ril/content-helper;1",
michael@0 26 "nsIIccProvider");
michael@0 27 #endif
michael@0 28
michael@0 29 this.PhoneNumberUtils = {
michael@0 30 init: function() {
michael@0 31 ppmm.addMessageListener(["PhoneNumberService:FuzzyMatch"], this);
michael@0 32 },
michael@0 33 // 1. See whether we have a network mcc
michael@0 34 // 2. If we don't have that, look for the simcard mcc
michael@0 35 // 3. If we don't have that or its 0 (not activated), pick up the last used mcc
michael@0 36 // 4. If we don't have, default to some mcc
michael@0 37
michael@0 38 // mcc for Brasil
michael@0 39 _mcc: '724',
michael@0 40
michael@0 41 getCountryName: function getCountryName() {
michael@0 42 let mcc;
michael@0 43 let countryName;
michael@0 44
michael@0 45 #ifdef MOZ_B2G_RIL
michael@0 46 // TODO: Bug 926740 - PhoneNumberUtils for multisim
michael@0 47 // In Multi-sim, there is more than one client in
michael@0 48 // iccProvider/mobileConnectionProvider. Each client represents a
michael@0 49 // icc/mobileConnection service. To maintain the backward compatibility with
michael@0 50 // single sim, we always use client 0 for now. Adding support for multiple
michael@0 51 // sim will be addressed in bug 926740, if needed.
michael@0 52 let clientId = 0;
michael@0 53
michael@0 54 // Get network mcc
michael@0 55 let voice = mobileConnection.getVoiceConnectionInfo(clientId);
michael@0 56 if (voice && voice.network && voice.network.mcc) {
michael@0 57 mcc = voice.network.mcc;
michael@0 58 }
michael@0 59
michael@0 60 // Get SIM mcc
michael@0 61 let iccInfo = icc.getIccInfo(clientId);
michael@0 62 if (!mcc && iccInfo && iccInfo.mcc) {
michael@0 63 mcc = iccInfo.mcc;
michael@0 64 }
michael@0 65
michael@0 66 // Attempt to grab last known sim mcc from prefs
michael@0 67 if (!mcc) {
michael@0 68 try {
michael@0 69 mcc = Services.prefs.getCharPref("ril.lastKnownSimMcc");
michael@0 70 } catch (e) {}
michael@0 71 }
michael@0 72
michael@0 73 // Set to default mcc
michael@0 74 if (!mcc) {
michael@0 75 mcc = this._mcc;
michael@0 76 }
michael@0 77 #else
michael@0 78
michael@0 79 // Attempt to grab last known sim mcc from prefs
michael@0 80 if (!mcc) {
michael@0 81 try {
michael@0 82 mcc = Services.prefs.getCharPref("ril.lastKnownSimMcc");
michael@0 83 } catch (e) {}
michael@0 84 }
michael@0 85
michael@0 86 if (!mcc) {
michael@0 87 mcc = this._mcc;
michael@0 88 }
michael@0 89 #endif
michael@0 90
michael@0 91 countryName = MCC_ISO3166_TABLE[mcc];
michael@0 92 if (DEBUG) debug("MCC: " + mcc + "countryName: " + countryName);
michael@0 93 return countryName;
michael@0 94 },
michael@0 95
michael@0 96 parse: function(aNumber) {
michael@0 97 if (DEBUG) debug("call parse: " + aNumber);
michael@0 98 let result = PhoneNumber.Parse(aNumber, this.getCountryName());
michael@0 99
michael@0 100 if (result) {
michael@0 101 let countryName = result.countryName || this.getCountryName();
michael@0 102 let number = null;
michael@0 103 if (countryName) {
michael@0 104 if (Services.prefs.getPrefType("dom.phonenumber.substringmatching." + countryName) == Ci.nsIPrefBranch.PREF_INT) {
michael@0 105 let val = Services.prefs.getIntPref("dom.phonenumber.substringmatching." + countryName);
michael@0 106 if (val) {
michael@0 107 number = result.internationalNumber || result.nationalNumber;
michael@0 108 if (number && number.length > val) {
michael@0 109 number = number.slice(-val);
michael@0 110 }
michael@0 111 }
michael@0 112 }
michael@0 113 }
michael@0 114 Object.defineProperty(result, "nationalMatchingFormat", { value: number, enumerable: true });
michael@0 115 if (DEBUG) {
michael@0 116 debug("InternationalFormat: " + result.internationalFormat);
michael@0 117 debug("InternationalNumber: " + result.internationalNumber);
michael@0 118 debug("NationalNumber: " + result.nationalNumber);
michael@0 119 debug("NationalFormat: " + result.nationalFormat);
michael@0 120 debug("CountryName: " + result.countryName);
michael@0 121 debug("NationalMatchingFormat: " + result.nationalMatchingFormat);
michael@0 122 }
michael@0 123 } else if (DEBUG) {
michael@0 124 debug("NO PARSING RESULT!");
michael@0 125 }
michael@0 126 return result;
michael@0 127 },
michael@0 128
michael@0 129 parseWithMCC: function(aNumber, aMCC) {
michael@0 130 let countryName = MCC_ISO3166_TABLE[aMCC];
michael@0 131 if (DEBUG) debug("found country name: " + countryName);
michael@0 132 return PhoneNumber.Parse(aNumber, countryName);
michael@0 133 },
michael@0 134
michael@0 135 parseWithCountryName: function(aNumber, countryName) {
michael@0 136 return PhoneNumber.Parse(aNumber, countryName);
michael@0 137 },
michael@0 138
michael@0 139 isPlainPhoneNumber: function isPlainPhoneNumber(aNumber) {
michael@0 140 var isPlain = PhoneNumber.IsPlain(aNumber);
michael@0 141 if (DEBUG) debug("isPlain(" + aNumber + ") " + isPlain);
michael@0 142 return isPlain;
michael@0 143 },
michael@0 144
michael@0 145 normalize: function Normalize(aNumber, aNumbersOnly) {
michael@0 146 let normalized = PhoneNumberNormalizer.Normalize(aNumber, aNumbersOnly);
michael@0 147 if (DEBUG) debug("normalize(" + aNumber + "): " + normalized + ", " + aNumbersOnly);
michael@0 148 return normalized;
michael@0 149 },
michael@0 150
michael@0 151 fuzzyMatch: function fuzzyMatch(aNumber1, aNumber2) {
michael@0 152 let normalized1 = this.normalize(aNumber1);
michael@0 153 let normalized2 = this.normalize(aNumber2);
michael@0 154 if (DEBUG) debug("Normalized Number1: " + normalized1 + ", Number2: " + normalized2);
michael@0 155 if (normalized1 === normalized2) {
michael@0 156 return true;
michael@0 157 }
michael@0 158 let parsed1 = this.parse(aNumber1);
michael@0 159 let parsed2 = this.parse(aNumber2);
michael@0 160 if (parsed1 && parsed2) {
michael@0 161 if ((parsed1.internationalNumber && parsed1.internationalNumber === parsed2.internationalNumber)
michael@0 162 || (parsed1.nationalNumber && parsed1.nationalNumber === parsed2.nationalNumber)) {
michael@0 163 return true;
michael@0 164 }
michael@0 165 }
michael@0 166 let countryName = this.getCountryName();
michael@0 167 let ssPref = "dom.phonenumber.substringmatching." + countryName;
michael@0 168 if (Services.prefs.getPrefType(ssPref) == Ci.nsIPrefBranch.PREF_INT) {
michael@0 169 let val = Services.prefs.getIntPref(ssPref);
michael@0 170 if (normalized1.length > val && normalized2.length > val
michael@0 171 && normalized1.slice(-val) === normalized2.slice(-val)) {
michael@0 172 return true;
michael@0 173 }
michael@0 174 }
michael@0 175 return false;
michael@0 176 },
michael@0 177
michael@0 178 receiveMessage: function(aMessage) {
michael@0 179 if (DEBUG) debug("receiveMessage " + aMessage.name);
michael@0 180 let mm = aMessage.target;
michael@0 181 let msg = aMessage.data;
michael@0 182
michael@0 183 switch (aMessage.name) {
michael@0 184 case "PhoneNumberService:FuzzyMatch":
michael@0 185 mm.sendAsyncMessage("PhoneNumberService:FuzzyMatch:Return:OK", {
michael@0 186 requestID: msg.requestID,
michael@0 187 result: this.fuzzyMatch(msg.options.number1, msg.options.number2)
michael@0 188 });
michael@0 189 break;
michael@0 190 default:
michael@0 191 if (DEBUG) debug("WRONG MESSAGE NAME: " + aMessage.name);
michael@0 192 }
michael@0 193 }
michael@0 194 };
michael@0 195
michael@0 196 let inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
michael@0 197 .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
michael@0 198 if (inParent) {
michael@0 199 Cu.import("resource://gre/modules/PhoneNumber.jsm");
michael@0 200 XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
michael@0 201 "@mozilla.org/parentprocessmessagemanager;1",
michael@0 202 "nsIMessageListenerManager");
michael@0 203 PhoneNumberUtils.init();
michael@0 204 }
michael@0 205

mercurial