Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | |
michael@0 | 6 | const Cc = Components.classes; |
michael@0 | 7 | const Ci = Components.interfaces; |
michael@0 | 8 | const Cr = Components.results; |
michael@0 | 9 | const Cu = Components.utils; |
michael@0 | 10 | |
michael@0 | 11 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | function INIProcessorFactory() { |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | INIProcessorFactory.prototype = { |
michael@0 | 17 | classID: Components.ID("{6ec5f479-8e13-4403-b6ca-fe4c2dca14fd}"), |
michael@0 | 18 | QueryInterface : XPCOMUtils.generateQI([Ci.nsIINIParserFactory]), |
michael@0 | 19 | |
michael@0 | 20 | createINIParser : function (aINIFile) { |
michael@0 | 21 | return new INIProcessor(aINIFile); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | }; // end of INIProcessorFactory implementation |
michael@0 | 25 | |
michael@0 | 26 | const MODE_WRONLY = 0x02; |
michael@0 | 27 | const MODE_CREATE = 0x08; |
michael@0 | 28 | const MODE_TRUNCATE = 0x20; |
michael@0 | 29 | |
michael@0 | 30 | // nsIINIParser implementation |
michael@0 | 31 | function INIProcessor(aFile) { |
michael@0 | 32 | this._iniFile = aFile; |
michael@0 | 33 | this._iniData = {}; |
michael@0 | 34 | this._readFile(); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | INIProcessor.prototype = { |
michael@0 | 38 | QueryInterface : XPCOMUtils.generateQI([Ci.nsIINIParser, Ci.nsIINIParserWriter]), |
michael@0 | 39 | |
michael@0 | 40 | __utf8Converter : null, // UCS2 <--> UTF8 string conversion |
michael@0 | 41 | get _utf8Converter() { |
michael@0 | 42 | if (!this.__utf8Converter) { |
michael@0 | 43 | this.__utf8Converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. |
michael@0 | 44 | createInstance(Ci.nsIScriptableUnicodeConverter); |
michael@0 | 45 | this.__utf8Converter.charset = "UTF-8"; |
michael@0 | 46 | } |
michael@0 | 47 | return this.__utf8Converter; |
michael@0 | 48 | }, |
michael@0 | 49 | |
michael@0 | 50 | __utf16leConverter : null, // UCS2 <--> UTF16LE string conversion |
michael@0 | 51 | get _utf16leConverter() { |
michael@0 | 52 | if (!this.__utf16leConverter) { |
michael@0 | 53 | this.__utf16leConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. |
michael@0 | 54 | createInstance(Ci.nsIScriptableUnicodeConverter); |
michael@0 | 55 | this.__utf16leConverter.charset = "UTF-16LE"; |
michael@0 | 56 | } |
michael@0 | 57 | return this.__utf16leConverter; |
michael@0 | 58 | }, |
michael@0 | 59 | |
michael@0 | 60 | _utfConverterReset : function() { |
michael@0 | 61 | this.__utf8Converter = null; |
michael@0 | 62 | this.__utf16leConverter = null; |
michael@0 | 63 | }, |
michael@0 | 64 | |
michael@0 | 65 | _iniFile : null, |
michael@0 | 66 | _iniData : null, |
michael@0 | 67 | |
michael@0 | 68 | /* |
michael@0 | 69 | * Reads the INI file and stores the data internally. |
michael@0 | 70 | */ |
michael@0 | 71 | _readFile : function() { |
michael@0 | 72 | // If file doesn't exist, there's nothing to do. |
michael@0 | 73 | if (!this._iniFile.exists() || 0 == this._iniFile.fileSize) |
michael@0 | 74 | return; |
michael@0 | 75 | |
michael@0 | 76 | let iniParser = Cc["@mozilla.org/xpcom/ini-parser-factory;1"] |
michael@0 | 77 | .getService(Ci.nsIINIParserFactory).createINIParser(this._iniFile); |
michael@0 | 78 | for (let section in XPCOMUtils.IterStringEnumerator(iniParser.getSections())) { |
michael@0 | 79 | this._iniData[section] = {}; |
michael@0 | 80 | for (let key in XPCOMUtils.IterStringEnumerator(iniParser.getKeys(section))) { |
michael@0 | 81 | this._iniData[section][key] = iniParser.getString(section, key); |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | }, |
michael@0 | 85 | |
michael@0 | 86 | // nsIINIParser |
michael@0 | 87 | |
michael@0 | 88 | getSections : function() { |
michael@0 | 89 | let sections = []; |
michael@0 | 90 | for (let section in this._iniData) |
michael@0 | 91 | sections.push(section); |
michael@0 | 92 | return new stringEnumerator(sections); |
michael@0 | 93 | }, |
michael@0 | 94 | |
michael@0 | 95 | getKeys : function(aSection) { |
michael@0 | 96 | let keys = []; |
michael@0 | 97 | if (aSection in this._iniData) |
michael@0 | 98 | for (let key in this._iniData[aSection]) |
michael@0 | 99 | keys.push(key); |
michael@0 | 100 | return new stringEnumerator(keys); |
michael@0 | 101 | }, |
michael@0 | 102 | |
michael@0 | 103 | getString : function(aSection, aKey) { |
michael@0 | 104 | if (!(aSection in this._iniData)) |
michael@0 | 105 | throw Cr.NS_ERROR_FAILURE; |
michael@0 | 106 | if (!(aKey in this._iniData[aSection])) |
michael@0 | 107 | throw Cr.NS_ERROR_FAILURE; |
michael@0 | 108 | return this._iniData[aSection][aKey]; |
michael@0 | 109 | }, |
michael@0 | 110 | |
michael@0 | 111 | |
michael@0 | 112 | // nsIINIParserWriter |
michael@0 | 113 | |
michael@0 | 114 | setString : function(aSection, aKey, aValue) { |
michael@0 | 115 | const isSectionIllegal = /[\0\r\n\[\]]/; |
michael@0 | 116 | const isKeyValIllegal = /[\0\r\n=]/; |
michael@0 | 117 | |
michael@0 | 118 | if (isSectionIllegal.test(aSection)) |
michael@0 | 119 | throw Components.Exception("bad character in section name", |
michael@0 | 120 | Cr.ERROR_ILLEGAL_VALUE); |
michael@0 | 121 | if (isKeyValIllegal.test(aKey) || isKeyValIllegal.test(aValue)) |
michael@0 | 122 | throw Components.Exception("bad character in key/value", |
michael@0 | 123 | Cr.ERROR_ILLEGAL_VALUE); |
michael@0 | 124 | |
michael@0 | 125 | if (!(aSection in this._iniData)) |
michael@0 | 126 | this._iniData[aSection] = {}; |
michael@0 | 127 | |
michael@0 | 128 | this._iniData[aSection][aKey] = aValue; |
michael@0 | 129 | }, |
michael@0 | 130 | |
michael@0 | 131 | writeFile : function(aFile, aFlags) { |
michael@0 | 132 | |
michael@0 | 133 | let converter; |
michael@0 | 134 | function writeLine(data) { |
michael@0 | 135 | data += "\n"; |
michael@0 | 136 | data = converter.ConvertFromUnicode(data); |
michael@0 | 137 | data += converter.Finish(); |
michael@0 | 138 | outputStream.write(data, data.length); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | if (!aFile) |
michael@0 | 142 | aFile = this._iniFile; |
michael@0 | 143 | |
michael@0 | 144 | let safeStream = Cc["@mozilla.org/network/safe-file-output-stream;1"]. |
michael@0 | 145 | createInstance(Ci.nsIFileOutputStream); |
michael@0 | 146 | safeStream.init(aFile, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, |
michael@0 | 147 | 0600, null); |
michael@0 | 148 | |
michael@0 | 149 | var outputStream = Cc["@mozilla.org/network/buffered-output-stream;1"]. |
michael@0 | 150 | createInstance(Ci.nsIBufferedOutputStream); |
michael@0 | 151 | outputStream.init(safeStream, 8192); |
michael@0 | 152 | outputStream.QueryInterface(Ci.nsISafeOutputStream); // for .finish() |
michael@0 | 153 | |
michael@0 | 154 | if (Ci.nsIINIParserWriter.WRITE_UTF16 == aFlags |
michael@0 | 155 | && 'nsIWindowsRegKey' in Ci) { |
michael@0 | 156 | outputStream.write("\xFF\xFE", 2); |
michael@0 | 157 | converter = this._utf16leConverter; |
michael@0 | 158 | } else { |
michael@0 | 159 | converter = this._utf8Converter; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | for (let section in this._iniData) { |
michael@0 | 163 | writeLine("[" + section + "]"); |
michael@0 | 164 | for (let key in this._iniData[section]) { |
michael@0 | 165 | writeLine(key + "=" + this._iniData[section][key]); |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | outputStream.finish(); |
michael@0 | 170 | } |
michael@0 | 171 | }; |
michael@0 | 172 | |
michael@0 | 173 | function stringEnumerator(stringArray) { |
michael@0 | 174 | this._strings = stringArray; |
michael@0 | 175 | } |
michael@0 | 176 | stringEnumerator.prototype = { |
michael@0 | 177 | QueryInterface : XPCOMUtils.generateQI([Ci.nsIUTF8StringEnumerator]), |
michael@0 | 178 | |
michael@0 | 179 | _strings : null, |
michael@0 | 180 | _enumIndex: 0, |
michael@0 | 181 | |
michael@0 | 182 | hasMore : function() { |
michael@0 | 183 | return (this._enumIndex < this._strings.length); |
michael@0 | 184 | }, |
michael@0 | 185 | |
michael@0 | 186 | getNext : function() { |
michael@0 | 187 | return this._strings[this._enumIndex++]; |
michael@0 | 188 | } |
michael@0 | 189 | }; |
michael@0 | 190 | |
michael@0 | 191 | let component = [INIProcessorFactory]; |
michael@0 | 192 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component); |