task_manager_view.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/shelf/shelf_util.h"
45#include "ash/wm/window_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_VIDEO_MEMORY_COLUMN,
322                                     ui::TableColumn::RIGHT, -1, 0));
323  columns_.back().sortable = true;
324  columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_SQLITE_MEMORY_USED_COLUMN,
325                                     ui::TableColumn::RIGHT, -1, 0));
326  columns_.back().sortable = true;
327  columns_.push_back(ui::TableColumn(
328      IDS_TASK_MANAGER_NACL_DEBUG_STUB_PORT_COLUMN,
329      ui::TableColumn::RIGHT, -1, 0));
330  columns_.back().sortable = true;
331  columns_.push_back(
332      ui::TableColumn(IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN,
333                      ui::TableColumn::RIGHT, -1, 0));
334  columns_.back().sortable = true;
335
336  tab_table_ = new views::TableView(
337      table_model_.get(), columns_, views::ICON_AND_TEXT, false);
338  tab_table_->SetGrouper(table_model_.get());
339
340  // Hide some columns by default
341  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_PROFILE_NAME_COLUMN, false);
342  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_SHARED_MEM_COLUMN, false);
343  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_PRIVATE_MEM_COLUMN, false);
344  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_WEBCORE_IMAGE_CACHE_COLUMN,
345                                  false);
346  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_WEBCORE_SCRIPTS_CACHE_COLUMN,
347                                  false);
348  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_WEBCORE_CSS_CACHE_COLUMN,
349                                  false);
350  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_VIDEO_MEMORY_COLUMN,
351                                  false);
352  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_SQLITE_MEMORY_USED_COLUMN,
353                                  false);
354  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_NACL_DEBUG_STUB_PORT_COLUMN,
355                                  false);
356  tab_table_->SetColumnVisibility(
357      IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN, false);
358  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_GOATS_TELEPORTED_COLUMN,
359                                  false);
360  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_GDI_HANDLES_COLUMN, false);
361  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_USER_HANDLES_COLUMN, false);
362
363  UpdateStatsCounters();
364  tab_table_->SetObserver(this);
365  tab_table_->set_context_menu_controller(this);
366  set_context_menu_controller(this);
367  kill_button_ = new views::LabelButton(this,
368      l10n_util::GetStringUTF16(IDS_TASK_MANAGER_KILL));
369  kill_button_->SetStyle(views::Button::STYLE_BUTTON);
370  about_memory_link_ = new views::Link(
371      l10n_util::GetStringUTF16(IDS_TASK_MANAGER_ABOUT_MEMORY_LINK));
372  about_memory_link_->set_listener(this);
373
374  // Makes sure our state is consistent.
375  OnSelectionChanged();
376
377  ui::Accelerator ctrl_w(ui::VKEY_W, ui::EF_CONTROL_DOWN);
378  AddAccelerator(ctrl_w);
379}
380
381void TaskManagerView::UpdateStatsCounters() {
382  base::StatsTable* stats = base::StatsTable::current();
383  if (stats != NULL) {
384    int max = stats->GetMaxCounters();
385    // skip the first row (it's header data)
386    for (int i = 1; i < max; i++) {
387      const char* row = stats->GetRowName(i);
388      if (row != NULL && row[0] != '\0' && !tab_table_->HasColumn(i)) {
389        // TODO(erikkay): Use l10n to get display names for stats.  Right
390        // now we're just displaying the internal counter name.  Perhaps
391        // stat names not in the string table would be filtered out.
392        ui::TableColumn col;
393        col.id = i;
394        col.title = base::ASCIIToUTF16(row);
395        col.alignment = ui::TableColumn::RIGHT;
396        // TODO(erikkay): Width is hard-coded right now, so many column
397        // names are clipped.
398        col.width = 90;
399        col.sortable = true;
400        columns_.push_back(col);
401        tab_table_->AddColumn(col);
402      }
403    }
404  }
405}
406
407void TaskManagerView::ViewHierarchyChanged(
408    const ViewHierarchyChangedDetails& details) {
409  // Since we want the Kill button and the Memory Details link to show up in
410  // the same visual row as the close button, which is provided by the
411  // framework, we must add the buttons to the non-client view, which is the
412  // parent of this view. Similarly, when we're removed from the view
413  // hierarchy, we must take care to clean up those items as well.
414  if (details.child == this) {
415    if (details.is_add) {
416      details.parent->AddChildView(about_memory_link_);
417      details.parent->AddChildView(kill_button_);
418      tab_table_parent_ = tab_table_->CreateParentIfNecessary();
419      AddChildView(tab_table_parent_);
420    } else {
421      details.parent->RemoveChildView(kill_button_);
422      details.parent->RemoveChildView(about_memory_link_);
423    }
424  }
425}
426
427void TaskManagerView::Layout() {
428  gfx::Size size = kill_button_->GetPreferredSize();
429  gfx::Rect parent_bounds = parent()->GetContentsBounds();
430  const int horizontal_margin = views::kPanelHorizMargin;
431  const int vertical_margin = views::kButtonVEdgeMargin;
432  int x = width() - size.width() - horizontal_margin;
433  int y_buttons = parent_bounds.bottom() - size.height() - vertical_margin;
434  kill_button_->SetBounds(x, y_buttons, size.width(), size.height());
435
436  size = about_memory_link_->GetPreferredSize();
437  about_memory_link_->SetBounds(
438      horizontal_margin,
439      y_buttons + (kill_button_->height() - size.height()) / 2,
440      size.width(), size.height());
441
442  gfx::Rect rect = GetLocalBounds();
443  rect.Inset(horizontal_margin, views::kPanelVertMargin);
444  rect.Inset(0, 0, 0,
445             kill_button_->height() + views::kUnrelatedControlVerticalSpacing);
446  tab_table_parent_->SetBoundsRect(rect);
447}
448
449gfx::Size TaskManagerView::GetPreferredSize() const {
450  return gfx::Size(460, 270);
451}
452
453bool TaskManagerView::AcceleratorPressed(const ui::Accelerator& accelerator) {
454  DCHECK_EQ(ui::VKEY_W, accelerator.key_code());
455  DCHECK_EQ(ui::EF_CONTROL_DOWN, accelerator.modifiers());
456  GetWidget()->Close();
457  return true;
458}
459
460// static
461void TaskManagerView::Show(Browser* browser) {
462  // In ash we can come here through the ChromeShellDelegate. If there is no
463  // browser window at that time of the call, browser could be passed as NULL.
464  const chrome::HostDesktopType desktop_type =
465      browser ? browser->host_desktop_type() : chrome::HOST_DESKTOP_TYPE_ASH;
466
467  if (instance_) {
468    // If there's a Task manager window open already, just activate it.
469    instance_->GetWidget()->Activate();
470    return;
471  }
472  instance_ = new TaskManagerView(desktop_type);
473  gfx::NativeWindow window =
474      browser ? browser->window()->GetNativeWindow() : NULL;
475#if defined(USE_ASH)
476  if (!window)
477    window = ash::wm::GetActiveWindow();
478#endif
479  DialogDelegate::CreateDialogWidget(instance_, window, NULL);
480  instance_->InitAlwaysOnTopState();
481  instance_->model_->StartUpdating();
482#if defined(OS_WIN)
483  // Set the app id for the task manager to the app id of its parent browser. If
484  // no parent is specified, the app id will default to that of the initial
485  // process.
486  if (browser) {
487    ui::win::SetAppIdForWindow(
488        ShellIntegration::GetChromiumModelIdForProfile(
489            browser->profile()->GetPath()),
490        views::HWNDForWidget(instance_->GetWidget()));
491  }
492#endif
493  instance_->GetWidget()->Show();
494
495  // Set the initial focus to the list of tasks.
496  views::FocusManager* focus_manager = instance_->GetFocusManager();
497  if (focus_manager)
498    focus_manager->SetFocusedView(instance_->tab_table_);
499
500#if defined(USE_ASH)
501  ash::SetShelfItemDetailsForDialogWindow(
502      instance_->GetWidget()->GetNativeWindow(),
503      IDR_ASH_SHELF_ICON_TASK_MANAGER);
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(
597      new views::MenuRunner(&menu_model, views::MenuRunner::CONTEXT_MENU));
598  if (menu_runner_->RunMenuAt(GetWidget(),
599                              NULL,
600                              gfx::Rect(point, gfx::Size()),
601                              views::MENU_ANCHOR_TOPLEFT,
602                              source_type) == views::MenuRunner::MENU_DELETED) {
603    return;
604  }
605}
606
607bool TaskManagerView::IsCommandIdChecked(int id) const {
608  return tab_table_->IsColumnVisible(id);
609}
610
611bool TaskManagerView::IsCommandIdEnabled(int id) const {
612  return true;
613}
614
615bool TaskManagerView::GetAcceleratorForCommandId(
616    int command_id,
617    ui::Accelerator* accelerator) {
618  return false;
619}
620
621void TaskManagerView::ExecuteCommand(int id, int event_flags) {
622  tab_table_->SetColumnVisibility(id, !tab_table_->IsColumnVisible(id));
623}
624
625void TaskManagerView::InitAlwaysOnTopState() {
626  is_always_on_top_ = false;
627  if (GetSavedAlwaysOnTopState(&is_always_on_top_))
628    GetWidget()->SetAlwaysOnTop(is_always_on_top_);
629}
630
631void TaskManagerView::ActivateFocusedTab() {
632  const int active_row = tab_table_->selection_model().active();
633  if (active_row != -1)
634    task_manager_->ActivateProcess(active_row);
635}
636
637bool TaskManagerView::GetSavedAlwaysOnTopState(bool* always_on_top) const {
638  if (!g_browser_process->local_state())
639    return false;
640
641  const base::DictionaryValue* dictionary =
642      g_browser_process->local_state()->GetDictionary(GetWindowName().c_str());
643  return dictionary &&
644      dictionary->GetBoolean("always_on_top", always_on_top) && always_on_top;
645}
646
647}  // namespace
648
649namespace chrome {
650
651// Declared in browser_dialogs.h so others don't need to depend on our header.
652void ShowTaskManager(Browser* browser) {
653  TaskManagerView::Show(browser);
654}
655
656}  // namespace chrome
657