OLD | NEW |
| (Empty) |
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 <stddef.h> | |
8 | |
9 #include "base/compiler_specific.h" | |
10 #include "base/macros.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "build/build_config.h" | |
13 #include "chrome/app/chrome_command_ids.h" | |
14 #include "chrome/browser/browser_process.h" | |
15 #include "chrome/browser/ui/browser.h" | |
16 #include "chrome/browser/ui/browser_list.h" | |
17 #include "chrome/browser/ui/browser_window.h" | |
18 #include "chrome/browser/ui/views/new_task_manager_view.h" | |
19 #include "chrome/common/chrome_switches.h" | |
20 #include "chrome/common/pref_names.h" | |
21 #include "chrome/grit/chromium_strings.h" | |
22 #include "chrome/grit/generated_resources.h" | |
23 #include "components/prefs/pref_service.h" | |
24 #include "components/prefs/scoped_user_pref_update.h" | |
25 #include "ui/base/accelerators/accelerator.h" | |
26 #include "ui/base/l10n/l10n_util.h" | |
27 #include "ui/base/models/simple_menu_model.h" | |
28 #include "ui/base/models/table_model.h" | |
29 #include "ui/base/models/table_model_observer.h" | |
30 #include "ui/events/event_constants.h" | |
31 #include "ui/events/keycodes/keyboard_codes.h" | |
32 #include "ui/gfx/canvas.h" | |
33 #include "ui/views/context_menu_controller.h" | |
34 #include "ui/views/controls/button/label_button.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_win.h" | |
51 #include "ui/base/win/shell.h" | |
52 #include "ui/views/win/hwnd_util.h" | |
53 #endif | |
54 | |
55 namespace { | |
56 | |
57 //////////////////////////////////////////////////////////////////////////////// | |
58 // TaskManagerTableModel class | |
59 //////////////////////////////////////////////////////////////////////////////// | |
60 | |
61 class 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 ~TaskManagerTableModel() override { model_->RemoveObserver(this); } | |
73 | |
74 // TableModel overrides: | |
75 int RowCount() override; | |
76 base::string16 GetText(int row, int column) override; | |
77 gfx::ImageSkia GetIcon(int row) override; | |
78 void SetObserver(ui::TableModelObserver* observer) override; | |
79 int CompareValues(int row1, int row2, int column_id) override; | |
80 | |
81 // TableGrouper overrides: | |
82 void GetGroupRange(int model_index, views::GroupRange* range) override; | |
83 | |
84 // TaskManagerModelObserver overrides: | |
85 void OnModelChanged() override; | |
86 void OnItemsChanged(int start, int length) override; | |
87 void OnItemsAdded(int start, int length) override; | |
88 void OnItemsRemoved(int start, int length) override; | |
89 | |
90 private: | |
91 TaskManagerModel* model_; | |
92 ui::TableModelObserver* observer_; | |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(TaskManagerTableModel); | |
95 }; | |
96 | |
97 int TaskManagerTableModel::RowCount() { | |
98 return model_->ResourceCount(); | |
99 } | |
100 | |
101 base::string16 TaskManagerTableModel::GetText(int row, int col_id) { | |
102 return model_->GetResourceById(row, col_id); | |
103 } | |
104 | |
105 gfx::ImageSkia TaskManagerTableModel::GetIcon(int row) { | |
106 return model_->GetResourceIcon(row); | |
107 } | |
108 | |
109 void TaskManagerTableModel::SetObserver(ui::TableModelObserver* observer) { | |
110 observer_ = observer; | |
111 } | |
112 | |
113 int TaskManagerTableModel::CompareValues(int row1, int row2, int column_id) { | |
114 return model_->CompareValues(row1, row2, column_id); | |
115 } | |
116 | |
117 void TaskManagerTableModel::GetGroupRange(int model_index, | |
118 views::GroupRange* range) { | |
119 TaskManagerModel::GroupRange range_pair = | |
120 model_->GetGroupRangeForResource(model_index); | |
121 range->start = range_pair.first; | |
122 range->length = range_pair.second; | |
123 } | |
124 | |
125 void TaskManagerTableModel::OnModelChanged() { | |
126 if (observer_) | |
127 observer_->OnModelChanged(); | |
128 } | |
129 | |
130 void TaskManagerTableModel::OnItemsChanged(int start, int length) { | |
131 if (observer_) | |
132 observer_->OnItemsChanged(start, length); | |
133 } | |
134 | |
135 void TaskManagerTableModel::OnItemsAdded(int start, int length) { | |
136 if (observer_) | |
137 observer_->OnItemsAdded(start, length); | |
138 } | |
139 | |
140 void TaskManagerTableModel::OnItemsRemoved(int start, int length) { | |
141 if (observer_) | |
142 observer_->OnItemsRemoved(start, length); | |
143 } | |
144 | |
145 // The Task Manager UI container. | |
146 class TaskManagerView : public views::ButtonListener, | |
147 public views::DialogDelegateView, | |
148 public views::TableViewObserver, | |
149 public views::ContextMenuController, | |
150 public ui::SimpleMenuModel::Delegate { | |
151 public: | |
152 TaskManagerView(); | |
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::ContextMenuController: | |
188 void ShowContextMenuForView(views::View* source, | |
189 const gfx::Point& point, | |
190 ui::MenuSourceType source_type) override; | |
191 | |
192 // ui::SimpleMenuModel::Delegate: | |
193 bool IsCommandIdChecked(int id) const override; | |
194 bool IsCommandIdEnabled(int id) const override; | |
195 bool GetAcceleratorForCommandId(int command_id, | |
196 ui::Accelerator* accelerator) override; | |
197 void ExecuteCommand(int id, int event_flags) override; | |
198 | |
199 private: | |
200 // Creates the child controls. | |
201 void Init(); | |
202 | |
203 // Initializes the state of the always-on-top setting as the window is shown. | |
204 void InitAlwaysOnTopState(); | |
205 | |
206 // Activates the tab associated with the focused row. | |
207 void ActivateFocusedTab(); | |
208 | |
209 // Restores saved always on top state from a previous session. | |
210 bool GetSavedAlwaysOnTopState(bool* always_on_top) const; | |
211 | |
212 views::LabelButton* kill_button_; | |
213 views::TableView* tab_table_; | |
214 views::View* tab_table_parent_; | |
215 | |
216 TaskManager* task_manager_; | |
217 | |
218 TaskManagerModel* model_; | |
219 | |
220 // all possible columns, not necessarily visible | |
221 std::vector<ui::TableColumn> columns_; | |
222 | |
223 std::unique_ptr<TaskManagerTableModel> table_model_; | |
224 | |
225 // True when the Task Manager window should be shown on top of other windows. | |
226 bool is_always_on_top_; | |
227 | |
228 // We need to own the text of the menu, the Windows API does not copy it. | |
229 base::string16 always_on_top_menu_text_; | |
230 | |
231 // An open Task manager window. There can only be one open at a time. This | |
232 // is reset to NULL when the window is closed. | |
233 static TaskManagerView* instance_; | |
234 | |
235 std::unique_ptr<views::MenuRunner> menu_runner_; | |
236 | |
237 DISALLOW_COPY_AND_ASSIGN(TaskManagerView); | |
238 }; | |
239 | |
240 // static | |
241 TaskManagerView* TaskManagerView::instance_ = NULL; | |
242 | |
243 TaskManagerView::TaskManagerView() | |
244 : kill_button_(NULL), | |
245 tab_table_(NULL), | |
246 tab_table_parent_(NULL), | |
247 task_manager_(TaskManager::GetInstance()), | |
248 model_(TaskManager::GetInstance()->model()), | |
249 is_always_on_top_(false) { | |
250 Init(); | |
251 } | |
252 | |
253 TaskManagerView::~TaskManagerView() { | |
254 // Delete child views now, while our table model still exists. | |
255 RemoveAllChildViews(true); | |
256 } | |
257 | |
258 void TaskManagerView::Init() { | |
259 table_model_.reset(new TaskManagerTableModel(model_)); | |
260 | |
261 // Page column has no header label. | |
262 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_TASK_COLUMN, | |
263 ui::TableColumn::LEFT, -1, 1)); | |
264 columns_.back().sortable = true; | |
265 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_PROFILE_NAME_COLUMN, | |
266 ui::TableColumn::LEFT, -1, 0)); | |
267 columns_.back().sortable = true; | |
268 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_PHYSICAL_MEM_COLUMN, | |
269 ui::TableColumn::RIGHT, -1, 0)); | |
270 columns_.back().sortable = true; | |
271 columns_.back().initial_sort_is_ascending = false; | |
272 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_SHARED_MEM_COLUMN, | |
273 ui::TableColumn::RIGHT, -1, 0)); | |
274 columns_.back().sortable = true; | |
275 columns_.back().initial_sort_is_ascending = false; | |
276 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_PRIVATE_MEM_COLUMN, | |
277 ui::TableColumn::RIGHT, -1, 0)); | |
278 columns_.back().sortable = true; | |
279 columns_.back().initial_sort_is_ascending = false; | |
280 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_CPU_COLUMN, | |
281 ui::TableColumn::RIGHT, -1, 0)); | |
282 columns_.back().sortable = true; | |
283 columns_.back().initial_sort_is_ascending = false; | |
284 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_NET_COLUMN, | |
285 ui::TableColumn::RIGHT, -1, 0)); | |
286 columns_.back().sortable = true; | |
287 columns_.back().initial_sort_is_ascending = false; | |
288 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_PROCESS_ID_COLUMN, | |
289 ui::TableColumn::RIGHT, -1, 0)); | |
290 columns_.back().sortable = true; | |
291 #if defined(OS_WIN) | |
292 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_GDI_HANDLES_COLUMN, | |
293 ui::TableColumn::RIGHT, -1, 0)); | |
294 columns_.back().sortable = true; | |
295 columns_.back().initial_sort_is_ascending = false; | |
296 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_USER_HANDLES_COLUMN, | |
297 ui::TableColumn::RIGHT, -1, 0)); | |
298 columns_.back().sortable = true; | |
299 columns_.back().initial_sort_is_ascending = false; | |
300 #endif | |
301 columns_.push_back(ui::TableColumn( | |
302 IDS_TASK_MANAGER_WEBCORE_IMAGE_CACHE_COLUMN, | |
303 ui::TableColumn::RIGHT, -1, 0)); | |
304 columns_.back().sortable = true; | |
305 columns_.back().initial_sort_is_ascending = false; | |
306 columns_.push_back(ui::TableColumn( | |
307 IDS_TASK_MANAGER_WEBCORE_SCRIPTS_CACHE_COLUMN, | |
308 ui::TableColumn::RIGHT, -1, 0)); | |
309 columns_.back().sortable = true; | |
310 columns_.back().initial_sort_is_ascending = false; | |
311 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_WEBCORE_CSS_CACHE_COLUMN, | |
312 ui::TableColumn::RIGHT, -1, 0)); | |
313 columns_.back().sortable = true; | |
314 columns_.back().initial_sort_is_ascending = false; | |
315 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_VIDEO_MEMORY_COLUMN, | |
316 ui::TableColumn::RIGHT, -1, 0)); | |
317 columns_.back().sortable = true; | |
318 columns_.back().initial_sort_is_ascending = false; | |
319 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_SQLITE_MEMORY_USED_COLUMN, | |
320 ui::TableColumn::RIGHT, -1, 0)); | |
321 columns_.back().sortable = true; | |
322 columns_.back().initial_sort_is_ascending = false; | |
323 columns_.push_back(ui::TableColumn( | |
324 IDS_TASK_MANAGER_NACL_DEBUG_STUB_PORT_COLUMN, | |
325 ui::TableColumn::RIGHT, -1, 0)); | |
326 columns_.back().sortable = true; | |
327 columns_.push_back( | |
328 ui::TableColumn(IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN, | |
329 ui::TableColumn::RIGHT, -1, 0)); | |
330 columns_.back().sortable = true; | |
331 columns_.back().initial_sort_is_ascending = false; | |
332 // TODO(port) http://crbug.com/120488 for non-Linux. | |
333 #if defined(OS_LINUX) | |
334 columns_.push_back(ui::TableColumn(IDS_TASK_MANAGER_IDLE_WAKEUPS_COLUMN, | |
335 ui::TableColumn::RIGHT, -1, 0)); | |
336 columns_.back().sortable = true; | |
337 columns_.back().initial_sort_is_ascending = false; | |
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 tab_table_->SetObserver(this); | |
367 tab_table_->set_context_menu_controller(this); | |
368 set_context_menu_controller(this); | |
369 kill_button_ = new views::LabelButton(this, | |
370 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_KILL)); | |
371 kill_button_->SetStyle(views::Button::STYLE_BUTTON); | |
372 | |
373 // Makes sure our state is consistent. | |
374 OnSelectionChanged(); | |
375 | |
376 ui::Accelerator ctrl_w(ui::VKEY_W, ui::EF_CONTROL_DOWN); | |
377 AddAccelerator(ctrl_w); | |
378 } | |
379 | |
380 void TaskManagerView::ViewHierarchyChanged( | |
381 const ViewHierarchyChangedDetails& details) { | |
382 views::DialogDelegateView::ViewHierarchyChanged(details); | |
383 // Since we want the Kill button to show up in the same visual row as the | |
384 // close button, which is provided by the framework, we must add it to the | |
385 // non-client view, which is the parent of this view. Similarly, when we're | |
386 // removed from the view hierarchy, we must take care to clean up that item. | |
387 if (details.child == this) { | |
388 if (details.is_add) { | |
389 details.parent->AddChildView(kill_button_); | |
390 tab_table_parent_ = tab_table_->CreateParentIfNecessary(); | |
391 AddChildView(tab_table_parent_); | |
392 } else { | |
393 details.parent->RemoveChildView(kill_button_); | |
394 } | |
395 } | |
396 } | |
397 | |
398 void TaskManagerView::Layout() { | |
399 gfx::Size size = kill_button_->GetPreferredSize(); | |
400 gfx::Rect parent_bounds = parent()->GetContentsBounds(); | |
401 const int horizontal_margin = views::kPanelHorizMargin; | |
402 const int vertical_margin = views::kButtonVEdgeMargin; | |
403 int x = width() - size.width() - horizontal_margin; | |
404 int y_buttons = parent_bounds.bottom() - size.height() - vertical_margin; | |
405 kill_button_->SetBounds(x, y_buttons, size.width(), size.height()); | |
406 | |
407 gfx::Rect rect = GetLocalBounds(); | |
408 rect.Inset(horizontal_margin, views::kPanelVertMargin); | |
409 rect.Inset(0, 0, 0, | |
410 kill_button_->height() + views::kUnrelatedControlVerticalSpacing); | |
411 tab_table_parent_->SetBoundsRect(rect); | |
412 } | |
413 | |
414 gfx::Size TaskManagerView::GetPreferredSize() const { | |
415 return gfx::Size(460, 270); | |
416 } | |
417 | |
418 bool TaskManagerView::AcceleratorPressed(const ui::Accelerator& accelerator) { | |
419 DCHECK_EQ(ui::VKEY_W, accelerator.key_code()); | |
420 DCHECK_EQ(ui::EF_CONTROL_DOWN, accelerator.modifiers()); | |
421 GetWidget()->Close(); | |
422 return true; | |
423 } | |
424 | |
425 // static | |
426 void TaskManagerView::Show(Browser* browser) { | |
427 if (instance_) { | |
428 // If there's a Task manager window open already, just activate it. | |
429 instance_->GetWidget()->Activate(); | |
430 return; | |
431 } | |
432 instance_ = new TaskManagerView(); | |
433 gfx::NativeWindow window = | |
434 browser ? browser->window()->GetNativeWindow() : NULL; | |
435 #if defined(USE_ASH) | |
436 if (!window) | |
437 window = ash::wm::GetActiveWindow(); | |
438 #endif | |
439 DialogDelegate::CreateDialogWidget(instance_, window, NULL); | |
440 instance_->InitAlwaysOnTopState(); | |
441 instance_->model_->StartUpdating(); | |
442 #if defined(OS_WIN) | |
443 // Set the app id for the task manager to the app id of its parent browser. If | |
444 // no parent is specified, the app id will default to that of the initial | |
445 // process. | |
446 if (browser) { | |
447 ui::win::SetAppIdForWindow( | |
448 shell_integration::win::GetChromiumModelIdForProfile( | |
449 browser->profile()->GetPath()), | |
450 views::HWNDForWidget(instance_->GetWidget())); | |
451 } | |
452 #endif | |
453 instance_->GetWidget()->Show(); | |
454 | |
455 // Set the initial focus to the list of tasks. | |
456 views::FocusManager* focus_manager = instance_->GetFocusManager(); | |
457 if (focus_manager) | |
458 focus_manager->SetFocusedView(instance_->tab_table_); | |
459 | |
460 #if defined(USE_ASH) | |
461 gfx::NativeWindow native_window = instance_->GetWidget()->GetNativeWindow(); | |
462 ash::SetShelfItemDetailsForDialogWindow( | |
463 native_window, IDR_ASH_SHELF_ICON_TASK_MANAGER, native_window->title()); | |
464 #endif | |
465 } | |
466 | |
467 // static | |
468 void TaskManagerView::Hide() { | |
469 if (instance_) | |
470 instance_->GetWidget()->Close(); | |
471 } | |
472 | |
473 // ButtonListener implementation. | |
474 void TaskManagerView::ButtonPressed( | |
475 views::Button* sender, | |
476 const ui::Event& event) { | |
477 typedef ui::ListSelectionModel::SelectedIndices SelectedIndices; | |
478 DCHECK_EQ(kill_button_, sender); | |
479 SelectedIndices selection(tab_table_->selection_model().selected_indices()); | |
480 for (SelectedIndices::const_reverse_iterator i = selection.rbegin(); | |
481 i != selection.rend(); ++i) { | |
482 task_manager_->KillProcess(*i); | |
483 } | |
484 } | |
485 | |
486 // DialogDelegate implementation. | |
487 bool TaskManagerView::CanResize() const { | |
488 return true; | |
489 } | |
490 | |
491 bool TaskManagerView::CanMaximize() const { | |
492 return true; | |
493 } | |
494 | |
495 bool TaskManagerView::CanMinimize() const { | |
496 return true; | |
497 } | |
498 | |
499 bool TaskManagerView::ExecuteWindowsCommand(int command_id) { | |
500 return false; | |
501 } | |
502 | |
503 base::string16 TaskManagerView::GetWindowTitle() const { | |
504 return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_TITLE); | |
505 } | |
506 | |
507 std::string TaskManagerView::GetWindowName() const { | |
508 return prefs::kTaskManagerWindowPlacement; | |
509 } | |
510 | |
511 int TaskManagerView::GetDialogButtons() const { | |
512 return ui::DIALOG_BUTTON_NONE; | |
513 } | |
514 | |
515 void TaskManagerView::WindowClosing() { | |
516 // Now that the window is closed, we can allow a new one to be opened. | |
517 // (WindowClosing comes in asynchronously from the call to Close() and we | |
518 // may have already opened a new instance). | |
519 if (instance_ == this) | |
520 instance_ = NULL; | |
521 task_manager_->OnWindowClosed(); | |
522 } | |
523 | |
524 bool TaskManagerView::UseNewStyleForThisDialog() const { | |
525 return false; | |
526 } | |
527 | |
528 // views::TableViewObserver implementation. | |
529 void TaskManagerView::OnSelectionChanged() { | |
530 const ui::ListSelectionModel::SelectedIndices& selection( | |
531 tab_table_->selection_model().selected_indices()); | |
532 bool selection_contains_browser_process = false; | |
533 for (size_t i = 0; i < selection.size(); ++i) { | |
534 if (task_manager_->IsBrowserProcess(selection[i])) { | |
535 selection_contains_browser_process = true; | |
536 break; | |
537 } | |
538 } | |
539 kill_button_->SetEnabled(!selection_contains_browser_process && | |
540 !selection.empty() && | |
541 TaskManager::IsEndProcessEnabled()); | |
542 } | |
543 | |
544 void TaskManagerView::OnDoubleClick() { | |
545 ActivateFocusedTab(); | |
546 } | |
547 | |
548 void TaskManagerView::OnKeyDown(ui::KeyboardCode keycode) { | |
549 if (keycode == ui::VKEY_RETURN) | |
550 ActivateFocusedTab(); | |
551 } | |
552 | |
553 void TaskManagerView::ShowContextMenuForView(views::View* source, | |
554 const gfx::Point& point, | |
555 ui::MenuSourceType source_type) { | |
556 ui::SimpleMenuModel menu_model(this); | |
557 for (std::vector<ui::TableColumn>::iterator i(columns_.begin()); | |
558 i != columns_.end(); ++i) { | |
559 menu_model.AddCheckItem(i->id, l10n_util::GetStringUTF16(i->id)); | |
560 } | |
561 menu_runner_.reset( | |
562 new views::MenuRunner(&menu_model, views::MenuRunner::CONTEXT_MENU)); | |
563 if (menu_runner_->RunMenuAt(GetWidget(), | |
564 NULL, | |
565 gfx::Rect(point, gfx::Size()), | |
566 views::MENU_ANCHOR_TOPLEFT, | |
567 source_type) == views::MenuRunner::MENU_DELETED) { | |
568 return; | |
569 } | |
570 } | |
571 | |
572 bool TaskManagerView::IsCommandIdChecked(int id) const { | |
573 return tab_table_->IsColumnVisible(id); | |
574 } | |
575 | |
576 bool TaskManagerView::IsCommandIdEnabled(int id) const { | |
577 return true; | |
578 } | |
579 | |
580 bool TaskManagerView::GetAcceleratorForCommandId( | |
581 int command_id, | |
582 ui::Accelerator* accelerator) { | |
583 return false; | |
584 } | |
585 | |
586 void TaskManagerView::ExecuteCommand(int id, int event_flags) { | |
587 tab_table_->SetColumnVisibility(id, !tab_table_->IsColumnVisible(id)); | |
588 } | |
589 | |
590 void TaskManagerView::InitAlwaysOnTopState() { | |
591 is_always_on_top_ = false; | |
592 if (GetSavedAlwaysOnTopState(&is_always_on_top_)) | |
593 GetWidget()->SetAlwaysOnTop(is_always_on_top_); | |
594 } | |
595 | |
596 void TaskManagerView::ActivateFocusedTab() { | |
597 const int active_row = tab_table_->selection_model().active(); | |
598 if (active_row != -1) | |
599 task_manager_->ActivateProcess(active_row); | |
600 } | |
601 | |
602 bool TaskManagerView::GetSavedAlwaysOnTopState(bool* always_on_top) const { | |
603 if (!g_browser_process->local_state()) | |
604 return false; | |
605 | |
606 const base::DictionaryValue* dictionary = | |
607 g_browser_process->local_state()->GetDictionary(GetWindowName()); | |
608 return dictionary && | |
609 dictionary->GetBoolean("always_on_top", always_on_top) && always_on_top; | |
610 } | |
611 | |
612 } // namespace | |
613 | |
614 namespace chrome { | |
615 | |
616 // Declared in browser_dialogs.h so others don't need to depend on our header. | |
617 void ShowTaskManager(Browser* browser) { | |
618 if (switches::NewTaskManagerEnabled()) { | |
619 task_management::NewTaskManagerView::Show(browser); | |
620 return; | |
621 } | |
622 | |
623 TaskManagerView::Show(browser); | |
624 } | |
625 | |
626 void HideTaskManager() { | |
627 if (switches::NewTaskManagerEnabled()) { | |
628 task_management::NewTaskManagerView::Hide(); | |
629 return; | |
630 } | |
631 | |
632 TaskManagerView::Hide(); | |
633 } | |
634 | |
635 } // namespace chrome | |
OLD | NEW |