| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "dbus/bus.h" | 12 #include "dbus/bus.h" |
| 13 #include "dbus/message.h" | 13 #include "dbus/message.h" |
| 14 #include "dbus/object_path.h" | 14 #include "dbus/object_path.h" |
| 15 #include "dbus/object_proxy.h" | 15 #include "dbus/object_proxy.h" |
| 16 #include "third_party/cros_system_api/dbus/service_constants.h" | 16 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 17 | 17 |
| 18 namespace chromeos { | 18 namespace chromeos { |
| 19 | 19 |
| 20 // The SystemClockClient implementation used in production. | 20 // The SystemClockClient implementation used in production. |
| 21 class SystemClockClientImpl : public SystemClockClient { | 21 class SystemClockClientImpl : public SystemClockClient { |
| 22 public: | 22 public: |
| 23 SystemClockClientImpl() | 23 SystemClockClientImpl() |
| 24 : can_set_time_(false), | 24 : can_set_time_(false), |
| 25 can_set_time_initialized_(false), | 25 can_set_time_initialized_(false), |
| 26 system_clock_proxy_(NULL), | 26 system_clock_proxy_(nullptr), |
| 27 weak_ptr_factory_(this) {} | 27 weak_ptr_factory_(this) {} |
| 28 | 28 |
| 29 ~SystemClockClientImpl() override {} | 29 ~SystemClockClientImpl() override {} |
| 30 | 30 |
| 31 void AddObserver(Observer* observer) override { | 31 void AddObserver(Observer* observer) override { |
| 32 observers_.AddObserver(observer); | 32 observers_.AddObserver(observer); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void RemoveObserver(Observer* observer) override { | 35 void RemoveObserver(Observer* observer) override { |
| 36 observers_.RemoveObserver(observer); | 36 observers_.RemoveObserver(observer); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 51 dbus::ObjectProxy::EmptyResponseCallback()); | 51 dbus::ObjectProxy::EmptyResponseCallback()); |
| 52 } | 52 } |
| 53 | 53 |
| 54 bool CanSetTime() override { return can_set_time_; } | 54 bool CanSetTime() override { return can_set_time_; } |
| 55 | 55 |
| 56 protected: | 56 protected: |
| 57 void Init(dbus::Bus* bus) override { | 57 void Init(dbus::Bus* bus) override { |
| 58 system_clock_proxy_ = bus->GetObjectProxy( | 58 system_clock_proxy_ = bus->GetObjectProxy( |
| 59 system_clock::kSystemClockServiceName, | 59 system_clock::kSystemClockServiceName, |
| 60 dbus::ObjectPath(system_clock::kSystemClockServicePath)); | 60 dbus::ObjectPath(system_clock::kSystemClockServicePath)); |
| 61 | |
| 62 // Check whether the system clock can be set. | |
| 63 GetCanSet(); | |
| 64 | |
| 65 // Monitor the D-Bus signal for TimeUpdated changes. | |
| 66 system_clock_proxy_->ConnectToSignal( | 61 system_clock_proxy_->ConnectToSignal( |
| 67 system_clock::kSystemClockInterface, | 62 system_clock::kSystemClockInterface, |
| 68 system_clock::kSystemClockUpdated, | 63 system_clock::kSystemClockUpdated, |
| 69 base::Bind(&SystemClockClientImpl::TimeUpdatedReceived, | 64 base::Bind(&SystemClockClientImpl::TimeUpdatedReceived, |
| 70 weak_ptr_factory_.GetWeakPtr()), | 65 weak_ptr_factory_.GetWeakPtr()), |
| 71 base::Bind(&SystemClockClientImpl::TimeUpdatedConnected, | 66 base::Bind(&SystemClockClientImpl::TimeUpdatedConnected, |
| 72 weak_ptr_factory_.GetWeakPtr())); | 67 weak_ptr_factory_.GetWeakPtr())); |
| 68 system_clock_proxy_->WaitForServiceToBeAvailable( |
| 69 base::Bind(&SystemClockClientImpl::ServiceInitiallyAvailable, |
| 70 weak_ptr_factory_.GetWeakPtr())); |
| 73 } | 71 } |
| 74 | 72 |
| 75 private: | 73 private: |
| 74 // Called once when the service initially becomes available (or immediately if |
| 75 // it's already available). |
| 76 void ServiceInitiallyAvailable(bool service_is_available) { |
| 77 if (service_is_available) |
| 78 GetCanSet(); |
| 79 else |
| 80 LOG(ERROR) << "Failed to wait for D-Bus service availability"; |
| 81 } |
| 82 |
| 76 // Called when a TimeUpdated signal is received. | 83 // Called when a TimeUpdated signal is received. |
| 77 void TimeUpdatedReceived(dbus::Signal* signal) { | 84 void TimeUpdatedReceived(dbus::Signal* signal) { |
| 78 VLOG(1) << "TimeUpdated signal received: " << signal->ToString(); | 85 VLOG(1) << "TimeUpdated signal received: " << signal->ToString(); |
| 79 dbus::MessageReader reader(signal); | 86 dbus::MessageReader reader(signal); |
| 80 FOR_EACH_OBSERVER(Observer, observers_, SystemClockUpdated()); | 87 FOR_EACH_OBSERVER(Observer, observers_, SystemClockUpdated()); |
| 81 | 88 |
| 82 // Check if the system clock can be changed now. | 89 // Check if the system clock can be changed now. |
| 83 GetCanSet(); | 90 GetCanSet(); |
| 84 } | 91 } |
| 85 | 92 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 115 FOR_EACH_OBSERVER( | 122 FOR_EACH_OBSERVER( |
| 116 Observer, observers_, SystemClockCanSetTimeChanged(can_set_time)); | 123 Observer, observers_, SystemClockCanSetTimeChanged(can_set_time)); |
| 117 } | 124 } |
| 118 | 125 |
| 119 // Check whether the time can be set. | 126 // Check whether the time can be set. |
| 120 void GetCanSet() { | 127 void GetCanSet() { |
| 121 dbus::MethodCall method_call(system_clock::kSystemClockInterface, | 128 dbus::MethodCall method_call(system_clock::kSystemClockInterface, |
| 122 system_clock::kSystemClockCanSet); | 129 system_clock::kSystemClockCanSet); |
| 123 dbus::MessageWriter writer(&method_call); | 130 dbus::MessageWriter writer(&method_call); |
| 124 system_clock_proxy_->CallMethod( | 131 system_clock_proxy_->CallMethod( |
| 125 &method_call, | 132 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 126 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 127 base::Bind(&SystemClockClientImpl::OnGetCanSet, | 133 base::Bind(&SystemClockClientImpl::OnGetCanSet, |
| 128 weak_ptr_factory_.GetWeakPtr())); | 134 weak_ptr_factory_.GetWeakPtr())); |
| 129 } | 135 } |
| 130 | 136 |
| 131 // Whether the time can be set. Value is false until the first | 137 // Whether the time can be set. Value is false until the first |
| 132 // CanSetTime response is received. | 138 // CanSetTime response is received. |
| 133 bool can_set_time_; | 139 bool can_set_time_; |
| 134 bool can_set_time_initialized_; | 140 bool can_set_time_initialized_; |
| 135 dbus::ObjectProxy* system_clock_proxy_; | 141 dbus::ObjectProxy* system_clock_proxy_; |
| 136 base::ObserverList<Observer> observers_; | 142 base::ObserverList<Observer> observers_; |
| 137 | 143 |
| 138 base::WeakPtrFactory<SystemClockClientImpl> weak_ptr_factory_; | 144 base::WeakPtrFactory<SystemClockClientImpl> weak_ptr_factory_; |
| 139 | 145 |
| 140 DISALLOW_COPY_AND_ASSIGN(SystemClockClientImpl); | 146 DISALLOW_COPY_AND_ASSIGN(SystemClockClientImpl); |
| 141 }; | 147 }; |
| 142 | 148 |
| 143 void SystemClockClient::Observer::SystemClockUpdated() { | |
| 144 } | |
| 145 | |
| 146 void SystemClockClient::Observer::SystemClockCanSetTimeChanged( | |
| 147 bool can_set_time) { | |
| 148 } | |
| 149 | |
| 150 SystemClockClient::SystemClockClient() { | |
| 151 } | |
| 152 | |
| 153 SystemClockClient::~SystemClockClient() { | |
| 154 } | |
| 155 | |
| 156 // static | 149 // static |
| 157 SystemClockClient* SystemClockClient::Create() { | 150 SystemClockClient* SystemClockClient::Create() { |
| 158 return new SystemClockClientImpl(); | 151 return new SystemClockClientImpl(); |
| 159 } | 152 } |
| 160 | 153 |
| 154 SystemClockClient::SystemClockClient() {} |
| 155 |
| 161 } // namespace chromeos | 156 } // namespace chromeos |
| OLD | NEW |