Bug 1707290 [wpt PR 28671] - Auto-expand details elements for find-in-page, a=testonly
[gecko.git] / dom / console / ConsoleAPIStorage.jsm
blobda0d9de7d2d7659af63856d173675cfc421e4ac1
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 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}"
17 /**
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.
21  *
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
25  * ID.
26  *
27  * Usage:
28  *    let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
29  *                              .getService(Ci.nsIConsoleAPIStorage);
30  *
31  *    // Get the cached events array for the window you want (use the inner
32  *    // window ID).
33  *    let events = ConsoleAPIStorage.getEvents(innerWindowID);
34  *    events.forEach(function(event) { ... });
35  *
36  *    // Clear the events for the given inner window ID.
37  *    ConsoleAPIStorage.clearEvents(innerWindowID);
38  */
39 function ConsoleAPIStorageService() {
40   this.init();
43 ConsoleAPIStorageService.prototype = {
44   classID: CONSOLEAPISTORAGE_CID,
45   QueryInterface: ChromeUtils.generateQI([
46     "nsIConsoleAPIStorage",
47     "nsIObserver",
48   ]),
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") {
59       this.clearEvents();
60     }
61   },
63   /** @private */
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");
68   },
70   /**
71    * Get the events array by inner window ID or all events from all windows.
72    *
73    * @param string [aId]
74    *        Optional, the inner window ID for which you want to get the array of
75    *        cached events.
76    * @returns array
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
79    *          window.
80    */
81   getEvents: function CS_getEvents(aId) {
82     if (aId != null) {
83       return (_consoleStorage.get(aId) || []).slice(0);
84     }
86     let result = [];
88     for (let [, events] of _consoleStorage) {
89       result.push.apply(result, events);
90     }
92     return result.sort(function(a, b) {
93       return a.timeStamp - b.timeStamp;
94     });
95   },
97   /**
98    * Record an event associated with the given window ID.
99    *
100    * @param string aId
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
105    *        notification.
106    * @param object aEvent
107    *        A JavaScript object you want to store.
108    */
109   recordEvent: function CS_recordEvent(aId, aOuterId, aEvent) {
110     if (!_consoleStorage.has(aId)) {
111       _consoleStorage.set(aId, []);
112     }
114     let storage = _consoleStorage.get(aId);
116     storage.push(aEvent);
118     // truncate
119     if (storage.length > STORAGE_MAX_EVENTS) {
120       storage.shift();
121     }
123     Services.obs.notifyObservers(aEvent, "console-api-log-event", aOuterId);
124     Services.obs.notifyObservers(aEvent, "console-storage-cache-event", aId);
125   },
127   /**
128    * Clear storage data for the given window.
129    *
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.
134    */
135   clearEvents: function CS_clearEvents(aId) {
136     if (aId != null) {
137       _consoleStorage.delete(aId);
138     } else {
139       _consoleStorage.clear();
140       Services.obs.notifyObservers(null, "console-storage-reset");
141     }
142   },
145 var EXPORTED_SYMBOLS = ["ConsoleAPIStorageService"];