Bumping manifests a=b2g-bump
[gecko.git] / dom / base / ConsoleAPIStorage.js
blob224eb3b95f46855cd7c3fa33bc59ba20b913477f
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 "use strict";
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}');
20 /**
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.
24  *
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
28  * ID.
29  *
30  * Usage:
31  *    Cu.import("resource://gre/modules/ConsoleAPIStorage.jsm");
32  *
33  *    // Get the cached events array for the window you want (use the inner
34  *    // window ID).
35  *    let events = ConsoleAPIStorage.getEvents(innerWindowID);
36  *    events.forEach(function(event) { ... });
37  *
38  *    // Clear the events for the given inner window ID.
39  *    ConsoleAPIStorage.clearEvents(innerWindowID);
40  */
41 function ConsoleAPIStorageService() {
42   this.init();
45 ConsoleAPIStorageService.prototype = {
46   classID : CONSOLEAPISTORAGE_CID,
47   QueryInterface: XPCOMUtils.generateQI([Ci.nsIConsoleAPIStorage,
48                                          Ci.nsIObserver]),
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
54   }),
56   observe: function CS_observe(aSubject, aTopic, aData)
57   {
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");
62     }
63     else if (aTopic == "inner-window-destroyed") {
64       let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
65       this.clearEvents(innerWindowID + "");
66     }
67     else if (aTopic == "memory-pressure") {
68       this.clearEvents();
69     }
70   },
72   /** @private */
73   init: function CS_init()
74   {
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);
78   },
80   /**
81    * Get the events array by inner window ID or all events from all windows.
82    *
83    * @param string [aId]
84    *        Optional, the inner window ID for which you want to get the array of
85    *        cached events.
86    * @returns array
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
89    *          window.
90    */
91   getEvents: function CS_getEvents(aId)
92   {
93     if (aId != null) {
94       return (_consoleStorage.get(aId) || []).slice(0);
95     }
97     let result = [];
99     for (let [id, events] of _consoleStorage) {
100       result.push.apply(result, events);
101     }
103     return result.sort(function(a, b) {
104       return a.timeStamp - b.timeStamp;
105     });
106   },
108   /**
109    * Record an event associated with the given window ID.
110    *
111    * @param string aId
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.
116    */
117   recordEvent: function CS_recordEvent(aId, aEvent)
118   {
119     if (!_consoleStorage.has(aId)) {
120       _consoleStorage.set(aId, []);
121     }
123     let storage = _consoleStorage.get(aId);
124     storage.push(aEvent);
126     // truncate
127     if (storage.length > STORAGE_MAX_EVENTS) {
128       storage.shift();
129     }
131     Services.obs.notifyObservers(aEvent, "console-storage-cache-event", aId);
132   },
134   /**
135    * Clear storage data for the given window.
136    *
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.
141    */
142   clearEvents: function CS_clearEvents(aId)
143   {
144     if (aId != null) {
145       _consoleStorage.delete(aId);
146     }
147     else {
148       _consoleStorage.clear();
149       Services.obs.notifyObservers(null, "console-storage-reset", null);
150     }
151   },
154 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ConsoleAPIStorageService]);