addon-sdk/source/lib/sdk/test/tmp-file.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 "use strict";
michael@0 5
michael@0 6 const { Cc, Ci } = require("chrome");
michael@0 7
michael@0 8 const system = require("sdk/system");
michael@0 9 const file = require("sdk/io/file");
michael@0 10 const unload = require("sdk/system/unload");
michael@0 11
michael@0 12 // Retrieve the path to the OS temporary directory:
michael@0 13 const tmpDir = require("sdk/system").pathFor("TmpD");
michael@0 14
michael@0 15 // List of all tmp file created
michael@0 16 let files = [];
michael@0 17
michael@0 18 // Remove all tmp files on addon disabling
michael@0 19 unload.when(function () {
michael@0 20 files.forEach(function (path){
michael@0 21 // Catch exception in order to avoid leaking following files
michael@0 22 try {
michael@0 23 if (file.exists(path))
michael@0 24 file.remove(path);
michael@0 25 }
michael@0 26 catch(e) {
michael@0 27 console.exception(e);
michael@0 28 }
michael@0 29 });
michael@0 30 });
michael@0 31
michael@0 32 // Utility function that synchronously reads local resource from the given
michael@0 33 // `uri` and returns content string. Read in binary mode.
michael@0 34 function readBinaryURI(uri) {
michael@0 35 let ioservice = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 36 let channel = ioservice.newChannel(uri, "UTF-8", null);
michael@0 37 let stream = Cc["@mozilla.org/binaryinputstream;1"].
michael@0 38 createInstance(Ci.nsIBinaryInputStream);
michael@0 39 stream.setInputStream(channel.open());
michael@0 40
michael@0 41 let data = "";
michael@0 42 while (true) {
michael@0 43 let available = stream.available();
michael@0 44 if (available <= 0)
michael@0 45 break;
michael@0 46 data += stream.readBytes(available);
michael@0 47 }
michael@0 48 stream.close();
michael@0 49
michael@0 50 return data;
michael@0 51 }
michael@0 52
michael@0 53 // Create a temporary file from a given string and returns its path
michael@0 54 exports.createFromString = function createFromString(data, tmpName) {
michael@0 55 let filename = (tmpName ? tmpName : "tmp-file") + "-" + (new Date().getTime());
michael@0 56 let path = file.join(tmpDir, filename);
michael@0 57
michael@0 58 let tmpFile = file.open(path, "wb");
michael@0 59 tmpFile.write(data);
michael@0 60 tmpFile.close();
michael@0 61
michael@0 62 // Register tmp file path
michael@0 63 files.push(path);
michael@0 64
michael@0 65 return path;
michael@0 66 }
michael@0 67
michael@0 68 // Create a temporary file from a given URL and returns its path
michael@0 69 exports.createFromURL = function createFromURL(url, tmpName) {
michael@0 70 let data = readBinaryURI(url);
michael@0 71 return exports.createFromString(data, tmpName);
michael@0 72 }
michael@0 73

mercurial