- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / examples / api / processes / process_monitor / popup.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 // Shows an updating list of process statistics.
6 function init() {
7   chrome.processes.onUpdatedWithMemory.addListener(
8     function(processes) {
9       var table = "<table>\n" +
10         "<tr><td><b>Process</b></td>" +
11         "<td>OS ID</td>" +
12         "<td>Type</td>" +
13         "<td>Tabs</td>" +
14         "<td>CPU</td>" +
15         "<td>Network</td>" +
16         "<td>Private Memory</td>" +
17         "<td>FPS</td>" +
18         "<td>JS Memory</td>" +
19         "<td></td>" +
20         "</tr>\n";
21       for (pid in processes) {
22         table = displayProcessInfo(processes[pid], table);
23       }
24       table += "</table>\n";
25       var div = document.getElementById("process-list");
26       div.innerHTML = table;
27     });
28
29   document.getElementById("killProcess").onclick = function () {
30     var procId = parseInt(prompt("Enter process ID"));
31     chrome.processes.terminate(procId);
32   }
33 }
34
35 function displayProcessInfo(process, table) {
36   // Format network string like task manager
37   var network = process.network;
38   if (network > 1024) {
39     network = (network / 1024).toFixed(1) + " kB/s";
40   } else if (network > 0) {
41     network += " B/s";
42   } else if (network == -1) {
43     network = "N/A";
44   }
45
46   table +=
47     "<tr><td>" + process.id + "</td>" +
48     "<td>" + process.osProcessId + "</td>" +
49     "<td>" + process.type + "</td>" +
50     "<td>" + process.tabs + "</td>" +
51     "<td>" + process.cpu + "</td>" +
52     "<td>" + network + "</td>";
53
54   if ("privateMemory" in process) {
55     table += "<td>" + (process.privateMemory / 1024) + "K</td>";
56   } else {
57     table += "<td>N/A</td>";
58   }
59   if ("fps" in process) {
60     table += "<td>" + process.fps.toFixed(2) + "</td>";
61   } else {
62     table += "<td>N/A</td>";
63   }
64
65   if ("jsMemoryAllocated" in process) {
66     var allocated = process.jsMemoryAllocated / 1024;
67     var used = process.jsMemoryUsed / 1024;
68     table += "<td>" + allocated.toFixed(2) + "K (" + used.toFixed(2) +
69         "K live)</td>";
70   } else {
71     table += "<td>N/A</td>";
72   }
73
74   table +=
75     "<td></td>" +
76     "</tr>\n";
77   return table;
78 }
79
80 document.addEventListener('DOMContentLoaded', init);