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 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
9 const STORAGE_MAX_EVENTS = 1000;
11 var _consoleStorage = new Map();
13 const CONSOLEAPISTORAGE_CID = Components.ID(
14 "{96cf7855-dfa9-4c6d-8276-f9705b4890f2}"
18 * The ConsoleAPIStorage is meant to cache window.console API calls for later
19 * reuse by other components when needed. For example, the Web Console code can
20 * display the cached messages when it opens for the active tab.
22 * ConsoleAPI messages are stored as they come from the ConsoleAPI code, with
23 * all their properties. They are kept around until the inner window object that
24 * created the messages is destroyed. Messages are indexed by the inner window
28 * let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
29 * .getService(Ci.nsIConsoleAPIStorage);
31 * // Get the cached events array for the window you want (use the inner
33 * let events = ConsoleAPIStorage.getEvents(innerWindowID);
34 * events.forEach(function(event) { ... });
36 * // Clear the events for the given inner window ID.
37 * ConsoleAPIStorage.clearEvents(innerWindowID);
39 function ConsoleAPIStorageService() {
43 ConsoleAPIStorageService.prototype = {
44 classID: CONSOLEAPISTORAGE_CID,
45 QueryInterface: ChromeUtils.generateQI([
46 "nsIConsoleAPIStorage",
50 observe: function CS_observe(aSubject, aTopic, aData) {
51 if (aTopic == "xpcom-shutdown") {
52 Services.obs.removeObserver(this, "xpcom-shutdown");
53 Services.obs.removeObserver(this, "inner-window-destroyed");
54 Services.obs.removeObserver(this, "memory-pressure");
55 } else if (aTopic == "inner-window-destroyed") {
56 let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
57 this.clearEvents(innerWindowID + "");
58 } else if (aTopic == "memory-pressure") {
64 init: function CS_init() {
65 Services.obs.addObserver(this, "xpcom-shutdown");
66 Services.obs.addObserver(this, "inner-window-destroyed");
67 Services.obs.addObserver(this, "memory-pressure");
71 * Get the events array by inner window ID or all events from all windows.
74 * Optional, the inner window ID for which you want to get the array of
77 * The array of cached events for the given window. If no |aId| is
78 * given this function returns all of the cached events, from any
81 getEvents: function CS_getEvents(aId) {
83 return (_consoleStorage.get(aId) || []).slice(0);
88 for (let [, 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 string aOuterId
104 * This ID is used as 3rd parameters for the console-api-log-event
106 * @param object aEvent
107 * A JavaScript object you want to store.
109 recordEvent: function CS_recordEvent(aId, aOuterId, aEvent) {
110 if (!_consoleStorage.has(aId)) {
111 _consoleStorage.set(aId, []);
114 let storage = _consoleStorage.get(aId);
116 storage.push(aEvent);
119 if (storage.length > STORAGE_MAX_EVENTS) {
123 Services.obs.notifyObservers(aEvent, "console-api-log-event", aOuterId);
124 Services.obs.notifyObservers(aEvent, "console-storage-cache-event", aId);
128 * Clear storage data for the given window.
130 * @param string [aId]
131 * Optional, the inner window ID for which you want to clear the
132 * messages. If this is not specified all of the cached messages are
133 * cleared, from all window objects.
135 clearEvents: function CS_clearEvents(aId) {
137 _consoleStorage.delete(aId);
139 _consoleStorage.clear();
140 Services.obs.notifyObservers(null, "console-storage-reset");
145 var EXPORTED_SYMBOLS = ["ConsoleAPIStorageService"];