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 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | this.EXPORTED_SYMBOLS = ["ChromeManifestParser"]; |
michael@0 | 8 | |
michael@0 | 9 | const Cc = Components.classes; |
michael@0 | 10 | const Ci = Components.interfaces; |
michael@0 | 11 | const Cr = Components.results; |
michael@0 | 12 | const Cu = Components.utils; |
michael@0 | 13 | |
michael@0 | 14 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 15 | Cu.import("resource://gre/modules/NetUtil.jsm"); |
michael@0 | 16 | |
michael@0 | 17 | const MSG_JAR_FLUSH = "AddonJarFlush"; |
michael@0 | 18 | |
michael@0 | 19 | |
michael@0 | 20 | /** |
michael@0 | 21 | * Sends local and remote notifications to flush a JAR file cache entry |
michael@0 | 22 | * |
michael@0 | 23 | * @param aJarFile |
michael@0 | 24 | * The ZIP/XPI/JAR file as a nsIFile |
michael@0 | 25 | */ |
michael@0 | 26 | function flushJarCache(aJarFile) { |
michael@0 | 27 | Services.obs.notifyObservers(aJarFile, "flush-cache-entry", null); |
michael@0 | 28 | Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageBroadcaster) |
michael@0 | 29 | .broadcastAsyncMessage(MSG_JAR_FLUSH, aJarFile.path); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | |
michael@0 | 33 | /** |
michael@0 | 34 | * Parses chrome manifest files. |
michael@0 | 35 | */ |
michael@0 | 36 | this.ChromeManifestParser = { |
michael@0 | 37 | |
michael@0 | 38 | /** |
michael@0 | 39 | * Reads and parses a chrome manifest file located at a specified URI, and all |
michael@0 | 40 | * secondary manifests it references. |
michael@0 | 41 | * |
michael@0 | 42 | * @param aURI |
michael@0 | 43 | * A nsIURI pointing to a chrome manifest. |
michael@0 | 44 | * Typically a file: or jar: URI. |
michael@0 | 45 | * @return Array of objects describing each manifest instruction, in the form: |
michael@0 | 46 | * { type: instruction-type, baseURI: string-uri, args: [arguments] } |
michael@0 | 47 | **/ |
michael@0 | 48 | parseSync: function CMP_parseSync(aURI) { |
michael@0 | 49 | function parseLine(aLine) { |
michael@0 | 50 | let line = aLine.trim(); |
michael@0 | 51 | if (line.length == 0 || line.charAt(0) == '#') |
michael@0 | 52 | return; |
michael@0 | 53 | let tokens = line.split(/\s+/); |
michael@0 | 54 | let type = tokens.shift(); |
michael@0 | 55 | if (type == "manifest") { |
michael@0 | 56 | let uri = NetUtil.newURI(tokens.shift(), null, aURI); |
michael@0 | 57 | data = data.concat(this.parseSync(uri)); |
michael@0 | 58 | } else { |
michael@0 | 59 | data.push({type: type, baseURI: baseURI, args: tokens}); |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | let contents = ""; |
michael@0 | 64 | try { |
michael@0 | 65 | if (aURI.scheme == "jar") |
michael@0 | 66 | contents = this._readFromJar(aURI); |
michael@0 | 67 | else |
michael@0 | 68 | contents = this._readFromFile(aURI); |
michael@0 | 69 | } catch (e) { |
michael@0 | 70 | // Silently fail. |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | if (!contents) |
michael@0 | 74 | return []; |
michael@0 | 75 | |
michael@0 | 76 | let baseURI = NetUtil.newURI(".", null, aURI).spec; |
michael@0 | 77 | |
michael@0 | 78 | let data = []; |
michael@0 | 79 | let lines = contents.split("\n"); |
michael@0 | 80 | lines.forEach(parseLine.bind(this)); |
michael@0 | 81 | return data; |
michael@0 | 82 | }, |
michael@0 | 83 | |
michael@0 | 84 | _readFromJar: function CMP_readFromJar(aURI) { |
michael@0 | 85 | let data = ""; |
michael@0 | 86 | let entries = []; |
michael@0 | 87 | let readers = []; |
michael@0 | 88 | |
michael@0 | 89 | try { |
michael@0 | 90 | // Deconstrict URI, which can be nested jar: URIs. |
michael@0 | 91 | let uri = aURI.clone(); |
michael@0 | 92 | while (uri instanceof Ci.nsIJARURI) { |
michael@0 | 93 | entries.push(uri.JAREntry); |
michael@0 | 94 | uri = uri.JARFile; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | // Open the base jar. |
michael@0 | 98 | let reader = Cc["@mozilla.org/libjar/zip-reader;1"]. |
michael@0 | 99 | createInstance(Ci.nsIZipReader); |
michael@0 | 100 | reader.open(uri.QueryInterface(Ci.nsIFileURL).file); |
michael@0 | 101 | readers.push(reader); |
michael@0 | 102 | |
michael@0 | 103 | // Open the nested jars. |
michael@0 | 104 | for (let i = entries.length - 1; i > 0; i--) { |
michael@0 | 105 | let innerReader = Cc["@mozilla.org/libjar/zip-reader;1"]. |
michael@0 | 106 | createInstance(Ci.nsIZipReader); |
michael@0 | 107 | innerReader.openInner(reader, entries[i]); |
michael@0 | 108 | readers.push(innerReader); |
michael@0 | 109 | reader = innerReader; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | // First entry is the actual file we want to read. |
michael@0 | 113 | let zis = reader.getInputStream(entries[0]); |
michael@0 | 114 | data = NetUtil.readInputStreamToString(zis, zis.available()); |
michael@0 | 115 | } |
michael@0 | 116 | finally { |
michael@0 | 117 | // Close readers in reverse order. |
michael@0 | 118 | for (let i = readers.length - 1; i >= 0; i--) { |
michael@0 | 119 | readers[i].close(); |
michael@0 | 120 | flushJarCache(readers[i].file); |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | return data; |
michael@0 | 125 | }, |
michael@0 | 126 | |
michael@0 | 127 | _readFromFile: function CMP_readFromFile(aURI) { |
michael@0 | 128 | let file = aURI.QueryInterface(Ci.nsIFileURL).file; |
michael@0 | 129 | if (!file.exists() || !file.isFile()) |
michael@0 | 130 | return ""; |
michael@0 | 131 | |
michael@0 | 132 | let data = ""; |
michael@0 | 133 | let fis = Cc["@mozilla.org/network/file-input-stream;1"]. |
michael@0 | 134 | createInstance(Ci.nsIFileInputStream); |
michael@0 | 135 | try { |
michael@0 | 136 | fis.init(file, -1, -1, false); |
michael@0 | 137 | data = NetUtil.readInputStreamToString(fis, fis.available()); |
michael@0 | 138 | } finally { |
michael@0 | 139 | fis.close(); |
michael@0 | 140 | } |
michael@0 | 141 | return data; |
michael@0 | 142 | }, |
michael@0 | 143 | |
michael@0 | 144 | /** |
michael@0 | 145 | * Detects if there were any instructions of a specified type in a given |
michael@0 | 146 | * chrome manifest. |
michael@0 | 147 | * |
michael@0 | 148 | * @param aManifest |
michael@0 | 149 | * Manifest data, as returned by ChromeManifestParser.parseSync(). |
michael@0 | 150 | * @param aType |
michael@0 | 151 | * Instruction type to filter by. |
michael@0 | 152 | * @return True if any matching instructions were found in the manifest. |
michael@0 | 153 | */ |
michael@0 | 154 | hasType: function CMP_hasType(aManifest, aType) { |
michael@0 | 155 | return aManifest.some(function hasType_matchEntryType(aEntry) { |
michael@0 | 156 | return aEntry.type == aType; |
michael@0 | 157 | }); |
michael@0 | 158 | } |
michael@0 | 159 | }; |