Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
dtapuska
2015/02/18 13:59:12
Copyrights should be 2015.
varkha
2015/02/18 18:21:17
Done.
| |
| 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/time/time.h" | |
| 10 #include "base/timer/timer.h" | |
| 11 #include "dbus/bus.h" | |
| 12 #include "dbus/message.h" | |
| 13 #include "dbus/object_path.h" | |
| 14 #include "dbus/object_proxy.h" | |
| 15 | |
| 16 // TODO(benchan): Move these DBus constants to system_api. | |
| 17 namespace metronome { | |
| 18 | |
| 19 const char kMetronomeInterface[] = "org.chromium.Metronome"; | |
| 20 const char kMetronomeServiceName[] = "org.chromium.Metronome"; | |
| 21 const char kMetronomeServicePath[] = "/org/chromium/Metronome"; | |
| 22 const char kTimestampUpdatedSignal[] = "TimestampUpdated"; | |
| 23 | |
| 24 } // namespace metronome | |
| 25 | |
| 26 namespace chromeos { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 //////////////////////////////////////////////////////////////////////////////// | |
| 31 | |
| 32 // The MetronomeClient implementation. | |
| 33 class MetronomeClientImpl : public MetronomeClient { | |
| 34 public: | |
| 35 MetronomeClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {} | |
| 36 | |
| 37 virtual ~MetronomeClientImpl() {} | |
| 38 | |
| 39 // MetronomeClient override. | |
| 40 virtual void SetTimestampUpdatedHandler( | |
|
dtapuska
2015/02/18 13:59:12
Remove the virtual
varkha
2015/02/18 18:21:17
Done.
| |
| 41 const TimestampUpdatedHandler& timestamp_updated_handler) override { | |
| 42 proxy_->ConnectToSignal( | |
| 43 metronome::kMetronomeInterface, metronome::kTimestampUpdatedSignal, | |
| 44 base::Bind(&MetronomeClientImpl::OnTimestampUpdated, | |
| 45 weak_ptr_factory_.GetWeakPtr(), timestamp_updated_handler), | |
| 46 base::Bind(&MetronomeClientImpl::OnSignalConnected, | |
| 47 weak_ptr_factory_.GetWeakPtr())); | |
| 48 } | |
| 49 | |
| 50 protected: | |
| 51 virtual void Init(dbus::Bus* bus) override { | |
|
dtapuska
2015/02/18 13:59:12
Remove the virtual
varkha
2015/02/18 18:21:17
Done.
| |
| 52 proxy_ = | |
| 53 bus->GetObjectProxy(metronome::kMetronomeServiceName, | |
| 54 dbus::ObjectPath(metronome::kMetronomeServicePath)); | |
| 55 } | |
| 56 | |
| 57 private: | |
| 58 // Handles TimestampUpdated signal and calls |handler|. | |
| 59 void OnTimestampUpdated(TimestampUpdatedHandler handler, | |
| 60 dbus::Signal* signal) { | |
| 61 dbus::MessageReader reader(signal); | |
| 62 uint64 beacon_timestamp = 0; | |
| 63 uint64 mac_timestamp = 0; | |
| 64 if (!reader.PopUint64(&beacon_timestamp) || | |
| 65 !reader.PopUint64(&mac_timestamp)) { | |
| 66 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
| 67 return; | |
| 68 } | |
| 69 handler.Run(beacon_timestamp, mac_timestamp); | |
| 70 } | |
| 71 | |
| 72 // Handles the result of signal connection setup. | |
| 73 void OnSignalConnected(const std::string& interface, | |
| 74 const std::string& signal, | |
| 75 bool succeeded) { | |
| 76 LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " << signal | |
| 77 << " failed."; | |
| 78 } | |
| 79 | |
| 80 dbus::ObjectProxy* proxy_; | |
| 81 | |
| 82 // Note: This should remain the last member so it'll be destroyed and | |
| 83 // invalidate its weak pointers before any other members are destroyed. | |
| 84 base::WeakPtrFactory<MetronomeClientImpl> weak_ptr_factory_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(MetronomeClientImpl); | |
| 87 }; | |
| 88 | |
| 89 //////////////////////////////////////////////////////////////////////////////// | |
| 90 | |
| 91 // A stub implementation of MetronomeClient. | |
| 92 class MetronomeClientStubImpl : public MetronomeClient { | |
| 93 public: | |
| 94 MetronomeClientStubImpl() : weak_ptr_factory_(this), timer_(true, true) {} | |
| 95 | |
| 96 virtual ~MetronomeClientStubImpl() { timer_.Stop(); } | |
| 97 | |
| 98 // MetronomeClient overrides: | |
| 99 virtual void Init(dbus::Bus* bus) override { | |
| 100 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(1000), | |
| 101 base::Bind(&MetronomeClientStubImpl::OnTimer, | |
| 102 weak_ptr_factory_.GetWeakPtr())); | |
| 103 } | |
| 104 | |
| 105 virtual void SetTimestampUpdatedHandler( | |
| 106 const TimestampUpdatedHandler& timestamp_updated_handler) override { | |
| 107 timestamp_updated_handler_ = timestamp_updated_handler; | |
| 108 } | |
| 109 | |
| 110 void OnTimer() { | |
| 111 base::Time now_time = base::Time::Now(); | |
|
dtapuska
2015/02/18 13:59:12
I don't get why we are using the wall clock anywhe
varkha
2015/02/18 18:21:17
Right. For now this is just a stub implementation
| |
| 112 base::TimeTicks now_ticks = base::TimeTicks::Now(); | |
| 113 uint64 fake_beacon_timestamp = now_time.ToInternalValue(); | |
| 114 uint64 fake_mac_timestamp = now_ticks.ToInternalValue(); | |
| 115 timestamp_updated_handler_.Run(fake_beacon_timestamp, fake_mac_timestamp); | |
| 116 } | |
| 117 | |
| 118 private: | |
| 119 TimestampUpdatedHandler timestamp_updated_handler_; | |
| 120 base::WeakPtrFactory<MetronomeClientStubImpl> weak_ptr_factory_; | |
|
dtapuska
2015/02/18 13:59:12
I thought Weak Ptr Factories should be listed towa
varkha
2015/02/18 18:21:17
Done.
| |
| 121 base::Timer timer_; | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(MetronomeClientStubImpl); | |
| 124 }; | |
| 125 | |
| 126 } // namespace | |
| 127 | |
| 128 //////////////////////////////////////////////////////////////////////////////// | |
| 129 | |
| 130 MetronomeClient::MetronomeClient() { | |
| 131 } | |
| 132 | |
| 133 MetronomeClient::~MetronomeClient() { | |
| 134 } | |
| 135 | |
| 136 // static | |
| 137 MetronomeClient* MetronomeClient::Create(DBusClientImplementationType type) { | |
| 138 // if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
| 139 // return new MetronomeClientImpl(); | |
| 140 // | |
| 141 // DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
| 142 return new MetronomeClientStubImpl(); | |
| 143 } | |
| 144 | |
| 145 } // namespace chromeos | |
| OLD | NEW |