[Extensions UI] Fix another chrome://extensions commands bug
[chromium-blink-merge.git] / chromeos / dbus / system_clock_client.cc
blob160c9c11b0e7ccc33a49569b1e95b7cefa98a7fd
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
3 // found in the LICENSE file.
5 #include "chromeos/dbus/system_clock_client.h"
7 #include "base/bind.h"
8 #include "base/observer_list.h"
9 #include "dbus/bus.h"
10 #include "dbus/message.h"
11 #include "dbus/object_path.h"
12 #include "dbus/object_proxy.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
15 namespace chromeos {
17 // The SystemClockClient implementation used in production.
18 class SystemClockClientImpl : public SystemClockClient {
19 public:
20 SystemClockClientImpl()
21 : can_set_time_(false),
22 can_set_time_initialized_(false),
23 system_clock_proxy_(NULL),
24 weak_ptr_factory_(this) {}
26 ~SystemClockClientImpl() override {}
28 void AddObserver(Observer* observer) override {
29 observers_.AddObserver(observer);
32 void RemoveObserver(Observer* observer) override {
33 observers_.RemoveObserver(observer);
36 bool HasObserver(const Observer* observer) const override {
37 return observers_.HasObserver(observer);
40 void SetTime(int64 time_in_seconds) override {
41 // Always try to set the time, because |can_set_time_| may be stale.
42 dbus::MethodCall method_call(system_clock::kSystemClockInterface,
43 system_clock::kSystemClockSet);
44 dbus::MessageWriter writer(&method_call);
45 writer.AppendInt64(time_in_seconds);
46 system_clock_proxy_->CallMethod(&method_call,
47 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
48 dbus::ObjectProxy::EmptyResponseCallback());
51 bool CanSetTime() override { return can_set_time_; }
53 protected:
54 void Init(dbus::Bus* bus) override {
55 system_clock_proxy_ = bus->GetObjectProxy(
56 system_clock::kSystemClockServiceName,
57 dbus::ObjectPath(system_clock::kSystemClockServicePath));
59 // Check whether the system clock can be set.
60 GetCanSet();
62 // Monitor the D-Bus signal for TimeUpdated changes.
63 system_clock_proxy_->ConnectToSignal(
64 system_clock::kSystemClockInterface,
65 system_clock::kSystemClockUpdated,
66 base::Bind(&SystemClockClientImpl::TimeUpdatedReceived,
67 weak_ptr_factory_.GetWeakPtr()),
68 base::Bind(&SystemClockClientImpl::TimeUpdatedConnected,
69 weak_ptr_factory_.GetWeakPtr()));
72 private:
73 // Called when a TimeUpdated signal is received.
74 void TimeUpdatedReceived(dbus::Signal* signal) {
75 VLOG(1) << "TimeUpdated signal received: " << signal->ToString();
76 dbus::MessageReader reader(signal);
77 FOR_EACH_OBSERVER(Observer, observers_, SystemClockUpdated());
79 // Check if the system clock can be changed now.
80 GetCanSet();
83 // Called when the TimeUpdated signal is initially connected.
84 void TimeUpdatedConnected(const std::string& interface_name,
85 const std::string& signal_name,
86 bool success) {
87 LOG_IF(ERROR, !success)
88 << "Failed to connect to TimeUpdated signal.";
91 // Callback for CanSetTime method.
92 void OnGetCanSet(dbus::Response* response) {
93 if (!response) {
94 VLOG(1) << "CanSetTime request failed.";
95 return;
98 dbus::MessageReader reader(response);
99 bool can_set_time;
100 if (!reader.PopBool(&can_set_time)) {
101 LOG(ERROR) << "CanSetTime response invalid: " << response->ToString();
102 return;
105 // Nothing to do if the CanSetTime response hasn't changed.
106 if (can_set_time_initialized_ && can_set_time_ == can_set_time)
107 return;
109 can_set_time_initialized_ = true;
110 can_set_time_ = can_set_time;
112 FOR_EACH_OBSERVER(
113 Observer, observers_, SystemClockCanSetTimeChanged(can_set_time));
116 // Check whether the time can be set.
117 void GetCanSet() {
118 dbus::MethodCall method_call(system_clock::kSystemClockInterface,
119 system_clock::kSystemClockCanSet);
120 dbus::MessageWriter writer(&method_call);
121 system_clock_proxy_->CallMethod(
122 &method_call,
123 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
124 base::Bind(&SystemClockClientImpl::OnGetCanSet,
125 weak_ptr_factory_.GetWeakPtr()));
128 // Whether the time can be set. Value is false until the first
129 // CanSetTime response is received.
130 bool can_set_time_;
131 bool can_set_time_initialized_;
132 dbus::ObjectProxy* system_clock_proxy_;
133 base::ObserverList<Observer> observers_;
135 base::WeakPtrFactory<SystemClockClientImpl> weak_ptr_factory_;
137 DISALLOW_COPY_AND_ASSIGN(SystemClockClientImpl);
140 void SystemClockClient::Observer::SystemClockUpdated() {
143 void SystemClockClient::Observer::SystemClockCanSetTimeChanged(
144 bool can_set_time) {
147 SystemClockClient::SystemClockClient() {
150 SystemClockClient::~SystemClockClient() {
153 // static
154 SystemClockClient* SystemClockClient::Create() {
155 return new SystemClockClientImpl();
158 } // namespace chromeos