Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / resources / net_internals / log_grouper.js
bloba45b5cde5e2b3ce92d757d257971c1c536890ae5
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 /**
6 * @fileoverview
7 * LogGroupEntry is a wrapper around log entries, which makes it easier to
8 * find the corresponding start/end of events.
10 * This is used internally by the log and timeline views to pretty print
11 * collections of log entries.
14 // TODO(eroman): document these methods!
16 var LogGroupEntry = (function() {
17 'use strict';
19 function LogGroupEntry(origEntry, index) {
20 this.orig = origEntry;
21 this.index = index;
24 LogGroupEntry.prototype = {
25 isBegin: function() {
26 return this.orig.phase == EventPhase.PHASE_BEGIN;
29 isEnd: function() {
30 return this.orig.phase == EventPhase.PHASE_END;
33 getDepth: function() {
34 var depth = 0;
35 var p = this.parentEntry;
36 while (p) {
37 depth += 1;
38 p = p.parentEntry;
40 return depth;
44 function findParentIndex(parentStack, eventType) {
45 for (var i = parentStack.length - 1; i >= 0; --i) {
46 if (parentStack[i].orig.type == eventType)
47 return i;
49 return -1;
52 /**
53 * Returns a list of LogGroupEntrys. This basically wraps the original log
54 * entry, but makes it easier to find the start/end of the event.
56 LogGroupEntry.createArrayFrom = function(origEntries) {
57 var groupedEntries = [];
59 // Stack of enclosing PHASE_BEGIN elements.
60 var parentStack = [];
62 for (var i = 0; i < origEntries.length; ++i) {
63 var origEntry = origEntries[i];
65 var groupEntry = new LogGroupEntry(origEntry, i);
66 groupedEntries.push(groupEntry);
68 // If this is the end of an event, match it to the start.
69 if (groupEntry.isEnd()) {
70 // Walk up the parent stack to find the corresponding BEGIN for this
71 // END.
72 var parentIndex =
73 findParentIndex(parentStack, groupEntry.orig.type);
75 if (parentIndex == -1) {
76 // Unmatched end.
77 } else {
78 groupEntry.begin = parentStack[parentIndex];
80 // Consider this as the terminator for all open BEGINs up until
81 // parentIndex.
82 while (parentIndex < parentStack.length) {
83 var p = parentStack.pop();
84 p.end = groupEntry;
89 // Inherit the current parent.
90 if (parentStack.length > 0)
91 groupEntry.parentEntry = parentStack[parentStack.length - 1];
93 if (groupEntry.isBegin())
94 parentStack.push(groupEntry);
97 return groupedEntries;
100 return LogGroupEntry;
101 })();