Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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/metronome_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/weak_ptr.h" | |
| 10 #include "base/observer_list.h" | |
| 11 #include "base/time/time.h" | |
| 12 #include "base/timer/timer.h" | |
| 13 #include "dbus/bus.h" | |
| 14 #include "dbus/message.h" | |
| 15 #include "dbus/object_path.h" | |
| 16 #include "dbus/object_proxy.h" | |
| 17 | |
| 18 // TODO(benchan): Move these DBus constants to system_api. | |
| 19 namespace metronome { | |
| 20 | |
| 21 const char kMetronomeInterface[] = "org.chromium.Metronome"; | |
| 22 const char kMetronomeServiceName[] = "org.chromium.Metronome"; | |
| 23 const char kMetronomeServicePath[] = "/org/chromium/Metronome"; | |
| 24 const char kTimestampUpdatedSignal[] = "TimestampUpdated"; | |
| 25 | |
| 26 } // namespace metronome | |
| 27 | |
| 28 namespace chromeos { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 //////////////////////////////////////////////////////////////////////////////// | |
| 33 | |
| 34 // The MetronomeClient implementation. | |
| 35 class MetronomeClientImpl : public MetronomeClient { | |
| 36 public: | |
| 37 MetronomeClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {} | |
| 38 | |
| 39 virtual ~MetronomeClientImpl() {} | |
|
Ben Chan
2015/02/19 19:03:14
~MetronomeClientImpl() override {}
varkha
2015/02/19 20:33:42
Done.
| |
| 40 | |
| 41 // MetronomeClient: | |
| 42 void AddObserver(Observer* observer) override; | |
| 43 void RemoveObserver(Observer* observer) override; | |
| 44 | |
| 45 protected: | |
| 46 void Init(dbus::Bus* bus) override; | |
| 47 | |
| 48 private: | |
| 49 // Handles TimestampUpdated signal and notifies |observers_|. | |
| 50 void OnTimestampUpdated(dbus::Signal* signal); | |
| 51 | |
| 52 // Handles the result of signal connection setup. | |
| 53 void OnSignalConnected(const std::string& interface, | |
| 54 const std::string& signal, | |
| 55 bool succeeded); | |
| 56 | |
| 57 dbus::ObjectProxy* proxy_; | |
| 58 // List of observers interested in event notifications from us. | |
| 59 ObserverList<Observer> observers_; | |
| 60 | |
| 61 // Note: This should remain the last member so it'll be destroyed and | |
| 62 // invalidate its weak pointers before any other members are destroyed. | |
| 63 base::WeakPtrFactory<MetronomeClientImpl> weak_ptr_factory_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(MetronomeClientImpl); | |
| 66 }; | |
| 67 | |
| 68 void MetronomeClientImpl::AddObserver(Observer* observer) { | |
| 69 DCHECK(observer); | |
| 70 if (!observers_.might_have_observers()) { | |
| 71 proxy_->ConnectToSignal(metronome::kMetronomeInterface, | |
| 72 metronome::kTimestampUpdatedSignal, | |
| 73 base::Bind(&MetronomeClientImpl::OnTimestampUpdated, | |
| 74 weak_ptr_factory_.GetWeakPtr()), | |
| 75 base::Bind(&MetronomeClientImpl::OnSignalConnected, | |
| 76 weak_ptr_factory_.GetWeakPtr())); | |
| 77 } | |
| 78 observers_.AddObserver(observer); | |
| 79 } | |
| 80 | |
| 81 void MetronomeClientImpl::RemoveObserver(Observer* observer) { | |
| 82 DCHECK(observer); | |
| 83 observers_.RemoveObserver(observer); | |
| 84 } | |
| 85 | |
| 86 void MetronomeClientImpl::Init(dbus::Bus* bus) { | |
| 87 proxy_ = | |
| 88 bus->GetObjectProxy(metronome::kMetronomeServiceName, | |
| 89 dbus::ObjectPath(metronome::kMetronomeServicePath)); | |
| 90 } | |
| 91 | |
| 92 void MetronomeClientImpl::OnTimestampUpdated(dbus::Signal* signal) { | |
| 93 dbus::MessageReader reader(signal); | |
| 94 uint64 beacon_timestamp = 0; | |
| 95 uint64 mac_timestamp = 0; | |
| 96 if (!reader.PopUint64(&beacon_timestamp) || | |
| 97 !reader.PopUint64(&mac_timestamp)) { | |
| 98 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
| 99 return; | |
| 100 } | |
| 101 FOR_EACH_OBSERVER(Observer, observers_, | |
| 102 OnTimestampUpdated(beacon_timestamp, mac_timestamp)); | |
| 103 } | |
| 104 | |
| 105 void MetronomeClientImpl::OnSignalConnected(const std::string& interface, | |
| 106 const std::string& signal, | |
| 107 bool succeeded) { | |
| 108 LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " << signal | |
| 109 << " failed."; | |
| 110 } | |
| 111 | |
| 112 //////////////////////////////////////////////////////////////////////////////// | |
| 113 | |
| 114 // A stub implementation of MetronomeClient. It does not provide true | |
| 115 // synchronization and only exists to exercise the interfaces. | |
| 116 class MetronomeClientStubImpl : public MetronomeClient { | |
| 117 public: | |
| 118 MetronomeClientStubImpl() : timer_(true, true), weak_ptr_factory_(this) {} | |
| 119 | |
| 120 virtual ~MetronomeClientStubImpl() { timer_.Stop(); } | |
|
Ben Chan
2015/02/19 19:03:14
~MetronomeClientImpl() override { ... }
varkha
2015/02/19 20:33:42
Done.
| |
| 121 | |
| 122 // MetronomeClient overrides: | |
| 123 virtual void Init(dbus::Bus* bus) override { | |
|
Ben Chan
2015/02/19 19:03:14
drop virtual
varkha
2015/02/19 20:33:42
Done.
| |
| 124 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(1000), | |
| 125 base::Bind(&MetronomeClientStubImpl::OnTimer, | |
| 126 weak_ptr_factory_.GetWeakPtr())); | |
| 127 } | |
| 128 | |
| 129 void AddObserver(Observer* observer) { | |
|
Ben Chan
2015/02/19 19:03:14
override
varkha
2015/02/19 20:33:43
Done.
| |
| 130 DCHECK(observer); | |
| 131 observers_.AddObserver(observer); | |
| 132 } | |
| 133 | |
| 134 void RemoveObserver(Observer* observer) { | |
|
Ben Chan
2015/02/19 19:03:14
override
varkha
2015/02/19 20:33:42
Done.
| |
| 135 DCHECK(observer); | |
| 136 observers_.RemoveObserver(observer); | |
| 137 } | |
| 138 | |
| 139 void OnTimer() { | |
| 140 base::Time now_time = base::Time::Now(); | |
| 141 base::TimeTicks now_ticks = base::TimeTicks::Now(); | |
| 142 uint64 fake_beacon_timestamp = now_time.ToInternalValue(); | |
| 143 uint64 fake_mac_timestamp = now_ticks.ToInternalValue(); | |
| 144 FOR_EACH_OBSERVER( | |
| 145 Observer, observers_, | |
| 146 OnTimestampUpdated(fake_beacon_timestamp, fake_mac_timestamp)); | |
| 147 } | |
| 148 | |
| 149 private: | |
| 150 base::Timer timer_; | |
| 151 // List of observers interested in event notifications from us. | |
| 152 ObserverList<Observer> observers_; | |
| 153 | |
| 154 // Note: This should remain the last member so it'll be destroyed and | |
| 155 // invalidate its weak pointers before any other members are destroyed. | |
| 156 base::WeakPtrFactory<MetronomeClientStubImpl> weak_ptr_factory_; | |
| 157 | |
| 158 DISALLOW_COPY_AND_ASSIGN(MetronomeClientStubImpl); | |
| 159 }; | |
| 160 | |
| 161 } // namespace | |
| 162 | |
| 163 //////////////////////////////////////////////////////////////////////////////// | |
| 164 | |
| 165 MetronomeClient::MetronomeClient() { | |
| 166 } | |
| 167 | |
| 168 MetronomeClient::~MetronomeClient() { | |
| 169 } | |
| 170 | |
| 171 // static | |
| 172 MetronomeClient* MetronomeClient::Create(DBusClientImplementationType type) { | |
| 173 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
| 174 return new MetronomeClientImpl(); | |
| 175 | |
| 176 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
| 177 return new MetronomeClientStubImpl(); | |
| 178 } | |
| 179 | |
| 180 } // namespace chromeos | |
| OLD | NEW |