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(system_clock::kSystemClockInterface, |
| 45 system_clock::kSystemClockSet); |
| 46 dbus::MessageWriter writer(&method_call); |
| 47 writer.AppendInt64(time_in_seconds); |
| 48 system_clock_proxy_->CallMethod(&method_call, |
| 49 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 50 dbus::ObjectProxy::EmptyResponseCallback()); |
| 51 } |
| 52 |
| 53 virtual bool CanSetTime() OVERRIDE { return can_set_time_; } |
| 54 |
37 protected: | 55 protected: |
38 virtual void Init(dbus::Bus* bus) OVERRIDE { | 56 virtual void Init(dbus::Bus* bus) OVERRIDE { |
39 system_clock_proxy_ = bus->GetObjectProxy( | 57 system_clock_proxy_ = bus->GetObjectProxy( |
40 system_clock::kSystemClockServiceName, | 58 system_clock::kSystemClockServiceName, |
41 dbus::ObjectPath(system_clock::kSystemClockServicePath)); | 59 dbus::ObjectPath(system_clock::kSystemClockServicePath)); |
42 | 60 |
| 61 // Check whether the system clock can be set. |
| 62 GetCanSet(); |
| 63 |
43 // Monitor the D-Bus signal for TimeUpdated changes. | 64 // Monitor the D-Bus signal for TimeUpdated changes. |
44 system_clock_proxy_->ConnectToSignal( | 65 system_clock_proxy_->ConnectToSignal( |
45 system_clock::kSystemClockInterface, | 66 system_clock::kSystemClockInterface, |
46 system_clock::kSystemClockUpdated, | 67 system_clock::kSystemClockUpdated, |
47 base::Bind(&SystemClockClientImpl::TimeUpdatedReceived, | 68 base::Bind(&SystemClockClientImpl::TimeUpdatedReceived, |
48 weak_ptr_factory_.GetWeakPtr()), | 69 weak_ptr_factory_.GetWeakPtr()), |
49 base::Bind(&SystemClockClientImpl::TimeUpdatedConnected, | 70 base::Bind(&SystemClockClientImpl::TimeUpdatedConnected, |
50 weak_ptr_factory_.GetWeakPtr())); | 71 weak_ptr_factory_.GetWeakPtr())); |
51 } | 72 } |
52 | 73 |
53 private: | 74 private: |
54 // Called when a TimeUpdated signal is received. | 75 // Called when a TimeUpdated signal is received. |
55 void TimeUpdatedReceived(dbus::Signal* signal) { | 76 void TimeUpdatedReceived(dbus::Signal* signal) { |
56 VLOG(1) << "TimeUpdated signal received: " << signal->ToString(); | 77 VLOG(1) << "TimeUpdated signal received: " << signal->ToString(); |
57 dbus::MessageReader reader(signal); | 78 dbus::MessageReader reader(signal); |
58 FOR_EACH_OBSERVER(Observer, observers_, SystemClockUpdated()); | 79 FOR_EACH_OBSERVER(Observer, observers_, SystemClockUpdated()); |
| 80 |
| 81 // Check if the system clock can be changed now. |
| 82 GetCanSet(); |
59 } | 83 } |
60 | 84 |
61 // Called when the TimeUpdated signal is initially connected. | 85 // Called when the TimeUpdated signal is initially connected. |
62 void TimeUpdatedConnected(const std::string& interface_name, | 86 void TimeUpdatedConnected(const std::string& interface_name, |
63 const std::string& signal_name, | 87 const std::string& signal_name, |
64 bool success) { | 88 bool success) { |
65 LOG_IF(ERROR, !success) | 89 LOG_IF(ERROR, !success) |
66 << "Failed to connect to TimeUpdated signal."; | 90 << "Failed to connect to TimeUpdated signal."; |
67 } | 91 } |
68 | 92 |
| 93 // Callback for CanSetTime method. |
| 94 void OnGetCanSet(dbus::Response* response) { |
| 95 if (!response) { |
| 96 LOG(WARNING) << "CanSetTime request failed."; |
| 97 return; |
| 98 } |
| 99 |
| 100 dbus::MessageReader reader(response); |
| 101 bool can_set_time; |
| 102 if (!reader.PopBool(&can_set_time)) { |
| 103 LOG(ERROR) << "CanSetTime response invalid: " << response->ToString(); |
| 104 return; |
| 105 } |
| 106 |
| 107 // Nothing to do if the CanSetTime response hasn't changed. |
| 108 if (can_set_time_initialized_ && can_set_time_ == can_set_time) |
| 109 return; |
| 110 |
| 111 can_set_time_initialized_ = true; |
| 112 can_set_time_ = can_set_time; |
| 113 |
| 114 FOR_EACH_OBSERVER( |
| 115 Observer, observers_, SystemClockCanSetTimeChanged(can_set_time)); |
| 116 } |
| 117 |
| 118 // Check whether the time can be set. |
| 119 void GetCanSet() { |
| 120 dbus::MethodCall method_call(system_clock::kSystemClockInterface, |
| 121 system_clock::kSystemClockCanSet); |
| 122 dbus::MessageWriter writer(&method_call); |
| 123 system_clock_proxy_->CallMethod( |
| 124 &method_call, |
| 125 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 126 base::Bind(&SystemClockClientImpl::OnGetCanSet, |
| 127 weak_ptr_factory_.GetWeakPtr())); |
| 128 } |
| 129 |
| 130 // Whether the time can be set. Value is false until the first |
| 131 // CanSetTime response is received. |
| 132 bool can_set_time_; |
| 133 bool can_set_time_initialized_; |
69 dbus::ObjectProxy* system_clock_proxy_; | 134 dbus::ObjectProxy* system_clock_proxy_; |
70 ObserverList<Observer> observers_; | 135 ObserverList<Observer> observers_; |
71 | 136 |
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_; | 137 base::WeakPtrFactory<SystemClockClientImpl> weak_ptr_factory_; |
75 | 138 |
76 DISALLOW_COPY_AND_ASSIGN(SystemClockClientImpl); | 139 DISALLOW_COPY_AND_ASSIGN(SystemClockClientImpl); |
77 }; | 140 }; |
78 | 141 |
| 142 void SystemClockClient::Observer::SystemClockUpdated() { |
| 143 } |
| 144 |
| 145 void SystemClockClient::Observer::SystemClockCanSetTimeChanged( |
| 146 bool can_set_time) { |
| 147 } |
| 148 |
79 SystemClockClient::SystemClockClient() { | 149 SystemClockClient::SystemClockClient() { |
80 } | 150 } |
81 | 151 |
82 SystemClockClient::~SystemClockClient() { | 152 SystemClockClient::~SystemClockClient() { |
83 } | 153 } |
84 | 154 |
85 // static | 155 // static |
86 SystemClockClient* SystemClockClient::Create() { | 156 SystemClockClient* SystemClockClient::Create() { |
87 return new SystemClockClientImpl(); | 157 return new SystemClockClientImpl(); |
88 } | 158 } |
89 | 159 |
90 } // namespace chromeos | 160 } // namespace chromeos |
OLD | NEW |