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.
5 #include "chrome/browser/ui/cocoa/task_manager_mac.h"
10 #include "base/mac/bundle_locations.h"
11 #include "base/mac/mac_util.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "chrome/browser/browser_process.h"
15 #import "chrome/browser/ui/cocoa/window_size_autosaver.h"
16 #include "chrome/browser/ui/host_desktop.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "ui/base/l10n/l10n_util_mac.h"
21 #include "ui/gfx/image/image_skia.h"
25 // Width of "a" and most other letters/digits in "small" table views.
26 const int kCharWidth = 6;
28 // Some of the strings below have spaces at the end or are missing letters, to
29 // make the columns look nicer, and to take potentially longer localized strings
31 const struct ColumnWidth {
34 int maxWidth; // If this is -1, 1.5*minColumWidth is used as max width.
36 // Note that arraysize includes the trailing \0. That's intended.
37 { IDS_TASK_MANAGER_TASK_COLUMN, 120, 600 },
38 { IDS_TASK_MANAGER_PROFILE_NAME_COLUMN, 60, 200 },
39 { IDS_TASK_MANAGER_PHYSICAL_MEM_COLUMN,
40 arraysize("800 MiB") * kCharWidth, -1 },
41 { IDS_TASK_MANAGER_SHARED_MEM_COLUMN,
42 arraysize("800 MiB") * kCharWidth, -1 },
43 { IDS_TASK_MANAGER_PRIVATE_MEM_COLUMN,
44 arraysize("800 MiB") * kCharWidth, -1 },
45 { IDS_TASK_MANAGER_CPU_COLUMN,
46 arraysize("99.9") * kCharWidth, -1 },
47 { IDS_TASK_MANAGER_NET_COLUMN,
48 arraysize("150 kiB/s") * kCharWidth, -1 },
49 { IDS_TASK_MANAGER_PROCESS_ID_COLUMN,
50 arraysize("73099 ") * kCharWidth, -1 },
51 { IDS_TASK_MANAGER_WEBCORE_IMAGE_CACHE_COLUMN,
52 arraysize("2000.0K (2000.0 live)") * kCharWidth, -1 },
53 { IDS_TASK_MANAGER_WEBCORE_SCRIPTS_CACHE_COLUMN,
54 arraysize("2000.0K (2000.0 live)") * kCharWidth, -1 },
55 { IDS_TASK_MANAGER_WEBCORE_CSS_CACHE_COLUMN,
56 arraysize("2000.0K (2000.0 live)") * kCharWidth, -1 },
57 { IDS_TASK_MANAGER_VIDEO_MEMORY_COLUMN,
58 arraysize("2000.0K") * kCharWidth, -1 },
59 { IDS_TASK_MANAGER_SQLITE_MEMORY_USED_COLUMN,
60 arraysize("800 kB") * kCharWidth, -1 },
61 { IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN,
62 arraysize("2000.0K (2000.0 live)") * kCharWidth, -1 },
63 { IDS_TASK_MANAGER_NACL_DEBUG_STUB_PORT_COLUMN,
64 arraysize("32767") * kCharWidth, -1 },
65 { IDS_TASK_MANAGER_IDLE_WAKEUPS_COLUMN,
66 arraysize("idlewakeups") * kCharWidth, -1 },
71 SortHelper(TaskManagerModel* model, NSSortDescriptor* column)
72 : sort_column_([[column key] intValue]),
73 ascending_([column ascending]),
76 bool operator()(int a, int b) {
77 TaskManagerModel::GroupRange group_range1 =
78 model_->GetGroupRangeForResource(a);
79 TaskManagerModel::GroupRange group_range2 =
80 model_->GetGroupRangeForResource(b);
81 if (group_range1 == group_range2) {
82 // The two rows are in the same group, sort so that items in the same
83 // group always appear in the same order. |ascending_| is intentionally
87 // Sort by the first entry of each of the groups.
88 int cmp_result = model_->CompareValues(
89 group_range1.first, group_range2.first, sort_column_);
91 cmp_result = -cmp_result;
92 return cmp_result < 0;
97 TaskManagerModel* model_; // weak;
102 @interface TaskManagerWindowController (Private)
103 - (NSTableColumn*)addColumnWithId:(int)columnId visible:(BOOL)isVisible;
104 - (void)setUpTableColumns;
105 - (void)setUpTableHeaderContextMenu;
106 - (void)toggleColumn:(id)sender;
107 - (void)adjustSelectionAndEndProcessButton;
108 - (void)deselectRows;
111 ////////////////////////////////////////////////////////////////////////////////
112 // TaskManagerWindowController implementation:
114 @implementation TaskManagerWindowController
116 - (id)initWithTaskManagerObserver:(TaskManagerMac*)taskManagerObserver {
117 NSString* nibpath = [base::mac::FrameworkBundle()
118 pathForResource:@"TaskManager"
120 if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
121 taskManagerObserver_ = taskManagerObserver;
122 taskManager_ = taskManagerObserver_->task_manager();
123 model_ = taskManager_->model();
125 if (g_browser_process && g_browser_process->local_state()) {
126 size_saver_.reset([[WindowSizeAutosaver alloc]
127 initWithWindow:[self window]
128 prefService:g_browser_process->local_state()
129 path:prefs::kTaskManagerWindowPlacement]);
131 [self showWindow:self];
136 - (void)sortShuffleArray {
137 viewToModelMap_.resize(model_->ResourceCount());
138 for (size_t i = 0; i < viewToModelMap_.size(); ++i)
139 viewToModelMap_[i] = i;
141 std::sort(viewToModelMap_.begin(), viewToModelMap_.end(),
142 SortHelper(model_, currentSortDescriptor_.get()));
144 modelToViewMap_.resize(viewToModelMap_.size());
145 for (size_t i = 0; i < viewToModelMap_.size(); ++i)
146 modelToViewMap_[viewToModelMap_[i]] = i;
150 // Store old view indices, and the model indices they map to.
151 NSIndexSet* viewSelection = [tableView_ selectedRowIndexes];
152 std::vector<int> modelSelection;
153 for (NSUInteger i = [viewSelection lastIndex];
155 i = [viewSelection indexLessThanIndex:i]) {
156 modelSelection.push_back(viewToModelMap_[i]);
160 [self sortShuffleArray];
162 // Use the model indices to get the new view indices of the selection, and
163 // set selection to that. This assumes that no rows were added or removed
164 // (in that case, the selection is cleared before -reloadData is called).
165 if (!modelSelection.empty())
166 DCHECK_EQ([tableView_ numberOfRows], model_->ResourceCount());
167 NSMutableIndexSet* indexSet = [NSMutableIndexSet indexSet];
168 for (size_t i = 0; i < modelSelection.size(); ++i)
169 [indexSet addIndex:modelToViewMap_[modelSelection[i]]];
170 [tableView_ selectRowIndexes:indexSet byExtendingSelection:NO];
172 [tableView_ reloadData];
173 [self adjustSelectionAndEndProcessButton];
176 - (IBAction)statsLinkClicked:(id)sender {
177 TaskManager::GetInstance()->OpenAboutMemory(chrome::HOST_DESKTOP_TYPE_NATIVE);
180 - (IBAction)killSelectedProcesses:(id)sender {
181 NSIndexSet* selection = [tableView_ selectedRowIndexes];
182 for (NSUInteger i = [selection lastIndex];
184 i = [selection indexLessThanIndex:i]) {
185 taskManager_->KillProcess(viewToModelMap_[i]);
189 - (void)selectDoubleClickedTab:(id)sender {
190 NSInteger row = [tableView_ clickedRow];
192 return; // Happens e.g. if the table header is double-clicked.
193 taskManager_->ActivateProcess(viewToModelMap_[row]);
196 - (NSTableView*)tableView {
200 - (void)awakeFromNib {
201 [self setUpTableColumns];
202 [self setUpTableHeaderContextMenu];
203 [self adjustSelectionAndEndProcessButton];
205 [tableView_ setDoubleAction:@selector(selectDoubleClickedTab:)];
206 [tableView_ setIntercellSpacing:NSMakeSize(0.0, 0.0)];
207 [tableView_ sizeToFit];
211 [tableView_ setDelegate:nil];
212 [tableView_ setDataSource:nil];
216 // Adds a column which has the given string id as title. |isVisible| specifies
217 // if the column is initially visible.
218 - (NSTableColumn*)addColumnWithId:(int)columnId visible:(BOOL)isVisible {
219 base::scoped_nsobject<NSTableColumn> column([[NSTableColumn alloc]
220 initWithIdentifier:[NSString stringWithFormat:@"%d", columnId]]);
222 NSTextAlignment textAlignment =
223 (columnId == IDS_TASK_MANAGER_TASK_COLUMN ||
224 columnId == IDS_TASK_MANAGER_PROFILE_NAME_COLUMN) ?
225 NSLeftTextAlignment : NSRightTextAlignment;
227 [[column.get() headerCell]
228 setStringValue:l10n_util::GetNSStringWithFixup(columnId)];
229 [[column.get() headerCell] setAlignment:textAlignment];
230 [[column.get() dataCell] setAlignment:textAlignment];
232 NSFont* font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
233 [[column.get() dataCell] setFont:font];
235 [column.get() setHidden:!isVisible];
236 [column.get() setEditable:NO];
238 // The page column should by default be sorted ascending.
239 BOOL ascending = columnId == IDS_TASK_MANAGER_TASK_COLUMN;
241 base::scoped_nsobject<NSSortDescriptor> sortDescriptor(
242 [[NSSortDescriptor alloc]
243 initWithKey:[NSString stringWithFormat:@"%d", columnId]
244 ascending:ascending]);
245 [column.get() setSortDescriptorPrototype:sortDescriptor.get()];
247 // Default values, only used in release builds if nobody notices the DCHECK
248 // during development when adding new columns.
249 int minWidth = 200, maxWidth = 400;
252 for (i = 0; i < arraysize(columnWidths); ++i) {
253 if (columnWidths[i].columnId == columnId) {
254 minWidth = columnWidths[i].minWidth;
255 maxWidth = columnWidths[i].maxWidth;
257 maxWidth = 3 * minWidth / 2; // *1.5 for ints.
261 DCHECK(i < arraysize(columnWidths)) << "Could not find " << columnId;
262 [column.get() setMinWidth:minWidth];
263 [column.get() setMaxWidth:maxWidth];
264 [column.get() setResizingMask:NSTableColumnAutoresizingMask |
265 NSTableColumnUserResizingMask];
267 [tableView_ addTableColumn:column.get()];
268 return column.get(); // Now retained by |tableView_|.
271 // Adds all the task manager's columns to the table.
272 - (void)setUpTableColumns {
273 for (NSTableColumn* column in [tableView_ tableColumns])
274 [tableView_ removeTableColumn:column];
275 NSTableColumn* nameColumn = [self addColumnWithId:IDS_TASK_MANAGER_TASK_COLUMN
277 // |nameColumn| displays an icon for every row -- this is done by an
279 base::scoped_nsobject<NSButtonCell> nameCell(
280 [[NSButtonCell alloc] initTextCell:@""]);
281 [nameCell.get() setImagePosition:NSImageLeft];
282 [nameCell.get() setButtonType:NSSwitchButton];
283 [nameCell.get() setAlignment:[[nameColumn dataCell] alignment]];
284 [nameCell.get() setFont:[[nameColumn dataCell] font]];
285 [nameColumn setDataCell:nameCell.get()];
287 // Initially, sort on the tab name.
288 [tableView_ setSortDescriptors:
289 [NSArray arrayWithObject:[nameColumn sortDescriptorPrototype]]];
290 [self addColumnWithId:IDS_TASK_MANAGER_PROFILE_NAME_COLUMN visible:NO];
291 [self addColumnWithId:IDS_TASK_MANAGER_PHYSICAL_MEM_COLUMN visible:YES];
292 [self addColumnWithId:IDS_TASK_MANAGER_SHARED_MEM_COLUMN visible:NO];
293 [self addColumnWithId:IDS_TASK_MANAGER_PRIVATE_MEM_COLUMN visible:NO];
294 [self addColumnWithId:IDS_TASK_MANAGER_CPU_COLUMN visible:YES];
295 [self addColumnWithId:IDS_TASK_MANAGER_NET_COLUMN visible:YES];
296 [self addColumnWithId:IDS_TASK_MANAGER_PROCESS_ID_COLUMN visible:YES];
297 [self addColumnWithId:IDS_TASK_MANAGER_WEBCORE_IMAGE_CACHE_COLUMN
299 [self addColumnWithId:IDS_TASK_MANAGER_WEBCORE_SCRIPTS_CACHE_COLUMN
301 [self addColumnWithId:IDS_TASK_MANAGER_WEBCORE_CSS_CACHE_COLUMN visible:NO];
302 [self addColumnWithId:IDS_TASK_MANAGER_VIDEO_MEMORY_COLUMN visible:NO];
303 [self addColumnWithId:IDS_TASK_MANAGER_SQLITE_MEMORY_USED_COLUMN visible:NO];
304 [self addColumnWithId:IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN
306 [self addColumnWithId:IDS_TASK_MANAGER_NACL_DEBUG_STUB_PORT_COLUMN
308 [self addColumnWithId:IDS_TASK_MANAGER_IDLE_WAKEUPS_COLUMN
312 // Creates a context menu for the table header that allows the user to toggle
313 // which columns should be shown and which should be hidden (like e.g.
314 // Task Manager.app's table header context menu).
315 - (void)setUpTableHeaderContextMenu {
316 base::scoped_nsobject<NSMenu> contextMenu(
317 [[NSMenu alloc] initWithTitle:@"Task Manager context menu"]);
318 for (NSTableColumn* column in [tableView_ tableColumns]) {
319 NSMenuItem* item = [contextMenu.get()
320 addItemWithTitle:[[column headerCell] stringValue]
321 action:@selector(toggleColumn:)
323 [item setTarget:self];
324 [item setRepresentedObject:column];
325 [item setState:[column isHidden] ? NSOffState : NSOnState];
327 [[tableView_ headerView] setMenu:contextMenu.get()];
330 // Callback for the table header context menu. Toggles visibility of the table
331 // column associated with the clicked menu item.
332 - (void)toggleColumn:(id)item {
333 DCHECK([item isKindOfClass:[NSMenuItem class]]);
334 if (![item isKindOfClass:[NSMenuItem class]])
337 NSTableColumn* column = [item representedObject];
339 NSInteger oldState = [item state];
340 NSInteger newState = oldState == NSOnState ? NSOffState : NSOnState;
341 [column setHidden:newState == NSOffState];
342 [item setState:newState];
343 [tableView_ sizeToFit];
344 [tableView_ setNeedsDisplay];
347 // This function appropriately sets the enabled states on the table's editing
349 - (void)adjustSelectionAndEndProcessButton {
350 bool selectionContainsBrowserProcess = false;
352 // If a row is selected, make sure that all rows belonging to the same process
353 // are selected as well. Also, check if the selection contains the browser
355 NSIndexSet* selection = [tableView_ selectedRowIndexes];
356 for (NSUInteger i = [selection lastIndex];
358 i = [selection indexLessThanIndex:i]) {
359 int modelIndex = viewToModelMap_[i];
360 if (taskManager_->IsBrowserProcess(modelIndex))
361 selectionContainsBrowserProcess = true;
363 TaskManagerModel::GroupRange rangePair =
364 model_->GetGroupRangeForResource(modelIndex);
365 NSMutableIndexSet* indexSet = [NSMutableIndexSet indexSet];
366 for (int j = 0; j < rangePair.second; ++j)
367 [indexSet addIndex:modelToViewMap_[rangePair.first + j]];
368 [tableView_ selectRowIndexes:indexSet byExtendingSelection:YES];
371 bool enabled = [selection count] > 0 && !selectionContainsBrowserProcess;
372 [endProcessButton_ setEnabled:enabled];
375 - (void)deselectRows {
376 [tableView_ deselectAll:self];
379 // Table view delegate methods.
381 // The selection is being changed by mouse (drag/click).
382 - (void)tableViewSelectionIsChanging:(NSNotification*)aNotification {
383 [self adjustSelectionAndEndProcessButton];
386 // The selection is being changed by keyboard (arrows).
387 - (void)tableViewSelectionDidChange:(NSNotification*)aNotification {
388 [self adjustSelectionAndEndProcessButton];
391 - (void)windowWillClose:(NSNotification*)notification {
392 if (taskManagerObserver_) {
393 taskManagerObserver_->WindowWasClosed();
394 taskManagerObserver_ = nil;
401 @implementation TaskManagerWindowController (NSTableDataSource)
403 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView {
404 DCHECK(tableView == tableView_ || tableView_ == nil);
405 return model_->ResourceCount();
408 - (NSString*)modelTextForRow:(int)row column:(int)columnId {
409 DCHECK_LT(static_cast<size_t>(row), viewToModelMap_.size());
410 return base::SysUTF16ToNSString(
411 model_->GetResourceById(viewToModelMap_[row], columnId));
414 - (id)tableView:(NSTableView*)tableView
415 objectValueForTableColumn:(NSTableColumn*)tableColumn
416 row:(NSInteger)rowIndex {
417 // NSButtonCells expect an on/off state as objectValue. Their title is set
418 // in |tableView:dataCellForTableColumn:row:| below.
419 if ([[tableColumn identifier] intValue] == IDS_TASK_MANAGER_TASK_COLUMN) {
420 return [NSNumber numberWithInt:NSOffState];
423 return [self modelTextForRow:rowIndex
424 column:[[tableColumn identifier] intValue]];
427 - (NSCell*)tableView:(NSTableView*)tableView
428 dataCellForTableColumn:(NSTableColumn*)tableColumn
429 row:(NSInteger)rowIndex {
430 NSCell* cell = [tableColumn dataCellForRow:rowIndex];
432 // Set the favicon and title for the task in the name column.
433 if ([[tableColumn identifier] intValue] == IDS_TASK_MANAGER_TASK_COLUMN) {
434 DCHECK([cell isKindOfClass:[NSButtonCell class]]);
435 NSButtonCell* buttonCell = static_cast<NSButtonCell*>(cell);
436 NSString* title = [self modelTextForRow:rowIndex
437 column:[[tableColumn identifier] intValue]];
438 [buttonCell setTitle:title];
439 [buttonCell setImage:
440 taskManagerObserver_->GetImageForRow(viewToModelMap_[rowIndex])];
441 [buttonCell setRefusesFirstResponder:YES]; // Don't push in like a button.
442 [buttonCell setHighlightsBy:NSNoCellMask];
448 - (void) tableView:(NSTableView*)tableView
449 sortDescriptorsDidChange:(NSArray*)oldDescriptors {
450 NSArray* newDescriptors = [tableView sortDescriptors];
451 if ([newDescriptors count] < 1)
454 currentSortDescriptor_.reset([[newDescriptors objectAtIndex:0] retain]);
455 [self reloadData]; // Sorts.
460 ////////////////////////////////////////////////////////////////////////////////
461 // TaskManagerMac implementation:
463 TaskManagerMac::TaskManagerMac(TaskManager* task_manager)
464 : task_manager_(task_manager),
465 model_(task_manager->model()),
468 [[TaskManagerWindowController alloc] initWithTaskManagerObserver:this];
469 model_->AddObserver(this);
473 TaskManagerMac* TaskManagerMac::instance_ = NULL;
475 TaskManagerMac::~TaskManagerMac() {
476 if (this == instance_) {
477 // Do not do this when running in unit tests: |StartUpdating()| never got
478 // called in that case.
479 task_manager_->OnWindowClosed();
481 model_->RemoveObserver(this);
484 ////////////////////////////////////////////////////////////////////////////////
485 // TaskManagerMac, TaskManagerModelObserver implementation:
487 void TaskManagerMac::OnModelChanged() {
488 icon_cache_.OnModelChanged();
489 [window_controller_ deselectRows];
490 [window_controller_ reloadData];
493 void TaskManagerMac::OnItemsChanged(int start, int length) {
494 icon_cache_.OnItemsChanged(start, length);
495 [window_controller_ reloadData];
498 void TaskManagerMac::OnItemsAdded(int start, int length) {
499 icon_cache_.OnItemsAdded(start, length);
500 [window_controller_ deselectRows];
501 [window_controller_ reloadData];
504 void TaskManagerMac::OnItemsRemoved(int start, int length) {
505 icon_cache_.OnItemsRemoved(start, length);
506 [window_controller_ deselectRows];
507 [window_controller_ reloadData];
510 NSImage* TaskManagerMac::GetImageForRow(int row) {
511 return icon_cache_.GetImageForRow(row);
514 ////////////////////////////////////////////////////////////////////////////////
515 // TaskManagerMac, public:
517 void TaskManagerMac::WindowWasClosed() {
522 int TaskManagerMac::RowCount() const {
523 return model_->ResourceCount();
526 gfx::ImageSkia TaskManagerMac::GetIcon(int r) const {
527 return model_->GetResourceIcon(r);
531 void TaskManagerMac::Show() {
533 [[instance_->window_controller_ window]
534 makeKeyAndOrderFront:instance_->window_controller_];
537 // Create a new instance.
538 instance_ = new TaskManagerMac(TaskManager::GetInstance());
539 instance_->model_->StartUpdating();
544 // Declared in browser_dialogs.h.
545 void ShowTaskManager(Browser* browser) {
546 TaskManagerMac::Show();
549 } // namespace chrome