blob: c0b79f5e8d083a6c1a2a29df372a160a6279fe68 [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
5#include "chrome/browser/task_management/task_manager_interface.h"
6
afakhry174069722016-05-24 19:16:167#include "chrome/browser/browser_process.h"
afakhry05015032015-08-14 01:09:568#include "chrome/browser/task_management/sampling/task_manager_impl.h"
9#include "chrome/browser/task_management/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"
15
afakhry174069722016-05-24 19:16:1616#if defined(OS_MACOSX)
17#include "chrome/browser/ui/browser_dialogs.h"
18#endif // defined(OS_MACOSX)
19
afakhry54485752015-07-06 17:39:1620namespace task_management {
21
afakhry05015032015-08-14 01:09:5622// static
afakhry174069722016-05-24 19:16:1623void TaskManagerInterface::RegisterPrefs(PrefRegistrySimple* registry) {
24 registry->RegisterDictionaryPref(prefs::kTaskManagerWindowPlacement);
25 registry->RegisterDictionaryPref(prefs::kTaskManagerColumnVisibility);
26 registry->RegisterBooleanPref(prefs::kTaskManagerEndProcessEnabled, true);
27}
28
29// static
30bool TaskManagerInterface::IsEndProcessEnabled() {
31 PrefService* state = g_browser_process->local_state();
32 return !state || state->GetBoolean(prefs::kTaskManagerEndProcessEnabled);
33}
34
35#if defined(OS_MACOSX)
36// static
37bool TaskManagerInterface::IsNewTaskManagerEnabled() {
38 return chrome::ToolkitViewsDialogsEnabled();
39}
40#endif // defined(OS_MACOSX)
41
42// static
afakhry05015032015-08-14 01:09:5643TaskManagerInterface* TaskManagerInterface::GetTaskManager() {
44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
45
46 return TaskManagerImpl::GetInstance();
47}
48
49// static
50void TaskManagerInterface::OnRawBytesRead(const net::URLRequest& request,
sclittlece72c482015-08-24 20:20:5951 int64_t bytes_read) {
afakhry05015032015-08-14 01:09:5652 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
53
afakhry174069722016-05-24 19:16:1654#if defined(OS_MACOSX)
55 if (chrome::NotifyOldTaskManagerBytesRead(request, bytes_read))
56 return;
57#endif // defined(OS_MACOSX)
58
59 TaskManagerIoThreadHelper::OnRawBytesRead(request, bytes_read);
afakhry05015032015-08-14 01:09:5660}
61
afakhry54485752015-07-06 17:39:1662void TaskManagerInterface::AddObserver(TaskManagerObserver* observer) {
63 observers_.AddObserver(observer);
afakhry05015032015-08-14 01:09:5664 observer->observed_task_manager_ = this;
afakhry54485752015-07-06 17:39:1665
66 ResourceFlagsAdded(observer->desired_resources_flags());
67
afakhry05015032015-08-14 01:09:5668 base::TimeDelta current_refresh_time = GetCurrentRefreshTime();
69 if (current_refresh_time == base::TimeDelta::Max()) {
70 // This is the first observer to be added. Start updating.
71 StartUpdating();
72 }
73
74 if (observer->desired_refresh_time() > current_refresh_time)
afakhry54485752015-07-06 17:39:1675 return;
76
77 // Reached here, then this is EITHER (not the first observer to be added AND
78 // it requires a more frequent refresh rate) OR (it's the very first observer
79 // to be added).
80 // Reset the refresh timer.
81 ScheduleRefresh(observer->desired_refresh_time());
82}
83
84void TaskManagerInterface::RemoveObserver(TaskManagerObserver* observer) {
85 observers_.RemoveObserver(observer);
afakhry05015032015-08-14 01:09:5686 observer->observed_task_manager_ = nullptr;
afakhry54485752015-07-06 17:39:1687
88 // Recalculate the minimum refresh rate and the enabled resource flags.
avi664c07b2015-12-26 02:18:3189 int64_t flags = 0;
afakhry54485752015-07-06 17:39:1690 base::TimeDelta min_time = base::TimeDelta::Max();
91 base::ObserverList<TaskManagerObserver>::Iterator itr(&observers_);
afakhry05015032015-08-14 01:09:5692 while (TaskManagerObserver* obs = itr.GetNext()) {
afakhry54485752015-07-06 17:39:1693 if (obs->desired_refresh_time() < min_time)
94 min_time = obs->desired_refresh_time();
95
96 flags |= obs->desired_resources_flags();
97 }
98
99 if (min_time == base::TimeDelta::Max()) {
afakhry05015032015-08-14 01:09:56100 // This is the last observer to be removed. Stop updating.
afakhry54485752015-07-06 17:39:16101 SetEnabledResourceFlags(0);
102 refresh_timer_->Stop();
afakhry05015032015-08-14 01:09:56103 StopUpdating();
afakhry54485752015-07-06 17:39:16104 } else {
105 SetEnabledResourceFlags(flags);
106 ScheduleRefresh(min_time);
107 }
108}
109
afakhry05015032015-08-14 01:09:56110void TaskManagerInterface::RecalculateRefreshFlags() {
avi664c07b2015-12-26 02:18:31111 int64_t flags = 0;
afakhry05015032015-08-14 01:09:56112 base::ObserverList<TaskManagerObserver>::Iterator itr(&observers_);
113 while (TaskManagerObserver* obs = itr.GetNext())
114 flags |= obs->desired_resources_flags();
115
116 SetEnabledResourceFlags(flags);
117}
118
afakhry98241832016-03-11 19:27:47119bool TaskManagerInterface::IsResourceRefreshEnabled(RefreshType type) const {
afakhry54485752015-07-06 17:39:16120 return (enabled_resources_flags_ & type) != 0;
121}
122
123TaskManagerInterface::TaskManagerInterface()
thestig4949a1e2016-03-29 00:53:52124 : refresh_timer_(new base::Timer(true, true)),
afakhry54485752015-07-06 17:39:16125 enabled_resources_flags_(0) {
126}
127
128TaskManagerInterface::~TaskManagerInterface() {
129}
130
131void TaskManagerInterface::NotifyObserversOnTaskAdded(TaskId id) {
132 FOR_EACH_OBSERVER(TaskManagerObserver, observers_, OnTaskAdded(id));
133}
134
135void TaskManagerInterface::NotifyObserversOnTaskToBeRemoved(TaskId id) {
136 FOR_EACH_OBSERVER(TaskManagerObserver, observers_, OnTaskToBeRemoved(id));
137}
138
139void TaskManagerInterface::NotifyObserversOnRefresh(
140 const TaskIdList& task_ids) {
141 FOR_EACH_OBSERVER(TaskManagerObserver,
142 observers_,
143 OnTasksRefreshed(task_ids));
144}
145
afakhry98241832016-03-11 19:27:47146void TaskManagerInterface::NotifyObserversOnRefreshWithBackgroundCalculations(
147 const TaskIdList& task_ids) {
148 FOR_EACH_OBSERVER(TaskManagerObserver,
149 observers_,
150 OnTasksRefreshedWithBackgroundCalculations(task_ids));
151}
152
153void TaskManagerInterface::NotifyObserversOnTaskUnresponsive(TaskId id) {
154 FOR_EACH_OBSERVER(TaskManagerObserver, observers_, OnTaskUnresponsive(id));
155}
156
afakhry54485752015-07-06 17:39:16157base::TimeDelta TaskManagerInterface::GetCurrentRefreshTime() const {
158 return refresh_timer_->IsRunning() ? refresh_timer_->GetCurrentDelay()
159 : base::TimeDelta::Max();
160}
161
avi664c07b2015-12-26 02:18:31162void TaskManagerInterface::ResourceFlagsAdded(int64_t flags) {
afakhry54485752015-07-06 17:39:16163 enabled_resources_flags_ |= flags;
164}
165
avi664c07b2015-12-26 02:18:31166void TaskManagerInterface::SetEnabledResourceFlags(int64_t flags) {
afakhry54485752015-07-06 17:39:16167 enabled_resources_flags_ = flags;
168}
169
170void TaskManagerInterface::ScheduleRefresh(base::TimeDelta refresh_time) {
171 refresh_timer_->Start(FROM_HERE,
172 refresh_time,
173 base::Bind(&TaskManagerInterface::Refresh,
174 base::Unretained(this)));
175}
176
177} // namespace task_management