blob: e9808dc729616b688fb4a94bd5fc24ee14d90a9f (
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
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
|
// Copyright (c) 2009 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.
#include "chrome/browser/cocoa/task_manager_mac.h"
#include <algorithm>
#include <vector>
#include "app/l10n_util_mac.h"
#include "base/mac_util.h"
#include "base/sys_string_conversions.h"
#include "grit/generated_resources.h"
// TODO(thakis): Autoremember window size/pos (and selected columns?)
// TODO(thakis): Column sort comparator
// TODO(thakis): Clicking column header doesn't sort
// TODO(thakis): On window close, stop updating
// TODO(thakis): Favicons in rows
// TODO(thakis): Default sort column
// TODO(thakis): Metrics for all processes except browser process are missing.
@interface TaskManagerWindowController (Private)
- (void)addColumnWithId:(int)columnId visible:(BOOL)isVisible;
- (void)setUpTableColumns;
- (void)setUpTableHeaderContextMenu;
- (void)toggleColumn:(id)sender;
- (void)adjustEndProcessButton;
@end
////////////////////////////////////////////////////////////////////////////////
// TaskManagerWindowController implementation:
@implementation TaskManagerWindowController
- (id)initWithTaskManager:(TaskManager*)taskManager {
NSString* nibpath = [mac_util::MainAppBundle()
pathForResource:@"TaskManager"
ofType:@"nib"];
if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
taskManager_ = taskManager;
model_ = taskManager_->model();
[[self window] makeKeyAndOrderFront:self];
}
return self;
}
- (void)reloadData {
[tableView_ reloadData];
[self adjustEndProcessButton];
}
- (IBAction)statsLinkClicked:(id)sender {
TaskManager::GetInstance()->OpenAboutMemory();
}
- (IBAction)killSelectedProcesses:(id)sender {
NSIndexSet* selection = [tableView_ selectedRowIndexes];
for (NSUInteger i = [selection lastIndex];
i != NSNotFound;
i = [selection indexLessThanIndex:i]) {
taskManager_->KillProcess(i);
}
}
- (void)selectDoubleClickedTab:(id)sender {
NSInteger row = [tableView_ clickedRow];
if (row < 0)
return; // Happens e.g. if the table header is double-clicked.
taskManager_->ActivateProcess(row);
}
- (void)awakeFromNib {
[self setUpTableColumns];
[self setUpTableHeaderContextMenu];
[self adjustEndProcessButton];
[tableView_ setDoubleAction:@selector(selectDoubleClickedTab:)];
}
// Adds a column which has the given string id as title. |isVisible| specifies
// if the column is initially visible.
- (void)addColumnWithId:(int)columnId visible:(BOOL)isVisible {
scoped_nsobject<NSTableColumn> column([[NSTableColumn alloc]
initWithIdentifier:[NSNumber numberWithInt:columnId]]);
NSTextAlignment textAlignment = columnId == IDS_TASK_MANAGER_PAGE_COLUMN ?
NSLeftTextAlignment : NSRightTextAlignment;
[[column.get() headerCell]
setStringValue:l10n_util::GetNSStringWithFixup(columnId)];
[[column.get() headerCell] setAlignment:textAlignment];
[[column.get() dataCell] setAlignment:textAlignment];
[column.get() setHidden:!isVisible];
[column.get() setEditable:NO];
[tableView_ addTableColumn:column.get()];
}
// Adds all the task manager's columns to the table.
- (void)setUpTableColumns {
for (NSTableColumn* column in [tableView_ tableColumns])
[tableView_ removeTableColumn:column];
[self addColumnWithId:IDS_TASK_MANAGER_PAGE_COLUMN visible:YES];
[self addColumnWithId:IDS_TASK_MANAGER_PHYSICAL_MEM_COLUMN visible:YES];
[self addColumnWithId:IDS_TASK_MANAGER_SHARED_MEM_COLUMN visible:NO];
[self addColumnWithId:IDS_TASK_MANAGER_PRIVATE_MEM_COLUMN visible:NO];
[self addColumnWithId:IDS_TASK_MANAGER_CPU_COLUMN visible:YES];
[self addColumnWithId:IDS_TASK_MANAGER_NET_COLUMN visible:YES];
[self addColumnWithId:IDS_TASK_MANAGER_PROCESS_ID_COLUMN visible:NO];
[self addColumnWithId:IDS_TASK_MANAGER_WEBCORE_IMAGE_CACHE_COLUMN
visible:NO];
[self addColumnWithId:IDS_TASK_MANAGER_WEBCORE_SCRIPTS_CACHE_COLUMN
visible:NO];
[self addColumnWithId:IDS_TASK_MANAGER_WEBCORE_CSS_CACHE_COLUMN visible:NO];
[self addColumnWithId:IDS_TASK_MANAGER_GOATS_TELEPORTED_COLUMN visible:NO];
}
// Creates a context menu for the table header that allows the user to toggle
// which columns should be shown and which should be hidden (like e.g.
// Task Manager.app's table header context menu).
- (void)setUpTableHeaderContextMenu {
scoped_nsobject<NSMenu> contextMenu(
[[NSMenu alloc] initWithTitle:@"Task Manager context menu"]);
for (NSTableColumn* column in [tableView_ tableColumns]) {
NSMenuItem* item = [contextMenu.get()
addItemWithTitle:[[column headerCell] stringValue]
action:@selector(toggleColumn:)
keyEquivalent:@""];
[item setTarget:self];
[item setRepresentedObject:column];
[item setState:[column isHidden] ? NSOffState : NSOnState];
}
[[tableView_ headerView] setMenu:contextMenu.get()];
}
// Callback for the table header context menu. Toggles visibility of the table
// column associated with the clicked menu item.
- (void)toggleColumn:(id)item {
DCHECK([item isKindOfClass:[NSMenuItem class]]);
if (![item isKindOfClass:[NSMenuItem class]])
return;
NSTableColumn* column = [item representedObject];
DCHECK(column);
NSInteger oldState = [item state];
NSInteger newState = oldState == NSOnState ? NSOffState : NSOnState;
[column setHidden:newState == NSOffState];
[item setState:newState];
[tableView_ sizeToFit];
[tableView_ setNeedsDisplay];
}
// This function appropriately sets the enabled states on the table's editing
// buttons.
- (void)adjustEndProcessButton {
bool selection_contains_browser_process = false;
NSIndexSet* selection = [tableView_ selectedRowIndexes];
for (NSUInteger i = [selection lastIndex];
i != NSNotFound;
i = [selection indexLessThanIndex:i]) {
if (taskManager_->IsBrowserProcess(i)) {
selection_contains_browser_process = true;
break;
}
}
bool enabled = [selection count] > 0 && !selection_contains_browser_process;
[endProcessButton_ setEnabled:enabled];
}
// Table view delegate method.
- (void)tableViewSelectionDidChange:(NSNotification*)aNotification {
[self adjustEndProcessButton];
}
@end
@implementation TaskManagerWindowController (NSTableDataSource)
- (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView {
DCHECK(tableView == tableView_ || tableView_ == nil);
return model_->ResourceCount();
}
- (NSString*)modelTextForRow:(int)row column:(int)columnId {
switch (columnId) {
case IDS_TASK_MANAGER_PAGE_COLUMN: // Process
return base::SysWideToNSString(model_->GetResourceTitle(row));
case IDS_TASK_MANAGER_PRIVATE_MEM_COLUMN: // Memory
if (!model_->IsResourceFirstInGroup(row))
return @"";
return base::SysWideToNSString(model_->GetResourcePrivateMemory(row));
case IDS_TASK_MANAGER_SHARED_MEM_COLUMN: // Memory
if (!model_->IsResourceFirstInGroup(row))
return @"";
return base::SysWideToNSString(model_->GetResourceSharedMemory(row));
case IDS_TASK_MANAGER_PHYSICAL_MEM_COLUMN: // Memory
if (!model_->IsResourceFirstInGroup(row))
return @"";
return base::SysWideToNSString(model_->GetResourcePhysicalMemory(row));
case IDS_TASK_MANAGER_CPU_COLUMN: // CPU
if (!model_->IsResourceFirstInGroup(row))
return @"";
return base::SysWideToNSString(model_->GetResourceCPUUsage(row));
case IDS_TASK_MANAGER_NET_COLUMN: // Net
return base::SysWideToNSString(model_->GetResourceNetworkUsage(row));
case IDS_TASK_MANAGER_PROCESS_ID_COLUMN: // Process ID
if (!model_->IsResourceFirstInGroup(row))
return @"";
return base::SysWideToNSString(model_->GetResourceProcessId(row));
case IDS_TASK_MANAGER_WEBCORE_IMAGE_CACHE_COLUMN: // WebCore image cache
if (!model_->IsResourceFirstInGroup(row))
return @"";
return base::SysWideToNSString(
model_->GetResourceWebCoreImageCacheSize(row));
case IDS_TASK_MANAGER_WEBCORE_SCRIPTS_CACHE_COLUMN: // WebCore script cache
if (!model_->IsResourceFirstInGroup(row))
return @"";
return base::SysWideToNSString(
model_->GetResourceWebCoreScriptsCacheSize(row));
case IDS_TASK_MANAGER_WEBCORE_CSS_CACHE_COLUMN: // WebCore CSS cache
if (!model_->IsResourceFirstInGroup(row))
return @"";
return base::SysWideToNSString(
model_->GetResourceWebCoreCSSCacheSize(row));
case IDS_TASK_MANAGER_GOATS_TELEPORTED_COLUMN: // Goats Teleported!
return base::SysWideToNSString(model_->GetResourceGoatsTeleported(row));
default:
return base::SysWideToNSString(
model_->GetResourceStatsValue(row, columnId));
}
}
- (id)tableView:(NSTableView*)tableView
objectValueForTableColumn:(NSTableColumn*)tableColumn
row:(NSInteger)rowIndex {
return [self modelTextForRow:rowIndex
column:[[tableColumn identifier] intValue]];
}
@end
////////////////////////////////////////////////////////////////////////////////
// TaskManagerMac implementation:
TaskManagerMac::TaskManagerMac()
: task_manager_(TaskManager::GetInstance()),
model_(TaskManager::GetInstance()->model()) {
window_controller_.reset(
[[TaskManagerWindowController alloc] initWithTaskManager:task_manager_]);
model_->AddObserver(this);
}
// static
TaskManagerMac* TaskManagerMac::instance_ = NULL;
TaskManagerMac::~TaskManagerMac() {
task_manager_->OnWindowClosed();
model_->RemoveObserver(this);
}
////////////////////////////////////////////////////////////////////////////////
// TaskManagerMac, TaskManagerModelObserver implementation:
void TaskManagerMac::OnModelChanged() {
[window_controller_.get() reloadData];
}
void TaskManagerMac::OnItemsChanged(int start, int length) {
[window_controller_.get() reloadData];
}
void TaskManagerMac::OnItemsAdded(int start, int length) {
[window_controller_.get() reloadData];
}
void TaskManagerMac::OnItemsRemoved(int start, int length) {
[window_controller_.get() reloadData];
}
////////////////////////////////////////////////////////////////////////////////
// TaskManagerMac, public:
// static
void TaskManagerMac::Show() {
if (instance_) {
// If there's a Task manager window open already, just activate it.
[[instance_->window_controller_ window]
makeKeyAndOrderFront:instance_->window_controller_];
} else {
instance_ = new TaskManagerMac;
instance_->model_->StartUpdating();
}
}
|