toolkit/identity/LogUtils.jsm

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 /* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */
     2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 "use strict";
     9 this.EXPORTED_SYMBOLS = ["Logger"];
    10 const PREF_DEBUG = "toolkit.identity.debug";
    12 const Cu = Components.utils;
    13 const Ci = Components.interfaces;
    14 const Cc = Components.classes;
    15 const Cr = Components.results;
    17 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    18 Cu.import("resource://gre/modules/Services.jsm");
    20 function IdentityLogger() {
    21   Services.prefs.addObserver(PREF_DEBUG, this, false);
    22   this._debug = Services.prefs.getBoolPref(PREF_DEBUG);
    23   return this;
    24 }
    26 IdentityLogger.prototype = {
    27   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]),
    29   observe: function observe(aSubject, aTopic, aData) {
    30     switch(aTopic) {
    31       case "nsPref:changed":
    32         this._debug = Services.prefs.getBoolPref(PREF_DEBUG);
    33         break;
    35       case "quit-application-granted":
    36         Services.prefs.removeObserver(PREF_DEBUG, this);
    37         break;
    39       default:
    40         this.log("Logger observer", "Unknown topic:", aTopic);
    41         break;
    42     }
    43   },
    45   _generateLogMessage: function _generateLogMessage(aPrefix, args) {
    46     // create a string representation of a list of arbitrary things
    47     let strings = [];
    49     // XXX bug 770418 - args look like flattened array, not list of strings
    51     args.forEach(function(arg) {
    52       if (typeof arg === 'string') {
    53         strings.push(arg);
    54       } else if (typeof arg === 'undefined') {
    55         strings.push('undefined');
    56       } else if (arg === null) {
    57         strings.push('null');
    58       } else {
    59         try {
    60           strings.push(JSON.stringify(arg, null, 2));
    61         } catch(err) {
    62           strings.push("<<something>>");
    63         }
    64       }
    65     });
    66     return 'Identity ' + aPrefix + ': ' + strings.join(' ');
    67   },
    69   /**
    70    * log() - utility function to print a list of arbitrary things
    71    *
    72    * Enable with about:config pref toolkit.identity.debug
    73    */
    74   log: function log(aPrefix, ...args) {
    75     if (!this._debug) {
    76       return;
    77     }
    78     let output = this._generateLogMessage(aPrefix, args);
    79     dump(output + "\n");
    81     // Additionally, make the output visible in the Error Console
    82     Services.console.logStringMessage(output);
    83   },
    85   /**
    86    * reportError() - report an error through component utils as well as
    87    * our log function
    88    */
    89   reportError: function reportError(aPrefix, ...aArgs) {
    90     let prefix = aPrefix + ' ERROR';
    92     // Report the error in the browser
    93     let output = this._generateLogMessage(aPrefix, aArgs);
    94     Cu.reportError(output);
    95     dump("ERROR: " + output + "\n");
    96     for (let frame = Components.stack.caller; frame; frame = frame.caller) {
    97       dump(frame + "\n");
    98     }
    99   }
   101 };
   103 this.Logger = new IdentityLogger();

mercurial