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"
8 #include "chromeos/dbus/fake_system_clock_client.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"
17 // The SystemClockClient implementation used in production.
18 class SystemClockClientImpl
: public SystemClockClient
{
20 explicit SystemClockClientImpl(dbus::Bus
* bus
)
21 : system_clock_proxy_(NULL
),
22 weak_ptr_factory_(this) {
23 system_clock_proxy_
= bus
->GetObjectProxy(
24 system_clock::kSystemClockServiceName
,
25 dbus::ObjectPath(system_clock::kSystemClockServicePath
));
27 // Monitor the D-Bus signal for TimeUpdated changes.
28 system_clock_proxy_
->ConnectToSignal(
29 system_clock::kSystemClockInterface
,
30 system_clock::kSystemClockUpdated
,
31 base::Bind(&SystemClockClientImpl::TimeUpdatedReceived
,
32 weak_ptr_factory_
.GetWeakPtr()),
33 base::Bind(&SystemClockClientImpl::TimeUpdatedConnected
,
34 weak_ptr_factory_
.GetWeakPtr()));
37 virtual ~SystemClockClientImpl() {
40 // SystemClockClient overrides:
41 virtual void AddObserver(Observer
* observer
) OVERRIDE
{
42 observers_
.AddObserver(observer
);
45 virtual void RemoveObserver(Observer
* observer
) OVERRIDE
{
46 observers_
.RemoveObserver(observer
);
49 virtual bool HasObserver(Observer
* observer
) OVERRIDE
{
50 return observers_
.HasObserver(observer
);
54 // Called when a TimeUpdated signal is received.
55 void TimeUpdatedReceived(dbus::Signal
* signal
) {
56 VLOG(1) << "TimeUpdated signal received: " << signal
->ToString();
57 dbus::MessageReader
reader(signal
);
58 FOR_EACH_OBSERVER(Observer
, observers_
, SystemClockUpdated());
61 // Called when the TimeUpdated signal is initially connected.
62 void TimeUpdatedConnected(const std::string
& interface_name
,
63 const std::string
& signal_name
,
65 LOG_IF(ERROR
, !success
)
66 << "Failed to connect to TimeUpdated signal.";
69 dbus::ObjectProxy
* system_clock_proxy_
;
70 ObserverList
<Observer
> observers_
;
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_
;
76 DISALLOW_COPY_AND_ASSIGN(SystemClockClientImpl
);
79 SystemClockClient::SystemClockClient() {
82 SystemClockClient::~SystemClockClient() {
86 SystemClockClient
* SystemClockClient::Create(
87 DBusClientImplementationType type
,
89 if (type
== REAL_DBUS_CLIENT_IMPLEMENTATION
) {
90 return new SystemClockClientImpl(bus
);
92 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION
, type
);
93 return new FakeSystemClockClient();
96 } // namespace chromeos