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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
// Copyright (c) 2011 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.
/** @constructor */
function TaskManager() { }
cr.addSingletonGetter(TaskManager);
/*
* Default columns (column_id, label_id, width, is_default)
* @const
*/
var DEFAULT_COLUMNS = [
['title', 'PAGE_COLUMN', 300, true],
['profileName', 'PROFILE_NAME_COLUMN', 120, false],
['physicalMemory', 'PHYSICAL_MEM_COLUMN', 80, true],
['sharedMemory', 'SHARED_MEM_COLUMN', 80, false],
['privateMemory', 'PRIVATE_MEM_COLUMN', 80, false],
['cpuUsage', 'CPU_COLUMN', 80, true],
['networkUsage', 'NET_COLUMN', 85, true],
['processId', 'PROCESS_ID_COLUMN', 100, false],
['webCoreImageCacheSize', 'WEBCORE_IMAGE_CACHE_COLUMN', 120, false],
['webCoreScriptsCacheSize', 'WEBCORE_SCRIPTS_CACHE_COLUMN', 120, false],
['webCoreCSSCacheSize', 'WEBCORE_CSS_CACHE_COLUMN', 120, false],
['fps', 'FPS_COLUMN', 50, true],
['sqliteMemoryUsed', 'SQLITE_MEMORY_USED_COLUMN', 80, false],
['goatsTeleported', 'GOATS_TELEPORTED_COLUMN', 80, false],
['v8MemoryAllocatedSize', 'JAVASCRIPT_MEMORY_ALLOCATED_COLUMN', 120, false],
];
var localStrings = new LocalStrings();
TaskManager.prototype = {
/**
* Handle window close.
* @public
*/
onClose: function () {
if (!this.disabled_) {
this.disabled_ = true;
this.disableTaskManager();
}
},
/**
* Closes taskmanager dialog.
* After this function is called, onClose() will be called.
* @public
*/
close: function () {
window.close();
},
/**
* Sends commands to kill a process.
* @public
*/
killProcess: function () {
var selectedIndexes = this.selectionModel_.selectedIndexes;
chrome.send('killProcess', selectedIndexes);
},
/**
* Sends command to kill a process.
* @public
*/
openAboutMemory: function () {
chrome.send('openAboutMemory');
},
/**
* Sends command to disable taskmanager model.
* @public
*/
disableTaskManager: function () {
chrome.send('disableTaskManager');
},
/**
* Sends command to enable taskmanager model.
* @public
*/
enableTaskManager: function () {
chrome.send('enableTaskManager');
},
/**
* Initializes taskmanager.
* @public
*/
initialize: function (dialogDom, opt) {
if (!dialogDom) {
console.log('ERROR: dialogDom is not defined.');
return;
}
this.opt_ = opt;
this.initialized_ = true;
this.enableTaskManager();
this.dialogDom_ = dialogDom;
this.document_ = dialogDom.ownerDocument;
$('close-window').addEventListener('click', this.close.bind(this));
$('kill-process').addEventListener('click', this.killProcess.bind(this));
$('about-memory-link').addEventListener('click',
this.openAboutMemory.bind(this));
this.is_column_shown_ = [];
for (var i = 0; i < DEFAULT_COLUMNS.length; i++) {
this.is_column_shown_[i] = DEFAULT_COLUMNS[i][3];
}
this.localized_column_ = [];
for (var i = 0; i < DEFAULT_COLUMNS.length; i++) {
var column_label_id = DEFAULT_COLUMNS[i][1];
var localized_label = localStrings.getString(column_label_id);
// Falls back to raw column_label_id if localized string is not defined.
if (localized_label == "")
localized_label = column_label_id;
this.localized_column_[i] = localized_label;
}
this.initColumnModel_();
this.selectionModel_ = new cr.ui.ListSelectionModel();
this.dataModel_ = new cr.ui.ArrayDataModel([]);
// Initializes compare functions for column sort.
var dm = this.dataModel_;
// Columns to sort by value instead of itself.
var columns_sorted_by_value = [
'cpuUsage', 'physicalMemory', 'sharedMemory', 'privateMemory',
'networkUsage', 'webCoreImageCacheSize', 'webCoreScriptsCacheSize',
'webCoreCSSCacheSize', 'fps', 'sqliteMemoryUsed', 'goatsTeleported',
'v8MemoryAllocatedSize'];
for (var i in columns_sorted_by_value) {
var column_id = columns_sorted_by_value[i];
var compare_func = function() {
var value_id = column_id + 'Value';
return function(a, b) {
return dm.defaultValuesCompareFunction(a[value_id][0],
b[value_id][0]);
};
}();
dm.setCompareFunction(column_id, compare_func);
}
var ary = this.dialogDom_.querySelectorAll('[visibleif]');
for (var i = 0; i < ary.length; i++) {
var expr = ary[i].getAttribute('visibleif');
if (!eval(expr))
ary[i].hidden = true;
}
this.initTable_();
this.initColumnMenu_();
this.table_.redraw();
},
initColumnModel_: function () {
var table_columns = new Array();
for (var i = 0; i < DEFAULT_COLUMNS.length; i++) {
if (!this.is_column_shown_[i])
continue;
var column = DEFAULT_COLUMNS[i];
table_columns.push(new cr.ui.table.TableColumn(column[0],
this.localized_column_[i],
column[2]));
}
for (var i = 0; i < table_columns.length; i++) {
table_columns[i].renderFunction = this.renderColumn_.bind(this);
}
this.columnModel_ = new cr.ui.table.TableColumnModel(table_columns);
},
initColumnMenu_: function () {
this.column_menu_commands_ = [];
this.commandsElement_ = this.document_.createElement('commands');
this.document_.body.appendChild(this.commandsElement_);
this.columnSelectContextMenu_ = this.document_.createElement('menu');
for (var i = 0; i < DEFAULT_COLUMNS.length; i++) {
var column = DEFAULT_COLUMNS[i];
// Creates command element to receive event.
var command = this.document_.createElement('command');
command.id = 'columnContextMenu-' + column[0];
cr.ui.Command.decorate(command);
this.column_menu_commands_[command.id] = command;
this.commandsElement_.appendChild(command);
// Creates menuitem element.
var item = this.document_.createElement('menuitem');
item.command = command;
command.menuitem = item;
item.textContent = this.localized_column_[i];
if (this.is_column_shown_[i])
item.setAttributeNode(this.document_.createAttribute("checked"));
this.columnSelectContextMenu_.appendChild(item);
}
this.document_.body.appendChild(this.columnSelectContextMenu_);
cr.ui.Menu.decorate(this.columnSelectContextMenu_);
cr.ui.contextMenuHandler.addContextMenuProperty(this.table_.header);
this.table_.header.contextMenu = this.columnSelectContextMenu_;
this.document_.addEventListener('command', this.onCommand_.bind(this));
this.document_.addEventListener('canExecute',
this.onCommandCanExecute_.bind(this));
},
initTable_: function () {
if (!this.dataModel_ || !this.selectionModel_ || !this.columnModel_) {
console.log('ERROR: some models are not defined.');
return;
}
this.table_ = this.dialogDom_.querySelector('.detail-table');
cr.ui.Table.decorate(this.table_);
this.table_.dataModel = this.dataModel_;
this.table_.selectionModel = this.selectionModel_;
this.table_.columnModel = this.columnModel_;
// Expands height of row when a process has some tasks.
this.table_.autoExpands = true;
// Sets custom row render function.
this.table_.setRenderFunction(this.renderRow_.bind(this));
},
renderRow_: function(dataItem, table) {
var cm = table.columnModel;
var listItem = new cr.ui.ListItem({label: ''});
listItem.className = 'table-row';
if (this.opt_.isBackgroundMode_ && dataItem.isBackgroundResource)
listItem.className += ' table-background-row';
for (var i = 0; i < cm.size; i++) {
var cell = document.createElement('div');
cell.style.width = cm.getWidth(i) + '%';
cell.className = 'table-row-cell';
cell.appendChild(
cm.getRenderFunction(i).call(null, dataItem, cm.getId(i), table));
listItem.appendChild(cell);
}
return listItem;
},
renderColumn_: function(entry, columnId, table) {
var container = this.document_.createElement('div');
container.id = 'detail-container-' + columnId + '-pid' + entry.processId;
container.className = 'detail-container-' + columnId;
if (entry[columnId]) {
for (var i = 0; i < entry[columnId].length; i++) {
var label = document.createElement('div');
if (columnId == 'title') {
var image = this.document_.createElement('img');
image.className = 'detail-title-image';
image.src = entry['icon'][i];
label.appendChild(image);
var text = this.document_.createElement('div');
text.className = 'detail-title-text';
text.textContent = entry['title'][i];
label.appendChild(text);
} else {
label.textContent = entry[columnId][i];
}
label.id = 'detail-' + columnId + '-pid' + entry.processId + '-' + i;
label.className = 'detail-' + columnId + ' pid' + entry.processId;
container.appendChild(label);
}
}
return container;
},
onTaskChange: function (start, length, tasks) {
var dm = this.dataModel_;
var sm = this.selectionModel_;
if (!dm || !sm)
return;
// Splice takes the to-be-spliced-in array as individual parameters,
// rather than as an array, so we need to perform some acrobatics...
var args = [].slice.call(tasks);
args.unshift(start, length);
var oldSelectedIndexes = sm.selectedIndexes;
dm.splice.apply(dm, args);
sm.selectedIndexes = oldSelectedIndexes;
},
onTaskAdd: function (start, length, tasks) {
var dm = this.dataModel_;
if (!dm)
return;
// Splice takes the to-be-spliced-in array as individual parameters,
// rather than as an array, so we need to perform some acrobatics...
var args = [].slice.call(tasks);
args.unshift(start, 0);
dm.splice.apply(dm, args);
},
onTaskRemove: function (start, length, tasks) {
var dm = this.dataModel_;
if (!dm)
return;
dm.splice(start, length);
},
/**
* Respond to a command being executed.
*/
onCommand_: function(event) {
var command = event.command;
if (command.id.substr(0, 18) == 'columnContextMenu-') {
console.log(command.id.substr(18));
this.onColumnContextMenu_(command.id.substr(18), command);
}
},
onCommandCanExecute_: function(event) {
event.canExecute = true;
},
onColumnContextMenu_: function(id, command) {
var menuitem = command.menuitem;
var checked_item_count = 0;
var is_uncheck = 0;
// Leaves a item visible when user tries making invisible but it is the
// last one.
for (var i = 0; i < DEFAULT_COLUMNS.length; i++) {
var column = DEFAULT_COLUMNS[i];
if (column[0] == id && this.is_column_shown_[i]) {
is_uncheck = 1;
}
if (this.is_column_shown_[i])
checked_item_count++;
}
if (checked_item_count == 1 && is_uncheck) {
return;
}
// Toggles the visibility of the column.
for (var i = 0; i < DEFAULT_COLUMNS.length; i++) {
var column = DEFAULT_COLUMNS[i];
if (column[0] == id) {
this.is_column_shown_[i] = !this.is_column_shown_[i];
menuitem.checked = this.is_column_shown_[i];
this.initColumnModel_()
this.table_.columnModel = this.columnModel_;
this.table_.redraw();
return;
}
}
},
};
var taskmanager = TaskManager.getInstance();
function init() {
var params = parseQueryParams(window.location);
var opt = {};
opt['isShowTitle'] = params.showtitle;
opt['isBackgroundMode'] = params.background;
opt['isShowCloseButton'] = params.showclose;
taskmanager.initialize(document.body, opt);
}
function onClose() {
return taskmanager.onClose();
}
document.addEventListener('DOMContentLoaded', init);
document.addEventListener('Close', onClose);
function taskAdded(start, length, tasks) {
// Sometimes this can get called too early.
if (!taskmanager)
return;
taskmanager.onTaskAdd(start, length, tasks);
}
function taskChanged(start, length,tasks) {
// Sometimes this can get called too early.
if (!taskmanager)
return;
taskmanager.onTaskChange(start, length, tasks);
}
function taskRemoved(start, length) {
// Sometimes this can get called too early.
if (!taskmanager)
return;
taskmanager.onTaskRemove(start, length);
}
|