blob: 5a1d310c6361c02297fed787613a1e339a0ffcf6 (
plain)
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
|
// Copyright 2013 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.
var g_main_view = null;
/**
* This class is the root view object of the page.
*/
var MainView = (function() {
'use strict';
/**
* @constructor
*/
function MainView() {
$('button-update').onclick = function() {
chrome.send('update');
};
};
MainView.prototype = {
/**
* Receiving notification to display memory snapshot.
* @param {Object} Information about memory in JSON format.
*/
onSetSnapshot: function(browser) {
$('json').textContent = JSON.stringify(browser);
$('json').style.display = 'block';
$('os-value').textContent = browser['os'] + ' (' +
browser['os_version'] + ')';
$('uptime-value').textContent =
secondsToHMS(Math.floor(browser['uptime'] / 1000));
this.updateSnapshot(browser['processes']);
this.updateExtensions(browser['extensions']);
},
/**
* Update process information table.
* @param {Object} processes information about memory.
*/
updateSnapshot: function(processes) {
// Remove existing processes.
var size = $('snapshot-view').getElementsByClassName('process').length;
for (var i = 0; i < size; ++i) {
$('snapshot-view').deleteRow(-1);
}
var template = $('process-template').childNodes;
// Add processes.
for (var p in processes) {
var process = processes[p];
var row = $('snapshot-view').insertRow(-1);
// We skip |template[0]|, because it is a (invalid) Text object.
for (var i = 1; i < template.length; ++i) {
var value = '---';
switch (template[i].className) {
case 'process-id':
value = process['pid'];
break;
case 'process-info':
value = process['type'];
if (process['type'].match(/^Tab/) && 'history' in process) {
// Append each tab's history.
for (var j = 0; j < process['history'].length; ++j) {
value += '<dl><dt>History ' + j + ':' +
JoinLinks(process['history'][j]) + '</dl>';
}
} else {
value += '<br>' + process['titles'].join('<br>');
}
break;
case 'process-memory-private':
value = process['memory_private'];
break;
case 'process-memory-v8':
if (process['v8_alloc'] !== undefined) {
value = process['v8_used'] + '<br>/ ' + process['v8_alloc'];
}
break;
}
var col = row.insertCell(-1);
col.innerHTML = value;
col.className = template[i].className;
}
row.setAttribute('class', 'process');
}
},
/**
* Update extension information table.
* @param {Object} extensions information about memory.
*/
updateExtensions: function(extensions) {
// Remove existing information.
var size =
$('extension-view').getElementsByClassName('extension').length;
for (var i = 0; i < size; ++i) {
$('extension-view').deleteRow(-1);
}
var template = $('extension-template').childNodes;
for (var id in extensions) {
var extension = extensions[id];
var row = $('extension-view').insertRow(-1);
// We skip |template[0]|, because it is a (invalid) Text object.
for (var i = 1; i < template.length; ++i) {
var value = '---';
switch (template[i].className) {
case 'extension-id':
value = extension['pid'];
break;
case 'extension-info':
value = extension['titles'].join('<br>');
break;
case 'extension-memory':
value = extension['memory_private'];
break;
}
var col = row.insertCell(-1);
col.innerHTML = value;
col.className = template[i].className;
}
row.setAttribute('class', 'extension');
}
}
};
function JoinLinks(tab) {
var line = '';
for (var l in tab['history']) {
var history = tab['history'][l];
var title = (history['title'] == '') ? history['url'] : history['title'];
var url = '<a href="' + history['url'] + '">' + HTMLEscape(title) +
'</a> (' + secondsToHMS(history['time']) + ' ago)';
if (l == tab['index']) {
url = '<strong>' + url + '</strong>';
}
line += '<dd>' + url;
}
return line;
};
/**
* Produces a readable string int the format '<HH> hours <MM> min. <SS> sec.'
* representing the amount of time provided as the number of seconds.
* @param {number} totalSeconds The total amount of seconds.
* @return {string} The formatted HH hours/hours MM min. SS sec. string
*/
function secondsToHMS(totalSeconds) {
totalSeconds = Number(totalSeconds);
var hour = Math.floor(totalSeconds / 3600);
var min = Math.floor(totalSeconds % 3600 / 60);
var sec = Math.floor(totalSeconds % 60);
return (hour > 0 ? (hour + (hour > 1 ? ' hours ' : ' hour ')) : '') +
(min > 0 ? (min + ' min. ') : '') +
(sec + ' sec. ');
}
return MainView;
})();
/**
* Initialize everything once we have access to chrome://memory-internals.
*/
document.addEventListener('DOMContentLoaded', function() {
g_main_view = new MainView();
});
|