Chromium Code Reviews| Index: components/mus/ws/user_activity_monitor.cc |
| diff --git a/components/mus/ws/user_activity_monitor.cc b/components/mus/ws/user_activity_monitor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7e6cd523f210aa26edfbd598c36bb2739db4c6c6 |
| --- /dev/null |
| +++ b/components/mus/ws/user_activity_monitor.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/mus/ws/user_activity_monitor.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/time/default_tick_clock.h" |
| + |
| +namespace mus { |
| +namespace ws { |
| + |
| +UserActivityMonitor::UserActivityMonitor(std::unique_ptr<base::TickClock> clock) |
| + : now_clock_(std::move(clock)) { |
| + if (!now_clock_) |
| + now_clock_ = base::WrapUnique(new base::DefaultTickClock); |
| + last_activity_ = now_clock_->NowTicks(); |
| + idle_timer_.Start(FROM_HERE, base::TimeDelta::FromMinutes(1), this, |
| + &UserActivityMonitor::OnMinuteTimer); |
| +} |
| + |
| +UserActivityMonitor::~UserActivityMonitor() {} |
| + |
| +void UserActivityMonitor::OnUserActivity() { |
| + base::TimeTicks now = now_clock_->NowTicks(); |
| + for (auto& pair : activity_observers_) { |
|
sky
2016/06/27 15:54:26
Should you remove from both of these lists any int
sadrul
2016/06/27 20:46:13
Yep (meant to do that before sending out for revie
|
| + ActivityObserverInfo* info = &(pair.first); |
| + if (info->last_activity_notification.is_null() || |
| + (now - info->last_activity_notification) > info->delay) { |
| + pair.second->OnUserActivity(); |
| + info->last_activity_notification = now; |
| + } |
| + } |
| + |
| + // Wake up all the 'idle' observers. |
| + for (auto& pair : idleness_observers_) { |
| + IdlenessObserverInfo* info = &(pair.first); |
| + if (info->idle_state == mojom::UserIdlenessObserver::IdleState::ACTIVE) |
| + continue; |
| + info->last_idle_state_notification = now; |
| + info->idle_state = mojom::UserIdlenessObserver::IdleState::ACTIVE; |
| + pair.second->OnUserIdleStateChanged(info->idle_state); |
| + } |
| + |
| + // Restart the timer. |
| + last_activity_ = now; |
| + idle_timer_.Reset(); |
| +} |
| + |
| +void UserActivityMonitor::Add(mojom::UserActivityMonitorRequest request) { |
| + bindings_.AddBinding(this, std::move(request)); |
| +} |
| + |
| +void UserActivityMonitor::AddUserActivityObserver( |
| + uint32_t delay_between_notify_secs, |
| + mojom::UserActivityObserverPtr observer) { |
| + ActivityObserverInfo info; |
| + info.delay = base::TimeDelta::FromSeconds(delay_between_notify_secs); |
| + activity_observers_.push_back(std::make_pair(info, std::move(observer))); |
| +} |
| + |
| +void UserActivityMonitor::AddUserIdlenessObserver( |
| + uint32_t idleness_in_minutes, |
| + mojom::UserIdlenessObserverPtr observer) { |
| + IdlenessObserverInfo info; |
| + info.idle_duration = base::TimeDelta::FromMinutes(idleness_in_minutes); |
| + base::TimeTicks now = now_clock_->NowTicks(); |
| + DCHECK(!last_activity_.is_null()); |
| + if (now - last_activity_ >= info.idle_duration) |
| + info.idle_state = mojom::UserIdlenessObserver::IdleState::IDLE; |
| + else |
| + info.idle_state = mojom::UserIdlenessObserver::IdleState::ACTIVE; |
| + info.last_idle_state_notification = now; |
| + observer->OnUserIdleStateChanged(info.idle_state); |
| + idleness_observers_.push_back(std::make_pair(info, std::move(observer))); |
| +} |
| + |
| +void UserActivityMonitor::OnMinuteTimer() { |
| + base::TimeTicks now = now_clock_->NowTicks(); |
| + for (auto& pair : idleness_observers_) { |
| + IdlenessObserverInfo* info = &(pair.first); |
| + if (info->idle_state == mojom::UserIdlenessObserver::IdleState::IDLE) |
| + continue; |
| + if (now - info->last_idle_state_notification < info->idle_duration) |
| + continue; |
| + info->last_idle_state_notification = now; |
| + info->idle_state = mojom::UserIdlenessObserver::IdleState::IDLE; |
| + pair.second->OnUserIdleStateChanged(info->idle_state); |
| + } |
| +} |
| + |
| +} // namespace ws |
| +} // namespace mus |