prefcalls.js (5130B)
1 /* global processLDAPValues */ 2 /* -*- tab-width: 4; indent-tabs-mode: nil; js-indent-level: 4 -*- 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* globals gSandbox */ 8 9 const LDAPSyncQueryContractID = "@mozilla.org/ldapsyncquery;1"; 10 11 var gIsUTF8; 12 13 function getPrefBranch() { 14 return Services.prefs.getBranch(null); 15 } 16 17 function pref(prefName, value) { 18 try { 19 var prefBranch = getPrefBranch(); 20 21 if (typeof value == "string") { 22 if (gIsUTF8) { 23 prefBranch.setStringPref(prefName, value); 24 return; 25 } 26 prefBranch.setCharPref(prefName, value); 27 } else if (typeof value == "number") { 28 prefBranch.setIntPref(prefName, value); 29 } else if (typeof value == "boolean") { 30 prefBranch.setBoolPref(prefName, value); 31 } 32 } catch (e) { 33 displayError("pref", e); 34 } 35 } 36 37 function defaultPref(prefName, value) { 38 try { 39 var prefBranch = Services.prefs.getDefaultBranch(null); 40 if (typeof value == "string") { 41 if (gIsUTF8) { 42 prefBranch.setStringPref(prefName, value); 43 return; 44 } 45 prefBranch.setCharPref(prefName, value); 46 } else if (typeof value == "number") { 47 prefBranch.setIntPref(prefName, value); 48 } else if (typeof value == "boolean") { 49 prefBranch.setBoolPref(prefName, value); 50 } 51 } catch (e) { 52 displayError("defaultPref", e); 53 } 54 } 55 56 function lockPref(prefName, value) { 57 try { 58 var prefBranch = getPrefBranch(); 59 60 if (prefBranch.prefIsLocked(prefName)) { 61 prefBranch.unlockPref(prefName); 62 } 63 64 defaultPref(prefName, value); 65 66 prefBranch.lockPref(prefName); 67 } catch (e) { 68 displayError("lockPref", e); 69 } 70 } 71 72 function unlockPref(prefName) { 73 try { 74 var prefBranch = getPrefBranch(); 75 prefBranch.unlockPref(prefName); 76 } catch (e) { 77 displayError("unlockPref", e); 78 } 79 } 80 81 function getPref(prefName) { 82 try { 83 var prefBranch = getPrefBranch(); 84 85 switch (prefBranch.getPrefType(prefName)) { 86 case prefBranch.PREF_STRING: 87 if (gIsUTF8) { 88 return prefBranch.getStringPref(prefName); 89 } 90 return prefBranch.getCharPref(prefName); 91 92 case prefBranch.PREF_INT: 93 return prefBranch.getIntPref(prefName); 94 95 case prefBranch.PREF_BOOL: 96 return prefBranch.getBoolPref(prefName); 97 default: 98 return null; 99 } 100 } catch (e) { 101 displayError("getPref", e); 102 } 103 return undefined; 104 } 105 106 function clearPref(prefName) { 107 try { 108 var prefBranch = getPrefBranch(); 109 prefBranch.clearUserPref(prefName); 110 } catch (e) {} 111 } 112 113 function setLDAPVersion(_version) { 114 // No longer implemented, but still here to avoid breaking anything. 115 } 116 117 function getLDAPAttributes(host, base, filter, attribs, isSecure) { 118 try { 119 var urlSpec = 120 "ldap" + 121 (isSecure ? "s" : "") + 122 "://" + 123 host + 124 (isSecure ? ":636" : "") + 125 "/" + 126 base + 127 "?" + 128 attribs + 129 "?sub?" + 130 filter; 131 132 // nsILDAP* are only defined in comm-central. 133 // eslint-disable-next-line mozilla/valid-ci-uses 134 var url = Services.io.newURI(urlSpec).QueryInterface(Ci.nsILDAPURL); 135 136 var ldapquery = Cc[LDAPSyncQueryContractID].createInstance( 137 // eslint-disable-next-line mozilla/valid-ci-uses 138 Ci.nsILDAPSyncQuery 139 ); 140 // user supplied method 141 if ("processLDAPValues" in gSandbox) { 142 gSandbox.processLDAPValues(ldapquery.getQueryResults(url)); 143 } else { 144 processLDAPValues(ldapquery.getQueryResults(url)); 145 } 146 } catch (e) { 147 displayError("getLDAPAttributes", e); 148 } 149 } 150 151 function getLDAPValue(str, key) { 152 try { 153 if (str == null || key == null) { 154 return null; 155 } 156 157 var search_key = "\n" + key + "="; 158 159 var start_pos = str.indexOf(search_key); 160 if (start_pos == -1) { 161 return null; 162 } 163 164 start_pos += search_key.length; 165 166 var end_pos = str.indexOf("\n", start_pos); 167 if (end_pos == -1) { 168 end_pos = str.length; 169 } 170 171 return str.substring(start_pos, end_pos); 172 } catch (e) { 173 displayError("getLDAPValue", e); 174 } 175 return undefined; 176 } 177 178 function displayError(funcname, message) { 179 try { 180 var bundle = Services.strings.createBundle( 181 "chrome://autoconfig/locale/autoconfig.properties" 182 ); 183 184 var title = bundle.GetStringFromName("autoConfigTitle"); 185 var msg = bundle.formatStringFromName("autoConfigMsg", [funcname]); 186 Services.prompt.alert(null, title, msg + " " + message); 187 } catch (e) {} 188 } 189 190 function getenv(name) { 191 try { 192 return Services.env.get(name); 193 } catch (e) { 194 displayError("getEnvironment", e); 195 } 196 return undefined; 197 } 198 199 var APIs = { 200 pref, 201 defaultPref, 202 lockPref, 203 unlockPref, 204 getPref, 205 clearPref, 206 setLDAPVersion, 207 getLDAPAttributes, 208 getLDAPValue, 209 displayError, 210 getenv, 211 }; 212 213 for (let [defineAs, func] of Object.entries(APIs)) { 214 Cu.exportFunction(func, gSandbox, { defineAs }); 215 } 216 217 Object.defineProperty(Cu.waiveXrays(gSandbox), "gIsUTF8", { 218 get: Cu.exportFunction(() => gIsUTF8, gSandbox), 219 });