toolkit/modules/Deprecated.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 this.EXPORTED_SYMBOLS = [ "Deprecated" ];
michael@0 8
michael@0 9 const Cu = Components.utils;
michael@0 10 const Ci = Components.interfaces;
michael@0 11 const PREF_DEPRECATION_WARNINGS = "devtools.errorconsole.deprecation_warnings";
michael@0 12
michael@0 13 Cu.import("resource://gre/modules/Services.jsm");
michael@0 14
michael@0 15 // A flag that indicates whether deprecation warnings should be logged.
michael@0 16 let logWarnings = Services.prefs.getBoolPref(PREF_DEPRECATION_WARNINGS);
michael@0 17
michael@0 18 Services.prefs.addObserver(PREF_DEPRECATION_WARNINGS,
michael@0 19 function (aSubject, aTopic, aData) {
michael@0 20 logWarnings = Services.prefs.getBoolPref(PREF_DEPRECATION_WARNINGS);
michael@0 21 }, false);
michael@0 22
michael@0 23 /**
michael@0 24 * Build a callstack log message.
michael@0 25 *
michael@0 26 * @param nsIStackFrame aStack
michael@0 27 * A callstack to be converted into a string log message.
michael@0 28 */
michael@0 29 function stringifyCallstack (aStack) {
michael@0 30 // If aStack is invalid, use Components.stack (ignoring the last frame).
michael@0 31 if (!aStack || !(aStack instanceof Ci.nsIStackFrame)) {
michael@0 32 aStack = Components.stack.caller;
michael@0 33 }
michael@0 34
michael@0 35 let frame = aStack.caller;
michael@0 36 let msg = "";
michael@0 37 // Get every frame in the callstack.
michael@0 38 while (frame) {
michael@0 39 msg += frame.filename + " " + frame.lineNumber +
michael@0 40 " " + frame.name + "\n";
michael@0 41 frame = frame.caller;
michael@0 42 }
michael@0 43 return msg;
michael@0 44 }
michael@0 45
michael@0 46 const Deprecated = {
michael@0 47 /**
michael@0 48 * Log a deprecation warning.
michael@0 49 *
michael@0 50 * @param string aText
michael@0 51 * Deprecation warning text.
michael@0 52 * @param string aUrl
michael@0 53 * A URL pointing to documentation describing deprecation
michael@0 54 * and the way to address it.
michael@0 55 * @param nsIStackFrame aStack
michael@0 56 * An optional callstack. If it is not provided a
michael@0 57 * snapshot of the current JavaScript callstack will be
michael@0 58 * logged.
michael@0 59 */
michael@0 60 warning: function (aText, aUrl, aStack) {
michael@0 61 if (!logWarnings) {
michael@0 62 return;
michael@0 63 }
michael@0 64
michael@0 65 // If URL is not provided, report an error.
michael@0 66 if (!aUrl) {
michael@0 67 Cu.reportError("Error in Deprecated.warning: warnings must " +
michael@0 68 "provide a URL documenting this deprecation.");
michael@0 69 return;
michael@0 70 }
michael@0 71
michael@0 72 let textMessage = "DEPRECATION WARNING: " + aText +
michael@0 73 "\nYou may find more details about this deprecation at: " +
michael@0 74 aUrl + "\n" +
michael@0 75 // Append a callstack part to the deprecation message.
michael@0 76 stringifyCallstack(aStack);
michael@0 77
michael@0 78 // Report deprecation warning.
michael@0 79 Cu.reportError(textMessage);
michael@0 80 }
michael@0 81 };

mercurial