addon-sdk/source/lib/sdk/console/traceback.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 const { Cc, Ci, components } = require("chrome");
    12 const { parseStack, sourceURI } = require("toolkit/loader");
    13 const { readURISync } = require("../net/url");
    15 exports.sourceURI = sourceURI
    17 function safeGetFileLine(path, line) {
    18   try {
    19     var scheme = require("../url").URL(path).scheme;
    20     // TODO: There should be an easier, more accurate way to figure out
    21     // what's the case here.
    22     if (!(scheme == "http" || scheme == "https"))
    23       return readURISync(path).split("\n")[line - 1];
    24   } catch (e) {}
    25   return null;
    26 }
    28 function nsIStackFramesToJSON(frame) {
    29   var stack = [];
    31   while (frame) {
    32     if (frame.filename) {
    33       stack.unshift({
    34         fileName: sourceURI(frame.filename),
    35         lineNumber: frame.lineNumber,
    36         name: frame.name
    37       });
    38     }
    39     frame = frame.caller;
    40   }
    42   return stack;
    43 };
    45 var fromException = exports.fromException = function fromException(e) {
    46   if (e instanceof Ci.nsIException)
    47     return nsIStackFramesToJSON(e.location);
    48   if (e.stack && e.stack.length)
    49     return parseStack(e.stack);
    50   if (e.fileName && typeof(e.lineNumber == "number"))
    51     return [{fileName: sourceURI(e.fileName),
    52              lineNumber: e.lineNumber,
    53              name: null}];
    54   return [];
    55 };
    57 var get = exports.get = function get() {
    58   return nsIStackFramesToJSON(components.stack.caller);
    59 };
    61 var format = exports.format = function format(tbOrException) {
    62   if (tbOrException === undefined) {
    63     tbOrException = get();
    64     tbOrException.pop();
    65   }
    67   var tb;
    68   if (typeof(tbOrException) == "object" &&
    69       tbOrException.constructor.name == "Array")
    70     tb = tbOrException;
    71   else
    72     tb = fromException(tbOrException);
    74   var lines = ["Traceback (most recent call last):"];
    76   tb.forEach(
    77     function(frame) {
    78       if (!(frame.fileName || frame.lineNumber || frame.name))
    79       	return;
    81       lines.push('  File "' + frame.fileName + '", line ' +
    82                  frame.lineNumber + ', in ' + frame.name);
    83       var sourceLine = safeGetFileLine(frame.fileName, frame.lineNumber);
    84       if (sourceLine)
    85         lines.push('    ' + sourceLine.trim());
    86     });
    88   return lines.join("\n");
    89 };

mercurial