Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / system_stats / system_stats_snapshot_view.js
1 // Copyright (c) 2013 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('system_stats.system_stats_snapshot_view');
8
9 tvcm.require('tracing.analysis.object_snapshot_view');
10 tvcm.require('tracing.analysis.util');
11
12 tvcm.exportTo('system_stats', function() {
13   /*
14    * Displays a system stats snapshot in a human readable form. @constructor
15    */
16   var SystemStatsSnapshotView = tvcm.ui.define('system-stats-snapshot-view',
17       tracing.analysis.ObjectSnapshotView);
18
19   SystemStatsSnapshotView.prototype = {
20     __proto__: tracing.analysis.ObjectSnapshotView.prototype,
21
22     decorate: function() {
23       this.classList.add('system-stats-snapshot-view');
24     },
25
26     updateContents: function() {
27       var snapshot = this.objectSnapshot_;
28       if (!snapshot || !snapshot.getStats()) {
29         this.textContent = 'No system stats snapshot found.';
30         return;
31       }
32       // Clear old snapshot view.
33       this.textContent = '';
34
35       var stats = snapshot.getStats();
36       this.appendChild(this.buildList_(stats));
37     },
38
39     isFloat: function(n) {
40       return typeof n === 'number' && n % 1 !== 0;
41     },
42
43     /**
44      * Creates nested lists.
45      *
46      * @param {Object} stats The current trace system stats entry.
47      * @return {Element} A
48      *         <ul>
49      *         list element.
50      */
51     buildList_: function(stats) {
52       var statList = document.createElement('ul');
53
54       for (var statName in stats) {
55         var statText = document.createElement('li');
56         statText.textContent = '' + statName + ': ';
57         statList.appendChild(statText);
58
59         if (stats[statName] instanceof Object) {
60           statList.appendChild(this.buildList_(stats[statName]));
61         } else {
62           if (this.isFloat(stats[statName]))
63             statText.textContent += stats[statName].toFixed(2);
64           else
65             statText.textContent += stats[statName];
66         }
67       }
68
69       return statList;
70     }
71   };
72
73   tracing.analysis.ObjectSnapshotView.register(
74       'base::TraceEventSystemStatsMonitor::SystemStats',
75       SystemStatsSnapshotView);
76
77   return {
78     SystemStatsSnapshotView: SystemStatsSnapshotView
79   };
80
81 });