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