task_manager_view.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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#include "chrome/browser/task_manager/task_manager.h"
6
7#include "base/compiler_specific.h"
8#include "base/metrics/stats_table.h"
9#include "base/prefs/pref_service.h"
10#include "base/prefs/scoped_user_pref_update.h"
11#include "base/strings/utf_string_conversions.h"
12#include "chrome/app/chrome_command_ids.h"
13#include "chrome/browser/browser_process.h"
14#include "chrome/browser/ui/browser.h"
15#include "chrome/browser/ui/browser_list.h"
16#include "chrome/browser/ui/browser_window.h"
17#include "chrome/browser/ui/host_desktop.h"
18#include "chrome/browser/ui/views/browser_dialogs.h"
19#include "chrome/common/pref_names.h"
20#include "grit/chromium_strings.h"
21#include "grit/generated_resources.h"
22#include "grit/theme_resources.h"
23#include "ui/base/accelerators/accelerator.h"
24#include "ui/base/l10n/l10n_util.h"
25#include "ui/base/models/simple_menu_model.h"
26#include "ui/base/models/table_model.h"
27#include "ui/base/models/table_model_observer.h"
28#include "ui/events/event_constants.h"
29#include "ui/events/keycodes/keyboard_codes.h"
30#include "ui/gfx/canvas.h"
31#include "ui/views/context_menu_controller.h"
32#include "ui/views/controls/button/label_button.h"
33#include "ui/views/controls/link.h"
34#include "ui/views/controls/link_listener.h"
35#include "ui/views/controls/menu/menu_runner.h"
36#include "ui/views/controls/table/table_grouper.h"
37#include "ui/views/controls/table/table_view.h"
38#include "ui/views/controls/table/table_view_observer.h"
39#include "ui/views/layout/layout_constants.h"
40#include "ui/views/widget/widget.h"
41#include "ui/views/window/dialog_delegate.h"
42
43#if defined(USE_ASH)
44#include "ash/wm/window_util.h"
45#include "chrome/browser/ui/ash/launcher/launcher_item_util.h"
46#include "grit/ash_resources.h"
47#endif
48
49#if defined(OS_WIN)
50#include "chrome/browser/shell_integration.h"
51#include "ui/base/win/shell.h"
52#include "ui/views/win/hwnd_util.h"
53#endif
54
55namespace {
56
57////////////////////////////////////////////////////////////////////////////////
58// TaskManagerTableModel class
59////////////////////////////////////////////////////////////////////////////////
60
61class TaskManagerTableModel
62    : public ui::TableModel,
63      public views::TableGrouper,
64      public TaskManagerModelObserver {
65 public:
66  explicit TaskManagerTableModel(TaskManagerModel* model)
67      : model_(model),
68        observer_(NULL) {
69    model_->AddObserver(this);
70  }
71
72  virtual ~TaskManagerTableModel() {
73    model_->RemoveObserver(this);
74  }
75
76  // TableModel overrides:
77  virtual int RowCount() OVERRIDE;
78  virtual base::string16 GetText(int row, int column) OVERRIDE;
79  virtual gfx::ImageSkia GetIcon(int row) OVERRIDE;
80  virtual void SetObserver(ui::TableModelObserver* observer) OVERRIDE;
81  virtual int CompareValues(int row1, int row2, int column_id) OVERRIDE;
82
83  // TableGrouper overrides:
84  virtual void GetGroupRange(int model_index,
85                             views::GroupRange* range) OVERRIDE;
86
87  // TaskManagerModelObserver overrides:
88  virtual void OnModelChanged() OVERRIDE;
89  virtual void OnItemsChanged(int start, int length) OVERRIDE;
90  virtual void OnItemsAdded(int start, int length) OVERRIDE;
91  virtual void OnItemsRemoved(int start, int length) OVERRIDE;
92
93 private:
94  TaskManagerModel* model_;
95  ui::TableModelObserver* observer_;
96
97  DISALLOW_COPY_AND_ASSIGN(TaskManagerTableModel);
98};
99
100int TaskManagerTableModel::RowCount() {
101  return model_->ResourceCount();
102}
103
104base::string16 TaskManagerTableModel::GetText(int row, int col_id) {
105  return model_->GetResourceById(row, col_id);
106}
107
108gfx::ImageSkia TaskManagerTableModel::GetIcon(int row) {
109  return model_->GetResourceIcon(row);
110}
111
112void TaskManagerTableModel::SetObserver(ui::TableModelObserver* observer) {
113  observer_ = observer;
114}
115
116int TaskManagerTableModel::CompareValues(int row1, int row2, int column_id) {
117  return model_->CompareValues(row1, row2, column_id);
118}
119
120void TaskManagerTableModel::GetGroupRange(int model_index,
121                                          views::GroupRange* range) {
122  TaskManagerModel::GroupRange range_pair =
123      model_->GetGroupRangeForResource(model_index);
124  range->start = range_pair.first;
125  range->length = range_pair.second;
126}
127
128void TaskManagerTableModel::OnModelChanged() {
129  if (observer_)
130    observer_->OnModelChanged();
131}
132
133void TaskManagerTableModel::OnItemsChanged(int start, int length) {
134  if (observer_)
135    observer_->OnItemsChanged(start, length);
136}
137
138void TaskManagerTableModel::OnItemsAdded(int start, int length) {
139  if (observer_)
140    observer_->OnItemsAdded(start, length);
141}
142
143void TaskManagerTableModel::OnItemsRemoved(int start, int length) {
144  if (observer_)
145    observer_->OnItemsRemoved(start, length);
146}
147
148// The Task manager UI container.
149class TaskManagerView : public views::ButtonListener,
150                        public views::DialogDelegateView,
151                        public views::TableViewObserver,
152                        public views::LinkListener,
153                        public views::ContextMenuController,
154                        public ui::SimpleMenuModel::Delegate {
155 public:
156  explicit TaskManagerView(chrome::HostDesktopType desktop_type);
157  virtual ~TaskManagerView();
158
159  // Shows the Task manager window, or re-activates an existing one.
160  static void Show(Browser* browser);
161
162  // views::View:
163  virtual void Layout() OVERRIDE;
164  virtual gfx::Size GetPreferredSize() const OVERRIDE;
165  virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
166  virtual void ViewHierarchyChanged(
167      const ViewHierarchyChangedDetails& details) OVERRIDE;
168
169  // views::ButtonListener:
170  virtual void ButtonPressed(views::Button* sender,
171                             const ui::Event& event) OVERRIDE;
172
173  // views::DialogDelegateView:
174  virtual bool CanResize() const OVERRIDE;
175  virtual bool CanMaximize() const OVERRIDE;
176  virtual bool ExecuteWindowsCommand(int command_id) OVERRIDE;
177  virtual base::string16 GetWindowTitle() const OVERRIDE;
178  virtual std::string GetWindowName() const OVERRIDE;
179  virtual int GetDialogButtons() const OVERRIDE;
180  virtual void WindowClosing() OVERRIDE;
181  virtual bool UseNewStyleForThisDialog() const OVERRIDE;
182
183  // views::TableViewObserver:
184  virtual void OnSelectionChanged() OVERRIDE;
185  virtual void OnDoubleClick() OVERRIDE;
186  virtual void OnKeyDown(ui::KeyboardCode keycode) OVERRIDE;
187
188  // views::LinkListener:
189  virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE;
190
191  // Called by the column picker to pick up any new stat counters that
192  // may have appeared since last time.
193  void UpdateStatsCounters();
194
195  // views::ContextMenuController:
196  virtual void ShowContextMenuForView(views::View* source,
197                                      const gfx::Point& point,
198                                      ui::MenuSourceType source_type) OVERRIDE;
199
200  // ui::SimpleMenuModel::Delegate:
201  virtual bool IsCommandIdChecked(int id) const OVERRIDE;
202  virtual bool IsCommandIdEnabled(int id) const OVERRIDE;
203  virtual bool GetAcceleratorForCommandId(
204      int command_id,
205      ui::Accelerator* accelerator) OVERRIDE;
206  virtual void ExecuteCommand(int id, int event_flags) OVERRIDE;
207
208 private:
209  // Creates the child controls.
210  void Init();
211
212  // Initializes the state of the always-on-top setting as the window is shown.
213  void InitAlwaysOnTopState();
214
215  // Activates the tab associated with the focused row.
216  void ActivateFocusedTab();
217
218  // Restores saved always on top state from a previous session.
219  bool GetSavedAlwaysOnTopState(bool* always_on_top) const;
220
221  views::LabelButton* kill_button_;
222  views::Link* about_memory_link_;
223  views::TableView* tab_table_;
224  views::View* tab_table_parent_;
225
226  TaskManager* task_manager_;
227
228  TaskManagerModel* model_;
229
230  // all possible columns, not necessarily visible
231  std::vector<ui::TableColumn> columns_;
232
233  scoped_ptr<TaskManagerTableModel> table_model_;
234
235  // True when the Task Manager window should be shown on top of other windows.
236  bool is_always_on_top_;
237
238  // The host desktop type this task manager belongs to.
239  const chrome::HostDesktopType desktop_type_;
240
241  // We need to own the text of the menu, the Windows API does not copy it.
242  base::string16 always_on_top_menu_text_;
243
244  // An open Task manager window. There can only be one open at a time. This
245  // is reset to NULL when the window is closed.
246  static TaskManagerView* instance_;
247
248  scoped_ptr<views::MenuRunner> menu_runner_;
249
250  DISALLOW_COPY_AND_ASSIGN(TaskManagerView);
251};
252
253// static
254TaskManagerView* TaskManagerView::instance_ = NULL;
255
256
257TaskManagerView::TaskManagerView(chrome::HostDesktopType desktop_type)
258    : kill_button_(NULL),
259      about_memory_link_(NULL),
260      tab_table_(NULL),
261      tab_table_parent_(NULL),
262      task_manager_(TaskManager::GetInstance()),
263      model_(TaskManager::GetInstance()->model()),
264      is_always_on_top_(false),
265      desktop_type_(desktop_type) {
266  Init();
267}
268
269TaskManagerView::~TaskManagerView() {
270  // Delete child views now, while our table model still exists.
271  RemoveAllChildViews(true);
272}
273
274void TaskManagerView::Init() {
275  table_model_.reset(new TaskManagerTableModel(model_));
276
277  // Page column has no header label.
278  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_TASK_COLUMN,
279                                     ui::TableColumn::LEFT, -1, 1));
280  columns_.back().sortable = true;
281  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_PROFILE_NAME_COLUMN,
282                                     ui::TableColumn::LEFT, -1, 0));
283  columns_.back().sortable = true;
284  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_PHYSICAL_MEM_COLUMN,
285                                     ui::TableColumn::RIGHT, -1, 0));
286  columns_.back().sortable = true;
287  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_SHARED_MEM_COLUMN,
288                                     ui::TableColumn::RIGHT, -1, 0));
289  columns_.back().sortable = true;
290  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_PRIVATE_MEM_COLUMN,
291                                     ui::TableColumn::RIGHT, -1, 0));
292  columns_.back().sortable = true;
293  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_CPU_COLUMN,
294                                     ui::TableColumn::RIGHT, -1, 0));
295  columns_.back().sortable = true;
296  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_NET_COLUMN,
297                                     ui::TableColumn::RIGHT, -1, 0));
298  columns_.back().sortable = true;
299  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_PROCESS_ID_COLUMN,
300                                     ui::TableColumn::RIGHT, -1, 0));
301  columns_.back().sortable = true;
302#if defined(OS_WIN)
303  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_GDI_HANDLES_COLUMN,
304                                     ui::TableColumn::RIGHT, -1, 0));
305  columns_.back().sortable = true;
306  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_USER_HANDLES_COLUMN,
307                                     ui::TableColumn::RIGHT, -1, 0));
308  columns_.back().sortable = true;
309#endif
310  columns_.push_back(ui::TableColumn(
311      IDS_TASK_MANAGER_WEBCORE_IMAGE_CACHE_COLUMN,
312      ui::TableColumn::RIGHT, -1, 0));
313  columns_.back().sortable = true;
314  columns_.push_back(ui::TableColumn(
315      IDS_TASK_MANAGER_WEBCORE_SCRIPTS_CACHE_COLUMN,
316      ui::TableColumn::RIGHT, -1, 0));
317  columns_.back().sortable = true;
318  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_WEBCORE_CSS_CACHE_COLUMN,
319                                     ui::TableColumn::RIGHT, -1, 0));
320  columns_.back().sortable = true;
321  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_FPS_COLUMN,
322                                     ui::TableColumn::RIGHT, -1, 0));
323  columns_.back().sortable = true;
324  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_VIDEO_MEMORY_COLUMN,
325                                     ui::TableColumn::RIGHT, -1, 0));
326  columns_.back().sortable = true;
327  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_SQLITE_MEMORY_USED_COLUMN,
328                                     ui::TableColumn::RIGHT, -1, 0));
329  columns_.back().sortable = true;
330  columns_.push_back(ui::TableColumn(
331        IDS_TASK_MANAGER_NACL_DEBUG_STUB_PORT_COLUMN,
332        ui::TableColumn::RIGHT, -1, 0));
333  columns_.back().sortable = true;
334  columns_.push_back(
335      ui::TableColumn(IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN,
336                      ui::TableColumn::RIGHT, -1, 0));
337  columns_.back().sortable = true;
338
339  tab_table_ = new views::TableView(
340      table_model_.get(), columns_, views::ICON_AND_TEXT, false);
341  tab_table_->SetGrouper(table_model_.get());
342
343  // Hide some columns by default
344  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_PROFILE_NAME_COLUMN, false);
345  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_SHARED_MEM_COLUMN, false);
346  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_PRIVATE_MEM_COLUMN, false);
347  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_WEBCORE_IMAGE_CACHE_COLUMN,
348                                  false);
349  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_WEBCORE_SCRIPTS_CACHE_COLUMN,
350                                  false);
351  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_WEBCORE_CSS_CACHE_COLUMN,
352                                  false);
353  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_VIDEO_MEMORY_COLUMN,
354                                  false);
355  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_SQLITE_MEMORY_USED_COLUMN,
356                                  false);
357  tab_table_->SetColumnVisibility(
358      IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN, false);
359  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_GOATS_TELEPORTED_COLUMN,
360                                  false);
361  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_GDI_HANDLES_COLUMN, false);
362  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_USER_HANDLES_COLUMN, false);
363
364  UpdateStatsCounters();
365  tab_table_->SetObserver(this);
366  tab_table_->set_context_menu_controller(this);
367  set_context_menu_controller(this);
368  kill_button_ = new views::LabelButton(this,
369      l10n_util::GetStringUTF16(IDS_TASK_MANAGER_KILL));
370  kill_button_->SetStyle(views::Button::STYLE_BUTTON);
371  about_memory_link_ = new views::Link(
372      l10n_util::GetStringUTF16(IDS_TASK_MANAGER_ABOUT_MEMORY_LINK));
373  about_memory_link_->set_listener(this);
374
375  // Makes sure our state is consistent.
376  OnSelectionChanged();
377
378  ui::Accelerator ctrl_w(ui::VKEY_W, ui::EF_CONTROL_DOWN);
379  AddAccelerator(ctrl_w);
380}
381
382void TaskManagerView::UpdateStatsCounters() {
383  base::StatsTable* stats = base::StatsTable::current();
384  if (stats != NULL) {
385    int max = stats->GetMaxCounters();
386    // skip the first row (it's header data)
387    for (int i = 1; i < max; i++) {
388      const char* row = stats->GetRowName(i);
389      if (row != NULL && row[0] != '\0' && !tab_table_->HasColumn(i)) {
390        // TODO(erikkay): Use l10n to get display names for stats.  Right
391        // now we're just displaying the internal counter name.  Perhaps
392        // stat names not in the string table would be filtered out.
393        ui::TableColumn col;
394        col.id = i;
395        col.title = base::ASCIIToUTF16(row);
396        col.alignment = ui::TableColumn::RIGHT;
397        // TODO(erikkay): Width is hard-coded right now, so many column
398        // names are clipped.
399        col.width = 90;
400        col.sortable = true;
401        columns_.push_back(col);
402        tab_table_->AddColumn(col);
403      }
404    }
405  }
406}
407
408void TaskManagerView::ViewHierarchyChanged(
409    const ViewHierarchyChangedDetails& details) {
410  // Since we want the Kill button and the Memory Details link to show up in
411  // the same visual row as the close button, which is provided by the
412  // framework, we must add the buttons to the non-client view, which is the
413  // parent of this view. Similarly, when we're removed from the view
414  // hierarchy, we must take care to clean up those items as well.
415  if (details.child == this) {
416    if (details.is_add) {
417      details.parent->AddChildView(about_memory_link_);
418      details.parent->AddChildView(kill_button_);
419      tab_table_parent_ = tab_table_->CreateParentIfNecessary();
420      AddChildView(tab_table_parent_);
421    } else {
422      details.parent->RemoveChildView(kill_button_);
423      details.parent->RemoveChildView(about_memory_link_);
424    }
425  }
426}
427
428void TaskManagerView::Layout() {
429  gfx::Size size = kill_button_->GetPreferredSize();
430  gfx::Rect parent_bounds = parent()->GetContentsBounds();
431  const int horizontal_margin = views::kPanelHorizMargin;
432  const int vertical_margin = views::kButtonVEdgeMargin;
433  int x = width() - size.width() - horizontal_margin;
434  int y_buttons = parent_bounds.bottom() - size.height() - vertical_margin;
435  kill_button_->SetBounds(x, y_buttons, size.width(), size.height());
436
437  size = about_memory_link_->GetPreferredSize();
438  about_memory_link_->SetBounds(
439      horizontal_margin,
440      y_buttons + (kill_button_->height() - size.height()) / 2,
441      size.width(), size.height());
442
443  gfx::Rect rect = GetLocalBounds();
444  rect.Inset(horizontal_margin, views::kPanelVertMargin);
445  rect.Inset(0, 0, 0,
446             kill_button_->height() + views::kUnrelatedControlVerticalSpacing);
447  tab_table_parent_->SetBoundsRect(rect);
448}
449
450gfx::Size TaskManagerView::GetPreferredSize() const {
451  return gfx::Size(460, 270);
452}
453
454bool TaskManagerView::AcceleratorPressed(const ui::Accelerator& accelerator) {
455  DCHECK_EQ(ui::VKEY_W, accelerator.key_code());
456  DCHECK_EQ(ui::EF_CONTROL_DOWN, accelerator.modifiers());
457  GetWidget()->Close();
458  return true;
459}
460
461// static
462void TaskManagerView::Show(Browser* browser) {
463  // In ash we can come here through the ChromeShellDelegate. If there is no
464  // browser window at that time of the call, browser could be passed as NULL.
465  const chrome::HostDesktopType desktop_type =
466      browser ? browser->host_desktop_type() : chrome::HOST_DESKTOP_TYPE_ASH;
467
468  if (instance_) {
469    // If there's a Task manager window open already, just activate it.
470    instance_->GetWidget()->Activate();
471    return;
472  }
473  instance_ = new TaskManagerView(desktop_type);
474  gfx::NativeWindow window =
475      browser ? browser->window()->GetNativeWindow() : NULL;
476#if defined(USE_ASH)
477  if (!window)
478    window = ash::wm::GetActiveWindow();
479#endif
480  DialogDelegate::CreateDialogWidget(instance_, window, NULL);
481  instance_->InitAlwaysOnTopState();
482  instance_->model_->StartUpdating();
483#if defined(OS_WIN)
484  // Set the app id for the task manager to the app id of its parent browser. If
485  // no parent is specified, the app id will default to that of the initial
486  // process.
487  if (browser) {
488    ui::win::SetAppIdForWindow(
489        ShellIntegration::GetChromiumModelIdForProfile(
490            browser->profile()->GetPath()),
491        views::HWNDForWidget(instance_->GetWidget()));
492  }
493#endif
494  instance_->GetWidget()->Show();
495
496  // Set the initial focus to the list of tasks.
497  views::FocusManager* focus_manager = instance_->GetFocusManager();
498  if (focus_manager)
499    focus_manager->SetFocusedView(instance_->tab_table_);
500
501#if defined(USE_ASH)
502  CreateShelfItemForDialog(IDR_ASH_SHELF_ICON_TASK_MANAGER,
503                           instance_->GetWidget()->GetNativeWindow());
504#endif
505}
506
507// ButtonListener implementation.
508void TaskManagerView::ButtonPressed(
509    views::Button* sender,
510    const ui::Event& event) {
511  typedef ui::ListSelectionModel::SelectedIndices SelectedIndices;
512  DCHECK_EQ(kill_button_, sender);
513  SelectedIndices selection(tab_table_->selection_model().selected_indices());
514  for (SelectedIndices::const_reverse_iterator i = selection.rbegin();
515        i != selection.rend(); ++i) {
516    task_manager_->KillProcess(*i);
517  }
518}
519
520// DialogDelegate implementation.
521bool TaskManagerView::CanResize() const {
522  return true;
523}
524
525bool TaskManagerView::CanMaximize() const {
526  return true;
527}
528
529bool TaskManagerView::ExecuteWindowsCommand(int command_id) {
530  return false;
531}
532
533base::string16 TaskManagerView::GetWindowTitle() const {
534  return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_TITLE);
535}
536
537std::string TaskManagerView::GetWindowName() const {
538  return prefs::kTaskManagerWindowPlacement;
539}
540
541int TaskManagerView::GetDialogButtons() const {
542  return ui::DIALOG_BUTTON_NONE;
543}
544
545void TaskManagerView::WindowClosing() {
546  // Now that the window is closed, we can allow a new one to be opened.
547  // (WindowClosing comes in asynchronously from the call to Close() and we
548  // may have already opened a new instance).
549  if (instance_ == this)
550    instance_ = NULL;
551  task_manager_->OnWindowClosed();
552}
553
554bool TaskManagerView::UseNewStyleForThisDialog() const {
555  return false;
556}
557
558// views::TableViewObserver implementation.
559void TaskManagerView::OnSelectionChanged() {
560  const ui::ListSelectionModel::SelectedIndices& selection(
561      tab_table_->selection_model().selected_indices());
562  bool selection_contains_browser_process = false;
563  for (size_t i = 0; i < selection.size(); ++i) {
564    if (task_manager_->IsBrowserProcess(selection[i])) {
565      selection_contains_browser_process = true;
566      break;
567    }
568  }
569  kill_button_->SetEnabled(!selection_contains_browser_process &&
570                           !selection.empty());
571}
572
573void TaskManagerView::OnDoubleClick() {
574  ActivateFocusedTab();
575}
576
577void TaskManagerView::OnKeyDown(ui::KeyboardCode keycode) {
578  if (keycode == ui::VKEY_RETURN)
579    ActivateFocusedTab();
580}
581
582void TaskManagerView::LinkClicked(views::Link* source, int event_flags) {
583  DCHECK_EQ(about_memory_link_, source);
584  task_manager_->OpenAboutMemory(desktop_type_);
585}
586
587void TaskManagerView::ShowContextMenuForView(views::View* source,
588                                             const gfx::Point& point,
589                                             ui::MenuSourceType source_type) {
590  UpdateStatsCounters();
591  ui::SimpleMenuModel menu_model(this);
592  for (std::vector<ui::TableColumn>::iterator i(columns_.begin());
593       i != columns_.end(); ++i) {
594    menu_model.AddCheckItem(i->id, l10n_util::GetStringUTF16(i->id));
595  }
596  menu_runner_.reset(new views::MenuRunner(&menu_model));
597  if (menu_runner_->RunMenuAt(GetWidget(),
598                              NULL,
599                              gfx::Rect(point, gfx::Size()),
600                              views::MENU_ANCHOR_TOPLEFT,
601                              source_type,
602                              views::MenuRunner::CONTEXT_MENU) ==
603      views::MenuRunner::MENU_DELETED) {
604    return;
605  }
606}
607
608bool TaskManagerView::IsCommandIdChecked(int id) const {
609  return tab_table_->IsColumnVisible(id);
610}
611
612bool TaskManagerView::IsCommandIdEnabled(int id) const {
613  return true;
614}
615
616bool TaskManagerView::GetAcceleratorForCommandId(
617    int command_id,
618    ui::Accelerator* accelerator) {
619  return false;
620}
621
622void TaskManagerView::ExecuteCommand(int id, int event_flags) {
623  tab_table_->SetColumnVisibility(id, !tab_table_->IsColumnVisible(id));
624}
625
626void TaskManagerView::InitAlwaysOnTopState() {
627  is_always_on_top_ = false;
628  if (GetSavedAlwaysOnTopState(&is_always_on_top_))
629    GetWidget()->SetAlwaysOnTop(is_always_on_top_);
630}
631
632void TaskManagerView::ActivateFocusedTab() {
633  const int active_row = tab_table_->selection_model().active();
634  if (active_row != -1)
635    task_manager_->ActivateProcess(active_row);
636}
637
638bool TaskManagerView::GetSavedAlwaysOnTopState(bool* always_on_top) const {
639  if (!g_browser_process->local_state())
640    return false;
641
642  const base::DictionaryValue* dictionary =
643      g_browser_process->local_state()->GetDictionary(GetWindowName().c_str());
644  return dictionary &&
645      dictionary->GetBoolean("always_on_top", always_on_top) && always_on_top;
646}
647
648}  // namespace
649
650namespace chrome {
651
652// Declared in browser_dialogs.h so others don't need to depend on our header.
653void ShowTaskManager(Browser* browser) {
654  TaskManagerView::Show(browser);
655}
656
657}  // namespace chrome
658