2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2011-08-25 00:34:16 +04:00
|
|
|
|
2013-06-07 21:13:48 +04:00
|
|
|
"use strict";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2011-08-25 00:34:16 +04:00
|
|
|
|
2016-04-19 15:10:15 +03:00
|
|
|
const STORAGE_MAX_EVENTS = 1000;
|
2011-08-25 00:34:16 +04:00
|
|
|
|
2013-06-07 21:13:48 +04:00
|
|
|
var _consoleStorage = new Map();
|
2011-08-25 00:34:16 +04:00
|
|
|
|
2018-12-20 01:38:44 +03:00
|
|
|
const CONSOLEAPISTORAGE_CID = Components.ID(
|
|
|
|
"{96cf7855-dfa9-4c6d-8276-f9705b4890f2}"
|
|
|
|
);
|
2014-02-28 03:38:54 +04:00
|
|
|
|
2011-08-25 00:34:16 +04:00
|
|
|
/**
|
|
|
|
* 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:
|
2016-11-28 14:29:31 +03:00
|
|
|
* let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
|
|
|
|
* .getService(Ci.nsIConsoleAPIStorage);
|
2011-08-25 00:34:16 +04:00
|
|
|
*
|
|
|
|
* // 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);
|
|
|
|
*/
|
2014-02-28 03:38:54 +04:00
|
|
|
function ConsoleAPIStorageService() {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleAPIStorageService.prototype = {
|
2018-12-20 01:38:44 +03:00
|
|
|
classID: CONSOLEAPISTORAGE_CID,
|
2014-02-28 03:38:54 +04:00
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
Bug 1649221: Update ChromeUtils.generateQI callers to pass strings. r=mccr8,remote-protocol-reviewers,marionette-reviewers,perftest-reviewers,webcompat-reviewers,geckoview-reviewers,preferences-reviewers,agi,whimboo,Bebe,twisniewski
Differential Revision: https://phabricator.services.mozilla.com/D81594
2020-07-11 02:58:28 +03:00
|
|
|
"nsIConsoleAPIStorage",
|
|
|
|
"nsIObserver",
|
2014-02-28 03:38:54 +04:00
|
|
|
]),
|
2011-08-25 00:34:16 +04:00
|
|
|
|
2018-12-20 01:38:44 +03:00
|
|
|
observe: function CS_observe(aSubject, aTopic, aData) {
|
2011-08-25 00:34:16 +04:00
|
|
|
if (aTopic == "xpcom-shutdown") {
|
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
|
|
|
Services.obs.removeObserver(this, "memory-pressure");
|
2018-12-20 01:38:44 +03:00
|
|
|
} else if (aTopic == "inner-window-destroyed") {
|
2011-08-25 00:34:16 +04:00
|
|
|
let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
2014-02-28 03:39:17 +04:00
|
|
|
this.clearEvents(innerWindowID + "");
|
2018-12-20 01:38:44 +03:00
|
|
|
} else if (aTopic == "memory-pressure") {
|
2013-04-26 05:36:53 +04:00
|
|
|
this.clearEvents();
|
2011-08-25 00:34:16 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/** @private */
|
2018-12-20 01:38:44 +03:00
|
|
|
init: function CS_init() {
|
2017-04-15 00:39:22 +03:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown");
|
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed");
|
|
|
|
Services.obs.addObserver(this, "memory-pressure");
|
2011-08-25 00:34:16 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-10-06 14:29:57 +04:00
|
|
|
* Get the events array by inner window ID or all events from all windows.
|
2011-08-25 00:34:16 +04:00
|
|
|
*
|
2012-10-06 14:29:57 +04:00
|
|
|
* @param string [aId]
|
|
|
|
* Optional, the inner window ID for which you want to get the array of
|
|
|
|
* cached events.
|
2011-08-25 00:34:16 +04:00
|
|
|
* @returns array
|
2012-10-06 14:29:57 +04:00
|
|
|
* 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.
|
2011-08-25 00:34:16 +04:00
|
|
|
*/
|
2018-12-20 01:38:44 +03:00
|
|
|
getEvents: function CS_getEvents(aId) {
|
2012-10-06 14:29:57 +04:00
|
|
|
if (aId != null) {
|
2013-06-07 21:13:48 +04:00
|
|
|
return (_consoleStorage.get(aId) || []).slice(0);
|
2012-10-06 14:29:57 +04:00
|
|
|
}
|
|
|
|
|
2013-06-07 21:13:48 +04:00
|
|
|
let result = [];
|
2012-10-06 14:29:57 +04:00
|
|
|
|
2018-12-20 01:39:34 +03:00
|
|
|
for (let [, events] of _consoleStorage) {
|
2013-06-07 21:13:48 +04:00
|
|
|
result.push.apply(result, events);
|
2012-10-06 14:29:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result.sort(function(a, b) {
|
|
|
|
return a.timeStamp - b.timeStamp;
|
|
|
|
});
|
2011-08-25 00:34:16 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record an event associated with the given window ID.
|
|
|
|
*
|
2013-06-07 21:13:48 +04:00
|
|
|
* @param string aId
|
|
|
|
* The ID of the inner window for which the event occurred or "jsm" for
|
|
|
|
* messages logged from JavaScript modules..
|
2015-07-08 22:10:29 +03:00
|
|
|
* @param string aOuterId
|
|
|
|
* This ID is used as 3rd parameters for the console-api-log-event
|
|
|
|
* notification.
|
2011-08-25 00:34:16 +04:00
|
|
|
* @param object aEvent
|
|
|
|
* A JavaScript object you want to store.
|
|
|
|
*/
|
2018-12-20 01:38:44 +03:00
|
|
|
recordEvent: function CS_recordEvent(aId, aOuterId, aEvent) {
|
2013-06-07 21:13:48 +04:00
|
|
|
if (!_consoleStorage.has(aId)) {
|
|
|
|
_consoleStorage.set(aId, []);
|
2011-08-25 00:34:16 +04:00
|
|
|
}
|
|
|
|
|
2013-06-07 21:13:48 +04:00
|
|
|
let storage = _consoleStorage.get(aId);
|
2016-11-16 20:03:08 +03:00
|
|
|
|
2011-08-25 00:34:16 +04:00
|
|
|
storage.push(aEvent);
|
|
|
|
|
|
|
|
// truncate
|
|
|
|
if (storage.length > STORAGE_MAX_EVENTS) {
|
|
|
|
storage.shift();
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
Services.obs.notifyObservers(aEvent, "console-api-log-event", aOuterId);
|
2013-06-07 21:13:48 +04:00
|
|
|
Services.obs.notifyObservers(aEvent, "console-storage-cache-event", aId);
|
2011-08-25 00:34:16 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-12-20 01:38:44 +03:00
|
|
|
clearEvents: function CS_clearEvents(aId) {
|
2011-08-25 00:34:16 +04:00
|
|
|
if (aId != null) {
|
2013-06-07 21:13:48 +04:00
|
|
|
_consoleStorage.delete(aId);
|
2018-12-20 01:38:44 +03:00
|
|
|
} else {
|
2013-06-07 21:13:48 +04:00
|
|
|
_consoleStorage.clear();
|
2017-04-15 00:39:17 +03:00
|
|
|
Services.obs.notifyObservers(null, "console-storage-reset");
|
2011-08-25 00:34:16 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-01-30 22:34:41 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["ConsoleAPIStorageService"];
|