1<!DOCTYPE html>
2<!--
3Copyright 2015 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/base/iteration_helpers.html">
9<link rel="import" href="/tracing/model/container_memory_dump.html">
10<link rel="import" href="/tracing/ui/analysis/analysis_link.html">
11<link rel="import" href="/tracing/ui/analysis/analysis_sub_view.html">
12<link rel="import" href="/tracing/ui/analysis/memory_dump_header_pane.html">
13<link rel="import" href="/tracing/ui/analysis/stacked_pane_view.html">
14<link rel="import" href="/tracing/ui/base/dom_helpers.html">
15<link rel="import" href="/tracing/value/ui/scalar_span.html">
16<link rel="import" href="/tracing/value/unit.html">
17
18<polymer-element name="tr-ui-a-container-memory-dump-sub-view"
19    extends="tr-ui-a-sub-view">
20  <template>
21    <div id="content"></div>
22  </template>
23</polymer-element>
24<script>
25'use strict';
26
27tr.exportTo('tr.ui.analysis', function() {
28
29  Polymer('tr-ui-a-container-memory-dump-sub-view', {
30    set selection(selection) {
31      if (selection === undefined) {
32        this.currentSelection_ = undefined;
33        this.dumpsByContainerName_ = undefined;
34        this.updateContents_();
35        return;
36      }
37
38      // Check that the selection contains only container memory dumps.
39      selection.forEach(function(event) {
40        if (!(event instanceof tr.model.ContainerMemoryDump)) {
41          throw new Error(
42              'Memory dump sub-view only supports container memory dumps');
43        }
44      });
45      this.currentSelection_ = selection;
46
47      // Group the selected memory dumps by container name and sort them
48      // chronologically.
49      this.dumpsByContainerName_ = tr.b.group(this.currentSelection_.toArray(),
50          function(dump) {
51            return dump.containerName;
52          });
53      tr.b.iterItems(this.dumpsByContainerName_,
54          function(containerName, dumps) {
55            dumps.sort(function(a, b) { return a.start - b.start; });
56          });
57
58      this.updateContents_();
59    },
60
61    get selection() {
62      return this.currentSelection_;
63    },
64
65    get requiresTallView() {
66      return true;
67    },
68
69    updateContents_: function() {
70      this.$.content.textContent = '';
71
72      if (this.dumpsByContainerName_ === undefined)
73        return;
74
75      var containerNames = Object.keys(this.dumpsByContainerName_);
76      if (containerNames.length === 0)
77        return;
78
79      if (containerNames.length > 1)
80        this.buildViewForMultipleContainerNames_();
81      else
82        this.buildViewForSingleContainerName_();
83    },
84
85    buildViewForSingleContainerName_: function() {
86      var containerMemoryDumps =
87          tr.b.dictionaryValues(this.dumpsByContainerName_)[0];
88      var dumpView = this.ownerDocument.createElement(
89          'tr-ui-a-stacked-pane-view');
90      this.$.content.appendChild(dumpView);
91      dumpView.setPaneBuilder(function() {
92        var headerPane = document.createElement(
93            'tr-ui-a-memory-dump-header-pane');
94        headerPane.containerMemoryDumps = containerMemoryDumps;
95        return headerPane;
96      });
97    },
98
99    buildViewForMultipleContainerNames_: function() {
100      // TODO(petrcermak): Provide a more sophisticated view for this case.
101      var ownerDocument = this.ownerDocument;
102
103      var rows = tr.b.dictionaryValues(tr.b.mapItems(this.dumpsByContainerName_,
104          function(containerName, dumps) {
105            return {
106              containerName: containerName,
107              subRows: dumps,
108              isExpanded: true
109            };
110          }));
111      rows.sort(function(a, b) {
112        return a.containerName.localeCompare(b.containerName);
113      });
114
115      var columns = [
116        {
117          title: 'Dump',
118
119          value: function(row) {
120            if (row.subRows === undefined)
121              return this.singleDumpValue_(row);
122            else
123              return this.groupedDumpValue_(row);
124          },
125
126          singleDumpValue_: function(row) {
127            var linkEl = ownerDocument.createElement('tr-ui-a-analysis-link');
128            linkEl.setSelectionAndContent(new tr.model.EventSet([row]));
129            linkEl.appendChild(tr.v.ui.createScalarSpan(row.start, {
130              unit: tr.v.Unit.byName.timeStampInMs,
131              ownerDocument: ownerDocument
132            }));
133            return linkEl;
134          },
135
136          groupedDumpValue_: function(row) {
137            var linkEl = ownerDocument.createElement('tr-ui-a-analysis-link');
138            linkEl.setSelectionAndContent(new tr.model.EventSet(row.subRows));
139            linkEl.appendChild(tr.ui.b.createSpan({
140              ownerDocument: ownerDocument,
141              textContent: row.subRows.length + ' memory dump' +
142                  (row.subRows.length === 1 ? '' : 's') + ' in '
143            }));
144            linkEl.appendChild(tr.ui.b.createSpan({
145              ownerDocument: ownerDocument,
146              textContent: row.containerName,
147              bold: true
148            }));
149            return linkEl;
150          }
151        }
152      ];
153
154      var table = this.ownerDocument.createElement('tr-ui-b-table');
155      table.tableColumns = columns;
156      table.tableRows = rows;
157      table.showHeader = false;
158      table.rebuild();
159      this.$.content.appendChild(table);
160    }
161  });
162
163  return {};
164});
165</script>
166