• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dbus/bus.h"
9 #include "dbus/message.h"
10 #include "dbus/object_path.h"
11 #include "dbus/object_proxy.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
13 
14 namespace chromeos {
15 
16 // The SystemClockClient implementation used in production.
17 class SystemClockClientImpl : public SystemClockClient {
18  public:
SystemClockClientImpl()19   SystemClockClientImpl()
20       : system_clock_proxy_(NULL), weak_ptr_factory_(this) {}
21 
~SystemClockClientImpl()22   virtual ~SystemClockClientImpl() {
23   }
24 
AddObserver(Observer * observer)25   virtual void AddObserver(Observer* observer) OVERRIDE {
26     observers_.AddObserver(observer);
27   }
28 
RemoveObserver(Observer * observer)29   virtual void RemoveObserver(Observer* observer) OVERRIDE {
30     observers_.RemoveObserver(observer);
31   }
32 
HasObserver(Observer * observer)33   virtual bool HasObserver(Observer* observer) OVERRIDE {
34     return observers_.HasObserver(observer);
35   }
36 
37  protected:
Init(dbus::Bus * bus)38   virtual void Init(dbus::Bus* bus) OVERRIDE {
39     system_clock_proxy_ = bus->GetObjectProxy(
40         system_clock::kSystemClockServiceName,
41         dbus::ObjectPath(system_clock::kSystemClockServicePath));
42 
43     // Monitor the D-Bus signal for TimeUpdated changes.
44     system_clock_proxy_->ConnectToSignal(
45         system_clock::kSystemClockInterface,
46         system_clock::kSystemClockUpdated,
47         base::Bind(&SystemClockClientImpl::TimeUpdatedReceived,
48                    weak_ptr_factory_.GetWeakPtr()),
49         base::Bind(&SystemClockClientImpl::TimeUpdatedConnected,
50                    weak_ptr_factory_.GetWeakPtr()));
51   }
52 
53  private:
54   // Called when a TimeUpdated signal is received.
TimeUpdatedReceived(dbus::Signal * signal)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.
TimeUpdatedConnected(const std::string & interface_name,const std::string & signal_name,bool success)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 
SystemClockClient()79 SystemClockClient::SystemClockClient() {
80 }
81 
~SystemClockClient()82 SystemClockClient::~SystemClockClient() {
83 }
84 
85 // static
Create()86 SystemClockClient* SystemClockClient::Create() {
87   return new SystemClockClientImpl();
88 }
89 
90 }  // namespace chromeos
91