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. 4 5 #include "chromeos/dbus/system_clock_client.h" 6 7 #include "base/bind.h" 8 #include "chromeos/dbus/fake_system_clock_client.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" 14 15 namespace chromeos { 16 17 // The SystemClockClient implementation used in production. 18 class SystemClockClientImpl : public SystemClockClient { 19 public: 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)); 26 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())); 35 } 36 37 virtual ~SystemClockClientImpl() { 38 } 39 40 // SystemClockClient overrides: 41 virtual void AddObserver(Observer* observer) OVERRIDE { 42 observers_.AddObserver(observer); 43 } 44 45 virtual void RemoveObserver(Observer* observer) OVERRIDE { 46 observers_.RemoveObserver(observer); 47 } 48 49 virtual bool HasObserver(Observer* observer) OVERRIDE { 50 return observers_.HasObserver(observer); 51 } 52 53 private: 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()); 59 } 60 61 // Called when the TimeUpdated signal is initially connected. 62 void TimeUpdatedConnected(const std::string& interface_name, 63 const std::string& signal_name, 64 bool success) { 65 LOG_IF(ERROR, !success) 66 << "Failed to connect to TimeUpdated signal."; 67 } 68 69 dbus::ObjectProxy* system_clock_proxy_; 70 ObserverList<Observer> observers_; 71 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_; 75 76 DISALLOW_COPY_AND_ASSIGN(SystemClockClientImpl); 77 }; 78 79 SystemClockClient::SystemClockClient() { 80 } 81 82 SystemClockClient::~SystemClockClient() { 83 } 84 85 // static 86 SystemClockClient* SystemClockClient::Create( 87 DBusClientImplementationType type, 88 dbus::Bus* bus) { 89 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { 90 return new SystemClockClientImpl(bus); 91 } 92 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); 93 return new FakeSystemClockClient(); 94 } 95 96 } // namespace chromeos 97