1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/base/ConsoleAPIStorage.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,154 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +let Cu = Components.utils; 1.11 +let Ci = Components.interfaces; 1.12 +let Cc = Components.classes; 1.13 + 1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.15 +Cu.import("resource://gre/modules/Services.jsm"); 1.16 + 1.17 +const STORAGE_MAX_EVENTS = 200; 1.18 + 1.19 +var _consoleStorage = new Map(); 1.20 + 1.21 +const CONSOLEAPISTORAGE_CID = Components.ID('{96cf7855-dfa9-4c6d-8276-f9705b4890f2}'); 1.22 + 1.23 +/** 1.24 + * The ConsoleAPIStorage is meant to cache window.console API calls for later 1.25 + * reuse by other components when needed. For example, the Web Console code can 1.26 + * display the cached messages when it opens for the active tab. 1.27 + * 1.28 + * ConsoleAPI messages are stored as they come from the ConsoleAPI code, with 1.29 + * all their properties. They are kept around until the inner window object that 1.30 + * created the messages is destroyed. Messages are indexed by the inner window 1.31 + * ID. 1.32 + * 1.33 + * Usage: 1.34 + * Cu.import("resource://gre/modules/ConsoleAPIStorage.jsm"); 1.35 + * 1.36 + * // Get the cached events array for the window you want (use the inner 1.37 + * // window ID). 1.38 + * let events = ConsoleAPIStorage.getEvents(innerWindowID); 1.39 + * events.forEach(function(event) { ... }); 1.40 + * 1.41 + * // Clear the events for the given inner window ID. 1.42 + * ConsoleAPIStorage.clearEvents(innerWindowID); 1.43 + */ 1.44 +function ConsoleAPIStorageService() { 1.45 + this.init(); 1.46 +} 1.47 + 1.48 +ConsoleAPIStorageService.prototype = { 1.49 + classID : CONSOLEAPISTORAGE_CID, 1.50 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIConsoleAPIStorage, 1.51 + Ci.nsIObserver]), 1.52 + classInfo: XPCOMUtils.generateCI({ 1.53 + classID: CONSOLEAPISTORAGE_CID, 1.54 + contractID: '@mozilla.org/consoleAPI-storage;1', 1.55 + interfaces: [Ci.nsIConsoleAPIStorage, Ci.nsIObserver], 1.56 + flags: Ci.nsIClassInfo.SINGLETON 1.57 + }), 1.58 + 1.59 + observe: function CS_observe(aSubject, aTopic, aData) 1.60 + { 1.61 + if (aTopic == "xpcom-shutdown") { 1.62 + Services.obs.removeObserver(this, "xpcom-shutdown"); 1.63 + Services.obs.removeObserver(this, "inner-window-destroyed"); 1.64 + Services.obs.removeObserver(this, "memory-pressure"); 1.65 + } 1.66 + else if (aTopic == "inner-window-destroyed") { 1.67 + let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data; 1.68 + this.clearEvents(innerWindowID + ""); 1.69 + } 1.70 + else if (aTopic == "memory-pressure") { 1.71 + this.clearEvents(); 1.72 + } 1.73 + }, 1.74 + 1.75 + /** @private */ 1.76 + init: function CS_init() 1.77 + { 1.78 + Services.obs.addObserver(this, "xpcom-shutdown", false); 1.79 + Services.obs.addObserver(this, "inner-window-destroyed", false); 1.80 + Services.obs.addObserver(this, "memory-pressure", false); 1.81 + }, 1.82 + 1.83 + /** 1.84 + * Get the events array by inner window ID or all events from all windows. 1.85 + * 1.86 + * @param string [aId] 1.87 + * Optional, the inner window ID for which you want to get the array of 1.88 + * cached events. 1.89 + * @returns array 1.90 + * The array of cached events for the given window. If no |aId| is 1.91 + * given this function returns all of the cached events, from any 1.92 + * window. 1.93 + */ 1.94 + getEvents: function CS_getEvents(aId) 1.95 + { 1.96 + if (aId != null) { 1.97 + return (_consoleStorage.get(aId) || []).slice(0); 1.98 + } 1.99 + 1.100 + let result = []; 1.101 + 1.102 + for (let [id, events] of _consoleStorage) { 1.103 + result.push.apply(result, events); 1.104 + } 1.105 + 1.106 + return result.sort(function(a, b) { 1.107 + return a.timeStamp - b.timeStamp; 1.108 + }); 1.109 + }, 1.110 + 1.111 + /** 1.112 + * Record an event associated with the given window ID. 1.113 + * 1.114 + * @param string aId 1.115 + * The ID of the inner window for which the event occurred or "jsm" for 1.116 + * messages logged from JavaScript modules.. 1.117 + * @param object aEvent 1.118 + * A JavaScript object you want to store. 1.119 + */ 1.120 + recordEvent: function CS_recordEvent(aId, aEvent) 1.121 + { 1.122 + if (!_consoleStorage.has(aId)) { 1.123 + _consoleStorage.set(aId, []); 1.124 + } 1.125 + 1.126 + let storage = _consoleStorage.get(aId); 1.127 + storage.push(aEvent); 1.128 + 1.129 + // truncate 1.130 + if (storage.length > STORAGE_MAX_EVENTS) { 1.131 + storage.shift(); 1.132 + } 1.133 + 1.134 + Services.obs.notifyObservers(aEvent, "console-storage-cache-event", aId); 1.135 + }, 1.136 + 1.137 + /** 1.138 + * Clear storage data for the given window. 1.139 + * 1.140 + * @param string [aId] 1.141 + * Optional, the inner window ID for which you want to clear the 1.142 + * messages. If this is not specified all of the cached messages are 1.143 + * cleared, from all window objects. 1.144 + */ 1.145 + clearEvents: function CS_clearEvents(aId) 1.146 + { 1.147 + if (aId != null) { 1.148 + _consoleStorage.delete(aId); 1.149 + } 1.150 + else { 1.151 + _consoleStorage.clear(); 1.152 + Services.obs.notifyObservers(null, "console-storage-reset", null); 1.153 + } 1.154 + }, 1.155 +}; 1.156 + 1.157 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ConsoleAPIStorageService]);