Chromium Code Reviews| Index: chromeos/dbus/metronome_client.cc |
| diff --git a/chromeos/dbus/metronome_client.cc b/chromeos/dbus/metronome_client.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e4e7bcf58d5526b5fe6fd0188116fb36c6b0f834 |
| --- /dev/null |
| +++ b/chromeos/dbus/metronome_client.cc |
| @@ -0,0 +1,145 @@ |
| +// 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.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chromeos/dbus/metronome_client.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/time/time.h" |
| +#include "base/timer/timer.h" |
| +#include "dbus/bus.h" |
| +#include "dbus/message.h" |
| +#include "dbus/object_path.h" |
| +#include "dbus/object_proxy.h" |
| + |
| +// TODO(benchan): Move these DBus constants to system_api. |
| +namespace metronome { |
| + |
| +const char kMetronomeInterface[] = "org.chromium.Metronome"; |
| +const char kMetronomeServiceName[] = "org.chromium.Metronome"; |
| +const char kMetronomeServicePath[] = "/org/chromium/Metronome"; |
| +const char kTimestampUpdatedSignal[] = "TimestampUpdated"; |
| + |
| +} // namespace metronome |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| + |
| +// The MetronomeClient implementation. |
| +class MetronomeClientImpl : public MetronomeClient { |
| + public: |
| + MetronomeClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {} |
| + |
| + virtual ~MetronomeClientImpl() {} |
| + |
| + // MetronomeClient override. |
| + virtual void SetTimestampUpdatedHandler( |
|
dtapuska
2015/02/18 13:59:12
Remove the virtual
varkha
2015/02/18 18:21:17
Done.
|
| + const TimestampUpdatedHandler& timestamp_updated_handler) override { |
| + proxy_->ConnectToSignal( |
| + metronome::kMetronomeInterface, metronome::kTimestampUpdatedSignal, |
| + base::Bind(&MetronomeClientImpl::OnTimestampUpdated, |
| + weak_ptr_factory_.GetWeakPtr(), timestamp_updated_handler), |
| + base::Bind(&MetronomeClientImpl::OnSignalConnected, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| + |
| + protected: |
| + 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.
|
| + proxy_ = |
| + bus->GetObjectProxy(metronome::kMetronomeServiceName, |
| + dbus::ObjectPath(metronome::kMetronomeServicePath)); |
| + } |
| + |
| + private: |
| + // Handles TimestampUpdated signal and calls |handler|. |
| + void OnTimestampUpdated(TimestampUpdatedHandler handler, |
| + dbus::Signal* signal) { |
| + dbus::MessageReader reader(signal); |
| + uint64 beacon_timestamp = 0; |
| + uint64 mac_timestamp = 0; |
| + if (!reader.PopUint64(&beacon_timestamp) || |
| + !reader.PopUint64(&mac_timestamp)) { |
| + LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
| + return; |
| + } |
| + handler.Run(beacon_timestamp, mac_timestamp); |
| + } |
| + |
| + // Handles the result of signal connection setup. |
| + void OnSignalConnected(const std::string& interface, |
| + const std::string& signal, |
| + bool succeeded) { |
| + LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " << signal |
| + << " failed."; |
| + } |
| + |
| + dbus::ObjectProxy* proxy_; |
| + |
| + // Note: This should remain the last member so it'll be destroyed and |
| + // invalidate its weak pointers before any other members are destroyed. |
| + base::WeakPtrFactory<MetronomeClientImpl> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MetronomeClientImpl); |
| +}; |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| + |
| +// A stub implementation of MetronomeClient. |
| +class MetronomeClientStubImpl : public MetronomeClient { |
| + public: |
| + MetronomeClientStubImpl() : weak_ptr_factory_(this), timer_(true, true) {} |
| + |
| + virtual ~MetronomeClientStubImpl() { timer_.Stop(); } |
| + |
| + // MetronomeClient overrides: |
| + virtual void Init(dbus::Bus* bus) override { |
| + timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(1000), |
| + base::Bind(&MetronomeClientStubImpl::OnTimer, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| + |
| + virtual void SetTimestampUpdatedHandler( |
| + const TimestampUpdatedHandler& timestamp_updated_handler) override { |
| + timestamp_updated_handler_ = timestamp_updated_handler; |
| + } |
| + |
| + void OnTimer() { |
| + 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
|
| + base::TimeTicks now_ticks = base::TimeTicks::Now(); |
| + uint64 fake_beacon_timestamp = now_time.ToInternalValue(); |
| + uint64 fake_mac_timestamp = now_ticks.ToInternalValue(); |
| + timestamp_updated_handler_.Run(fake_beacon_timestamp, fake_mac_timestamp); |
| + } |
| + |
| + private: |
| + TimestampUpdatedHandler timestamp_updated_handler_; |
| + 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.
|
| + base::Timer timer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MetronomeClientStubImpl); |
| +}; |
| + |
| +} // namespace |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| + |
| +MetronomeClient::MetronomeClient() { |
| +} |
| + |
| +MetronomeClient::~MetronomeClient() { |
| +} |
| + |
| +// static |
| +MetronomeClient* MetronomeClient::Create(DBusClientImplementationType type) { |
| + // if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| + // return new MetronomeClientImpl(); |
| + // |
| + // DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| + return new MetronomeClientStubImpl(); |
| +} |
| + |
| +} // namespace chromeos |