Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var EventsTracker = (function() { | 5 var EventsTracker = (function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * This class keeps track of all NetLog events. | 9 * This class keeps track of all NetLog events. |
| 10 * It receives events from the browser and when loading a log file, and passes | 10 * It receives events from the browser and when loading a log file, and passes |
| 11 * them on to all its observers. | 11 * them on to all its observers. |
| 12 * | 12 * |
| 13 * @constructor | 13 * @constructor |
| 14 */ | 14 */ |
| 15 function EventsTracker() { | 15 function EventsTracker() { |
| 16 assertFirstConstructorCall(EventsTracker); | 16 assertFirstConstructorCall(EventsTracker); |
| 17 | 17 |
| 18 this.capturedEvents_ = []; | 18 this.capturedEvents_ = []; |
| 19 this.observers_ = []; | 19 this.observers_ = []; |
| 20 | |
| 21 // Controls how large |capturedEvents_| can grow. | |
| 22 this.softLimit_ = Infinity; | |
| 23 this.hardLimit_ = Infinity; | |
| 20 } | 24 } |
| 21 | 25 |
| 22 cr.addSingletonGetter(EventsTracker); | 26 cr.addSingletonGetter(EventsTracker); |
| 23 | 27 |
| 24 EventsTracker.prototype = { | 28 EventsTracker.prototype = { |
| 25 /** | 29 /** |
| 26 * Returns a list of all captured events. | 30 * Returns a list of all captured events. |
| 27 */ | 31 */ |
| 28 getAllCapturedEvents: function() { | 32 getAllCapturedEvents: function() { |
| 29 return this.capturedEvents_; | 33 return this.capturedEvents_; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 46 }, | 50 }, |
| 47 | 51 |
| 48 /** | 52 /** |
| 49 * Adds captured events, and broadcasts them to any observers. | 53 * Adds captured events, and broadcasts them to any observers. |
| 50 */ | 54 */ |
| 51 addLogEntries: function(logEntries) { | 55 addLogEntries: function(logEntries) { |
| 52 this.capturedEvents_ = this.capturedEvents_.concat(logEntries); | 56 this.capturedEvents_ = this.capturedEvents_.concat(logEntries); |
| 53 for (var i = 0; i < this.observers_.length; ++i) { | 57 for (var i = 0; i < this.observers_.length; ++i) { |
| 54 this.observers_[i].onReceivedLogEntries(logEntries); | 58 this.observers_[i].onReceivedLogEntries(logEntries); |
| 55 } | 59 } |
| 60 | |
| 61 // Check that we haven't grown too big. If so, toss out older events. | |
| 62 if (this.getNumCapturedEvents() > this.hardLimit_) { | |
|
mmenke
2012/03/06 15:21:18
nit: Remove extra space.
eroman
2012/03/06 17:14:14
Done.
| |
| 63 var originalEvents = this.capturedEvents_; | |
| 64 this.deleteAllLogEntries(); | |
| 65 // Delete the oldest events until we reach the soft limit. | |
| 66 originalEvents.splice(0, originalEvents.length - this.softLimit_); | |
| 67 this.addLogEntries(originalEvents); | |
| 68 } | |
| 56 }, | 69 }, |
| 57 | 70 |
| 58 /** | 71 /** |
| 59 * Adds a listener of log entries. |observer| will be called back when new | 72 * Adds a listener of log entries. |observer| will be called back when new |
| 60 * log data arrives or all entries are deleted: | 73 * log data arrives or all entries are deleted: |
| 61 * | 74 * |
| 62 * observer.onReceivedLogEntries(entries) | 75 * observer.onReceivedLogEntries(entries) |
| 63 * observer.onAllLogEntriesDeleted() | 76 * observer.onAllLogEntriesDeleted() |
| 64 */ | 77 */ |
| 65 addLogEntryObserver: function(observer) { | 78 addLogEntryObserver: function(observer) { |
| 66 this.observers_.push(observer); | 79 this.observers_.push(observer); |
| 80 }, | |
| 81 | |
| 82 /** | |
| 83 * Set bounds on the maximum number of events that will be tracked. This | |
| 84 * helps to bound the total amount of memory usage, since otherwise | |
| 85 * long-running capture sessions can exhaust the renderer's memory and | |
| 86 * crash. | |
| 87 * | |
| 88 * Once |hardLimit| number of events have been captured we do a garbage | |
| 89 * collection and toss out old events, bringing our count down to | |
| 90 * |softLimit|. | |
| 91 * | |
| 92 * To log observers this will look like all the events got deleted, and | |
| 93 * then subsequently a bunch of new events were received. In other words, it | |
| 94 * behaves the same as if the user had simply started logging a bit later | |
| 95 * in time! | |
| 96 */ | |
| 97 setLimits: function(softLimit, hardLimit) { | |
| 98 if (hardLimit != Infinity && softLimit >= hardLimit) | |
| 99 throw 'hardLimit must be greater than softLimit'; | |
| 100 | |
| 101 this.softLimit_ = softLimit; | |
| 102 this.hardLimit_ = hardLimit; | |
| 67 } | 103 } |
| 68 }; | 104 }; |
| 69 | 105 |
| 70 return EventsTracker; | 106 return EventsTracker; |
| 71 })(); | 107 })(); |
| OLD | NEW |