addon-sdk/source/lib/sdk/io/text-streams.js

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 /* 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 module.metadata = {
michael@0 8 "stability": "experimental"
michael@0 9 };
michael@0 10
michael@0 11 const {Cc,Ci,Cu,components} = require("chrome");
michael@0 12 var NetUtil = {};
michael@0 13 Cu.import("resource://gre/modules/NetUtil.jsm", NetUtil);
michael@0 14 NetUtil = NetUtil.NetUtil;
michael@0 15
michael@0 16 // NetUtil.asyncCopy() uses this buffer length, and since we call it, for best
michael@0 17 // performance we use it, too.
michael@0 18 const BUFFER_BYTE_LEN = 0x8000;
michael@0 19 const PR_UINT32_MAX = 0xffffffff;
michael@0 20 const DEFAULT_CHARSET = "UTF-8";
michael@0 21
michael@0 22 exports.TextReader = TextReader;
michael@0 23 exports.TextWriter = TextWriter;
michael@0 24
michael@0 25 /**
michael@0 26 * An input stream that reads text from a backing stream using a given text
michael@0 27 * encoding.
michael@0 28 *
michael@0 29 * @param inputStream
michael@0 30 * The stream is backed by this nsIInputStream. It must already be
michael@0 31 * opened.
michael@0 32 * @param charset
michael@0 33 * Text in inputStream is expected to be in this character encoding. If
michael@0 34 * not given, "UTF-8" is assumed. See nsICharsetConverterManager.idl for
michael@0 35 * documentation on how to determine other valid values for this.
michael@0 36 */
michael@0 37 function TextReader(inputStream, charset) {
michael@0 38 const self = this;
michael@0 39 charset = checkCharset(charset);
michael@0 40
michael@0 41 let stream = Cc["@mozilla.org/intl/converter-input-stream;1"].
michael@0 42 createInstance(Ci.nsIConverterInputStream);
michael@0 43 stream.init(inputStream, charset, BUFFER_BYTE_LEN,
michael@0 44 Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
michael@0 45
michael@0 46 let manager = new StreamManager(this, stream);
michael@0 47
michael@0 48 /**
michael@0 49 * Reads a string from the stream. If the stream is closed, an exception is
michael@0 50 * thrown.
michael@0 51 *
michael@0 52 * @param numChars
michael@0 53 * The number of characters to read. If not given, the remainder of
michael@0 54 * the stream is read.
michael@0 55 * @return The string read. If the stream is already at EOS, returns the
michael@0 56 * empty string.
michael@0 57 */
michael@0 58 this.read = function TextReader_read(numChars) {
michael@0 59 manager.ensureOpened();
michael@0 60
michael@0 61 let readAll = false;
michael@0 62 if (typeof(numChars) === "number")
michael@0 63 numChars = Math.max(numChars, 0);
michael@0 64 else
michael@0 65 readAll = true;
michael@0 66
michael@0 67 let str = "";
michael@0 68 let totalRead = 0;
michael@0 69 let chunkRead = 1;
michael@0 70
michael@0 71 // Read in numChars or until EOS, whichever comes first. Note that the
michael@0 72 // units here are characters, not bytes.
michael@0 73 while (true) {
michael@0 74 let chunk = {};
michael@0 75 let toRead = readAll ?
michael@0 76 PR_UINT32_MAX :
michael@0 77 Math.min(numChars - totalRead, PR_UINT32_MAX);
michael@0 78 if (toRead <= 0 || chunkRead <= 0)
michael@0 79 break;
michael@0 80
michael@0 81 // The converter stream reads in at most BUFFER_BYTE_LEN bytes in a call
michael@0 82 // to readString, enough to fill its byte buffer. chunkRead will be the
michael@0 83 // number of characters encoded by the bytes in that buffer.
michael@0 84 chunkRead = stream.readString(toRead, chunk);
michael@0 85 str += chunk.value;
michael@0 86 totalRead += chunkRead;
michael@0 87 }
michael@0 88
michael@0 89 return str;
michael@0 90 };
michael@0 91 }
michael@0 92
michael@0 93 /**
michael@0 94 * A buffered output stream that writes text to a backing stream using a given
michael@0 95 * text encoding.
michael@0 96 *
michael@0 97 * @param outputStream
michael@0 98 * The stream is backed by this nsIOutputStream. It must already be
michael@0 99 * opened.
michael@0 100 * @param charset
michael@0 101 * Text will be written to outputStream using this character encoding.
michael@0 102 * If not given, "UTF-8" is assumed. See nsICharsetConverterManager.idl
michael@0 103 * for documentation on how to determine other valid values for this.
michael@0 104 */
michael@0 105 function TextWriter(outputStream, charset) {
michael@0 106 const self = this;
michael@0 107 charset = checkCharset(charset);
michael@0 108
michael@0 109 let stream = outputStream;
michael@0 110
michael@0 111 // Buffer outputStream if it's not already.
michael@0 112 let ioUtils = Cc["@mozilla.org/io-util;1"].getService(Ci.nsIIOUtil);
michael@0 113 if (!ioUtils.outputStreamIsBuffered(outputStream)) {
michael@0 114 stream = Cc["@mozilla.org/network/buffered-output-stream;1"].
michael@0 115 createInstance(Ci.nsIBufferedOutputStream);
michael@0 116 stream.init(outputStream, BUFFER_BYTE_LEN);
michael@0 117 }
michael@0 118
michael@0 119 // I'd like to use nsIConverterOutputStream. But NetUtil.asyncCopy(), which
michael@0 120 // we use below in writeAsync(), naturally expects its sink to be an instance
michael@0 121 // of nsIOutputStream, which nsIConverterOutputStream's only implementation is
michael@0 122 // not. So we use uconv and manually convert all strings before writing to
michael@0 123 // outputStream.
michael@0 124 let uconv = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
michael@0 125 createInstance(Ci.nsIScriptableUnicodeConverter);
michael@0 126 uconv.charset = charset;
michael@0 127
michael@0 128 let manager = new StreamManager(this, stream);
michael@0 129
michael@0 130 /**
michael@0 131 * Flushes the backing stream's buffer.
michael@0 132 */
michael@0 133 this.flush = function TextWriter_flush() {
michael@0 134 manager.ensureOpened();
michael@0 135 stream.flush();
michael@0 136 };
michael@0 137
michael@0 138 /**
michael@0 139 * Writes a string to the stream. If the stream is closed, an exception is
michael@0 140 * thrown.
michael@0 141 *
michael@0 142 * @param str
michael@0 143 * The string to write.
michael@0 144 */
michael@0 145 this.write = function TextWriter_write(str) {
michael@0 146 manager.ensureOpened();
michael@0 147 let istream = uconv.convertToInputStream(str);
michael@0 148 let len = istream.available();
michael@0 149 while (len > 0) {
michael@0 150 stream.writeFrom(istream, len);
michael@0 151 len = istream.available();
michael@0 152 }
michael@0 153 istream.close();
michael@0 154 };
michael@0 155
michael@0 156 /**
michael@0 157 * Writes a string on a background thread. After the write completes, the
michael@0 158 * backing stream's buffer is flushed, and both the stream and the backing
michael@0 159 * stream are closed, also on the background thread. If the stream is already
michael@0 160 * closed, an exception is thrown immediately.
michael@0 161 *
michael@0 162 * @param str
michael@0 163 * The string to write.
michael@0 164 * @param callback
michael@0 165 * An optional function. If given, it's called as callback(error) when
michael@0 166 * the write completes. error is an Error object or undefined if there
michael@0 167 * was no error. Inside callback, |this| is the stream object.
michael@0 168 */
michael@0 169 this.writeAsync = function TextWriter_writeAsync(str, callback) {
michael@0 170 manager.ensureOpened();
michael@0 171 let istream = uconv.convertToInputStream(str);
michael@0 172 NetUtil.asyncCopy(istream, stream, function (result) {
michael@0 173 let err = components.isSuccessCode(result) ? undefined :
michael@0 174 new Error("An error occured while writing to the stream: " + result);
michael@0 175 if (err)
michael@0 176 console.error(err);
michael@0 177
michael@0 178 // asyncCopy() closes its output (and input) stream.
michael@0 179 manager.opened = false;
michael@0 180
michael@0 181 if (typeof(callback) === "function") {
michael@0 182 try {
michael@0 183 callback.call(self, err);
michael@0 184 }
michael@0 185 catch (exc) {
michael@0 186 console.exception(exc);
michael@0 187 }
michael@0 188 }
michael@0 189 });
michael@0 190 };
michael@0 191 }
michael@0 192
michael@0 193 // This manages the lifetime of stream, a TextReader or TextWriter. It defines
michael@0 194 // closed and close() on stream and registers an unload listener that closes
michael@0 195 // rawStream if it's still opened. It also provides ensureOpened(), which
michael@0 196 // throws an exception if the stream is closed.
michael@0 197 function StreamManager(stream, rawStream) {
michael@0 198 const self = this;
michael@0 199 this.rawStream = rawStream;
michael@0 200 this.opened = true;
michael@0 201
michael@0 202 /**
michael@0 203 * True iff the stream is closed.
michael@0 204 */
michael@0 205 stream.__defineGetter__("closed", function stream_closed() {
michael@0 206 return !self.opened;
michael@0 207 });
michael@0 208
michael@0 209 /**
michael@0 210 * Closes both the stream and its backing stream. If the stream is already
michael@0 211 * closed, an exception is thrown. For TextWriters, this first flushes the
michael@0 212 * backing stream's buffer.
michael@0 213 */
michael@0 214 stream.close = function stream_close() {
michael@0 215 self.ensureOpened();
michael@0 216 self.unload();
michael@0 217 };
michael@0 218
michael@0 219 require("../system/unload").ensure(this);
michael@0 220 }
michael@0 221
michael@0 222 StreamManager.prototype = {
michael@0 223 ensureOpened: function StreamManager_ensureOpened() {
michael@0 224 if (!this.opened)
michael@0 225 throw new Error("The stream is closed and cannot be used.");
michael@0 226 },
michael@0 227 unload: function StreamManager_unload() {
michael@0 228 // TextWriter.writeAsync() causes rawStream to close and therefore sets
michael@0 229 // opened to false, so check that we're still opened.
michael@0 230 if (this.opened) {
michael@0 231 // Calling close() on both an nsIUnicharInputStream and
michael@0 232 // nsIBufferedOutputStream closes their backing streams. It also forces
michael@0 233 // nsIOutputStreams to flush first.
michael@0 234 this.rawStream.close();
michael@0 235 this.opened = false;
michael@0 236 }
michael@0 237 }
michael@0 238 };
michael@0 239
michael@0 240 function checkCharset(charset) {
michael@0 241 return typeof(charset) === "string" ? charset : DEFAULT_CHARSET;
michael@0 242 }

mercurial