Bug 891986 - Keep the source ArrayBuffer to a decodeAudioData call alive until the...
[gecko.git] / dom / base / ConsoleAPIStorage.jsm
blob3ea7012af775e5bb28c16f19b5eb4fb570654c70
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 this.EXPORTED_SYMBOLS = ["ConsoleAPIStorage"];
18 var _consoleStorage = new Map();
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 this.ConsoleAPIStorage = {
43   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
45   observe: function CS_observe(aSubject, aTopic, aData)
46   {
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");
51     }
52     else if (aTopic == "inner-window-destroyed") {
53       let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
54       this.clearEvents(innerWindowID);
55     }
56     else if (aTopic == "memory-pressure") {
57       this.clearEvents();
58     }
59   },
61   /** @private */
62   init: function CS_init()
63   {
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);
67   },
69   /**
70    * Get the events array by inner window ID or all events from all windows.
71    *
72    * @param string [aId]
73    *        Optional, the inner window ID for which you want to get the array of
74    *        cached events.
75    * @returns array
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
78    *          window.
79    */
80   getEvents: function CS_getEvents(aId)
81   {
82     if (aId != null) {
83       return (_consoleStorage.get(aId) || []).slice(0);
84     }
86     let result = [];
88     for (let [id, 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 object aEvent
104    *        A JavaScript object you want to store.
105    */
106   recordEvent: function CS_recordEvent(aId, aEvent)
107   {
108     if (!_consoleStorage.has(aId)) {
109       _consoleStorage.set(aId, []);
110     }
112     let storage = _consoleStorage.get(aId);
113     storage.push(aEvent);
115     // truncate
116     if (storage.length > STORAGE_MAX_EVENTS) {
117       storage.shift();
118     }
120     Services.obs.notifyObservers(aEvent, "console-storage-cache-event", aId);
121   },
123   /**
124    * Clear storage data for the given window.
125    *
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.
130    */
131   clearEvents: function CS_clearEvents(aId)
132   {
133     if (aId != null) {
134       _consoleStorage.delete(aId);
135     }
136     else {
137       _consoleStorage.clear();
138       Services.obs.notifyObservers(null, "console-storage-reset", null);
139     }
140   },
143 ConsoleAPIStorage.init();