File: task.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (245 lines) | stat: -rw-r--r-- 9,610 bytes parent folder | download | duplicates (5)
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
244
245
// 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_TASK_MANAGER_PROVIDERS_TASK_H_
#define CHROME_BROWSER_TASK_MANAGER_PROVIDERS_TASK_H_

#include <stdint.h>

#include <string>

#include "base/memory/weak_ptr.h"
#include "base/process/kill.h"
#include "base/process/process_handle.h"
#include "base/time/time.h"
#include "components/sessions/core/session_id.h"
#include "third_party/blink/public/common/web_cache/web_cache_resource_type_stats.h"
#include "ui/gfx/image/image_skia.h"

class Profile;

namespace task_manager {

class TaskProviderObserver;

// Defines a task that corresponds to a tab, an app, an extension, ... etc. It
// represents one row in the task manager table. Multiple tasks can share the
// same process, in which case they're grouped together in the task manager
// table. See |task_manager::TaskGroup| which represents a process possibly
// shared by multiple tasks.
class Task {
 public:
  // Note that the declaration order here determines the default sort order
  // in the task manager.
  enum Type {
    UNKNOWN = 0,

    /* Singleton processes first that don't belong to a particular tab. */
    BROWSER,   /* The main browser process. */
    GPU,       /* A graphics process. */
    ARC,       /* An ARC process. */
    CROSTINI,  /* A Crostini VM process. */
    PLUGIN_VM, /* A Plugin VM process. */
    ZYGOTE,    /* A Linux zygote process. */
    UTILITY,   /* A browser utility process. */

    /* Per-Tab processes next. */
    RENDERER,  /* A normal WebContents renderer process. */
    EXTENSION, /* An extension or app process. */

    /* Plugin processes last.*/
    GUEST,            /* A browser plugin guest process. */
    PLUGIN,           /* A plugin process. */
    NACL,             /* A NativeClient loader or broker process. */
    SANDBOX_HELPER,   /* A sandbox helper process. */
    DEDICATED_WORKER, /* A dedicated worker running on the renderer process. */
    SHARED_WORKER,    /* A shared worker running on the renderer process. */
    SERVICE_WORKER,   /* A service worker running on the renderer process. */
  };

  // Additional Type Information about a Task.
  enum class SubType {
    kNoSubType = 0,

    /* Renderer Processes may also be marked as a specific renderer subtype. */
    kSpareRenderer,
    kUnknownRenderer,
  };

  // Create a task with the given |title| and the given favicon |icon|. This
  // task runs on a process whose handle is |handle|.
  // If |process_id| is not supplied, it will be determined by |handle|.
  Task(const std::u16string& title,
       const gfx::ImageSkia* icon,
       base::ProcessHandle handle,
       base::ProcessId process_id = base::kNullProcessId);
  Task(const Task&) = delete;
  Task& operator=(const Task&) = delete;
  virtual ~Task();

  // Gets the name of the given |profile| from the ProfileAttributesStorage.
  static std::u16string GetProfileNameFromProfile(Profile* profile);

  // Activates this TaskManager's task by bringing its container to the front
  // (if possible).
  virtual void Activate();

  // Returns if the task should be killable from the Task Manager UI.
  virtual bool IsKillable();

  // Kills this task. Returns true if the process terminates.
  virtual bool Kill();

  // Will be called to let the task refresh itself between refresh cycles.
  // |update_interval| is the time since the last task manager refresh.
  // the |refresh_flags| indicate which resources should be calculated on each
  // refresh.
  virtual void Refresh(const base::TimeDelta& update_interval,
                       int64_t refresh_flags);

  // Modifies the value of process_id(). To mutate the process ID, this Task is
  // temporarily unregistered from |observer|, and then re-registered before
  // returning.
  void UpdateProcessInfo(base::ProcessHandle handle,
                         base::ProcessId process_id,
                         TaskProviderObserver* observer);

  // Will receive this notification through the task manager from
  // |ChromeNetworkDelegate::OnNetworkBytesReceived()|. The task will add to the
  // |cummulative_read_bytes_|.
  void OnNetworkBytesRead(int64_t bytes_read);

  // Will receive this notification through the task manager from
  // |ChromeNetworkDelegate::OnNetworkBytesSent()|. The task will add to the
  // |cummulative_sent_bytes_| in this refresh cycle.
  void OnNetworkBytesSent(int64_t bytes_sent);

  // Returns the task type.
  virtual Type GetType() const = 0;

  // Returns the task subtype.
  virtual SubType GetSubType() const;

  // This is the unique ID of the BrowserChildProcessHost/RenderProcessHost. It
  // is not the PID nor the handle of the process.
  // For a task that represents the browser process, the return value is 0. For
  // other tasks that represent renderers and other child processes, the return
  // value is whatever unique IDs of their hosts in the browser process.
  virtual int GetChildProcessUniqueID() const = 0;

  // If the process, in which this task is running, is terminated, this gets the
  // termination status. Currently implemented only for Renderer processes.
  virtual void GetTerminationStatus(base::TerminationStatus* out_status,
                                    int* out_error_code) const;

  // The name of the profile owning this task.
  virtual std::u16string GetProfileName() const;

  // Returns the unique ID of the tab if this task represents a renderer
  // WebContents used for a tab. Returns SessionID::InvalidValue() if this task
  // does not represent a renderer, or a contents of a tab.
  virtual SessionID GetTabId() const;

  // For Tasks that represent a subactivity of some other task (e.g. a plugin
  // embedded in a page), this returns the Task representing the parent
  // activity.
  bool HasParentTask() const;
  virtual base::WeakPtr<Task> GetParentTask() const;

  // Getting the Sqlite used memory (in bytes). Not all tasks reports Sqlite
  // memory, in this case a default invalid value of -1 will be returned.
  // Check for whether the task reports it or not first.
  bool ReportsSqliteMemory() const;
  virtual int64_t GetSqliteMemoryUsed() const;

  // Getting the allocated and used V8 memory (in bytes). Not all tasks reports
  // V8 memory, in this case a default invalid value of -1 will be returned.
  virtual int64_t GetV8MemoryAllocated() const;
  virtual int64_t GetV8MemoryUsed() const;

  // Checking if the task reports Webkit resource cache statistics and getting
  // them if it does.
  virtual bool ReportsWebCacheStats() const;
  virtual blink::WebCacheResourceTypeStats GetWebCacheStats() const;

  // Returns the keep-alive counter if the Task is an event page, -1 otherwise.
  virtual int GetKeepaliveCount() const;

  // Returns true if the task is running inside a VM.
  virtual bool IsRunningInVM() const;

  // Returns the instantaneous rate, in bytes per second, of network usage
  // (sent and received), as measured over the last refresh cycle.
  virtual int64_t GetNetworkUsageRate() const;

  // Returns the cumulative number of bytes of network use (sent and received)
  // over the tasks lifetime. It is calculated independently of refreshes and
  // is based on the current |cumulative_bytes_read_| and
  // |cumulative_bytes_sent_|.
  virtual int64_t GetCumulativeNetworkUsage() const;

  int64_t task_id() const { return task_id_; }
  const std::u16string& title() const { return title_; }
  const gfx::ImageSkia& icon() const { return icon_; }
  const base::ProcessHandle& process_handle() const { return process_handle_; }
  const base::ProcessId& process_id() const { return process_id_; }

  base::WeakPtr<Task> AsWeakPtr();

 protected:
  // If |*result_image| is not already set, fetch the image with id
  // |id| from the resource database and put in |*result_image|.
  // Returns |*result_image|.
  static gfx::ImageSkia* FetchIcon(int id, gfx::ImageSkia** result_image);
  void set_title(const std::u16string& new_title) { title_ = new_title; }
  void set_icon(const gfx::ImageSkia& new_icon) { icon_ = new_icon; }

 private:
  // The unique ID of this task.
  const int64_t task_id_;

  // The sum of all bytes that have been uploaded from this task calculated at
  // the last refresh.
  int64_t last_refresh_cumulative_bytes_sent_;

  // The sum of all bytes that have been downloaded from this task calculated
  // at the last refresh.
  int64_t last_refresh_cumulative_bytes_read_;

  // A continuously updating sum of all bytes that have been uploaded from this
  // task. It is assigned to |last_refresh_cumulative_bytes_sent_| at the end
  // of a refresh.
  int64_t cumulative_bytes_sent_;

  // A continuously updating sum of all bytes that have been downloaded from
  // this task. It is assigned to |last_refresh_cumulative_bytes_sent_| at the
  // end of a refresh.
  int64_t cumulative_bytes_read_;

  // The upload rate (in bytes per second) for this task during the latest
  // refresh.
  int64_t network_sent_rate_;

  // The download rate (in bytes per second) for this task during the latest
  // refresh.
  int64_t network_read_rate_;

  // The title of the task.
  std::u16string title_;

  // The favicon.
  gfx::ImageSkia icon_;

  // The handle of the process on which this task is running.
  base::ProcessHandle process_handle_;

  // The PID of the process on which this task is running.
  base::ProcessId process_id_;

  base::WeakPtrFactory<Task> weak_ptr_factory_{this};
};

}  // namespace task_manager

#endif  // CHROME_BROWSER_TASK_MANAGER_PROVIDERS_TASK_H_