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 var _consoleStorage = new Map();
18 const CONSOLEAPISTORAGE_CID = Components.ID('{96cf7855-dfa9-4c6d-8276-f9705b4890f2}');
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 function ConsoleAPIStorageService() {
45 ConsoleAPIStorageService.prototype = {
46 classID : CONSOLEAPISTORAGE_CID,
47 QueryInterface: XPCOMUtils.generateQI([Ci.nsIConsoleAPIStorage,
49 classInfo: XPCOMUtils.generateCI({
50 classID: CONSOLEAPISTORAGE_CID,
51 contractID: '@mozilla.org/consoleAPI-storage;1',
52 interfaces: [Ci.nsIConsoleAPIStorage, Ci.nsIObserver],
53 flags: Ci.nsIClassInfo.SINGLETON
56 observe: function CS_observe(aSubject, aTopic, aData)
58 if (aTopic == "xpcom-shutdown") {
59 Services.obs.removeObserver(this, "xpcom-shutdown");
60 Services.obs.removeObserver(this, "inner-window-destroyed");
61 Services.obs.removeObserver(this, "memory-pressure");
63 else if (aTopic == "inner-window-destroyed") {
64 let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
65 this.clearEvents(innerWindowID + "");
67 else if (aTopic == "memory-pressure") {
73 init: function CS_init()
75 Services.obs.addObserver(this, "xpcom-shutdown", false);
76 Services.obs.addObserver(this, "inner-window-destroyed", false);
77 Services.obs.addObserver(this, "memory-pressure", false);
81 * Get the events array by inner window ID or all events from all windows.
84 * Optional, the inner window ID for which you want to get the array of
87 * The array of cached events for the given window. If no |aId| is
88 * given this function returns all of the cached events, from any
91 getEvents: function CS_getEvents(aId)
94 return (_consoleStorage.get(aId) || []).slice(0);
99 for (let [id, events] of _consoleStorage) {
100 result.push.apply(result, events);
103 return result.sort(function(a, b) {
104 return a.timeStamp - b.timeStamp;
109 * Record an event associated with the given window ID.
112 * The ID of the inner window for which the event occurred or "jsm" for
113 * messages logged from JavaScript modules..
114 * @param object aEvent
115 * A JavaScript object you want to store.
117 recordEvent: function CS_recordEvent(aId, aEvent)
119 if (!_consoleStorage.has(aId)) {
120 _consoleStorage.set(aId, []);
123 let storage = _consoleStorage.get(aId);
124 storage.push(aEvent);
127 if (storage.length > STORAGE_MAX_EVENTS) {
131 Services.obs.notifyObservers(aEvent, "console-storage-cache-event", aId);
135 * Clear storage data for the given window.
137 * @param string [aId]
138 * Optional, the inner window ID for which you want to clear the
139 * messages. If this is not specified all of the cached messages are
140 * cleared, from all window objects.
142 clearEvents: function CS_clearEvents(aId)
145 _consoleStorage.delete(aId);
148 _consoleStorage.clear();
149 Services.obs.notifyObservers(null, "console-storage-reset", null);
154 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ConsoleAPIStorageService]);