video_activity_notifier.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2013 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. #include "ash/system/power/video_activity_notifier.h"
  5. #include "ash/session/session_controller_impl.h"
  6. #include "ash/shell.h"
  7. #include "chromeos/dbus/power/power_manager_client.h"
  8. namespace ash {
  9. namespace {
  10. // Minimum number of seconds between repeated notifications of the same state.
  11. // This should be less than powerd's timeout for determining whether video is
  12. // still active for the purposes of controlling the keyboard backlight.
  13. const int kNotifyIntervalSec = 5;
  14. } // namespace
  15. VideoActivityNotifier::VideoActivityNotifier(VideoDetector* detector)
  16. : detector_(detector),
  17. video_state_(detector->state()),
  18. screen_is_locked_(Shell::Get()->session_controller()->IsScreenLocked()),
  19. scoped_session_observer_(this) {
  20. detector_->AddObserver(this);
  21. MaybeNotifyPowerManager();
  22. UpdateTimer();
  23. }
  24. VideoActivityNotifier::~VideoActivityNotifier() {
  25. detector_->RemoveObserver(this);
  26. }
  27. void VideoActivityNotifier::OnVideoStateChanged(VideoDetector::State state) {
  28. if (video_state_ != state) {
  29. video_state_ = state;
  30. MaybeNotifyPowerManager();
  31. UpdateTimer();
  32. }
  33. }
  34. void VideoActivityNotifier::OnLockStateChanged(bool locked) {
  35. if (screen_is_locked_ == locked)
  36. return;
  37. screen_is_locked_ = locked;
  38. MaybeNotifyPowerManager();
  39. UpdateTimer();
  40. }
  41. bool VideoActivityNotifier::TriggerTimeoutForTest() {
  42. if (!notify_timer_.IsRunning())
  43. return false;
  44. MaybeNotifyPowerManager();
  45. return true;
  46. }
  47. void VideoActivityNotifier::UpdateTimer() {
  48. if (!should_notify_power_manager()) {
  49. notify_timer_.Stop();
  50. } else {
  51. notify_timer_.Start(FROM_HERE, base::Seconds(kNotifyIntervalSec), this,
  52. &VideoActivityNotifier::MaybeNotifyPowerManager);
  53. }
  54. }
  55. void VideoActivityNotifier::MaybeNotifyPowerManager() {
  56. if (should_notify_power_manager()) {
  57. chromeos::PowerManagerClient::Get()->NotifyVideoActivity(
  58. video_state_ == VideoDetector::State::PLAYING_FULLSCREEN);
  59. }
  60. }
  61. } // namespace ash