task_manager_view.cc revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
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(
355      IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN, false);
356  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_GOATS_TELEPORTED_COLUMN,
357                                  false);
358  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_GDI_HANDLES_COLUMN, false);
359  tab_table_->SetColumnVisibility(IDS_TASK_MANAGER_USER_HANDLES_COLUMN, false);
360
361  UpdateStatsCounters();
362  tab_table_->SetObserver(this);
363  tab_table_->set_context_menu_controller(this);
364  set_context_menu_controller(this);
365  kill_button_ = new views::LabelButton(this,
366      l10n_util::GetStringUTF16(IDS_TASK_MANAGER_KILL));
367  kill_button_->SetStyle(views::Button::STYLE_BUTTON);
368  about_memory_link_ = new views::Link(
369      l10n_util::GetStringUTF16(IDS_TASK_MANAGER_ABOUT_MEMORY_LINK));
370  about_memory_link_->set_listener(this);
371
372  // Makes sure our state is consistent.
373  OnSelectionChanged();
374
375  ui::Accelerator ctrl_w(ui::VKEY_W, ui::EF_CONTROL_DOWN);
376  AddAccelerator(ctrl_w);
377}
378
379void TaskManagerView::UpdateStatsCounters() {
380  base::StatsTable* stats = base::StatsTable::current();
381  if (stats != NULL) {
382    int max = stats->GetMaxCounters();
383    // skip the first row (it's header data)
384    for (int i = 1; i < max; i++) {
385      const char* row = stats->GetRowName(i);
386      if (row != NULL && row[0] != '\0' && !tab_table_->HasColumn(i)) {
387        // TODO(erikkay): Use l10n to get display names for stats.  Right
388        // now we're just displaying the internal counter name.  Perhaps
389        // stat names not in the string table would be filtered out.
390        ui::TableColumn col;
391        col.id = i;
392        col.title = base::ASCIIToUTF16(row);
393        col.alignment = ui::TableColumn::RIGHT;
394        // TODO(erikkay): Width is hard-coded right now, so many column
395        // names are clipped.
396        col.width = 90;
397        col.sortable = true;
398        columns_.push_back(col);
399        tab_table_->AddColumn(col);
400      }
401    }
402  }
403}
404
405void TaskManagerView::ViewHierarchyChanged(
406    const ViewHierarchyChangedDetails& details) {
407  // Since we want the Kill button and the Memory Details link to show up in
408  // the same visual row as the close button, which is provided by the
409  // framework, we must add the buttons to the non-client view, which is the
410  // parent of this view. Similarly, when we're removed from the view
411  // hierarchy, we must take care to clean up those items as well.
412  if (details.child == this) {
413    if (details.is_add) {
414      details.parent->AddChildView(about_memory_link_);
415      details.parent->AddChildView(kill_button_);
416      tab_table_parent_ = tab_table_->CreateParentIfNecessary();
417      AddChildView(tab_table_parent_);
418    } else {
419      details.parent->RemoveChildView(kill_button_);
420      details.parent->RemoveChildView(about_memory_link_);
421    }
422  }
423}
424
425void TaskManagerView::Layout() {
426  gfx::Size size = kill_button_->GetPreferredSize();
427  gfx::Rect parent_bounds = parent()->GetContentsBounds();
428  const int horizontal_margin = views::kPanelHorizMargin;
429  const int vertical_margin = views::kButtonVEdgeMargin;
430  int x = width() - size.width() - horizontal_margin;
431  int y_buttons = parent_bounds.bottom() - size.height() - vertical_margin;
432  kill_button_->SetBounds(x, y_buttons, size.width(), size.height());
433
434  size = about_memory_link_->GetPreferredSize();
435  about_memory_link_->SetBounds(
436      horizontal_margin,
437      y_buttons + (kill_button_->height() - size.height()) / 2,
438      size.width(), size.height());
439
440  gfx::Rect rect = GetLocalBounds();
441  rect.Inset(horizontal_margin, views::kPanelVertMargin);
442  rect.Inset(0, 0, 0,
443             kill_button_->height() + views::kUnrelatedControlVerticalSpacing);
444  tab_table_parent_->SetBoundsRect(rect);
445}
446
447gfx::Size TaskManagerView::GetPreferredSize() const {
448  return gfx::Size(460, 270);
449}
450
451bool TaskManagerView::AcceleratorPressed(const ui::Accelerator& accelerator) {
452  DCHECK_EQ(ui::VKEY_W, accelerator.key_code());
453  DCHECK_EQ(ui::EF_CONTROL_DOWN, accelerator.modifiers());
454  GetWidget()->Close();
455  return true;
456}
457
458// static
459void TaskManagerView::Show(Browser* browser) {
460  // In ash we can come here through the ChromeShellDelegate. If there is no
461  // browser window at that time of the call, browser could be passed as NULL.
462  const chrome::HostDesktopType desktop_type =
463      browser ? browser->host_desktop_type() : chrome::HOST_DESKTOP_TYPE_ASH;
464
465  if (instance_) {
466    // If there's a Task manager window open already, just activate it.
467    instance_->GetWidget()->Activate();
468    return;
469  }
470  instance_ = new TaskManagerView(desktop_type);
471  gfx::NativeWindow window =
472      browser ? browser->window()->GetNativeWindow() : NULL;
473#if defined(USE_ASH)
474  if (!window)
475    window = ash::wm::GetActiveWindow();
476#endif
477  DialogDelegate::CreateDialogWidget(instance_, window, NULL);
478  instance_->InitAlwaysOnTopState();
479  instance_->model_->StartUpdating();
480#if defined(OS_WIN)
481  // Set the app id for the task manager to the app id of its parent browser. If
482  // no parent is specified, the app id will default to that of the initial
483  // process.
484  if (browser) {
485    ui::win::SetAppIdForWindow(
486        ShellIntegration::GetChromiumModelIdForProfile(
487            browser->profile()->GetPath()),
488        views::HWNDForWidget(instance_->GetWidget()));
489  }
490#endif
491  instance_->GetWidget()->Show();
492
493  // Set the initial focus to the list of tasks.
494  views::FocusManager* focus_manager = instance_->GetFocusManager();
495  if (focus_manager)
496    focus_manager->SetFocusedView(instance_->tab_table_);
497
498#if defined(USE_ASH)
499  ash::SetShelfItemDetailsForDialogWindow(
500      instance_->GetWidget()->GetNativeWindow(),
501      IDR_ASH_SHELF_ICON_TASK_MANAGER);
502#endif
503}
504
505// ButtonListener implementation.
506void TaskManagerView::ButtonPressed(
507    views::Button* sender,
508    const ui::Event& event) {
509  typedef ui::ListSelectionModel::SelectedIndices SelectedIndices;
510  DCHECK_EQ(kill_button_, sender);
511  SelectedIndices selection(tab_table_->selection_model().selected_indices());
512  for (SelectedIndices::const_reverse_iterator i = selection.rbegin();
513        i != selection.rend(); ++i) {
514    task_manager_->KillProcess(*i);
515  }
516}
517
518// DialogDelegate implementation.
519bool TaskManagerView::CanResize() const {
520  return true;
521}
522
523bool TaskManagerView::CanMaximize() const {
524  return true;
525}
526
527bool TaskManagerView::ExecuteWindowsCommand(int command_id) {
528  return false;
529}
530
531base::string16 TaskManagerView::GetWindowTitle() const {
532  return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_TITLE);
533}
534
535std::string TaskManagerView::GetWindowName() const {
536  return prefs::kTaskManagerWindowPlacement;
537}
538
539int TaskManagerView::GetDialogButtons() const {
540  return ui::DIALOG_BUTTON_NONE;
541}
542
543void TaskManagerView::WindowClosing() {
544  // Now that the window is closed, we can allow a new one to be opened.
545  // (WindowClosing comes in asynchronously from the call to Close() and we
546  // may have already opened a new instance).
547  if (instance_ == this)
548    instance_ = NULL;
549  task_manager_->OnWindowClosed();
550}
551
552bool TaskManagerView::UseNewStyleForThisDialog() const {
553  return false;
554}
555
556// views::TableViewObserver implementation.
557void TaskManagerView::OnSelectionChanged() {
558  const ui::ListSelectionModel::SelectedIndices& selection(
559      tab_table_->selection_model().selected_indices());
560  bool selection_contains_browser_process = false;
561  for (size_t i = 0; i < selection.size(); ++i) {
562    if (task_manager_->IsBrowserProcess(selection[i])) {
563      selection_contains_browser_process = true;
564      break;
565    }
566  }
567  kill_button_->SetEnabled(!selection_contains_browser_process &&
568                           !selection.empty());
569}
570
571void TaskManagerView::OnDoubleClick() {
572  ActivateFocusedTab();
573}
574
575void TaskManagerView::OnKeyDown(ui::KeyboardCode keycode) {
576  if (keycode == ui::VKEY_RETURN)
577    ActivateFocusedTab();
578}
579
580void TaskManagerView::LinkClicked(views::Link* source, int event_flags) {
581  DCHECK_EQ(about_memory_link_, source);
582  task_manager_->OpenAboutMemory(desktop_type_);
583}
584
585void TaskManagerView::ShowContextMenuForView(views::View* source,
586                                             const gfx::Point& point,
587                                             ui::MenuSourceType source_type) {
588  UpdateStatsCounters();
589  ui::SimpleMenuModel menu_model(this);
590  for (std::vector<ui::TableColumn>::iterator i(columns_.begin());
591       i != columns_.end(); ++i) {
592    menu_model.AddCheckItem(i->id, l10n_util::GetStringUTF16(i->id));
593  }
594  menu_runner_.reset(new views::MenuRunner(&menu_model));
595  if (menu_runner_->RunMenuAt(GetWidget(),
596                              NULL,
597                              gfx::Rect(point, gfx::Size()),
598                              views::MENU_ANCHOR_TOPLEFT,
599                              source_type,
600                              views::MenuRunner::CONTEXT_MENU) ==
601      views::MenuRunner::MENU_DELETED) {
602    return;
603  }
604}
605
606bool TaskManagerView::IsCommandIdChecked(int id) const {
607  return tab_table_->IsColumnVisible(id);
608}
609
610bool TaskManagerView::IsCommandIdEnabled(int id) const {
611  return true;
612}
613
614bool TaskManagerView::GetAcceleratorForCommandId(
615    int command_id,
616    ui::Accelerator* accelerator) {
617  return false;
618}
619
620void TaskManagerView::ExecuteCommand(int id, int event_flags) {
621  tab_table_->SetColumnVisibility(id, !tab_table_->IsColumnVisible(id));
622}
623
624void TaskManagerView::InitAlwaysOnTopState() {
625  is_always_on_top_ = false;
626  if (GetSavedAlwaysOnTopState(&is_always_on_top_))
627    GetWidget()->SetAlwaysOnTop(is_always_on_top_);
628}
629
630void TaskManagerView::ActivateFocusedTab() {
631  const int active_row = tab_table_->selection_model().active();
632  if (active_row != -1)
633    task_manager_->ActivateProcess(active_row);
634}
635
636bool TaskManagerView::GetSavedAlwaysOnTopState(bool* always_on_top) const {
637  if (!g_browser_process->local_state())
638    return false;
639
640  const base::DictionaryValue* dictionary =
641      g_browser_process->local_state()->GetDictionary(GetWindowName().c_str());
642  return dictionary &&
643      dictionary->GetBoolean("always_on_top", always_on_top) && always_on_top;
644}
645
646}  // namespace
647
648namespace chrome {
649
650// Declared in browser_dialogs.h so others don't need to depend on our header.
651void ShowTaskManager(Browser* browser) {
652  TaskManagerView::Show(browser);
653}
654
655}  // namespace chrome
656