summaryrefslogtreecommitdiffstats
path: root/toolkit/components/addoncompat/CompatWarning.jsm
blob: b32409a46b6b74278e4355481f3745f1a4f54333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

this.EXPORTED_SYMBOLS = ["CompatWarning"];

const Ci = Components.interfaces;
const Cc = Components.classes;
const Cu = Components.utils;

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Preferences.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "console",
                                  "resource://gre/modules/Console.jsm");

function section(number, url)
{
  const baseURL = "https://developer.mozilla.org/en-US/Firefox/Multiprocess_Firefox/Limitations_of_chrome_scripts";
  return { number, url: baseURL + url };
}

var CompatWarning = {
  // Sometimes we want to generate a warning, but put off issuing it
  // until later. For example, if someone registers a listener, we
  // might only want to warn about it if the listener actually
  // fires. However, we want the warning to show a stack for the
  // registration site.
  delayedWarning: function(msg, addon, warning) {
    function isShimLayer(filename) {
      return filename.indexOf("CompatWarning.jsm") != -1 ||
        filename.indexOf("RemoteAddonsParent.jsm") != -1 ||
        filename.indexOf("RemoteAddonsChild.jsm") != -1 ||
        filename.indexOf("multiprocessShims.js") != -1;
    }

    let stack = Components.stack;
    while (stack && isShimLayer(stack.filename))
      stack = stack.caller;

    let alreadyWarned = false;

    return function() {
      if (alreadyWarned) {
        return;
      }
      alreadyWarned = true;

      if (addon) {
        let histogram = Services.telemetry.getKeyedHistogramById("ADDON_SHIM_USAGE");
        histogram.add(addon, warning ? warning.number : 0);
      }

      if (!Preferences.get("dom.ipc.shims.enabledWarnings", false))
        return;

      let error = Cc['@mozilla.org/scripterror;1'].createInstance(Ci.nsIScriptError);
      if (!error || !Services.console) {
        // Too late during shutdown to use the nsIConsole
        return;
      }

      let message = `Warning: ${msg}`;
      if (warning)
        message += `\nMore info at: ${warning.url}`;

      error.init(
                 /* message*/ message,
                 /* sourceName*/ stack ? stack.filename : "",
                 /* sourceLine*/ stack ? stack.sourceLine : "",
                 /* lineNumber*/ stack ? stack.lineNumber : 0,
                 /* columnNumber*/ 0,
                 /* flags*/ Ci.nsIScriptError.warningFlag,
                 /* category*/ "chrome javascript");
      Services.console.logMessage(error);

      if (Preferences.get("dom.ipc.shims.dumpWarnings", false)) {
        dump(message + "\n");
        while (stack) {
          dump(stack + "\n");
          stack = stack.caller;
        }
        dump("\n");
      }
    };
  },

  warn: function(msg, addon, warning) {
    let delayed = this.delayedWarning(msg, addon, warning);
    delayed();
  },

  warnings: {
    content: section(1, "#gBrowser.contentWindow.2C_window.content..."),
    limitations_of_CPOWs: section(2, "#Limitations_of_CPOWs"),
    nsIContentPolicy: section(3, "#nsIContentPolicy"),
    nsIWebProgressListener: section(4, "#nsIWebProgressListener"),
    observers: section(5, "#Observers_in_the_chrome_process"),
    DOM_events: section(6, "#DOM_Events"),
    sandboxes: section(7, "#Sandboxes"),
    JSMs: section(8, "#JavaScript_code_modules_(JSMs)"),
    nsIAboutModule: section(9, "#nsIAboutModule"),
    // If more than 14 values appear here, you need to change the
    // ADDON_SHIM_USAGE histogram definition in Histograms.json.
  },
};