1 // Copyright 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.
5 var g_main_view = null;
8 * This class is the root view object of the page.
10 var MainView = (function() {
17 $('button-update').onclick = function() {
18 chrome.send('update');
22 MainView.prototype = {
24 * Receiving notification to display memory snapshot.
25 * @param {Object} Information about memory in JSON format.
27 onSetSnapshot: function(browser) {
28 this.updateSnapshot(browser['processes']);
29 this.updateExtensions(browser['extensions']);
31 $('os-value').textContent = browser['os'] + ' (' +
32 browser['os_version'] + ')';
33 $('uptime-value').textContent = Math.floor(browser['uptime'] / 1000) +
36 $('json').textContent = JSON.stringify(browser);
37 $('json').style.display = 'block';
41 * Update process information table.
42 * @param {Object} processes information about memory.
44 updateSnapshot: function(processes) {
45 // Remove existing processes.
46 var size = $('snapshot-view').getElementsByClassName('process').length;
47 for (var i = 0; i < size; ++i) {
48 $('snapshot-view').deleteRow(-1);
51 var template = $('process-template').childNodes;
53 for (var p in processes) {
54 var process = processes[p];
56 var row = $('snapshot-view').insertRow(-1);
57 // We skip |template[0]|, because it is a (invalid) Text object.
58 for (var i = 1; i < template.length; ++i) {
60 switch (template[i].className) {
62 value = process['pid'];
65 value = process['type'];
66 if (process['type'].match(/^Tab/)) {
67 // Append each tab's history.
68 for (var j = 0; j < process['history'].length; ++j) {
69 value += '<dl><dt>History ' + j + ':' +
70 JoinLinks(process['history'][j]) + '</dl>';
73 value += '<br>' + process['titles'].join('<br>');
76 case 'process-memory-private':
77 value = process['memory_private'];
79 case 'process-memory-v8':
80 if (process['v8_alloc'] !== undefined) {
81 value = process['v8_used'] + '<br>/ ' + process['v8_alloc'];
85 var col = row.insertCell(-1);
86 col.innerHTML = value;
87 col.className = template[i].className;
89 row.setAttribute('class', 'process');
94 * Update extension information table.
95 * @param {Object} extensions information about memory.
97 updateExtensions: function(extensions) {
98 // Remove existing information.
100 $('extension-view').getElementsByClassName('extension').length;
101 for (var i = 0; i < size; ++i) {
102 $('extension-view').deleteRow(-1);
105 var template = $('extension-template').childNodes;
106 for (var id in extensions) {
107 var extension = extensions[id];
109 var row = $('extension-view').insertRow(-1);
110 // We skip |template[0]|, because it is a (invalid) Text object.
111 for (var i = 1; i < template.length; ++i) {
113 switch (template[i].className) {
115 value = extension['pid'];
117 case 'extension-info':
118 value = extension['titles'].join('<br>');
120 case 'extension-memory':
121 value = extension['memory_private'];
124 var col = row.insertCell(-1);
125 col.innerHTML = value;
126 col.className = template[i].className;
128 row.setAttribute('class', 'extension');
133 function JoinLinks(tab) {
135 for (var l in tab['history']) {
136 var history = tab['history'][l];
137 var title = (history['title'] == '') ? history['url'] : history['title'];
138 var url = '<a href="' + history['url'] + '">' + title + '</a> (' +
139 history['time'] + ' sec. ago)';
140 if (l == tab['index']) {
141 url = '<strong>' + url + '</strong>';
143 line += '<dd>' + url;
152 * Initialize everything once we have access to chrome://memory-internals.
154 document.addEventListener('DOMContentLoaded', function() {
155 g_main_view = new MainView();