forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConsoleAPIStorage.js
209 lines (180 loc) · 6.31 KB
/
ConsoleAPIStorage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
let Cu = Components.utils;
let Ci = Components.interfaces;
let Cc = Components.classes;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
// The console API events have to be scheduled when stored using
// |recordPendingEvent|.
const CALL_DELAY = 15 // milliseconds
// This constant tells how many messages to process in a single timer execution.
const MESSAGES_IN_INTERVAL = 1500
const STORAGE_MAX_EVENTS = 200;
var _consoleStorage = new Map();
var _consolePendingStorage = new Map();
var _timer;
const CONSOLEAPISTORAGE_CID = Components.ID('{96cf7855-dfa9-4c6d-8276-f9705b4890f2}');
/**
* The ConsoleAPIStorage is meant to cache window.console API calls for later
* reuse by other components when needed. For example, the Web Console code can
* display the cached messages when it opens for the active tab.
*
* ConsoleAPI messages are stored as they come from the ConsoleAPI code, with
* all their properties. They are kept around until the inner window object that
* created the messages is destroyed. Messages are indexed by the inner window
* ID.
*
* Usage:
* Cu.import("resource://gre/modules/ConsoleAPIStorage.jsm");
*
* // Get the cached events array for the window you want (use the inner
* // window ID).
* let events = ConsoleAPIStorage.getEvents(innerWindowID);
* events.forEach(function(event) { ... });
*
* // Clear the events for the given inner window ID.
* ConsoleAPIStorage.clearEvents(innerWindowID);
*/
function ConsoleAPIStorageService() {
this.init();
}
ConsoleAPIStorageService.prototype = {
classID : CONSOLEAPISTORAGE_CID,
QueryInterface: XPCOMUtils.generateQI([Ci.nsIConsoleAPIStorage,
Ci.nsIObserver]),
classInfo: XPCOMUtils.generateCI({
classID: CONSOLEAPISTORAGE_CID,
contractID: '@mozilla.org/consoleAPI-storage;1',
interfaces: [Ci.nsIConsoleAPIStorage, Ci.nsIObserver],
flags: Ci.nsIClassInfo.SINGLETON
}),
observe: function CS_observe(aSubject, aTopic, aData)
{
if (aTopic == "xpcom-shutdown") {
Services.obs.removeObserver(this, "xpcom-shutdown");
Services.obs.removeObserver(this, "inner-window-destroyed");
Services.obs.removeObserver(this, "memory-pressure");
}
else if (aTopic == "inner-window-destroyed") {
let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
this.clearEvents(innerWindowID + "");
}
else if (aTopic == "memory-pressure") {
this.clearEvents();
}
},
/** @private */
init: function CS_init()
{
Services.obs.addObserver(this, "xpcom-shutdown", false);
Services.obs.addObserver(this, "inner-window-destroyed", false);
Services.obs.addObserver(this, "memory-pressure", false);
},
/**
* Get the events array by inner window ID or all events from all windows.
*
* @param string [aId]
* Optional, the inner window ID for which you want to get the array of
* cached events.
* @returns array
* The array of cached events for the given window. If no |aId| is
* given this function returns all of the cached events, from any
* window.
*/
getEvents: function CS_getEvents(aId)
{
if (aId != null) {
return (_consoleStorage.get(aId) || []).slice(0);
}
let result = [];
for (let [id, events] of _consoleStorage) {
result.push.apply(result, events);
}
return result.sort(function(a, b) {
return a.timeStamp - b.timeStamp;
});
},
/**
* Record an event associated with the given window ID.
*
* @param string aId
* The ID of the inner window for which the event occurred or "jsm" for
* messages logged from JavaScript modules..
* @param object aEvent
* A JavaScript object you want to store.
*/
recordEvent: function CS_recordEvent(aId, aOuterId, aEvent)
{
if (!_consoleStorage.has(aId)) {
_consoleStorage.set(aId, []);
}
let storage = _consoleStorage.get(aId);
storage.push(aEvent);
// truncate
if (storage.length > STORAGE_MAX_EVENTS) {
storage.shift();
}
Services.obs.notifyObservers(aEvent, "console-api-log-event", aOuterId);
Services.obs.notifyObservers(aEvent, "console-storage-cache-event", aId);
},
/**
* Similar to recordEvent, but these events are scheduled and stored any
* CALL_DELAY millisecs.
*/
recordPendingEvent: function CS_recordPendingEvent(aId, aOuterId, aEvent)
{
if (!_consolePendingStorage.has(aId)) {
_consolePendingStorage.set(aId, []);
}
let storage = _consolePendingStorage.get(aId);
storage.push({ outerId: aOuterId, event: aEvent });
if (!_timer) {
_timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
}
let self = this;
_timer.initWithCallback(function() { self.flushPendingEvents(); },
CALL_DELAY, Ci.nsITimer.TYPE_REPEATING_SLACK);
},
/**
* Processes the pending event queue.
*/
flushPendingEvents: function CS_flushPendingEvents()
{
for (let [id, objs] of _consolePendingStorage) {
for (let i = 0; i < objs.length && i < MESSAGES_IN_INTERVAL; ++i) {
this.recordEvent(id, objs[i].outerId, objs[i].event);
}
if (objs.length <= MESSAGES_IN_INTERVAL) {
_consolePendingStorage.delete(id);
} else {
_consolePendingStorage.set(id, objs.splice(MESSAGES_IN_INTERVAL));
}
}
if (_timer && _consolePendingStorage.size == 0) {
_timer.cancel();
_timer = null;
}
},
/**
* Clear storage data for the given window.
*
* @param string [aId]
* Optional, the inner window ID for which you want to clear the
* messages. If this is not specified all of the cached messages are
* cleared, from all window objects.
*/
clearEvents: function CS_clearEvents(aId)
{
if (aId != null) {
_consoleStorage.delete(aId);
}
else {
_consoleStorage.clear();
Services.obs.notifyObservers(null, "console-storage-reset", null);
}
},
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ConsoleAPIStorageService]);