• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 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 
5 #include "chrome/browser/chromeos/dbus/display_power_service_provider.h"
6 
7 #include "ash/shell.h"
8 #include "base/bind.h"
9 #include "dbus/bus.h"
10 #include "dbus/message.h"
11 #include "third_party/cros_system_api/dbus/service_constants.h"
12 #include "ui/display/chromeos/display_configurator.h"
13 #include "ui/wm/core/user_activity_detector.h"
14 
15 namespace chromeos {
16 
DisplayPowerServiceProvider()17 DisplayPowerServiceProvider::DisplayPowerServiceProvider()
18     : weak_ptr_factory_(this) {
19 }
20 
~DisplayPowerServiceProvider()21 DisplayPowerServiceProvider::~DisplayPowerServiceProvider() {}
22 
Start(scoped_refptr<dbus::ExportedObject> exported_object)23 void DisplayPowerServiceProvider::Start(
24     scoped_refptr<dbus::ExportedObject> exported_object) {
25   exported_object->ExportMethod(
26       kLibCrosServiceInterface,
27       kSetDisplayPower,
28       base::Bind(&DisplayPowerServiceProvider::SetDisplayPower,
29                  weak_ptr_factory_.GetWeakPtr()),
30       base::Bind(&DisplayPowerServiceProvider::OnExported,
31                  weak_ptr_factory_.GetWeakPtr()));
32   exported_object->ExportMethod(
33       kLibCrosServiceInterface,
34       kSetDisplaySoftwareDimming,
35       base::Bind(&DisplayPowerServiceProvider::SetDisplaySoftwareDimming,
36                  weak_ptr_factory_.GetWeakPtr()),
37       base::Bind(&DisplayPowerServiceProvider::OnExported,
38                  weak_ptr_factory_.GetWeakPtr()));
39 }
40 
OnExported(const std::string & interface_name,const std::string & method_name,bool success)41 void DisplayPowerServiceProvider::OnExported(const std::string& interface_name,
42                                              const std::string& method_name,
43                                              bool success) {
44   if (!success) {
45     LOG(ERROR) << "Failed to export " << interface_name << "."
46                << method_name;
47   }
48 }
49 
SetDisplayPower(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)50 void DisplayPowerServiceProvider::SetDisplayPower(
51     dbus::MethodCall* method_call,
52     dbus::ExportedObject::ResponseSender response_sender) {
53   dbus::MessageReader reader(method_call);
54   int int_state = 0;
55   if (reader.PopInt32(&int_state)) {
56     // Turning displays off when the device becomes idle or on just before
57     // we suspend may trigger a mouse move, which would then be incorrectly
58     // reported as user activity.  Let the UserActivityDetector
59     // know so that it can ignore such events.
60     ash::Shell::GetInstance()->user_activity_detector()->
61         OnDisplayPowerChanging();
62 
63     DisplayPowerState state = static_cast<DisplayPowerState>(int_state);
64     ash::Shell::GetInstance()->display_configurator()->SetDisplayPower(
65         state, ui::DisplayConfigurator::kSetDisplayPowerNoFlags);
66   } else {
67     LOG(ERROR) << "Unable to parse " << kSetDisplayPower << " request";
68   }
69 
70   response_sender.Run(dbus::Response::FromMethodCall(method_call));
71 }
72 
SetDisplaySoftwareDimming(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)73 void DisplayPowerServiceProvider::SetDisplaySoftwareDimming(
74     dbus::MethodCall* method_call,
75     dbus::ExportedObject::ResponseSender response_sender) {
76   dbus::MessageReader reader(method_call);
77   bool dimmed = false;
78   if (reader.PopBool(&dimmed)) {
79     ash::Shell::GetInstance()->SetDimming(dimmed);
80   } else {
81     LOG(ERROR) << "Unable to parse " << kSetDisplaySoftwareDimming
82                << " request";
83   }
84   response_sender.Run(dbus::Response::FromMethodCall(method_call));
85 }
86 
87 }  // namespace chromeos
88