Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: chromeos/dbus/metronome_client.cc

Issue 935933002: Adds metronome time sync dbus client (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/dbus/metronome_client.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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), once_(false), weak_ptr_factory_(this) {}
38
39 ~MetronomeClientImpl() override {}
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
59 // True when |proxy_| has been connected.
60 bool once_;
stevenjb 2015/02/24 23:42:54 nit: Use a more descriptive variable name, e.g. si
varkha 2015/02/25 01:08:47 Done.
61
62 // List of observers interested in event notifications from us.
63 ObserverList<Observer> observers_;
64
65 // Note: This should remain the last member so it'll be destroyed and
66 // invalidate its weak pointers before any other members are destroyed.
67 base::WeakPtrFactory<MetronomeClientImpl> weak_ptr_factory_;
68
69 DISALLOW_COPY_AND_ASSIGN(MetronomeClientImpl);
70 };
71
72 void MetronomeClientImpl::AddObserver(Observer* observer) {
73 DCHECK(observer);
74 if (!once_) {
75 once_ = true;
76 proxy_->ConnectToSignal(metronome::kMetronomeInterface,
77 metronome::kTimestampUpdatedSignal,
78 base::Bind(&MetronomeClientImpl::OnTimestampUpdated,
79 weak_ptr_factory_.GetWeakPtr()),
80 base::Bind(&MetronomeClientImpl::OnSignalConnected,
81 weak_ptr_factory_.GetWeakPtr()));
82 }
83 observers_.AddObserver(observer);
84 }
85
86 void MetronomeClientImpl::RemoveObserver(Observer* observer) {
87 DCHECK(observer);
88 observers_.RemoveObserver(observer);
89 }
90
91 void MetronomeClientImpl::Init(dbus::Bus* bus) {
92 proxy_ =
93 bus->GetObjectProxy(metronome::kMetronomeServiceName,
94 dbus::ObjectPath(metronome::kMetronomeServicePath));
95 }
96
97 void MetronomeClientImpl::OnTimestampUpdated(dbus::Signal* signal) {
98 dbus::MessageReader reader(signal);
99 uint64 beacon_timestamp = 0;
100 uint64 local_timestamp = 0;
101 if (!reader.PopUint64(&beacon_timestamp) ||
102 !reader.PopUint64(&local_timestamp)) {
103 LOG(ERROR) << "Invalid signal: " << signal->ToString();
104 return;
105 }
106 FOR_EACH_OBSERVER(Observer, observers_,
107 OnTimestampUpdated(beacon_timestamp, local_timestamp));
108 }
109
110 void MetronomeClientImpl::OnSignalConnected(const std::string& interface,
111 const std::string& signal,
112 bool succeeded) {
113 LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " << signal
114 << " failed.";
115 }
116
117 ////////////////////////////////////////////////////////////////////////////////
118
119 // A stub implementation of MetronomeClient. It does not provide true
120 // synchronization and only exists to exercise the interfaces.
121 class MetronomeClientStubImpl : public MetronomeClient {
122 public:
123 MetronomeClientStubImpl() : timer_(true, true), weak_ptr_factory_(this) {}
124
125 ~MetronomeClientStubImpl() override { timer_.Stop(); }
126
127 // MetronomeClient overrides:
128 void Init(dbus::Bus* bus) override {
129 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(1000),
130 base::Bind(&MetronomeClientStubImpl::OnTimer,
131 weak_ptr_factory_.GetWeakPtr()));
132 }
133
134 void AddObserver(Observer* observer) override {
135 DCHECK(observer);
136 observers_.AddObserver(observer);
137 }
138
139 void RemoveObserver(Observer* observer) override {
140 DCHECK(observer);
141 observers_.RemoveObserver(observer);
142 }
143
144 void OnTimer() {
145 base::Time now_time = base::Time::Now();
146 base::TimeTicks now_ticks = base::TimeTicks::Now();
147 uint64 fake_beacon_timestamp = now_time.ToInternalValue();
148 uint64 fake_local_timestamp = now_ticks.ToInternalValue();
149 FOR_EACH_OBSERVER(
150 Observer, observers_,
151 OnTimestampUpdated(fake_beacon_timestamp, fake_local_timestamp));
152 }
153
154 private:
155 base::Timer timer_;
156 // List of observers interested in event notifications from us.
157 ObserverList<Observer> observers_;
158
159 // Note: This should remain the last member so it'll be destroyed and
160 // invalidate its weak pointers before any other members are destroyed.
161 base::WeakPtrFactory<MetronomeClientStubImpl> weak_ptr_factory_;
162
163 DISALLOW_COPY_AND_ASSIGN(MetronomeClientStubImpl);
164 };
165
166 } // namespace
167
168 ////////////////////////////////////////////////////////////////////////////////
169
170 MetronomeClient::MetronomeClient() {
171 }
172
173 MetronomeClient::~MetronomeClient() {
174 }
175
176 // static
177 MetronomeClient* MetronomeClient::Create(DBusClientImplementationType type) {
178 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
179 return new MetronomeClientImpl();
180
181 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
182 return new MetronomeClientStubImpl();
183 }
184
185 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/metronome_client.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698