blob: 09c8843d360368e4928cf1a4be585e39a2becda2 [file] [log] [blame]
afakhry54485752015-07-06 17:39:161// Copyright 2015 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
avi24d693f2016-08-06 18:03:525#include "chrome/browser/task_manager/task_manager_interface.h"
afakhry54485752015-07-06 17:39:166
afakhry174069722016-05-24 19:16:167#include "chrome/browser/browser_process.h"
avi24d693f2016-08-06 18:03:528#include "chrome/browser/task_manager/sampling/task_manager_impl.h"
9#include "chrome/browser/task_manager/sampling/task_manager_io_thread_helper.h"
afakhry05015032015-08-14 01:09:5610#include "chrome/common/chrome_switches.h"
afakhry174069722016-05-24 19:16:1611#include "chrome/common/pref_names.h"
12#include "components/prefs/pref_registry_simple.h"
13#include "components/prefs/pref_service.h"
afakhry05015032015-08-14 01:09:5614#include "content/public/browser/browser_thread.h"
cburnc0a59532017-07-06 17:34:3915#include "content/public/browser/resource_request_info.h"
afakhry05015032015-08-14 01:09:5616
afakhry174069722016-05-24 19:16:1617#if defined(OS_MACOSX)
18#include "chrome/browser/ui/browser_dialogs.h"
19#endif // defined(OS_MACOSX)
20
avi24d693f2016-08-06 18:03:5221namespace task_manager {
afakhry54485752015-07-06 17:39:1622
cburnc0a59532017-07-06 17:34:3923namespace {
24BytesTransferredKey KeyForRequest(const net::URLRequest& request) {
25 // Only net::URLRequestJob instances created by the ResourceDispatcherHost
26 // have an associated ResourceRequestInfo and a render frame associated.
cburnc0a59532017-07-06 17:34:3927 const content::ResourceRequestInfo* info =
28 content::ResourceRequestInfo::ForRequest(&request);
cburnc0a59532017-07-06 17:34:3929
Nick Carter074b5202017-11-29 00:44:2530 // Requests without ResourceRequestInfo are attributed to the browser process.
31 if (!info)
32 return {content::ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE};
cburnc0a59532017-07-06 17:34:3933
Nick Carter074b5202017-11-29 00:44:2534 // Requests from PPAPI instances are proxied through the renderer, and specify
35 // the plugin_child_id of the plugin process.
36 if (info->GetPluginChildID() != content::ChildProcessHost::kInvalidUniqueID)
37 return {info->GetPluginChildID(), MSG_ROUTING_NONE};
38
39 // Other requests are associated with the child process (and frame, if
40 // originating from a renderer process).
41 return {info->GetChildID(), info->GetRenderFrameID()};
cburnc0a59532017-07-06 17:34:3942}
43} // namespace
44
afakhry05015032015-08-14 01:09:5645// static
afakhry174069722016-05-24 19:16:1646void TaskManagerInterface::RegisterPrefs(PrefRegistrySimple* registry) {
47 registry->RegisterDictionaryPref(prefs::kTaskManagerWindowPlacement);
48 registry->RegisterDictionaryPref(prefs::kTaskManagerColumnVisibility);
49 registry->RegisterBooleanPref(prefs::kTaskManagerEndProcessEnabled, true);
50}
51
52// static
53bool TaskManagerInterface::IsEndProcessEnabled() {
54 PrefService* state = g_browser_process->local_state();
55 return !state || state->GetBoolean(prefs::kTaskManagerEndProcessEnabled);
56}
57
afakhry174069722016-05-24 19:16:1658// static
afakhry05015032015-08-14 01:09:5659TaskManagerInterface* TaskManagerInterface::GetTaskManager() {
60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
61
62 return TaskManagerImpl::GetInstance();
63}
64
65// static
66void TaskManagerInterface::OnRawBytesRead(const net::URLRequest& request,
sclittlece72c482015-08-24 20:20:5967 int64_t bytes_read) {
afakhry05015032015-08-14 01:09:5668 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
cburnc0a59532017-07-06 17:34:3969 BytesTransferredKey key = KeyForRequest(request);
70 TaskManagerIoThreadHelper::OnRawBytesTransferred(key, bytes_read,
71 0 /*bytes_sent*/);
afakhry05015032015-08-14 01:09:5672}
73
cburne9d2f3612017-06-20 22:15:0374// static
75void TaskManagerInterface::OnRawBytesSent(const net::URLRequest& request,
76 int64_t bytes_sent) {
77 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
cburnc0a59532017-07-06 17:34:3978 BytesTransferredKey key = KeyForRequest(request);
79 TaskManagerIoThreadHelper::OnRawBytesTransferred(key, 0 /*bytes_read*/,
80 bytes_sent);
cburne9d2f3612017-06-20 22:15:0381}
82
afakhry54485752015-07-06 17:39:1683void TaskManagerInterface::AddObserver(TaskManagerObserver* observer) {
84 observers_.AddObserver(observer);
afakhry05015032015-08-14 01:09:5685 observer->observed_task_manager_ = this;
afakhry54485752015-07-06 17:39:1686
87 ResourceFlagsAdded(observer->desired_resources_flags());
88
afakhry05015032015-08-14 01:09:5689 base::TimeDelta current_refresh_time = GetCurrentRefreshTime();
90 if (current_refresh_time == base::TimeDelta::Max()) {
91 // This is the first observer to be added. Start updating.
92 StartUpdating();
93 }
94
95 if (observer->desired_refresh_time() > current_refresh_time)
afakhry54485752015-07-06 17:39:1696 return;
97
98 // Reached here, then this is EITHER (not the first observer to be added AND
99 // it requires a more frequent refresh rate) OR (it's the very first observer
100 // to be added).
101 // Reset the refresh timer.
102 ScheduleRefresh(observer->desired_refresh_time());
103}
104
105void TaskManagerInterface::RemoveObserver(TaskManagerObserver* observer) {
106 observers_.RemoveObserver(observer);
afakhry05015032015-08-14 01:09:56107 observer->observed_task_manager_ = nullptr;
afakhry54485752015-07-06 17:39:16108
109 // Recalculate the minimum refresh rate and the enabled resource flags.
avi664c07b2015-12-26 02:18:31110 int64_t flags = 0;
afakhry54485752015-07-06 17:39:16111 base::TimeDelta min_time = base::TimeDelta::Max();
dchengea687662016-10-13 16:59:52112 for (auto& observer : observers_) {
113 if (observer.desired_refresh_time() < min_time)
114 min_time = observer.desired_refresh_time();
afakhry54485752015-07-06 17:39:16115
dchengea687662016-10-13 16:59:52116 flags |= observer.desired_resources_flags();
afakhry54485752015-07-06 17:39:16117 }
118
119 if (min_time == base::TimeDelta::Max()) {
afakhry05015032015-08-14 01:09:56120 // This is the last observer to be removed. Stop updating.
afakhry54485752015-07-06 17:39:16121 SetEnabledResourceFlags(0);
122 refresh_timer_->Stop();
afakhry05015032015-08-14 01:09:56123 StopUpdating();
afakhry54485752015-07-06 17:39:16124 } else {
125 SetEnabledResourceFlags(flags);
126 ScheduleRefresh(min_time);
127 }
128}
129
afakhry05015032015-08-14 01:09:56130void TaskManagerInterface::RecalculateRefreshFlags() {
avi664c07b2015-12-26 02:18:31131 int64_t flags = 0;
dchengea687662016-10-13 16:59:52132 for (auto& observer : observers_)
133 flags |= observer.desired_resources_flags();
afakhry05015032015-08-14 01:09:56134
135 SetEnabledResourceFlags(flags);
136}
137
afakhry98241832016-03-11 19:27:47138bool TaskManagerInterface::IsResourceRefreshEnabled(RefreshType type) const {
afakhry54485752015-07-06 17:39:16139 return (enabled_resources_flags_ & type) != 0;
140}
141
142TaskManagerInterface::TaskManagerInterface()
thestig4949a1e2016-03-29 00:53:52143 : refresh_timer_(new base::Timer(true, true)),
afakhry54485752015-07-06 17:39:16144 enabled_resources_flags_(0) {
145}
146
147TaskManagerInterface::~TaskManagerInterface() {
148}
149
150void TaskManagerInterface::NotifyObserversOnTaskAdded(TaskId id) {
ericwilligers58b0e162016-10-21 07:15:56151 for (TaskManagerObserver& observer : observers_)
152 observer.OnTaskAdded(id);
afakhry54485752015-07-06 17:39:16153}
154
155void TaskManagerInterface::NotifyObserversOnTaskToBeRemoved(TaskId id) {
ericwilligers58b0e162016-10-21 07:15:56156 for (TaskManagerObserver& observer : observers_)
157 observer.OnTaskToBeRemoved(id);
afakhry54485752015-07-06 17:39:16158}
159
160void TaskManagerInterface::NotifyObserversOnRefresh(
161 const TaskIdList& task_ids) {
ericwilligers58b0e162016-10-21 07:15:56162 for (TaskManagerObserver& observer : observers_)
163 observer.OnTasksRefreshed(task_ids);
afakhry54485752015-07-06 17:39:16164}
165
afakhry98241832016-03-11 19:27:47166void TaskManagerInterface::NotifyObserversOnRefreshWithBackgroundCalculations(
167 const TaskIdList& task_ids) {
ericwilligers58b0e162016-10-21 07:15:56168 for (TaskManagerObserver& observer : observers_)
169 observer.OnTasksRefreshedWithBackgroundCalculations(task_ids);
afakhry98241832016-03-11 19:27:47170}
171
172void TaskManagerInterface::NotifyObserversOnTaskUnresponsive(TaskId id) {
ericwilligers58b0e162016-10-21 07:15:56173 for (TaskManagerObserver& observer : observers_)
174 observer.OnTaskUnresponsive(id);
afakhry98241832016-03-11 19:27:47175}
176
afakhry54485752015-07-06 17:39:16177base::TimeDelta TaskManagerInterface::GetCurrentRefreshTime() const {
178 return refresh_timer_->IsRunning() ? refresh_timer_->GetCurrentDelay()
179 : base::TimeDelta::Max();
180}
181
avi664c07b2015-12-26 02:18:31182void TaskManagerInterface::ResourceFlagsAdded(int64_t flags) {
afakhry54485752015-07-06 17:39:16183 enabled_resources_flags_ |= flags;
184}
185
avi664c07b2015-12-26 02:18:31186void TaskManagerInterface::SetEnabledResourceFlags(int64_t flags) {
afakhry54485752015-07-06 17:39:16187 enabled_resources_flags_ = flags;
188}
189
190void TaskManagerInterface::ScheduleRefresh(base::TimeDelta refresh_time) {
191 refresh_timer_->Start(FROM_HERE,
192 refresh_time,
193 base::Bind(&TaskManagerInterface::Refresh,
194 base::Unretained(this)));
195}
196
avi24d693f2016-08-06 18:03:52197} // namespace task_manager