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
5#include "ash/display/projecting_observer_chromeos.h"
6
7#include "chromeos/dbus/dbus_thread_manager.h"
8#include "chromeos/dbus/power_manager_client.h"
9#include "ui/display/types/display_snapshot.h"
10
11namespace ash {
12
13ProjectingObserver::ProjectingObserver()
14    : has_internal_output_(false),
15      output_count_(0),
16      casting_session_count_(0) {}
17
18ProjectingObserver::~ProjectingObserver() {}
19
20void ProjectingObserver::OnDisplayModeChanged(
21    const ui::DisplayConfigurator::DisplayStateList& display_states) {
22  has_internal_output_ = false;
23  output_count_ = display_states.size();
24
25  for (size_t i = 0; i < display_states.size(); ++i) {
26    if (display_states[i].display->type() ==
27        ui::DISPLAY_CONNECTION_TYPE_INTERNAL) {
28      has_internal_output_ = true;
29      break;
30    }
31  }
32
33  SetIsProjecting();
34}
35
36void ProjectingObserver::OnCastingSessionStartedOrStopped(bool started) {
37  if (started) {
38    ++casting_session_count_;
39  } else {
40    DCHECK_GT(casting_session_count_, 0);
41    --casting_session_count_;
42    if (casting_session_count_ < 0)
43      casting_session_count_ = 0;
44  }
45
46  SetIsProjecting();
47}
48
49void 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  bool projecting = has_internal_output_ &&
53      (output_count_ + casting_session_count_ > 1);
54
55  chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->SetIsProjecting(
56      projecting);
57}
58
59}  // namespace ash
60