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

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

Issue 2791983004: DBus MediaAnalyticsClient and media_perception pb. (Closed)
Patch Set: Addressing comments. 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 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName,
50 media_perception::kStateFunction);
51 // Check that a new state is requested.
tbarzic 2017/05/11 16:55:55 no need for the comment here, the code itself is c
Luke Sorenson 2017/05/11 18:16:48 Done.
52 DCHECK(state.has_status()) << "Attempting to SetState without status set.";
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.";
103 return;
104 }
tbarzic 2017/05/11 16:55:55 nit: new line here
Luke Sorenson 2017/05/11 18:16:48 Done.
105 const uint8_t* bytes = nullptr;
106 size_t length = 0;
107
108 dbus::MessageReader reader(signal);
109
110 if (!reader.PopArrayOfBytes(&bytes, &length)) {
111 LOG(ERROR) << "Invalid detection signal: " << signal->ToString();
112 return;
113 }
114
115 mri::MediaPerception media_perception;
116 if (!media_perception.ParseFromArray(bytes, length)) {
117 LOG(ERROR) << "Failed to parse MediaPerception message.";
118 return;
119 }
120
121 media_perception_signal_handler_.Run(media_perception);
122 }
123
124 void OnState(const StateCallback& callback, dbus::Response* response) {
125 mri::State state;
126 if (!response) {
127 LOG(ERROR) << "Call to State failed to get response.";
128 callback.Run(false, state);
129 return;
130 }
131
132 const uint8_t* bytes = nullptr;
133 size_t length = 0;
134
135 dbus::MessageReader reader(response);
136 if (!reader.PopArrayOfBytes(&bytes, &length)) {
137 LOG(ERROR) << "Invalid D-Bus response: " << response->ToString();
138 callback.Run(false, state);
139 return;
140 }
141
142 if (!state.ParseFromArray(bytes, length)) {
143 LOG(ERROR) << "Failed to parse State message.";
144 callback.Run(false, state);
145 return;
146 }
147
148 callback.Run(true, state);
149 }
150
151 void OnGetDiagnostics(const DiagnosticsCallback& callback,
152 dbus::Response* response) {
153 mri::Diagnostics diagnostics;
154 if (!response) {
155 LOG(ERROR) << "Call to GetDiagnostics failed to get response.";
156 callback.Run(false, diagnostics);
157 return;
158 }
159
160 const uint8_t* bytes = nullptr;
161 size_t length = 0;
162
163 dbus::MessageReader reader(response);
164 if (!reader.PopArrayOfBytes(&bytes, &length)) {
165 LOG(ERROR) << "Invalid GetDiagnostics response: " << response->ToString();
166 callback.Run(false, diagnostics);
167 return;
168 }
169
170 if (!diagnostics.ParseFromArray(bytes, length)) {
171 LOG(ERROR) << "Failed to parse Diagnostics message.";
172 callback.Run(false, diagnostics);
173 return;
174 }
175
176 callback.Run(true, diagnostics);
177 }
178
179 dbus::ObjectProxy* dbus_proxy_;
180
181 // Stores a handler registered for receiving the media_perception.proto byte
182 // array.
183 MediaPerceptionSignalHandler media_perception_signal_handler_;
184
185 base::WeakPtrFactory<MediaAnalyticsClientImpl> weak_ptr_factory_;
186
187 DISALLOW_COPY_AND_ASSIGN(MediaAnalyticsClientImpl);
188 };
189
190 MediaAnalyticsClient::~MediaAnalyticsClient() {}
191
192 MediaAnalyticsClient* MediaAnalyticsClient::Create() {
193 return new MediaAnalyticsClientImpl;
194 }
195
196 MediaAnalyticsClient::MediaAnalyticsClient() {}
197
198 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698