1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_DBUS_CHROMEOS_POWER_MANAGER_PROXY_H_
18 #define SHILL_DBUS_CHROMEOS_POWER_MANAGER_PROXY_H_
19 
20 // An implementation of PowerManagerProxyInterface.  It connects to the dbus and
21 // listens for events from the power manager.  When they occur, the delegate's
22 // member functions are called.
23 
24 #include <stdint.h>
25 
26 #include <string>
27 #include <vector>
28 
29 #include <base/compiler_specific.h>
30 #include <power_manager/dbus-proxies.h>
31 
32 #include "shill/power_manager_proxy_interface.h"
33 
34 namespace shill {
35 
36 class EventDispatcher;
37 
38 class ChromeosPowerManagerProxy : public PowerManagerProxyInterface {
39  public:
40   // Constructs a PowerManager DBus object proxy with signals dispatched to
41   // |delegate|.
42   ChromeosPowerManagerProxy(
43       EventDispatcher* dispatcher,
44       const scoped_refptr<dbus::Bus>& bus,
45       PowerManagerProxyDelegate* delegate,
46       const base::Closure& service_appeared_callback,
47       const base::Closure& service_vanished_callback);
48   ~ChromeosPowerManagerProxy() override;
49 
50   // Inherited from PowerManagerProxyInterface.
51   bool RegisterSuspendDelay(base::TimeDelta timeout,
52                             const std::string& description,
53                             int* delay_id_out) override;
54   bool UnregisterSuspendDelay(int delay_id) override;
55   bool ReportSuspendReadiness(int delay_id, int suspend_id) override;
56   bool RegisterDarkSuspendDelay(base::TimeDelta timeout,
57                                 const std::string& description,
58                                 int* delay_id_out) override;
59   bool UnregisterDarkSuspendDelay(int delay_id) override;
60   bool ReportDarkSuspendReadiness(int delay_id, int suspend_id) override;
61   bool RecordDarkResumeWakeReason(const std::string& wake_reason) override;
62 
63  private:
64   // Signal handlers.
65   void SuspendImminent(const std::vector<uint8_t>& serialized_proto);
66   void SuspendDone(const std::vector<uint8_t>& serialized_proto);
67   void DarkSuspendImminent(
68       const std::vector<uint8_t>& serialized_proto);
69 
70   bool RegisterSuspendDelayInternal(bool is_dark,
71                                     base::TimeDelta timeout,
72                                     const std::string& description,
73                                     int* delay_id_out);
74   bool UnregisterSuspendDelayInternal(bool is_dark, int delay_id);
75   bool ReportSuspendReadinessInternal(bool is_dark,
76                                       int delay_id,
77                                       int suspend_id);
78 
79   // Called when service appeared or vanished.
80   void OnServiceAvailable(bool available);
81 
82   // Service name owner changed handler.
83   void OnServiceOwnerChanged(const std::string& old_owner,
84                              const std::string& new_owner);
85 
86   // Called when signal is connected to the ObjectProxy.
87   void OnSignalConnected(const std::string& interface_name,
88                          const std::string& signal_name,
89                          bool success);
90 
91   std::unique_ptr<org::chromium::PowerManagerProxy> proxy_;
92   EventDispatcher* dispatcher_;
93   PowerManagerProxyDelegate* delegate_;
94   base::Closure service_appeared_callback_;
95   base::Closure service_vanished_callback_;
96   bool service_available_;
97 
98   base::WeakPtrFactory<ChromeosPowerManagerProxy> weak_factory_{this};
99   DISALLOW_COPY_AND_ASSIGN(ChromeosPowerManagerProxy);
100 };
101 
102 }  // namespace shill
103 
104 #endif  // SHILL_DBUS_CHROMEOS_POWER_MANAGER_PROXY_H_
105