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() | |
| 38 : proxy_(nullptr), signal_connected_(false), weak_ptr_factory_(this) {} | |
| 39 | |
| 40 ~MetronomeClientImpl() override {} | |
| 41 | |
| 42 // MetronomeClient: | |
| 43 void AddObserver(Observer* observer) override; | |
| 44 void RemoveObserver(Observer* observer) override; | |
| 45 | |
| 46 protected: | |
|
Ben Chan
2015/03/05 18:20:37
wrong indent
varkha
2015/03/05 19:26:01
Done.
| |
| 47 // DBusClient: | |
|
Ben Chan
2015/03/05 18:20:37
// DBusClient overrides
varkha
2015/03/05 19:26:01
This style seems to be a more dominant variant in
| |
| 48 void Init(dbus::Bus* bus) override; | |
| 49 | |
| 50 private: | |
| 51 // Handles TimestampUpdated signal and notifies |observers_|. | |
| 52 void OnTimestampUpdated(dbus::Signal* signal); | |
| 53 | |
| 54 // Handles the result of signal connection setup. | |
| 55 void OnSignalConnected(const std::string& interface, | |
| 56 const std::string& signal, | |
| 57 bool succeeded); | |
| 58 | |
| 59 dbus::ObjectProxy* proxy_; | |
| 60 | |
| 61 // True when |proxy_| has been connected. | |
| 62 bool signal_connected_; | |
| 63 | |
| 64 // List of observers interested in event notifications from us. | |
| 65 ObserverList<Observer> observers_; | |
| 66 | |
| 67 // Note: This should remain the last member so it'll be destroyed and | |
| 68 // invalidate its weak pointers before any other members are destroyed. | |
| 69 base::WeakPtrFactory<MetronomeClientImpl> weak_ptr_factory_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(MetronomeClientImpl); | |
| 72 }; | |
| 73 | |
| 74 void MetronomeClientImpl::AddObserver(Observer* observer) { | |
| 75 DCHECK(observer); | |
| 76 if (!signal_connected_) { | |
| 77 signal_connected_ = true; | |
| 78 proxy_->ConnectToSignal(metronome::kMetronomeInterface, | |
| 79 metronome::kTimestampUpdatedSignal, | |
| 80 base::Bind(&MetronomeClientImpl::OnTimestampUpdated, | |
| 81 weak_ptr_factory_.GetWeakPtr()), | |
| 82 base::Bind(&MetronomeClientImpl::OnSignalConnected, | |
| 83 weak_ptr_factory_.GetWeakPtr())); | |
| 84 } | |
| 85 observers_.AddObserver(observer); | |
| 86 } | |
| 87 | |
| 88 void MetronomeClientImpl::RemoveObserver(Observer* observer) { | |
| 89 DCHECK(observer); | |
| 90 observers_.RemoveObserver(observer); | |
| 91 } | |
| 92 | |
| 93 void MetronomeClientImpl::Init(dbus::Bus* bus) { | |
| 94 proxy_ = | |
| 95 bus->GetObjectProxy(metronome::kMetronomeServiceName, | |
| 96 dbus::ObjectPath(metronome::kMetronomeServicePath)); | |
| 97 } | |
| 98 | |
| 99 void MetronomeClientImpl::OnTimestampUpdated(dbus::Signal* signal) { | |
| 100 dbus::MessageReader reader(signal); | |
| 101 uint64 beacon_timestamp = 0; | |
| 102 uint64 local_timestamp = 0; | |
| 103 if (!reader.PopUint64(&beacon_timestamp) || | |
| 104 !reader.PopUint64(&local_timestamp)) { | |
| 105 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
| 106 return; | |
| 107 } | |
| 108 FOR_EACH_OBSERVER(Observer, observers_, | |
| 109 OnTimestampUpdated(beacon_timestamp, local_timestamp)); | |
| 110 } | |
| 111 | |
| 112 void MetronomeClientImpl::OnSignalConnected(const std::string& interface, | |
| 113 const std::string& signal, | |
| 114 bool succeeded) { | |
| 115 LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " << signal | |
| 116 << " failed."; | |
| 117 } | |
| 118 | |
| 119 //////////////////////////////////////////////////////////////////////////////// | |
| 120 | |
| 121 #ifdef USE_TIMER_BASED_METRONOME_DEMO_IMPLEMENTATION | |
|
Ben Chan
2015/03/05 18:20:37
can this be a parameter on the stub implementation
varkha
2015/03/05 19:26:01
Done.
| |
| 122 // A demo implementation of MetronomeClient. It does not provide true | |
| 123 // synchronization and only exists to exercise the interfaces. | |
| 124 class MetronomeClientDemoImpl : public MetronomeClient { | |
| 125 public: | |
| 126 MetronomeClientDemoImpl() : timer_(true, true), weak_ptr_factory_(this) {} | |
| 127 ~MetronomeClientDemoImpl() override { | |
| 128 timer_.Stop(); | |
| 129 } | |
| 130 | |
| 131 // MetronomeClient: | |
| 132 void AddObserver(Observer* observer) override { | |
| 133 DCHECK(observer); | |
| 134 observers_.AddObserver(observer); | |
| 135 } | |
| 136 | |
| 137 void RemoveObserver(Observer* observer) override { | |
| 138 DCHECK(observer); | |
| 139 observers_.RemoveObserver(observer); | |
| 140 } | |
| 141 | |
| 142 // DBusClient: | |
| 143 void Init(dbus::Bus* bus) override { | |
| 144 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(1000), | |
| 145 base::Bind(&MetronomeClientDemoImpl::OnTimer, | |
| 146 weak_ptr_factory_.GetWeakPtr())); | |
| 147 } | |
| 148 | |
| 149 private: | |
| 150 void OnTimer() { | |
| 151 base::Time now_time = base::Time::Now(); | |
| 152 base::TimeTicks now_ticks = base::TimeTicks::Now(); | |
| 153 uint64 fake_beacon_timestamp = now_time.ToInternalValue(); | |
| 154 uint64 fake_local_timestamp = now_ticks.ToInternalValue(); | |
| 155 FOR_EACH_OBSERVER( | |
| 156 Observer, observers_, | |
| 157 OnTimestampUpdated(fake_beacon_timestamp, fake_local_timestamp)); | |
| 158 } | |
| 159 | |
| 160 base::Timer timer_; | |
| 161 | |
| 162 // List of observers interested in event notifications from us. | |
| 163 ObserverList<Observer> observers_; | |
| 164 | |
| 165 // Note: This should remain the last member so it'll be destroyed and | |
| 166 // invalidate its weak pointers before any other members are destroyed. | |
| 167 base::WeakPtrFactory<MetronomeClientDemoImpl> weak_ptr_factory_; | |
| 168 | |
| 169 DISALLOW_COPY_AND_ASSIGN(MetronomeClientDemoImpl); | |
| 170 }; | |
| 171 #endif // USE_TIMER_BASED_METRONOME_DEMO_IMPLEMENTATION | |
| 172 | |
| 173 //////////////////////////////////////////////////////////////////////////////// | |
| 174 | |
| 175 // A stub implementation of MetronomeClient. It allows unit tests to complete. | |
| 176 class MetronomeClientStubImpl : public MetronomeClient { | |
| 177 public: | |
| 178 MetronomeClientStubImpl() : weak_ptr_factory_(this) {} | |
| 179 ~MetronomeClientStubImpl() override {} | |
| 180 | |
| 181 // MetronomeClient: | |
| 182 void AddObserver(Observer* observer) override {} | |
| 183 void RemoveObserver(Observer* observer) override {} | |
| 184 | |
| 185 // DBusClient: | |
| 186 void Init(dbus::Bus* bus) override {} | |
| 187 | |
| 188 private: | |
| 189 // Note: This should remain the last member so it'll be destroyed and | |
| 190 // invalidate its weak pointers before any other members are destroyed. | |
| 191 base::WeakPtrFactory<MetronomeClientStubImpl> weak_ptr_factory_; | |
| 192 | |
| 193 DISALLOW_COPY_AND_ASSIGN(MetronomeClientStubImpl); | |
| 194 }; | |
| 195 | |
| 196 } // namespace | |
| 197 | |
| 198 //////////////////////////////////////////////////////////////////////////////// | |
| 199 | |
| 200 MetronomeClient::MetronomeClient() { | |
| 201 } | |
| 202 | |
| 203 MetronomeClient::~MetronomeClient() { | |
| 204 } | |
| 205 | |
| 206 // static | |
| 207 MetronomeClient* MetronomeClient::Create(DBusClientImplementationType type) { | |
| 208 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { | |
| 209 #ifdef USE_TIMER_BASED_METRONOME_DEMO_IMPLEMENTATION | |
| 210 return new MetronomeClientDemoImpl(); | |
| 211 #else | |
| 212 return new MetronomeClientImpl(); | |
| 213 #endif | |
| 214 } | |
| 215 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
| 216 return new MetronomeClientStubImpl(); | |
| 217 } | |
| 218 | |
| 219 } // namespace chromeos | |
| OLD | NEW |