Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "components/mus/ws/user_activity_monitor.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/time/default_tick_clock.h" | |
| 9 | |
| 10 namespace mus { | |
| 11 namespace ws { | |
| 12 | |
| 13 UserActivityMonitor::UserActivityMonitor(std::unique_ptr<base::TickClock> clock) | |
| 14 : now_clock_(std::move(clock)) { | |
| 15 if (!now_clock_) | |
| 16 now_clock_ = base::WrapUnique(new base::DefaultTickClock); | |
| 17 last_activity_ = now_clock_->NowTicks(); | |
| 18 idle_timer_.Start(FROM_HERE, base::TimeDelta::FromMinutes(1), this, | |
| 19 &UserActivityMonitor::OnMinuteTimer); | |
| 20 } | |
| 21 | |
| 22 UserActivityMonitor::~UserActivityMonitor() {} | |
| 23 | |
| 24 void UserActivityMonitor::OnUserActivity() { | |
| 25 base::TimeTicks now = now_clock_->NowTicks(); | |
| 26 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
| |
| 27 ActivityObserverInfo* info = &(pair.first); | |
| 28 if (info->last_activity_notification.is_null() || | |
| 29 (now - info->last_activity_notification) > info->delay) { | |
| 30 pair.second->OnUserActivity(); | |
| 31 info->last_activity_notification = now; | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 // Wake up all the 'idle' observers. | |
| 36 for (auto& pair : idleness_observers_) { | |
| 37 IdlenessObserverInfo* info = &(pair.first); | |
| 38 if (info->idle_state == mojom::UserIdlenessObserver::IdleState::ACTIVE) | |
| 39 continue; | |
| 40 info->last_idle_state_notification = now; | |
| 41 info->idle_state = mojom::UserIdlenessObserver::IdleState::ACTIVE; | |
| 42 pair.second->OnUserIdleStateChanged(info->idle_state); | |
| 43 } | |
| 44 | |
| 45 // Restart the timer. | |
| 46 last_activity_ = now; | |
| 47 idle_timer_.Reset(); | |
| 48 } | |
| 49 | |
| 50 void UserActivityMonitor::Add(mojom::UserActivityMonitorRequest request) { | |
| 51 bindings_.AddBinding(this, std::move(request)); | |
| 52 } | |
| 53 | |
| 54 void UserActivityMonitor::AddUserActivityObserver( | |
| 55 uint32_t delay_between_notify_secs, | |
| 56 mojom::UserActivityObserverPtr observer) { | |
| 57 ActivityObserverInfo info; | |
| 58 info.delay = base::TimeDelta::FromSeconds(delay_between_notify_secs); | |
| 59 activity_observers_.push_back(std::make_pair(info, std::move(observer))); | |
| 60 } | |
| 61 | |
| 62 void UserActivityMonitor::AddUserIdlenessObserver( | |
| 63 uint32_t idleness_in_minutes, | |
| 64 mojom::UserIdlenessObserverPtr observer) { | |
| 65 IdlenessObserverInfo info; | |
| 66 info.idle_duration = base::TimeDelta::FromMinutes(idleness_in_minutes); | |
| 67 base::TimeTicks now = now_clock_->NowTicks(); | |
| 68 DCHECK(!last_activity_.is_null()); | |
| 69 if (now - last_activity_ >= info.idle_duration) | |
| 70 info.idle_state = mojom::UserIdlenessObserver::IdleState::IDLE; | |
| 71 else | |
| 72 info.idle_state = mojom::UserIdlenessObserver::IdleState::ACTIVE; | |
| 73 info.last_idle_state_notification = now; | |
| 74 observer->OnUserIdleStateChanged(info.idle_state); | |
| 75 idleness_observers_.push_back(std::make_pair(info, std::move(observer))); | |
| 76 } | |
| 77 | |
| 78 void UserActivityMonitor::OnMinuteTimer() { | |
| 79 base::TimeTicks now = now_clock_->NowTicks(); | |
| 80 for (auto& pair : idleness_observers_) { | |
| 81 IdlenessObserverInfo* info = &(pair.first); | |
| 82 if (info->idle_state == mojom::UserIdlenessObserver::IdleState::IDLE) | |
| 83 continue; | |
| 84 if (now - info->last_idle_state_notification < info->idle_duration) | |
| 85 continue; | |
| 86 info->last_idle_state_notification = now; | |
| 87 info->idle_state = mojom::UserIdlenessObserver::IdleState::IDLE; | |
| 88 pair.second->OnUserIdleStateChanged(info->idle_state); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 } // namespace ws | |
| 93 } // namespace mus | |
| OLD | NEW |