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 namespace task_management
{
9 void TaskManagerInterface::AddObserver(TaskManagerObserver
* observer
) {
10 observers_
.AddObserver(observer
);
12 ResourceFlagsAdded(observer
->desired_resources_flags());
14 if (observer
->desired_refresh_time() > GetCurrentRefreshTime())
17 // Reached here, then this is EITHER (not the first observer to be added AND
18 // it requires a more frequent refresh rate) OR (it's the very first observer
20 // Reset the refresh timer.
21 ScheduleRefresh(observer
->desired_refresh_time());
24 void TaskManagerInterface::RemoveObserver(TaskManagerObserver
* observer
) {
25 observers_
.RemoveObserver(observer
);
27 // Recalculate the minimum refresh rate and the enabled resource flags.
29 base::TimeDelta min_time
= base::TimeDelta::Max();
30 base::ObserverList
<TaskManagerObserver
>::Iterator
itr(&observers_
);
31 TaskManagerObserver
* obs
;
32 while ((obs
= itr
.GetNext()) != nullptr) {
33 if (obs
->desired_refresh_time() < min_time
)
34 min_time
= obs
->desired_refresh_time();
36 flags
|= obs
->desired_resources_flags();
39 if (min_time
== base::TimeDelta::Max()) {
40 SetEnabledResourceFlags(0);
41 refresh_timer_
->Stop();
43 SetEnabledResourceFlags(flags
);
44 ScheduleRefresh(min_time
);
48 bool TaskManagerInterface::IsResourceRefreshEnabled(RefreshType type
) {
49 return (enabled_resources_flags_
& type
) != 0;
52 TaskManagerInterface::TaskManagerInterface()
54 refresh_timer_(new base::Timer(true, true)),
55 enabled_resources_flags_(0) {
58 TaskManagerInterface::~TaskManagerInterface() {
61 void TaskManagerInterface::NotifyObserversOnTaskAdded(TaskId id
) {
62 FOR_EACH_OBSERVER(TaskManagerObserver
, observers_
, OnTaskAdded(id
));
65 void TaskManagerInterface::NotifyObserversOnTaskToBeRemoved(TaskId id
) {
66 FOR_EACH_OBSERVER(TaskManagerObserver
, observers_
, OnTaskToBeRemoved(id
));
69 void TaskManagerInterface::NotifyObserversOnRefresh(
70 const TaskIdList
& task_ids
) {
71 FOR_EACH_OBSERVER(TaskManagerObserver
,
73 OnTasksRefreshed(task_ids
));
76 base::TimeDelta
TaskManagerInterface::GetCurrentRefreshTime() const {
77 return refresh_timer_
->IsRunning() ? refresh_timer_
->GetCurrentDelay()
78 : base::TimeDelta::Max();
81 void TaskManagerInterface::ResourceFlagsAdded(int64 flags
) {
82 enabled_resources_flags_
|= flags
;
85 void TaskManagerInterface::SetEnabledResourceFlags(int64 flags
) {
86 enabled_resources_flags_
= flags
;
89 void TaskManagerInterface::ScheduleRefresh(base::TimeDelta refresh_time
) {
90 refresh_timer_
->Start(FROM_HERE
,
92 base::Bind(&TaskManagerInterface::Refresh
,
93 base::Unretained(this)));
96 } // namespace task_management