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

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

Issue 2791983004: DBus MediaAnalyticsClient and media_perception pb. (Closed)
Patch Set: Addressing nits. Created 3 years, 7 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
OLDNEW
(Empty)
1 // Copyright 2017 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/media_analytics_client.h"
6
7 #include <cstdint>
8
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/memory/weak_ptr.h"
12 #include "dbus/bus.h"
13 #include "dbus/message.h"
14 #include "dbus/object_path.h"
15 #include "dbus/object_proxy.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
17
18 namespace chromeos {
19
20 // The MediaAnalyticsClient implementation used in production.
21 class MediaAnalyticsClientImpl : public MediaAnalyticsClient {
22 public:
23 MediaAnalyticsClientImpl() : dbus_proxy_(nullptr), weak_ptr_factory_(this) {}
24
25 ~MediaAnalyticsClientImpl() override {}
26
27 void SetMediaPerceptionSignalHandler(
28 const MediaPerceptionSignalHandler& handler) override {
29 DCHECK(media_perception_signal_handler_.is_null())
30 << "MediaPerceptionSignalHandler already set and setting it again.";
31 media_perception_signal_handler_ = handler;
32 }
33
34 void ClearMediaPerceptionSignalHandler() override {
35 media_perception_signal_handler_.Reset();
36 }
37
38 void GetState(const StateCallback& callback) override {
39 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName,
40 media_perception::kStateFunction);
41 dbus_proxy_->CallMethod(
42 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
43 base::Bind(&MediaAnalyticsClientImpl::OnState,
44 weak_ptr_factory_.GetWeakPtr(), callback));
45 }
46
47 void SetState(const mri::State& state,
48 const StateCallback& callback) override {
49 DCHECK(state.has_status()) << "Attempting to SetState without status set.";
50
51 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName,
52 media_perception::kStateFunction);
53 int length = state.ByteSize();
54 uint8_t bytes[length];
55 state.SerializeToArray(bytes, length);
56 dbus::MessageWriter writer(&method_call);
57 writer.AppendArrayOfBytes(bytes, length);
58
59 dbus_proxy_->CallMethod(
60 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
61 base::Bind(&MediaAnalyticsClientImpl::OnState,
62 weak_ptr_factory_.GetWeakPtr(), callback));
63 }
64
65 void GetDiagnostics(const DiagnosticsCallback& callback) override {
66 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName,
67 media_perception::kGetDiagnosticsFunction);
68 // TODO(lasoren): Verify that this timeout setting is sufficient.
69 dbus_proxy_->CallMethod(
70 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
71 base::Bind(&MediaAnalyticsClientImpl::OnGetDiagnostics,
72 weak_ptr_factory_.GetWeakPtr(), callback));
73 }
74
75 protected:
76 void Init(dbus::Bus* bus) override {
77 dbus_proxy_ = bus->GetObjectProxy(
78 media_perception::kMediaPerceptionServiceName,
79 dbus::ObjectPath(media_perception::kMediaPerceptionServicePath));
80 // Connect to the MediaPerception signal.
81 dbus_proxy_->ConnectToSignal(
82 media_perception::kMediaPerceptionInterface,
83 media_perception::kDetectionSignal,
84 base::Bind(&MediaAnalyticsClientImpl::OnDetectionSignalReceived,
85 weak_ptr_factory_.GetWeakPtr()),
86 base::Bind(&MediaAnalyticsClientImpl::OnSignalConnected,
87 weak_ptr_factory_.GetWeakPtr()));
88 }
89
90 private:
91 void OnSignalConnected(const std::string& interface,
92 const std::string& signal,
93 bool succeeded) {
94 LOG_IF(ERROR, !succeeded)
95 << "Connect to " << interface << " " << signal << " failed.";
96 }
97
98 // Handler that is triggered when a MediaPerception proto is received from
99 // the media analytics process.
100 void OnDetectionSignalReceived(dbus::Signal* signal) {
101 if (media_perception_signal_handler_.is_null()) {
102 LOG(WARNING) << "Received DetectionSignal but handler is null.";
tbarzic 2017/05/11 18:49:57 remove this warning log - the signal handler not b
Luke Sorenson 2017/05/11 22:01:56 Done.
103 return;
104 }
105
106 const uint8_t* bytes = nullptr;
107 size_t length = 0;
108
109 dbus::MessageReader reader(signal);
110
111 if (!reader.PopArrayOfBytes(&bytes, &length)) {
112 LOG(ERROR) << "Invalid detection signal: " << signal->ToString();
113 return;
114 }
115
116 mri::MediaPerception media_perception;
117 if (!media_perception.ParseFromArray(bytes, length)) {
118 LOG(ERROR) << "Failed to parse MediaPerception message.";
119 return;
120 }
121
122 media_perception_signal_handler_.Run(media_perception);
123 }
124
125 void OnState(const StateCallback& callback, dbus::Response* response) {
126 mri::State state;
127 if (!response) {
128 LOG(ERROR) << "Call to State failed to get response.";
129 callback.Run(false, state);
130 return;
131 }
132
133 const uint8_t* bytes = nullptr;
134 size_t length = 0;
135
136 dbus::MessageReader reader(response);
137 if (!reader.PopArrayOfBytes(&bytes, &length)) {
138 LOG(ERROR) << "Invalid D-Bus response: " << response->ToString();
139 callback.Run(false, state);
140 return;
141 }
142
143 if (!state.ParseFromArray(bytes, length)) {
144 LOG(ERROR) << "Failed to parse State message.";
145 callback.Run(false, state);
146 return;
147 }
148
149 callback.Run(true, state);
150 }
151
152 void OnGetDiagnostics(const DiagnosticsCallback& callback,
153 dbus::Response* response) {
154 mri::Diagnostics diagnostics;
155 if (!response) {
156 LOG(ERROR) << "Call to GetDiagnostics failed to get response.";
157 callback.Run(false, diagnostics);
158 return;
159 }
160
161 const uint8_t* bytes = nullptr;
162 size_t length = 0;
163
164 dbus::MessageReader reader(response);
165 if (!reader.PopArrayOfBytes(&bytes, &length)) {
166 LOG(ERROR) << "Invalid GetDiagnostics response: " << response->ToString();
167 callback.Run(false, diagnostics);
168 return;
169 }
170
171 if (!diagnostics.ParseFromArray(bytes, length)) {
172 LOG(ERROR) << "Failed to parse Diagnostics message.";
173 callback.Run(false, diagnostics);
174 return;
175 }
176
177 callback.Run(true, diagnostics);
178 }
179
180 dbus::ObjectProxy* dbus_proxy_;
181
182 // Stores a handler registered for receiving the media_perception.proto byte
183 // array.
184 MediaPerceptionSignalHandler media_perception_signal_handler_;
185
186 base::WeakPtrFactory<MediaAnalyticsClientImpl> weak_ptr_factory_;
187
188 DISALLOW_COPY_AND_ASSIGN(MediaAnalyticsClientImpl);
189 };
190
191 MediaAnalyticsClient::~MediaAnalyticsClient() {}
192
193 MediaAnalyticsClient* MediaAnalyticsClient::Create() {
194 return new MediaAnalyticsClientImpl;
195 }
196
197 MediaAnalyticsClient::MediaAnalyticsClient() {}
198
199 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698