OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/system_clock_client.h" | 5 #include "chromeos/dbus/system_clock_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/observer_list.h" |
8 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
9 #include "dbus/message.h" | 11 #include "dbus/message.h" |
10 #include "dbus/object_path.h" | 12 #include "dbus/object_path.h" |
11 #include "dbus/object_proxy.h" | 13 #include "dbus/object_proxy.h" |
12 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
13 | 15 |
14 namespace chromeos { | 16 namespace chromeos { |
15 | 17 |
16 // The SystemClockClient implementation used in production. | 18 // The SystemClockClient implementation used in production. |
17 class SystemClockClientImpl : public SystemClockClient { | 19 class SystemClockClientImpl : public SystemClockClient { |
18 public: | 20 public: |
19 SystemClockClientImpl() | 21 SystemClockClientImpl() |
20 : system_clock_proxy_(NULL), weak_ptr_factory_(this) {} | 22 : can_set_time_(false), |
| 23 can_set_time_initialized_(false), |
| 24 system_clock_proxy_(NULL), |
| 25 weak_ptr_factory_(this) {} |
21 | 26 |
22 virtual ~SystemClockClientImpl() { | 27 virtual ~SystemClockClientImpl() { |
23 } | 28 } |
24 | 29 |
25 virtual void AddObserver(Observer* observer) OVERRIDE { | 30 virtual void AddObserver(Observer* observer) OVERRIDE { |
26 observers_.AddObserver(observer); | 31 observers_.AddObserver(observer); |
27 } | 32 } |
28 | 33 |
29 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 34 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
30 observers_.RemoveObserver(observer); | 35 observers_.RemoveObserver(observer); |
31 } | 36 } |
32 | 37 |
33 virtual bool HasObserver(Observer* observer) OVERRIDE { | 38 virtual bool HasObserver(Observer* observer) OVERRIDE { |
34 return observers_.HasObserver(observer); | 39 return observers_.HasObserver(observer); |
35 } | 40 } |
36 | 41 |
| 42 virtual void SetTime(int64 time_in_seconds) OVERRIDE { |
| 43 // Always try to set the time, because |can_set_time_| may be stale. |
| 44 dbus::MethodCall method_call( |
| 45 system_clock::kSystemClockInterface, |
| 46 system_clock::kSystemClockSet); |
| 47 dbus::MessageWriter writer(&method_call); |
| 48 writer.AppendInt64(time_in_seconds); |
| 49 system_clock_proxy_->CallMethod( |
| 50 &method_call, |
| 51 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 52 dbus::ObjectProxy::EmptyResponseCallback()); |
| 53 } |
| 54 |
| 55 virtual bool CanSetTime() OVERRIDE { |
| 56 return can_set_time_; |
| 57 } |
| 58 |
37 protected: | 59 protected: |
38 virtual void Init(dbus::Bus* bus) OVERRIDE { | 60 virtual void Init(dbus::Bus* bus) OVERRIDE { |
39 system_clock_proxy_ = bus->GetObjectProxy( | 61 system_clock_proxy_ = bus->GetObjectProxy( |
40 system_clock::kSystemClockServiceName, | 62 system_clock::kSystemClockServiceName, |
41 dbus::ObjectPath(system_clock::kSystemClockServicePath)); | 63 dbus::ObjectPath(system_clock::kSystemClockServicePath)); |
42 | 64 |
| 65 // Check whether the system clock can be set. |
| 66 GetCanSet(); |
| 67 |
43 // Monitor the D-Bus signal for TimeUpdated changes. | 68 // Monitor the D-Bus signal for TimeUpdated changes. |
44 system_clock_proxy_->ConnectToSignal( | 69 system_clock_proxy_->ConnectToSignal( |
45 system_clock::kSystemClockInterface, | 70 system_clock::kSystemClockInterface, |
46 system_clock::kSystemClockUpdated, | 71 system_clock::kSystemClockUpdated, |
47 base::Bind(&SystemClockClientImpl::TimeUpdatedReceived, | 72 base::Bind(&SystemClockClientImpl::TimeUpdatedReceived, |
48 weak_ptr_factory_.GetWeakPtr()), | 73 weak_ptr_factory_.GetWeakPtr()), |
49 base::Bind(&SystemClockClientImpl::TimeUpdatedConnected, | 74 base::Bind(&SystemClockClientImpl::TimeUpdatedConnected, |
50 weak_ptr_factory_.GetWeakPtr())); | 75 weak_ptr_factory_.GetWeakPtr())); |
51 } | 76 } |
52 | 77 |
53 private: | 78 private: |
54 // Called when a TimeUpdated signal is received. | 79 // Called when a TimeUpdated signal is received. |
55 void TimeUpdatedReceived(dbus::Signal* signal) { | 80 void TimeUpdatedReceived(dbus::Signal* signal) { |
56 VLOG(1) << "TimeUpdated signal received: " << signal->ToString(); | 81 VLOG(1) << "TimeUpdated signal received: " << signal->ToString(); |
57 dbus::MessageReader reader(signal); | 82 dbus::MessageReader reader(signal); |
58 FOR_EACH_OBSERVER(Observer, observers_, SystemClockUpdated()); | 83 FOR_EACH_OBSERVER(Observer, observers_, SystemClockUpdated()); |
| 84 |
| 85 // Check if the system clock can be changed now. |
| 86 GetCanSet(); |
59 } | 87 } |
60 | 88 |
61 // Called when the TimeUpdated signal is initially connected. | 89 // Called when the TimeUpdated signal is initially connected. |
62 void TimeUpdatedConnected(const std::string& interface_name, | 90 void TimeUpdatedConnected(const std::string& interface_name, |
63 const std::string& signal_name, | 91 const std::string& signal_name, |
64 bool success) { | 92 bool success) { |
65 LOG_IF(ERROR, !success) | 93 LOG_IF(ERROR, !success) |
66 << "Failed to connect to TimeUpdated signal."; | 94 << "Failed to connect to TimeUpdated signal."; |
67 } | 95 } |
68 | 96 |
| 97 // Callback for CanSetTime method. |
| 98 void OnGetCanSet(dbus::Response* response) { |
| 99 if (!response) { |
| 100 LOG(WARNING) << "CanSetTime request failed."; |
| 101 return; |
| 102 } |
| 103 |
| 104 dbus::MessageReader reader(response); |
| 105 bool can_set_time; |
| 106 if (!reader.PopBool(&can_set_time)) { |
| 107 LOG(ERROR) << "CanSetTime response invalid: " << response->ToString(); |
| 108 return; |
| 109 } |
| 110 |
| 111 // Nothing to do if the CanSetTime response hasn't changed. |
| 112 if (can_set_time_initialized_ && can_set_time_ == can_set_time) |
| 113 return; |
| 114 |
| 115 can_set_time_initialized_ = true; |
| 116 can_set_time_ = can_set_time; |
| 117 |
| 118 FOR_EACH_OBSERVER(Observer, |
| 119 observers_, |
| 120 SystemClockCanSetTimeChanged(can_set_time)); |
| 121 } |
| 122 |
| 123 // Check whether the time can be set. |
| 124 void GetCanSet() { |
| 125 dbus::MethodCall method_call( |
| 126 system_clock::kSystemClockInterface, |
| 127 system_clock::kSystemClockCanSet); |
| 128 dbus::MessageWriter writer(&method_call); |
| 129 system_clock_proxy_->CallMethod( |
| 130 &method_call, |
| 131 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 132 base::Bind(&SystemClockClientImpl::OnGetCanSet, |
| 133 weak_ptr_factory_.GetWeakPtr())); |
| 134 } |
| 135 |
| 136 // Whether the time can be set. Value is false until the first |
| 137 // CanSetTime response is received. |
| 138 bool can_set_time_; |
| 139 bool can_set_time_initialized_; |
69 dbus::ObjectProxy* system_clock_proxy_; | 140 dbus::ObjectProxy* system_clock_proxy_; |
70 ObserverList<Observer> observers_; | 141 ObserverList<Observer> observers_; |
71 | 142 |
72 // Note: This should remain the last member so it'll be destroyed and | |
73 // invalidate its weak pointers before any other members are destroyed. | |
74 base::WeakPtrFactory<SystemClockClientImpl> weak_ptr_factory_; | 143 base::WeakPtrFactory<SystemClockClientImpl> weak_ptr_factory_; |
75 | 144 |
76 DISALLOW_COPY_AND_ASSIGN(SystemClockClientImpl); | 145 DISALLOW_COPY_AND_ASSIGN(SystemClockClientImpl); |
77 }; | 146 }; |
78 | 147 |
| 148 void SystemClockClient::Observer::SystemClockUpdated() { |
| 149 } |
| 150 |
| 151 void SystemClockClient::Observer::SystemClockCanSetTimeChanged( |
| 152 bool can_set_time) { |
| 153 } |
| 154 |
79 SystemClockClient::SystemClockClient() { | 155 SystemClockClient::SystemClockClient() { |
80 } | 156 } |
81 | 157 |
82 SystemClockClient::~SystemClockClient() { | 158 SystemClockClient::~SystemClockClient() { |
83 } | 159 } |
84 | 160 |
85 // static | 161 // static |
86 SystemClockClient* SystemClockClient::Create() { | 162 SystemClockClient* SystemClockClient::Create() { |
87 return new SystemClockClientImpl(); | 163 return new SystemClockClientImpl(); |
88 } | 164 } |
89 | 165 |
90 } // namespace chromeos | 166 } // namespace chromeos |
OLD | NEW |