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 const STORAGE_MAX_EVENTS = 1000;
7 var _consoleStorage = new Map();
9 // NOTE: these listeners used to just be added as observers and notified via
10 // Services.obs.notifyObservers. However, that has enough overhead to be a
11 // problem for this. Using an explicit global array is much cheaper, and
12 // should be equivalent.
13 var _logEventListeners = [];
15 const CONSOLEAPISTORAGE_CID = Components.ID(
16 "{96cf7855-dfa9-4c6d-8276-f9705b4890f2}"
20 * The ConsoleAPIStorage is meant to cache window.console API calls for later
21 * reuse by other components when needed. For example, the Web Console code can
22 * display the cached messages when it opens for the active tab.
24 * ConsoleAPI messages are stored as they come from the ConsoleAPI code, with
25 * all their properties. They are kept around until the inner window object that
26 * created the messages is destroyed. Messages are indexed by the inner window
30 * let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
31 * .getService(Ci.nsIConsoleAPIStorage);
33 * // Get the cached events array for the window you want (use the inner
35 * let events = ConsoleAPIStorage.getEvents(innerWindowID);
36 * events.forEach(function(event) { ... });
38 * // Clear the events for the given inner window ID.
39 * ConsoleAPIStorage.clearEvents(innerWindowID);
41 export function ConsoleAPIStorageService() {
45 ConsoleAPIStorageService.prototype = {
46 classID: CONSOLEAPISTORAGE_CID,
47 QueryInterface: ChromeUtils.generateQI([
48 "nsIConsoleAPIStorage",
52 observe: function CS_observe(aSubject, aTopic, aData) {
53 if (aTopic == "xpcom-shutdown") {
54 Services.obs.removeObserver(this, "xpcom-shutdown");
55 Services.obs.removeObserver(this, "inner-window-destroyed");
56 Services.obs.removeObserver(this, "memory-pressure");
57 } else if (aTopic == "inner-window-destroyed") {
58 let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
59 this.clearEvents(innerWindowID + "");
60 } else if (aTopic == "memory-pressure") {
66 init: function CS_init() {
67 Services.obs.addObserver(this, "xpcom-shutdown");
68 Services.obs.addObserver(this, "inner-window-destroyed");
69 Services.obs.addObserver(this, "memory-pressure");
73 * Get the events array by inner window ID or all events from all windows.
76 * Optional, the inner window ID for which you want to get the array of
79 * The array of cached events for the given window. If no |aId| is
80 * given this function returns all of the cached events, from any
83 getEvents: function CS_getEvents(aId) {
85 return (_consoleStorage.get(aId) || []).slice(0);
90 for (let [, events] of _consoleStorage) {
91 result.push.apply(result, events);
94 return result.sort(function (a, b) {
95 return a.timeStamp - b.timeStamp;
100 * Adds a listener to be notified of log events.
102 * @param jsval [aListener]
103 * A JS listener which will be notified with the message object when
104 * a log event occurs.
105 * @param nsIPrincipal [aPrincipal]
106 * The principal of the listener - used to determine if we need to
107 * clone the message before forwarding it.
109 addLogEventListener: function CS_addLogEventListener(aListener, aPrincipal) {
110 // If our listener has a less-privileged principal than us, then they won't
111 // be able to access the log event object which was populated for our
112 // scope. Accordingly we need to clone it for these listeners.
114 // XXX: AFAICT these listeners which we need to clone messages for are all
115 // tests. Alternative solutions are welcome.
116 const clone = !aPrincipal.subsumes(
117 Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal)
119 _logEventListeners.push({
126 * Removes a listener added with `addLogEventListener`.
128 * @param jsval [aListener]
129 * A JS listener which was added with `addLogEventListener`.
131 removeLogEventListener: function CS_removeLogEventListener(aListener) {
132 const index = _logEventListeners.findIndex(l => l.callback === aListener);
134 _logEventListeners.splice(index, 1);
137 "Attempted to remove a log event listener that does not exist."
143 * Record an event associated with the given window ID.
146 * The ID of the inner window for which the event occurred or "jsm" for
147 * messages logged from JavaScript modules..
148 * @param object aEvent
149 * A JavaScript object you want to store.
151 recordEvent: function CS_recordEvent(aId, aEvent) {
152 if (!_consoleStorage.has(aId)) {
153 _consoleStorage.set(aId, []);
156 let storage = _consoleStorage.get(aId);
158 storage.push(aEvent);
161 if (storage.length > STORAGE_MAX_EVENTS) {
165 for (let { callback, clone } of _logEventListeners) {
168 callback(Cu.cloneInto(aEvent, callback));
173 // A failing listener should not prevent from calling other listeners.
180 * Clear storage data for the given window.
182 * @param string [aId]
183 * Optional, the inner window ID for which you want to clear the
184 * messages. If this is not specified all of the cached messages are
185 * cleared, from all window objects.
187 clearEvents: function CS_clearEvents(aId) {
189 _consoleStorage.delete(aId);
191 _consoleStorage.clear();
192 Services.obs.notifyObservers(null, "console-storage-reset");