addon-sdk/source/lib/sdk/io/byte-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.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 module.metadata = {
     8   "stability": "experimental"
     9 };
    11 exports.ByteReader = ByteReader;
    12 exports.ByteWriter = ByteWriter;
    14 const {Cc, Ci} = require("chrome");
    16 // This just controls the maximum number of bytes we read in at one time.
    17 const BUFFER_BYTE_LEN = 0x8000;
    19 function ByteReader(inputStream) {
    20   const self = this;
    22   let stream = Cc["@mozilla.org/binaryinputstream;1"].
    23                createInstance(Ci.nsIBinaryInputStream);
    24   stream.setInputStream(inputStream);
    26   let manager = new StreamManager(this, stream);
    28   this.read = function ByteReader_read(numBytes) {
    29     manager.ensureOpened();
    30     if (typeof(numBytes) !== "number")
    31       numBytes = Infinity;
    33     let data = "";
    34     let read = 0;
    35     try {
    36       while (true) {
    37         let avail = stream.available();
    38         let toRead = Math.min(numBytes - read, avail, BUFFER_BYTE_LEN);
    39         if (toRead <= 0)
    40           break;
    41         data += stream.readBytes(toRead);
    42         read += toRead;
    43       }
    44     }
    45     catch (err) {
    46       throw new Error("Error reading from stream: " + err);
    47     }
    49     return data;
    50   };
    51 }
    53 function ByteWriter(outputStream) {
    54   const self = this;
    56   let stream = Cc["@mozilla.org/binaryoutputstream;1"].
    57                createInstance(Ci.nsIBinaryOutputStream);
    58   stream.setOutputStream(outputStream);
    60   let manager = new StreamManager(this, stream);
    62   this.write = function ByteWriter_write(str) {
    63     manager.ensureOpened();
    64     try {
    65       stream.writeBytes(str, str.length);
    66     }
    67     catch (err) {
    68       throw new Error("Error writing to stream: " + err);
    69     }
    70   };
    71 }
    74 // This manages the lifetime of stream, a ByteReader or ByteWriter.  It defines
    75 // closed and close() on stream and registers an unload listener that closes
    76 // rawStream if it's still opened.  It also provides ensureOpened(), which
    77 // throws an exception if the stream is closed.
    78 function StreamManager(stream, rawStream) {
    79   const self = this;
    80   this.rawStream = rawStream;
    81   this.opened = true;
    83   stream.__defineGetter__("closed", function stream_closed() {
    84     return !self.opened;
    85   });
    87   stream.close = function stream_close() {
    88     self.ensureOpened();
    89     self.unload();
    90   };
    92   require("../system/unload").ensure(this);
    93 }
    95 StreamManager.prototype = {
    96   ensureOpened: function StreamManager_ensureOpened() {
    97     if (!this.opened)
    98       throw new Error("The stream is closed and cannot be used.");
    99   },
   100   unload: function StreamManager_unload() {
   101     this.rawStream.close();
   102     this.opened = false;
   103   }
   104 };

mercurial