dom/base/ConsoleAPIStorage.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 let Cu = Components.utils;
michael@0 8 let Ci = Components.interfaces;
michael@0 9 let Cc = Components.classes;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 12 Cu.import("resource://gre/modules/Services.jsm");
michael@0 13
michael@0 14 const STORAGE_MAX_EVENTS = 200;
michael@0 15
michael@0 16 var _consoleStorage = new Map();
michael@0 17
michael@0 18 const CONSOLEAPISTORAGE_CID = Components.ID('{96cf7855-dfa9-4c6d-8276-f9705b4890f2}');
michael@0 19
michael@0 20 /**
michael@0 21 * The ConsoleAPIStorage is meant to cache window.console API calls for later
michael@0 22 * reuse by other components when needed. For example, the Web Console code can
michael@0 23 * display the cached messages when it opens for the active tab.
michael@0 24 *
michael@0 25 * ConsoleAPI messages are stored as they come from the ConsoleAPI code, with
michael@0 26 * all their properties. They are kept around until the inner window object that
michael@0 27 * created the messages is destroyed. Messages are indexed by the inner window
michael@0 28 * ID.
michael@0 29 *
michael@0 30 * Usage:
michael@0 31 * Cu.import("resource://gre/modules/ConsoleAPIStorage.jsm");
michael@0 32 *
michael@0 33 * // Get the cached events array for the window you want (use the inner
michael@0 34 * // window ID).
michael@0 35 * let events = ConsoleAPIStorage.getEvents(innerWindowID);
michael@0 36 * events.forEach(function(event) { ... });
michael@0 37 *
michael@0 38 * // Clear the events for the given inner window ID.
michael@0 39 * ConsoleAPIStorage.clearEvents(innerWindowID);
michael@0 40 */
michael@0 41 function ConsoleAPIStorageService() {
michael@0 42 this.init();
michael@0 43 }
michael@0 44
michael@0 45 ConsoleAPIStorageService.prototype = {
michael@0 46 classID : CONSOLEAPISTORAGE_CID,
michael@0 47 QueryInterface: XPCOMUtils.generateQI([Ci.nsIConsoleAPIStorage,
michael@0 48 Ci.nsIObserver]),
michael@0 49 classInfo: XPCOMUtils.generateCI({
michael@0 50 classID: CONSOLEAPISTORAGE_CID,
michael@0 51 contractID: '@mozilla.org/consoleAPI-storage;1',
michael@0 52 interfaces: [Ci.nsIConsoleAPIStorage, Ci.nsIObserver],
michael@0 53 flags: Ci.nsIClassInfo.SINGLETON
michael@0 54 }),
michael@0 55
michael@0 56 observe: function CS_observe(aSubject, aTopic, aData)
michael@0 57 {
michael@0 58 if (aTopic == "xpcom-shutdown") {
michael@0 59 Services.obs.removeObserver(this, "xpcom-shutdown");
michael@0 60 Services.obs.removeObserver(this, "inner-window-destroyed");
michael@0 61 Services.obs.removeObserver(this, "memory-pressure");
michael@0 62 }
michael@0 63 else if (aTopic == "inner-window-destroyed") {
michael@0 64 let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
michael@0 65 this.clearEvents(innerWindowID + "");
michael@0 66 }
michael@0 67 else if (aTopic == "memory-pressure") {
michael@0 68 this.clearEvents();
michael@0 69 }
michael@0 70 },
michael@0 71
michael@0 72 /** @private */
michael@0 73 init: function CS_init()
michael@0 74 {
michael@0 75 Services.obs.addObserver(this, "xpcom-shutdown", false);
michael@0 76 Services.obs.addObserver(this, "inner-window-destroyed", false);
michael@0 77 Services.obs.addObserver(this, "memory-pressure", false);
michael@0 78 },
michael@0 79
michael@0 80 /**
michael@0 81 * Get the events array by inner window ID or all events from all windows.
michael@0 82 *
michael@0 83 * @param string [aId]
michael@0 84 * Optional, the inner window ID for which you want to get the array of
michael@0 85 * cached events.
michael@0 86 * @returns array
michael@0 87 * The array of cached events for the given window. If no |aId| is
michael@0 88 * given this function returns all of the cached events, from any
michael@0 89 * window.
michael@0 90 */
michael@0 91 getEvents: function CS_getEvents(aId)
michael@0 92 {
michael@0 93 if (aId != null) {
michael@0 94 return (_consoleStorage.get(aId) || []).slice(0);
michael@0 95 }
michael@0 96
michael@0 97 let result = [];
michael@0 98
michael@0 99 for (let [id, events] of _consoleStorage) {
michael@0 100 result.push.apply(result, events);
michael@0 101 }
michael@0 102
michael@0 103 return result.sort(function(a, b) {
michael@0 104 return a.timeStamp - b.timeStamp;
michael@0 105 });
michael@0 106 },
michael@0 107
michael@0 108 /**
michael@0 109 * Record an event associated with the given window ID.
michael@0 110 *
michael@0 111 * @param string aId
michael@0 112 * The ID of the inner window for which the event occurred or "jsm" for
michael@0 113 * messages logged from JavaScript modules..
michael@0 114 * @param object aEvent
michael@0 115 * A JavaScript object you want to store.
michael@0 116 */
michael@0 117 recordEvent: function CS_recordEvent(aId, aEvent)
michael@0 118 {
michael@0 119 if (!_consoleStorage.has(aId)) {
michael@0 120 _consoleStorage.set(aId, []);
michael@0 121 }
michael@0 122
michael@0 123 let storage = _consoleStorage.get(aId);
michael@0 124 storage.push(aEvent);
michael@0 125
michael@0 126 // truncate
michael@0 127 if (storage.length > STORAGE_MAX_EVENTS) {
michael@0 128 storage.shift();
michael@0 129 }
michael@0 130
michael@0 131 Services.obs.notifyObservers(aEvent, "console-storage-cache-event", aId);
michael@0 132 },
michael@0 133
michael@0 134 /**
michael@0 135 * Clear storage data for the given window.
michael@0 136 *
michael@0 137 * @param string [aId]
michael@0 138 * Optional, the inner window ID for which you want to clear the
michael@0 139 * messages. If this is not specified all of the cached messages are
michael@0 140 * cleared, from all window objects.
michael@0 141 */
michael@0 142 clearEvents: function CS_clearEvents(aId)
michael@0 143 {
michael@0 144 if (aId != null) {
michael@0 145 _consoleStorage.delete(aId);
michael@0 146 }
michael@0 147 else {
michael@0 148 _consoleStorage.clear();
michael@0 149 Services.obs.notifyObservers(null, "console-storage-reset", null);
michael@0 150 }
michael@0 151 },
michael@0 152 };
michael@0 153
michael@0 154 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ConsoleAPIStorageService]);

mercurial