1 // 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.
5 #include "chrome/browser/task_management/task_manager_interface.h"
7 #include "chrome/browser/task_management/sampling/task_manager_impl.h"
8 #include "chrome/browser/task_management/sampling/task_manager_io_thread_helper.h"
9 #include "chrome/browser/task_manager/task_manager.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "content/public/browser/browser_thread.h"
13 namespace task_management
{
16 TaskManagerInterface
* TaskManagerInterface::GetTaskManager() {
17 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
19 return TaskManagerImpl::GetInstance();
23 void TaskManagerInterface::OnRawBytesRead(const net::URLRequest
& request
,
25 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
27 if (switches::NewTaskManagerEnabled())
28 TaskManagerIoThreadHelper::OnRawBytesRead(request
, bytes_read
);
30 TaskManager::GetInstance()->model()->NotifyBytesRead(request
, bytes_read
);
33 void TaskManagerInterface::AddObserver(TaskManagerObserver
* observer
) {
34 observers_
.AddObserver(observer
);
35 observer
->observed_task_manager_
= this;
37 ResourceFlagsAdded(observer
->desired_resources_flags());
39 base::TimeDelta current_refresh_time
= GetCurrentRefreshTime();
40 if (current_refresh_time
== base::TimeDelta::Max()) {
41 // This is the first observer to be added. Start updating.
45 if (observer
->desired_refresh_time() > current_refresh_time
)
48 // Reached here, then this is EITHER (not the first observer to be added AND
49 // it requires a more frequent refresh rate) OR (it's the very first observer
51 // Reset the refresh timer.
52 ScheduleRefresh(observer
->desired_refresh_time());
55 void TaskManagerInterface::RemoveObserver(TaskManagerObserver
* observer
) {
56 observers_
.RemoveObserver(observer
);
57 observer
->observed_task_manager_
= nullptr;
59 // Recalculate the minimum refresh rate and the enabled resource flags.
61 base::TimeDelta min_time
= base::TimeDelta::Max();
62 base::ObserverList
<TaskManagerObserver
>::Iterator
itr(&observers_
);
63 while (TaskManagerObserver
* obs
= itr
.GetNext()) {
64 if (obs
->desired_refresh_time() < min_time
)
65 min_time
= obs
->desired_refresh_time();
67 flags
|= obs
->desired_resources_flags();
70 if (min_time
== base::TimeDelta::Max()) {
71 // This is the last observer to be removed. Stop updating.
72 SetEnabledResourceFlags(0);
73 refresh_timer_
->Stop();
76 SetEnabledResourceFlags(flags
);
77 ScheduleRefresh(min_time
);
81 void TaskManagerInterface::RecalculateRefreshFlags() {
83 base::ObserverList
<TaskManagerObserver
>::Iterator
itr(&observers_
);
84 while (TaskManagerObserver
* obs
= itr
.GetNext())
85 flags
|= obs
->desired_resources_flags();
87 SetEnabledResourceFlags(flags
);
90 bool TaskManagerInterface::IsResourceRefreshEnabled(RefreshType type
) {
91 return (enabled_resources_flags_
& type
) != 0;
94 TaskManagerInterface::TaskManagerInterface()
96 refresh_timer_(new base::Timer(true, true)),
97 enabled_resources_flags_(0) {
100 TaskManagerInterface::~TaskManagerInterface() {
103 void TaskManagerInterface::NotifyObserversOnTaskAdded(TaskId id
) {
104 FOR_EACH_OBSERVER(TaskManagerObserver
, observers_
, OnTaskAdded(id
));
107 void TaskManagerInterface::NotifyObserversOnTaskToBeRemoved(TaskId id
) {
108 FOR_EACH_OBSERVER(TaskManagerObserver
, observers_
, OnTaskToBeRemoved(id
));
111 void TaskManagerInterface::NotifyObserversOnRefresh(
112 const TaskIdList
& task_ids
) {
113 FOR_EACH_OBSERVER(TaskManagerObserver
,
115 OnTasksRefreshed(task_ids
));
118 base::TimeDelta
TaskManagerInterface::GetCurrentRefreshTime() const {
119 return refresh_timer_
->IsRunning() ? refresh_timer_
->GetCurrentDelay()
120 : base::TimeDelta::Max();
123 void TaskManagerInterface::ResourceFlagsAdded(int64 flags
) {
124 enabled_resources_flags_
|= flags
;
127 void TaskManagerInterface::SetEnabledResourceFlags(int64 flags
) {
128 enabled_resources_flags_
= flags
;
131 void TaskManagerInterface::ScheduleRefresh(base::TimeDelta refresh_time
) {
132 refresh_timer_
->Start(FROM_HERE
,
134 base::Bind(&TaskManagerInterface::Refresh
,
135 base::Unretained(this)));
138 } // namespace task_management