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
3 // found in the LICENSE file.
5 var SourceTracker = (function() {
9 * This class keeps track of all NetLog events, grouped into per-source
10 * streams. It receives events from EventsTracker, and passes
11 * them on to all its observers.
15 function SourceTracker() {
16 assertFirstConstructorCall(SourceTracker);
18 // Observers that only want to receive lists of updated SourceEntries.
19 this.sourceEntryObservers_ = [];
21 // True when cookies and authentication information should be removed from
22 // displayed events. When true, such information should be hidden from
24 this.privacyStripping_ = true;
26 // True when times should be displayed as milliseconds since the first
27 // event, as opposed to milliseconds since January 1, 1970.
28 this.useRelativeTimes_ = true;
32 EventsTracker.getInstance().addLogEntryObserver(this);
35 cr.addSingletonGetter(SourceTracker);
37 SourceTracker.prototype = {
39 * Clears all log entries and SourceEntries and related state.
41 clearEntries_: function() {
42 // Used for sorting entries with automatically assigned IDs.
43 this.maxReceivedSourceId_ = 0;
45 // Next unique id to be assigned to a log entry without a source.
46 // Needed to identify associated GUI elements, etc.
47 this.nextSourcelessEventId_ = -1;
49 // Ordered list of log entries. Needed to maintain original order when
50 // generating log dumps
51 this.capturedEvents_ = [];
53 this.sourceEntries_ = {};
57 * Returns a list of all SourceEntries.
59 getAllSourceEntries: function() {
60 return this.sourceEntries_;
64 * Returns the description of the specified SourceEntry, or an empty string
65 * if it doesn't exist.
67 getDescription: function(id) {
68 var entry = this.getSourceEntry(id);
70 return entry.getDescription();
75 * Returns the specified SourceEntry.
77 getSourceEntry: function(id) {
78 return this.sourceEntries_[id];
82 * Sends each entry to all observers and updates |capturedEvents_|.
83 * Also assigns unique ids to log entries without a source.
85 onReceivedLogEntries: function(logEntries) {
86 // List source entries with new log entries. Sorted chronologically, by
87 // first new log entry.
88 var updatedSourceEntries = [];
90 var updatedSourceEntryIdMap = {};
92 for (var e = 0; e < logEntries.length; ++e) {
93 var logEntry = logEntries[e];
95 // Assign unique ID, if needed.
96 // TODO(mmenke): Remove this, and all other code to handle 0 source
97 // IDs when M19 hits stable.
98 if (logEntry.source.id == 0) {
99 logEntry.source.id = this.nextSourcelessEventId_;
100 --this.nextSourcelessEventId_;
101 } else if (this.maxReceivedSourceId_ < logEntry.source.id) {
102 this.maxReceivedSourceId_ = logEntry.source.id;
105 // Create/update SourceEntry object.
106 var sourceEntry = this.sourceEntries_[logEntry.source.id];
108 sourceEntry = new SourceEntry(logEntry, this.maxReceivedSourceId_);
109 this.sourceEntries_[logEntry.source.id] = sourceEntry;
111 sourceEntry.update(logEntry);
114 // Add to updated SourceEntry list, if not already in it.
115 if (!updatedSourceEntryIdMap[logEntry.source.id]) {
116 updatedSourceEntryIdMap[logEntry.source.id] = sourceEntry;
117 updatedSourceEntries.push(sourceEntry);
121 this.capturedEvents_ = this.capturedEvents_.concat(logEntries);
122 for (var i = 0; i < this.sourceEntryObservers_.length; ++i) {
123 this.sourceEntryObservers_[i].onSourceEntriesUpdated(
124 updatedSourceEntries);
129 * Called when all log events have been deleted.
131 onAllLogEntriesDeleted: function() {
132 this.clearEntries_();
133 for (var i = 0; i < this.sourceEntryObservers_.length; ++i)
134 this.sourceEntryObservers_[i].onAllSourceEntriesDeleted();
138 * Sets the value of |privacyStripping_| and informs log observers
141 setPrivacyStripping: function(privacyStripping) {
142 this.privacyStripping_ = privacyStripping;
143 for (var i = 0; i < this.sourceEntryObservers_.length; ++i) {
144 if (this.sourceEntryObservers_[i].onPrivacyStrippingChanged)
145 this.sourceEntryObservers_[i].onPrivacyStrippingChanged();
150 * Returns whether or not cookies and authentication information should be
151 * displayed for events that contain them.
153 getPrivacyStripping: function() {
154 return this.privacyStripping_;
158 * Sets the value of |useRelativeTimes_| and informs log observers
161 setUseRelativeTimes: function(useRelativeTimes) {
162 this.useRelativeTimes_ = useRelativeTimes;
163 for (var i = 0; i < this.sourceEntryObservers_.length; ++i) {
164 if (this.sourceEntryObservers_[i].onUseRelativeTimesChanged)
165 this.sourceEntryObservers_[i].onUseRelativeTimesChanged();
170 * Returns true if times should be displayed as milliseconds since the first
173 getUseRelativeTimes: function() {
174 return this.useRelativeTimes_;
178 * Adds a listener of SourceEntries. |observer| will be called back when
179 * SourceEntries are added or modified, source entries are deleted, or
180 * privacy stripping changes:
182 * observer.onSourceEntriesUpdated(sourceEntries)
183 * observer.onAllSourceEntriesDeleted()
184 * observer.onPrivacyStrippingChanged()
186 addSourceEntryObserver: function(observer) {
187 this.sourceEntryObservers_.push(observer);
191 return SourceTracker;