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/. */
7 let Cu = Components.utils;
8 let Ci = Components.interfaces;
9 let Cc = Components.classes;
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
12 Cu.import("resource://gre/modules/Services.jsm");
14 const STORAGE_MAX_EVENTS = 200;
16 this.EXPORTED_SYMBOLS = ["ConsoleAPIStorage"];
18 var _consoleStorage = new Map();
21 * The ConsoleAPIStorage is meant to cache window.console API calls for later
22 * reuse by other components when needed. For example, the Web Console code can
23 * display the cached messages when it opens for the active tab.
25 * ConsoleAPI messages are stored as they come from the ConsoleAPI code, with
26 * all their properties. They are kept around until the inner window object that
27 * created the messages is destroyed. Messages are indexed by the inner window
31 * Cu.import("resource://gre/modules/ConsoleAPIStorage.jsm");
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 this.ConsoleAPIStorage = {
43 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
45 observe: function CS_observe(aSubject, aTopic, aData)
47 if (aTopic == "xpcom-shutdown") {
48 Services.obs.removeObserver(this, "xpcom-shutdown");
49 Services.obs.removeObserver(this, "inner-window-destroyed");
50 Services.obs.removeObserver(this, "memory-pressure");
52 else if (aTopic == "inner-window-destroyed") {
53 let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
54 this.clearEvents(innerWindowID);
56 else if (aTopic == "memory-pressure") {
62 init: function CS_init()
64 Services.obs.addObserver(this, "xpcom-shutdown", false);
65 Services.obs.addObserver(this, "inner-window-destroyed", false);
66 Services.obs.addObserver(this, "memory-pressure", false);
70 * Get the events array by inner window ID or all events from all windows.
73 * Optional, the inner window ID for which you want to get the array of
76 * The array of cached events for the given window. If no |aId| is
77 * given this function returns all of the cached events, from any
80 getEvents: function CS_getEvents(aId)
83 return (_consoleStorage.get(aId) || []).slice(0);
88 for (let [id, events] of _consoleStorage) {
89 result.push.apply(result, events);
92 return result.sort(function(a, b) {
93 return a.timeStamp - b.timeStamp;
98 * Record an event associated with the given window ID.
101 * The ID of the inner window for which the event occurred or "jsm" for
102 * messages logged from JavaScript modules..
103 * @param object aEvent
104 * A JavaScript object you want to store.
106 recordEvent: function CS_recordEvent(aId, aEvent)
108 if (!_consoleStorage.has(aId)) {
109 _consoleStorage.set(aId, []);
112 let storage = _consoleStorage.get(aId);
113 storage.push(aEvent);
116 if (storage.length > STORAGE_MAX_EVENTS) {
120 Services.obs.notifyObservers(aEvent, "console-storage-cache-event", aId);
124 * Clear storage data for the given window.
126 * @param string [aId]
127 * Optional, the inner window ID for which you want to clear the
128 * messages. If this is not specified all of the cached messages are
129 * cleared, from all window objects.
131 clearEvents: function CS_clearEvents(aId)
134 _consoleStorage.delete(aId);
137 _consoleStorage.clear();
138 Services.obs.notifyObservers(null, "console-storage-reset", null);
143 ConsoleAPIStorage.init();