Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / tracks / thread_track.js
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.
4
5 'use strict';
6
7 tvcm.requireStylesheet('tracing.tracks.thread_track');
8
9 tvcm.require('tracing.tracks.container_track');
10 tvcm.require('tracing.tracks.slice_track');
11 tvcm.require('tracing.tracks.slice_group_track');
12 tvcm.require('tracing.tracks.async_slice_group_track');
13 tvcm.require('tracing.filter');
14 tvcm.require('tvcm.ui');
15 tvcm.require('tvcm.iteration_helpers');
16
17 tvcm.exportTo('tracing.tracks', function() {
18
19   /**
20    * Visualizes a Thread using a series of of SliceTracks.
21    * @constructor
22    */
23   var ThreadTrack = tvcm.ui.define('thread-track',
24                                    tracing.tracks.ContainerTrack);
25   ThreadTrack.prototype = {
26     __proto__: tracing.tracks.ContainerTrack.prototype,
27
28     decorate: function(viewport) {
29       tracing.tracks.ContainerTrack.prototype.decorate.call(this, viewport);
30       this.classList.add('thread-track');
31     },
32
33     get thread() {
34       return this.thread_;
35     },
36
37     set thread(thread) {
38       this.thread_ = thread;
39       this.updateContents_();
40     },
41
42     get hasVisibleContent() {
43       return this.tracks_.length > 0;
44     },
45
46     updateContents_: function() {
47       this.detach();
48
49       if (!this.thread_)
50         return;
51
52       this.heading = this.thread_.userFriendlyName + ': ';
53       this.tooltip = this.thread_.userFriendlyDetails;
54
55       if (this.thread_.asyncSliceGroup.length) {
56         var asyncTrack = new tracing.tracks.AsyncSliceGroupTrack(this.viewport);
57         asyncTrack.group = this.thread_.asyncSliceGroup;
58         if (asyncTrack.hasVisibleContent)
59           this.appendChild(asyncTrack);
60       }
61
62       this.appendThreadSamplesTracks_();
63
64       if (this.thread_.timeSlices) {
65         var timeSlicesTrack = new tracing.tracks.SliceTrack(this.viewport);
66         timeSlicesTrack.heading = '';
67         timeSlicesTrack.height = '4px';
68         timeSlicesTrack.slices = this.thread_.timeSlices;
69         if (timeSlicesTrack.hasVisibleContent)
70           this.appendChild(timeSlicesTrack);
71       }
72
73       if (this.thread_.sliceGroup.length) {
74         var track = new tracing.tracks.SliceGroupTrack(this.viewport);
75         track.heading = this.thread_.userFriendlyName;
76         track.tooltip = this.thread_.userFriendlyDetails;
77         track.group = this.thread_.sliceGroup;
78         if (track.hasVisibleContent)
79           this.appendChild(track);
80       }
81     },
82
83     appendThreadSamplesTracks_: function() {
84       var threadSamples = this.thread_.samples;
85       if (threadSamples === undefined || threadSamples.length === 0)
86         return;
87       var samplesByTitle = {};
88       threadSamples.forEach(function(sample) {
89         if (samplesByTitle[sample.title] === undefined)
90           samplesByTitle[sample.title] = [];
91         samplesByTitle[sample.title].push(sample);
92       });
93
94       var sampleTitles = tvcm.dictionaryKeys(samplesByTitle);
95       sampleTitles.sort();
96
97       sampleTitles.forEach(function(sampleTitle) {
98         var samples = samplesByTitle[sampleTitle];
99         var samplesTrack = new tracing.tracks.SliceTrack(this.viewport);
100         samplesTrack.group = this.thread_;
101         samplesTrack.slices = samples;
102         samplesTrack.heading = this.thread_.userFriendlyName + ': ' +
103             sampleTitle;
104         samplesTrack.tooltip = this.thread_.userFriendlyDetails;
105         samplesTrack.selectionGenerator = function() {
106           var selection = new tracing.Selection();
107           for (var i = 0; i < samplesTrack.slices.length; i++) {
108             selection.push(samplesTrack.slices[i]);
109           }
110           return selection;
111         };
112         this.appendChild(samplesTrack);
113       }, this);
114     },
115
116     collapsedDidChange: function(collapsed) {
117       if (collapsed) {
118         var h = parseInt(this.tracks[0].height);
119         for (var i = 0; i < this.tracks.length; ++i) {
120           if (h > 2) {
121             this.tracks[i].height = Math.floor(h) + 'px';
122           } else {
123             this.tracks[i].style.display = 'none';
124           }
125           h = h * 0.5;
126         }
127       } else {
128         for (var i = 0; i < this.tracks.length; ++i) {
129           this.tracks[i].height = this.tracks[0].height;
130           this.tracks[i].style.display = '';
131         }
132       }
133     }
134   };
135
136   return {
137     ThreadTrack: ThreadTrack
138   };
139 });