File: task_manager_view.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (243 lines) | stat: -rw-r--r-- 8,870 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_VIEWS_TASK_MANAGER_VIEW_H_
#define CHROME_BROWSER_UI_VIEWS_TASK_MANAGER_VIEW_H_

#include <string_view>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "chrome/browser/task_manager/task_manager_metrics_recorder.h"
#include "chrome/browser/ui/task_manager/task_manager_table_model.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/task_manager_search_bar_view.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/models/table_model.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/menu_source_type.mojom-forward.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/views/context_menu_controller.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/separator.h"
#include "ui/views/controls/tabbed_pane/tabbed_pane.h"
#include "ui/views/controls/tabbed_pane/tabbed_pane_listener.h"
#include "ui/views/controls/table/table_grouper.h"
#include "ui/views/controls/table/table_view_observer.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/window/dialog_delegate.h"

class Browser;

namespace views {
class TableView;
class View;
}  // namespace views

namespace task_manager {

// The new task manager UI container.
class TaskManagerView : public TableViewDelegate,
                        public views::DialogDelegateView,
                        public views::TabbedPaneListener,
                        public views::TableGrouper,
                        public views::TableViewObserver,
                        public views::ContextMenuController,
                        public ui::SimpleMenuModel::Delegate,
                        public TaskManagerSearchBarView::Delegate {
  METADATA_HEADER(TaskManagerView, views::DialogDelegateView)

 public:
  struct FilterTab {
    DisplayCategory associated_category;
    int title_id;
    // This field is not a raw_ptr<> because it only ever points to statically-
    // allocated data which is never freed, and hence cannot dangle.
    RAW_PTR_EXCLUSION const gfx::VectorIcon* icon;
  };

  TaskManagerView(const TaskManagerView&) = delete;
  TaskManagerView& operator=(const TaskManagerView&) = delete;
  ~TaskManagerView() override;

  // Shows the Task Manager window, or re-activates an existing one.
  static task_manager::TaskManagerTableModel* Show(
      Browser* browser,
      StartAction start_action = StartAction::kOther);

  // Hides the Task Manager if it is showing.
  static void Hide();

  // task_manager::TableViewDelegate:
  bool IsColumnVisible(int column_id) const override;
  bool SetColumnVisibility(int column_id, bool new_visibility) override;
  bool IsTableSorted() const override;
  TableSortDescriptor GetSortDescriptor() const override;
  void SetSortDescriptor(const TableSortDescriptor& descriptor) override;

  // views::View:
  gfx::Size CalculatePreferredSize(
      const views::SizeBounds& available_size) const override;
  bool AcceleratorPressed(const ui::Accelerator& accelerator) override;

  // views::DialogDelegateView:
  views::View* GetInitiallyFocusedView() override;
  bool ExecuteWindowsCommand(int command_id) override;
  ui::ImageModel GetWindowIcon() override;
  std::string GetWindowName() const override;
  bool Accept() override;
  bool IsDialogButtonEnabled(ui::mojom::DialogButton button) const override;
  void WindowClosing() override;

  // WidgetDelegate:
  void OnWidgetInitialized() override;

  // views::TableGrouper:
  void GetGroupRange(size_t model_index, views::GroupRange* range) override;

  // views::TableViewObserver:
  void OnSelectionChanged() override;
  void OnDoubleClick() override;
  void OnKeyDown(ui::KeyboardCode keycode) override;

  // views::ContextMenuController:
  void ShowContextMenuForViewImpl(
      views::View* source,
      const gfx::Point& point,
      ui::mojom::MenuSourceType source_type) override;

  // ui::SimpleMenuModel::Delegate:
  bool IsCommandIdChecked(int id) const override;
  bool IsCommandIdEnabled(int id) const override;
  void ExecuteCommand(int id, int event_flags) override;
  void MenuClosed(ui::SimpleMenuModel* source) override;

  // TaskManagerSearchBarView::Delegate:
  void SearchBarOnInputChanged(std::u16string_view text) override;

  views::TableView* tab_table_for_testing() { return tab_table_; }

  static TaskManagerView* GetInstanceForTests();

 private:
  // Used for the TaskManagerDesktopRefresh.
  // Determines how the UI for the TaskManager is rendered. Each boolean
  // controls a specific deviation from the original TaskManager UI.
  // TODO(crbug.com/364926055): Remove after feature is enabled by default.
  struct TableConfigs {
    bool table_has_border;
    bool header_style;
    bool table_refresh;
    bool scroll_view_rounded;
    bool layout_refresh;
    bool dialog_button_disabled;
    bool sort_on_cpu_by_default;
  };

  friend class TaskManagerViewTest;

  explicit TaskManagerView(StartAction start_action = StartAction::kOther);

  // Returns flags that describe how the TaskManagerView should be rendered.
  static TableConfigs GetTableConfigs();

  // Creates the header for the view.
  void CreateHeader(const ChromeLayoutProvider* provider);
  std::unique_ptr<views::View> CreateHeaderContent(
      const ChromeLayoutProvider* provider);
  std::unique_ptr<views::View> CreateHeaderSeparatorUnderlay(int height);

  // Creates a new TableModel which only operates on the subset of tasks
  // associated with the DisplayCategory (e.g. kTabs means only Tab processes
  // are displayed).
  void PerformFilter(DisplayCategory category);

  // Creates all corresponding subcomponents for the header.
  std::unique_ptr<views::TabbedPaneTabStrip> CreateTabbedPane(
      const ChromeLayoutProvider* provider,
      const gfx::Insets& title_insets,
      const gfx::Outsets& tab_outsets);
  std::unique_ptr<views::View> CreateSearchBar(
      const ChromeLayoutProvider* provider);
  std::unique_ptr<views::ScrollView> CreateProcessView(
      std::unique_ptr<views::TableView> tab_table,
      bool table_has_border,
      bool layout_refresh);

  // Creates the child controls (header, table, etc).
  void Init();

  // Initializes the state of the always-on-top setting as the window is shown.
  void InitAlwaysOnTopState();

  // Activates the tab associated with the selected row.
  void ActivateSelectedTab();

  // Selects the active tab in the specified browser window.
  void SelectTaskOfActiveTab(Browser* browser);

  // Restores saved "always on top" state from a previous session.
  void RetrieveSavedAlwaysOnTopState();

  // Restores saved tab.
  void RestoreSavedCategory();

  // Saves the provided category in the browser's local_state(). This is used to
  // restore the category on the next boot up of the Task Manager.
  void SaveCategoryToLocalState(DisplayCategory category);

  void EndSelectedProcess();
  void AnnounceTaskEnded(bool any_task_ended);
  bool IsEndProcessButtonEnabled() const;

  // views::TabbedPaneListener:
  void TabSelectedAt(int index) override;

  std::unique_ptr<TaskManagerTableModel> table_model_;

  std::unique_ptr<ui::SimpleMenuModel> menu_model_;
  std::unique_ptr<views::MenuRunner> menu_runner_;

  // We need to own the text of the menu, the Windows API does not copy it.
  std::u16string always_on_top_menu_text_;

  raw_ptr<views::TableView, DanglingUntriaged> tab_table_;
  raw_ptr<views::View, DanglingUntriaged> tab_table_parent_;

  // Specifications on how to layout the table.
  TableConfigs table_config_;

  // all possible columns, not necessarily visible.
  std::vector<ui::TableColumn> columns_;

  // The tabs which holds different task categories which is not null if task
  // manager refresh is enabled.
  raw_ptr<views::TabbedPaneTabStrip> tabs_ = nullptr;

  // Search keyword the user input.
  std::u16string search_terms_;

  // This button is not the same as the dialog button. It is only non-null if
  // task manager refresh is enabled.
  raw_ptr<views::MdTextButton> end_process_btn_;

  // The first time this instance of the task manager was initialized.
  const base::TimeTicks start_time_ = base::TimeTicks::Now();

  // The last time a process was ended by the user.
  base::TimeTicks latest_end_process_time_ = base::TimeTicks::Now();

  // The number of times a process has been ended in this session.
  size_t end_process_count_ = 0;

  // True when the Task Manager window should be shown on top of other windows.
  bool is_always_on_top_;

  base::WeakPtrFactory<TaskManagerView> weak_factory_{this};
};

}  // namespace task_manager

#endif  // CHROME_BROWSER_UI_VIEWS_TASK_MANAGER_VIEW_H_