File: task.cc

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 (215 lines) | stat: -rw-r--r-- 6,251 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
// 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.

#include "chrome/browser/task_manager/providers/task.h"

#include <stddef.h>

#include "base/numerics/safe_conversions.h"
#include "base/process/process.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/task_manager/providers/task_provider_observer.h"
#include "chrome/browser/task_manager/task_manager_observer.h"
#include "content/public/common/result_codes.h"
#include "ui/base/resource/resource_bundle.h"

namespace task_manager {

namespace {

// The last ID given to the previously created task.
int64_t g_last_id = 0;

base::ProcessId DetermineProcessId(base::ProcessHandle handle,
                                   base::ProcessId process_id) {
  if (process_id != base::kNullProcessId)
    return process_id;
  return base::GetProcId(handle);
}

}  // namespace

Task::Task(const std::u16string& title,
           const gfx::ImageSkia* icon,
           base::ProcessHandle handle,
           base::ProcessId process_id)
    : task_id_(g_last_id++),
      last_refresh_cumulative_bytes_sent_(0),
      last_refresh_cumulative_bytes_read_(0),
      cumulative_bytes_sent_(0),
      cumulative_bytes_read_(0),
      network_sent_rate_(0),
      network_read_rate_(0),
      title_(title),
      icon_(icon ? *icon : gfx::ImageSkia()),
      process_handle_(handle),
      process_id_(DetermineProcessId(handle, process_id)) {}

Task::~Task() = default;

// static
std::u16string Task::GetProfileNameFromProfile(Profile* profile) {
  DCHECK(profile);
  ProfileAttributesEntry* entry =
      g_browser_process->profile_manager()
          ->GetProfileAttributesStorage()
          .GetProfileAttributesWithPath(
              profile->GetOriginalProfile()->GetPath());
  return entry ? entry->GetName() : std::u16string();
}

void Task::Activate() {}

bool Task::IsKillable() {
  // Protects from trying to kill a task that doesn't have an accurate process
  // Id yet. This can result in calling "kill 0" which kills all processes in
  // the process group.
  if (process_id() == base::kNullProcessId)
    return false;
  return true;
}

bool Task::Kill() {
  if (!IsKillable())
    return false;
  DCHECK_NE(process_id(), base::GetCurrentProcId());
  base::Process process = base::Process::Open(process_id());
  return process.Terminate(content::RESULT_CODE_KILLED, false);
}

void Task::Refresh(const base::TimeDelta& update_interval,
                   int64_t refresh_flags) {
  if ((refresh_flags & REFRESH_TYPE_NETWORK_USAGE) == 0 ||
      update_interval == base::TimeDelta())
    return;

  int64_t current_cycle_read_byte_count =
      cumulative_bytes_read_ - last_refresh_cumulative_bytes_read_;
  network_read_rate_ = base::ClampRound<int64_t>(current_cycle_read_byte_count /
                                                 update_interval.InSecondsF());

  int64_t current_cycle_sent_byte_count =
      cumulative_bytes_sent_ - last_refresh_cumulative_bytes_sent_;
  network_sent_rate_ = base::ClampRound<int64_t>(current_cycle_sent_byte_count /
                                                 update_interval.InSecondsF());

  last_refresh_cumulative_bytes_read_ = cumulative_bytes_read_;
  last_refresh_cumulative_bytes_sent_ = cumulative_bytes_sent_;
}

void Task::UpdateProcessInfo(base::ProcessHandle handle,
                             base::ProcessId process_id,
                             TaskProviderObserver* observer) {
  process_id = DetermineProcessId(handle, process_id);

  // Don't remove the task if there is no change to the process ID.
  if (process_id == process_id_)
    return;

  // TaskManagerImpl and TaskGroup implementations assume that a process ID is
  // consistent for the lifetime of a Task. So to change the process ID,
  // temporarily unregister this Task.
  observer->TaskRemoved(this);
  process_handle_ = handle;
  process_id_ = process_id;
  observer->TaskAdded(this);
}

void Task::OnNetworkBytesRead(int64_t bytes_read) {
  cumulative_bytes_read_ += bytes_read;
}

void Task::OnNetworkBytesSent(int64_t bytes_sent) {
  cumulative_bytes_sent_ += bytes_sent;
}

Task::SubType Task::GetSubType() const {
  return Task::SubType::kNoSubType;
}

void Task::GetTerminationStatus(base::TerminationStatus* out_status,
                                int* out_error_code) const {
  DCHECK(out_status);
  DCHECK(out_error_code);

  *out_status = base::TERMINATION_STATUS_STILL_RUNNING;
  *out_error_code = 0;
}

std::u16string Task::GetProfileName() const {
  return std::u16string();
}

SessionID Task::GetTabId() const {
  return SessionID::InvalidValue();
}

bool Task::HasParentTask() const {
  return GetParentTask() != nullptr;
}

base::WeakPtr<Task> Task::GetParentTask() const {
  return nullptr;
}

bool Task::ReportsSqliteMemory() const {
  return GetSqliteMemoryUsed() != -1;
}

int64_t Task::GetSqliteMemoryUsed() const {
  return -1;
}

int64_t Task::GetV8MemoryAllocated() const {
  return -1;
}

int64_t Task::GetV8MemoryUsed() const {
  return -1;
}

bool Task::ReportsWebCacheStats() const {
  return false;
}

blink::WebCacheResourceTypeStats Task::GetWebCacheStats() const {
  return blink::WebCacheResourceTypeStats();
}

int Task::GetKeepaliveCount() const {
  return -1;
}

bool Task::IsRunningInVM() const {
  return false;
}

int64_t Task::GetNetworkUsageRate() const {
  return network_sent_rate_ + network_read_rate_;
}

int64_t Task::GetCumulativeNetworkUsage() const {
  return cumulative_bytes_sent_ + cumulative_bytes_read_;
}

// static
gfx::ImageSkia* Task::FetchIcon(int id, gfx::ImageSkia** result_image) {
  if (!*result_image && ui::ResourceBundle::HasSharedInstance()) {
    *result_image =
        ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(id);
    if (*result_image)
      (*result_image)->MakeThreadSafe();
  }
  return *result_image;
}

base::WeakPtr<Task> Task::AsWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

}  // namespace task_manager