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