projecting_observer.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2014 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/display/projecting_observer.h"
  5. #include "ash/shell.h"
  6. #include "ash/system/power/power_event_observer.h"
  7. #include "base/check_op.h"
  8. #include "chromeos/dbus/power/power_manager_client.h"
  9. #include "ui/display/types/display_snapshot.h"
  10. namespace ash {
  11. ProjectingObserver::ProjectingObserver(
  12. display::DisplayConfigurator* display_configurator)
  13. : display_configurator_(display_configurator) {
  14. if (Shell::HasInstance())
  15. Shell::Get()->AddShellObserver(this);
  16. if (display_configurator_)
  17. display_configurator_->AddObserver(this);
  18. }
  19. ProjectingObserver::~ProjectingObserver() {
  20. if (Shell::HasInstance())
  21. Shell::Get()->RemoveShellObserver(this);
  22. if (display_configurator_)
  23. display_configurator_->RemoveObserver(this);
  24. }
  25. void ProjectingObserver::OnDisplayModeChanged(
  26. const display::DisplayConfigurator::DisplayStateList& display_states) {
  27. has_internal_output_ = false;
  28. output_count_ = display_states.size();
  29. for (size_t i = 0; i < display_states.size(); ++i) {
  30. if (display_states[i]->type() ==
  31. display::DISPLAY_CONNECTION_TYPE_INTERNAL) {
  32. has_internal_output_ = true;
  33. break;
  34. }
  35. }
  36. SetIsProjecting();
  37. }
  38. void ProjectingObserver::OnCastingSessionStartedOrStopped(bool started) {
  39. if (started) {
  40. ++casting_session_count_;
  41. } else {
  42. DCHECK_GT(casting_session_count_, 0);
  43. --casting_session_count_;
  44. if (casting_session_count_ < 0)
  45. casting_session_count_ = 0;
  46. }
  47. SetIsProjecting();
  48. }
  49. void ProjectingObserver::SetIsProjecting() {
  50. // "Projecting" is defined as having more than 1 output connected while at
  51. // least one of them is an internal output.
  52. is_projecting_ =
  53. has_internal_output_ && (output_count_ + casting_session_count_ > 1);
  54. chromeos::PowerManagerClient::Get()->SetIsProjecting(is_projecting_);
  55. if (Shell::HasInstance())
  56. Shell::Get()->power_event_observer()->SetIsProjecting(is_projecting_);
  57. }
  58. } // namespace ash