1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* EventsView displays a filtered list of all events sharing a source, and
* a details pane for the selected sources.
*
* +----------------------++----------------+
* | filter box || |
* +----------------------+| |
* | || |
* | || |
* | || |
* | || |
* | source list || details |
* | || view |
* | || |
* | || |
* | || |
* | || |
* | || |
* | || |
* +----------------------++----------------+
*/
var EventsView = (function() {
'use strict';
// How soon after updating the filter list the counter should be updated.
var REPAINT_FILTER_COUNTER_TIMEOUT_MS = 0;
// We inherit from View.
var superClass = View;
/*
* @constructor
*/
function EventsView() {
assertFirstConstructorCall(EventsView);
// Call superclass's constructor.
superClass.call(this);
// Initialize the sub-views.
var leftPane = new VerticalSplitView(new DivView(EventsView.TOPBAR_ID),
new DivView(EventsView.LIST_BOX_ID));
this.detailsView_ = new DetailsView(EventsView.DETAILS_LOG_BOX_ID);
this.splitterView_ = new ResizableVerticalSplitView(
leftPane, this.detailsView_, new DivView(EventsView.SIZER_ID));
SourceTracker.getInstance().addSourceEntryObserver(this);
this.tableBody_ = $(EventsView.TBODY_ID);
this.filterInput_ = $(EventsView.FILTER_INPUT_ID);
this.filterCount_ = $(EventsView.FILTER_COUNT_ID);
this.filterInput_.addEventListener('search',
this.onFilterTextChanged_.bind(this), true);
$(EventsView.SELECT_ALL_ID).addEventListener(
'click', this.selectAll_.bind(this), true);
$(EventsView.SORT_BY_ID_ID).addEventListener(
'click', this.sortById_.bind(this), true);
$(EventsView.SORT_BY_SOURCE_TYPE_ID).addEventListener(
'click', this.sortBySourceType_.bind(this), true);
$(EventsView.SORT_BY_DESCRIPTION_ID).addEventListener(
'click', this.sortByDescription_.bind(this), true);
// Sets sort order and filter.
this.setFilter_('');
this.initializeSourceList_();
}
// ID for special HTML element in category_tabs.html
EventsView.TAB_HANDLE_ID = 'tab-handle-events';
// IDs for special HTML elements in events_view.html
EventsView.TBODY_ID = 'events-view-source-list-tbody';
EventsView.FILTER_INPUT_ID = 'events-view-filter-input';
EventsView.FILTER_COUNT_ID = 'events-view-filter-count';
EventsView.SELECT_ALL_ID = 'events-view-select-all';
EventsView.SORT_BY_ID_ID = 'events-view-sort-by-id';
EventsView.SORT_BY_SOURCE_TYPE_ID = 'events-view-sort-by-source';
EventsView.SORT_BY_DESCRIPTION_ID = 'events-view-sort-by-description';
EventsView.DETAILS_LOG_BOX_ID = 'events-view-details-log-box';
EventsView.TOPBAR_ID = 'events-view-filter-box';
EventsView.LIST_BOX_ID = 'events-view-source-list';
EventsView.SIZER_ID = 'events-view-splitter-box';
cr.addSingletonGetter(EventsView);
EventsView.prototype = {
// Inherit the superclass's methods.
__proto__: superClass.prototype,
/**
* Initializes the list of source entries. If source entries are already,
* being displayed, removes them all in the process.
*/
initializeSourceList_: function() {
this.currentSelectedRows_ = [];
this.sourceIdToRowMap_ = {};
this.tableBody_.innerHTML = '';
this.numPrefilter_ = 0;
this.numPostfilter_ = 0;
this.invalidateFilterCounter_();
this.invalidateDetailsView_();
},
setGeometry: function(left, top, width, height) {
superClass.prototype.setGeometry.call(this, left, top, width, height);
this.splitterView_.setGeometry(left, top, width, height);
},
show: function(isVisible) {
superClass.prototype.show.call(this, isVisible);
this.splitterView_.show(isVisible);
},
getFilterText_: function() {
return this.filterInput_.value;
},
setFilterText_: function(filterText) {
this.filterInput_.value = filterText;
this.onFilterTextChanged_();
},
onFilterTextChanged_: function() {
this.setFilter_(this.getFilterText_());
},
/**
* Updates text in the details view when security stripping is toggled.
*/
onSecurityStrippingChanged: function() {
this.invalidateDetailsView_();
},
comparisonFuncWithReversing_: function(a, b) {
var result = this.comparisonFunction_(a, b);
if (this.doSortBackwards_)
result *= -1;
return result;
},
sort_: function() {
var sourceEntries = [];
for (var id in this.sourceIdToRowMap_) {
sourceEntries.push(this.sourceIdToRowMap_[id].getSourceEntry());
}
sourceEntries.sort(this.comparisonFuncWithReversing_.bind(this));
// Reposition source rows from back to front.
for (var i = sourceEntries.length - 2; i >= 0; --i) {
var sourceRow = this.sourceIdToRowMap_[sourceEntries[i].getSourceId()];
var nextSourceId = sourceEntries[i + 1].getSourceId();
if (sourceRow.getNextNodeSourceId() != nextSourceId) {
var nextSourceRow = this.sourceIdToRowMap_[nextSourceId];
sourceRow.moveBefore(nextSourceRow);
}
}
},
/**
* Looks for the first occurence of |directive|:parameter in |sourceText|.
* Parameter can be an empty string.
*
* On success, returns an object with two fields:
* |remainingText| - |sourceText| with |directive|:parameter removed,
and excess whitespace deleted.
* |parameter| - the parameter itself.
*
* On failure, returns null.
*/
parseDirective_: function(sourceText, directive) {
// Adding a leading space allows a single regexp to be used, regardless of
// whether or not the directive is at the start of the string.
sourceText = ' ' + sourceText;
var regExp = new RegExp('\\s+' + directive + ':(\\S*)\\s*', 'i');
var matchInfo = regExp.exec(sourceText);
if (matchInfo == null)
return null;
return {'remainingText': sourceText.replace(regExp, ' ').trim(),
'parameter': matchInfo[1]};
},
/**
* Just like parseDirective_, except can optionally be a '-' before or
* the parameter, to negate it. Before is more natural, after
* allows more convenient toggling.
*
* Returned value has the additional field |isNegated|, and a leading
* '-' will be removed from |parameter|, if present.
*/
parseNegatableDirective_: function(sourceText, directive) {
var matchInfo = this.parseDirective_(sourceText, directive);
if (matchInfo == null)
return null;
// Remove any leading or trailing '-' from the directive.
var negationInfo = /^(-?)(\S*?)$/.exec(matchInfo.parameter);
matchInfo.parameter = negationInfo[2];
matchInfo.isNegated = (negationInfo[1] == '-');
return matchInfo;
},
/**
* Parse any "sort:" directives, and update |comparisonFunction_| and
* |doSortBackwards_|as needed. Note only the last valid sort directive
* is used.
*
* Returns |filterText| with all sort directives removed, including
* invalid ones.
*/
parseSortDirectives_: function(filterText) {
this.comparisonFunction_ = compareSourceId;
this.doSortBackwards_ = false;
while (true) {
var sortInfo = this.parseNegatableDirective_(filterText, 'sort');
if (sortInfo == null)
break;
var comparisonName = sortInfo.parameter.toLowerCase();
if (COMPARISON_FUNCTION_TABLE[comparisonName] != null) {
this.comparisonFunction_ = COMPARISON_FUNCTION_TABLE[comparisonName];
this.doSortBackwards_ = sortInfo.isNegated;
}
filterText = sortInfo.remainingText;
}
return filterText;
},
/**
* Parse any "is:" directives, and update |filter| accordingly.
*
* Returns |filterText| with all "is:" directives removed, including
* invalid ones.
*/
parseRestrictDirectives_: function(filterText, filter) {
while (true) {
var filterInfo = this.parseNegatableDirective_(filterText, 'is');
if (filterInfo == null)
break;
if (filterInfo.parameter == 'active') {
if (!filterInfo.isNegated) {
filter.isActive = true;
} else {
filter.isInactive = true;
}
}
if (filterInfo.parameter == 'error') {
if (!filterInfo.isNegated) {
filter.isError = true;
} else {
filter.isNotError = true;
}
}
filterText = filterInfo.remainingText;
}
return filterText;
},
/**
* Parses all directives that take arbitrary strings as input,
* and updates |filter| accordingly. Directives of these types
* are stored as lists.
*
* Returns |filterText| with all recognized directives removed.
*/
parseStringDirectives_: function(filterText, filter) {
var directives = ['type', 'id'];
for (var i = 0; i < directives.length; ++i) {
while (true) {
var directive = directives[i];
var filterInfo = this.parseDirective_(filterText, directive);
if (filterInfo == null)
break;
if (!filter[directive])
filter[directive] = [];
filter[directive].push(filterInfo.parameter);
filterText = filterInfo.remainingText;
}
}
return filterText;
},
/*
* Converts |filterText| into an object representing the filter.
*/
createFilter_: function(filterText) {
var filter = {};
filterText = filterText.toLowerCase();
filterText = this.parseRestrictDirectives_(filterText, filter);
filterText = this.parseStringDirectives_(filterText, filter);
filter.text = filterText.trim();
return filter;
},
setFilter_: function(filterText) {
var lastComparisonFunction = this.comparisonFunction_;
var lastDoSortBackwards = this.doSortBackwards_;
filterText = this.parseSortDirectives_(filterText);
if (lastComparisonFunction != this.comparisonFunction_ ||
lastDoSortBackwards != this.doSortBackwards_) {
this.sort_();
}
this.currentFilter_ = this.createFilter_(filterText);
// Iterate through all of the rows and see if they match the filter.
for (var id in this.sourceIdToRowMap_) {
var entry = this.sourceIdToRowMap_[id];
entry.setIsMatchedByFilter(entry.matchesFilter(this.currentFilter_));
}
},
/**
* Repositions |sourceRow|'s in the table using an insertion sort.
* Significantly faster than sorting the entire table again, when only
* one entry has changed.
*/
insertionSort_: function(sourceRow) {
// SourceRow that should be after |sourceRow|, if it needs
// to be moved earlier in the list.
var sourceRowAfter = sourceRow;
while (true) {
var prevSourceId = sourceRowAfter.getPreviousNodeSourceId();
if (prevSourceId == null)
break;
var prevSourceRow = this.sourceIdToRowMap_[prevSourceId];
if (this.comparisonFuncWithReversing_(
sourceRow.getSourceEntry(),
prevSourceRow.getSourceEntry()) >= 0) {
break;
}
sourceRowAfter = prevSourceRow;
}
if (sourceRowAfter != sourceRow) {
sourceRow.moveBefore(sourceRowAfter);
return;
}
var sourceRowBefore = sourceRow;
while (true) {
var nextSourceId = sourceRowBefore.getNextNodeSourceId();
if (nextSourceId == null)
break;
var nextSourceRow = this.sourceIdToRowMap_[nextSourceId];
if (this.comparisonFuncWithReversing_(
sourceRow.getSourceEntry(),
nextSourceRow.getSourceEntry()) <= 0) {
break;
}
sourceRowBefore = nextSourceRow;
}
if (sourceRowBefore != sourceRow)
sourceRow.moveAfter(sourceRowBefore);
},
/**
* Called whenever SourceEntries are updated with new log entries. Updates
* the corresponding table rows, sort order, and the details view as needed.
*/
onSourceEntriesUpdated: function(sourceEntries) {
var isUpdatedSourceSelected = false;
var numNewSourceEntries = 0;
for (var i = 0; i < sourceEntries.length; ++i) {
var sourceEntry = sourceEntries[i];
// Lookup the row.
var sourceRow = this.sourceIdToRowMap_[sourceEntry.getSourceId()];
if (!sourceRow) {
sourceRow = new SourceRow(this, sourceEntry);
this.sourceIdToRowMap_[sourceEntry.getSourceId()] = sourceRow;
++numNewSourceEntries;
} else {
sourceRow.onSourceUpdated();
}
if (sourceRow.isSelected())
isUpdatedSourceSelected = true;
// TODO(mmenke): Fix sorting when sorting by duration.
// Duration continuously increases for all entries that
// are still active. This can result in incorrect
// sorting, until sort_ is called.
this.insertionSort_(sourceRow);
}
if (isUpdatedSourceSelected)
this.invalidateDetailsView_();
if (numNewSourceEntries)
this.incrementPrefilterCount(numNewSourceEntries);
},
/**
* Returns the SourceRow with the specified ID, if there is one.
* Otherwise, returns undefined.
*/
getSourceRow: function(id) {
return this.sourceIdToRowMap_[id];
},
/**
* Called whenever all log events are deleted.
*/
onAllSourceEntriesDeleted: function() {
this.initializeSourceList_();
},
/**
* Called when either a log file is loaded, after clearing the old entries,
* but before getting any new ones.
*/
onLoadLogStart: function() {
// Needed to sort new sourceless entries correctly.
this.maxReceivedSourceId_ = 0;
},
onLoadLogFinish: function(data) {
return true;
},
incrementPrefilterCount: function(offset) {
this.numPrefilter_ += offset;
this.invalidateFilterCounter_();
},
incrementPostfilterCount: function(offset) {
this.numPostfilter_ += offset;
this.invalidateFilterCounter_();
},
onSelectionChanged: function() {
this.invalidateDetailsView_();
},
clearSelection: function() {
var prevSelection = this.currentSelectedRows_;
this.currentSelectedRows_ = [];
// Unselect everything that is currently selected.
for (var i = 0; i < prevSelection.length; ++i) {
prevSelection[i].setSelected(false);
}
this.onSelectionChanged();
},
selectAll_: function(event) {
for (var id in this.sourceIdToRowMap_) {
var sourceRow = this.sourceIdToRowMap_[id];
if (sourceRow.isMatchedByFilter()) {
sourceRow.setSelected(true);
}
}
event.preventDefault();
},
unselectAll_: function() {
var entries = this.currentSelectedRows_.slice(0);
for (var i = 0; i < entries.length; ++i) {
entries[i].setSelected(false);
}
},
/**
* If |params| includes a query, replaces the current filter and unselects.
* all items. If it includes a selection, tries to select the relevant
* item.
*/
setParameters: function(params) {
if (params.q) {
this.unselectAll_();
this.setFilterText_(params.q);
}
if (params.s) {
var sourceRow = this.sourceIdToRowMap_[params.s];
if (sourceRow) {
sourceRow.setSelected(true);
this.scrollToSourceId(params.s);
}
}
},
/**
* Scrolls to the source indicated by |sourceId|, if displayed.
*/
scrollToSourceId: function(sourceId) {
this.detailsView_.scrollToSourceId(sourceId);
},
/**
* If already using the specified sort method, flips direction. Otherwise,
* removes pre-existing sort parameter before adding the new one.
*/
toggleSortMethod_: function(sortMethod) {
// Remove old sort directives, if any.
var filterText = this.parseSortDirectives_(this.getFilterText_());
// If already using specified sortMethod, sort backwards.
if (!this.doSortBackwards_ &&
COMPARISON_FUNCTION_TABLE[sortMethod] == this.comparisonFunction_)
sortMethod = '-' + sortMethod;
filterText = 'sort:' + sortMethod + ' ' + filterText;
this.setFilterText_(filterText.trim());
},
sortById_: function(event) {
this.toggleSortMethod_('id');
},
sortBySourceType_: function(event) {
this.toggleSortMethod_('source');
},
sortByDescription_: function(event) {
this.toggleSortMethod_('desc');
},
/**
* Modifies the map of selected rows to include/exclude the one with
* |sourceId|, if present. Does not modify checkboxes or the LogView.
* Should only be called by a SourceRow in response to its selection
* state changing.
*/
modifySelectionArray: function(sourceId, addToSelection) {
var sourceRow = this.sourceIdToRowMap_[sourceId];
if (!sourceRow)
return;
// Find the index for |sourceEntry| in the current selection list.
var index = -1;
for (var i = 0; i < this.currentSelectedRows_.length; ++i) {
if (this.currentSelectedRows_[i] == sourceRow) {
index = i;
break;
}
}
if (index != -1 && !addToSelection) {
// Remove from the selection.
this.currentSelectedRows_.splice(index, 1);
}
if (index == -1 && addToSelection) {
this.currentSelectedRows_.push(sourceRow);
}
},
getSelectedSourceEntries_: function() {
var sourceEntries = [];
for (var i = 0; i < this.currentSelectedRows_.length; ++i) {
sourceEntries.push(this.currentSelectedRows_[i].getSourceEntry());
}
return sourceEntries;
},
invalidateDetailsView_: function() {
this.detailsView_.setData(this.getSelectedSourceEntries_());
},
invalidateFilterCounter_: function() {
if (!this.outstandingRepaintFilterCounter_) {
this.outstandingRepaintFilterCounter_ = true;
window.setTimeout(this.repaintFilterCounter_.bind(this),
REPAINT_FILTER_COUNTER_TIMEOUT_MS);
}
},
repaintFilterCounter_: function() {
this.outstandingRepaintFilterCounter_ = false;
this.filterCount_.innerHTML = '';
addTextNode(this.filterCount_,
this.numPostfilter_ + ' of ' + this.numPrefilter_);
}
}; // end of prototype.
// ------------------------------------------------------------------------
// Helper code for comparisons
// ------------------------------------------------------------------------
var COMPARISON_FUNCTION_TABLE = {
// sort: and sort:- are allowed
'': compareSourceId,
'active': compareActive,
'desc': compareDescription,
'description': compareDescription,
'duration': compareDuration,
'id': compareSourceId,
'source': compareSourceType,
'type': compareSourceType
};
/**
* Sorts active entries first. If both entries are inactive, puts the one
* that was active most recently first. If both are active, uses source ID,
* which puts longer lived events at the top, and behaves better than using
* duration or time of first event.
*/
function compareActive(source1, source2) {
if (!source1.isInactive() && source2.isInactive())
return -1;
if (source1.isInactive() && !source2.isInactive())
return 1;
if (source1.isInactive()) {
var deltaEndTime = source1.getEndTime() - source2.getEndTime();
if (deltaEndTime != 0) {
// The one that ended most recently (Highest end time) should be sorted
// first.
return -deltaEndTime;
}
// If both ended at the same time, then odds are they were related events,
// started one after another, so sort in the opposite order of their
// source IDs to get a more intuitive ordering.
return -compareSourceId(source1, source2);
}
return compareSourceId(source1, source2);
}
function compareDescription(source1, source2) {
var source1Text = source1.getDescription().toLowerCase();
var source2Text = source2.getDescription().toLowerCase();
var compareResult = source1Text.localeCompare(source2Text);
if (compareResult != 0)
return compareResult;
return compareSourceId(source1, source2);
}
function compareDuration(source1, source2) {
var durationDifference = source2.getDuration() - source1.getDuration();
if (durationDifference)
return durationDifference;
return compareSourceId(source1, source2);
}
/**
* For the purposes of sorting by source IDs, entries without a source
* appear right after the SourceEntry with the highest source ID received
* before the sourceless entry. Any ambiguities are resolved by ordering
* the entries without a source by the order in which they were received.
*/
function compareSourceId(source1, source2) {
var sourceId1 = source1.getSourceId();
if (sourceId1 < 0)
sourceId1 = source1.getMaxPreviousEntrySourceId();
var sourceId2 = source2.getSourceId();
if (sourceId2 < 0)
sourceId2 = source2.getMaxPreviousEntrySourceId();
if (sourceId1 != sourceId2)
return sourceId1 - sourceId2;
// One or both have a negative ID. In either case, the source with the
// highest ID should be sorted first.
return source2.getSourceId() - source1.getSourceId();
}
function compareSourceType(source1, source2) {
var source1Text = source1.getSourceTypeString();
var source2Text = source2.getSourceTypeString();
var compareResult = source1Text.localeCompare(source2Text);
if (compareResult != 0)
return compareResult;
return compareSourceId(source1, source2);
}
return EventsView;
})();
|