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