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

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

Issue 2791983004: DBus MediaAnalyticsClient and media_perception pb. (Closed)
Patch Set: Fixing compile error 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 MediaAnalyticsCleint 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 StartMediaAnalytics(const UpstartCallback& callback) override {
28 dbus::MethodCall method_call(media_perception::kUpstartJobInterface,
29 media_perception::kUpstartStartMethod);
30 dbus::MessageWriter writer(&method_call);
31 writer.AppendArrayOfStrings(std::vector<std::string>());
32 writer.AppendBool(true); // Wait for response.
33 upstart_proxy_->CallMethod(
34 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
35 base::Bind(&MediaAnalyticsClientImpl::HandleUpstartResponse,
36 weak_ptr_factory_.GetWeakPtr(), callback));
37 }
38
39 void RestartMediaAnalytics(const UpstartCallback& callback) override {
40 dbus::MethodCall method_call(media_perception::kUpstartJobInterface,
41 media_perception::kUpstartRestartMethod);
42 dbus::MessageWriter writer(&method_call);
43 writer.AppendArrayOfStrings(std::vector<std::string>());
44 writer.AppendBool(true); // Wait for response.
45 upstart_proxy_->CallMethod(
46 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
47 base::Bind(&MediaAnalyticsClientImpl::HandleUpstartResponse,
48 weak_ptr_factory_.GetWeakPtr(), callback));
49 }
50
51 void SetMediaPerceptionSignalHandler(
52 const MediaPerceptionSignalHandler& handler) override {
53 media_perception_signal_handler_ = handler;
54 // Connect to the MediaPerception proto signal.
55 dbus_proxy_->ConnectToSignal(
56 media_perception::kMediaPerceptionInterface,
57 media_perception::kDetectionSignal,
58 base::Bind(&MediaAnalyticsClientImpl::OnDetectionSignalReceived,
59 weak_ptr_factory_.GetWeakPtr()),
60 base::Bind(&MediaAnalyticsClientImpl::OnSignalConnected,
61 weak_ptr_factory_.GetWeakPtr()));
62 }
63
64 void UnsetMediaPerceptionSignalHandler() override {
65 media_perception_signal_handler_.Reset();
66 }
67
68 void State(const mri::State& state, const StateCallback& callback) override {
69 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName,
70 media_perception::kState);
71 if (state.has_status()) { // Check that a new state is requested.
72 int length = state.ByteSize();
73 uint8_t bytes[length];
74 state.SerializeToArray(bytes, length);
75 dbus::MessageWriter writer(&method_call);
76 writer.AppendArrayOfBytes(bytes, length);
77 }
78 dbus_proxy_->CallMethod(
79 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
80 base::Bind(&MediaAnalyticsClientImpl::OnState,
81 weak_ptr_factory_.GetWeakPtr(), callback));
82 }
83
84 void GetDiagnostics(const DiagnosticsCallback& callback) override {
85 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName,
86 media_perception::kGetDiagnostics);
87 // TODO(lasoren): Verify that this timeout setting is sufficient.
88 dbus_proxy_->CallMethod(
89 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
90 base::Bind(&MediaAnalyticsClientImpl::OnGetDiagnostics,
91 weak_ptr_factory_.GetWeakPtr(), callback));
92 }
93
94 protected:
95 void Init(dbus::Bus* bus) override {
96 upstart_proxy_ = bus->GetObjectProxy(
97 media_perception::kUpstartServiceName,
98 dbus::ObjectPath(media_perception::kUpstartMediaAnalyticsPath));
99 dbus_proxy_ = bus->GetObjectProxy(
100 media_perception::kMediaPerceptionServiceName,
101 dbus::ObjectPath(media_perception::kMediaPerceptionServicePath));
102 }
103
104 private:
105 void HandleUpstartResponse(const UpstartCallback& callback,
106 dbus::Response* response) {
107 if (!response) {
108 LOG(ERROR) << "Failed to signal Upstart, response is null.";
109 callback.Run(false);
110 }
111 callback.Run(true);
112 }
113
114 void OnSignalConnected(const std::string& interface,
115 const std::string& signal,
116 bool succeeded) {
117 LOG_IF(ERROR, !succeeded)
118 << "Connect to " << interface << " " << signal << " failed.";
119 }
120
121 // Handler that is triggered when a MediaPerception proto is received from
122 // the media analytics process.
123 void OnDetectionSignalReceived(dbus::Signal* signal) {
124 const uint8_t* bytes = nullptr;
125 size_t length = 0;
126
127 dbus::MessageReader reader(signal);
128
129 if (!reader.PopArrayOfBytes(&bytes, &length)) {
130 LOG(ERROR) << "Invalid detection signal: " << signal->ToString();
131 return;
132 }
133
134 mri::MediaPerception media_perception;
135 if (!media_perception.ParseFromArray(bytes, length)) {
136 LOG(ERROR) << "Failed to parse MediaPerception message.";
137 return;
138 }
139
140 if (!media_perception_signal_handler_.is_null()) {
141 media_perception_signal_handler_.Run(media_perception);
142 }
143 }
144
145 void OnState(const StateCallback& callback, dbus::Response* response) {
146 mri::State state;
147 if (!response) {
148 LOG(ERROR) << "Call to State failed to get response.";
149 callback.Run(false, state);
150 return;
151 }
152
153 const uint8_t* bytes = nullptr;
154 size_t length = 0;
155
156 dbus::MessageReader reader(response);
157 if (!reader.PopArrayOfBytes(&bytes, &length)) {
158 LOG(ERROR) << "Invalid D-Bus response: " << response->ToString();
159 callback.Run(false, state);
160 }
161
162 if (!state.ParseFromArray(bytes, length)) {
163 LOG(ERROR) << "Failed to parse State message.";
164 callback.Run(false, state);
165 }
166
167 callback.Run(true, state);
168 }
169
170 void OnGetDiagnostics(const DiagnosticsCallback& callback,
171 dbus::Response* response) {
172 mri::Diagnostics diagnostics;
173 if (!response) {
174 LOG(ERROR) << "Call to GetDiagnostics failed to get response.";
175 callback.Run(false, diagnostics);
176 return;
177 }
178
179 const uint8_t* bytes = nullptr;
180 size_t length = 0;
181
182 dbus::MessageReader reader(response);
183 if (!reader.PopArrayOfBytes(&bytes, &length)) {
184 LOG(ERROR) << "Invalid GetDiagnostics response: " << response->ToString();
185 callback.Run(false, diagnostics);
186 }
187
188 if (!diagnostics.ParseFromArray(bytes, length)) {
189 LOG(ERROR) << "Failed to parse Diagnostics message.";
190 callback.Run(false, diagnostics);
191 }
192
193 callback.Run(true, diagnostics);
194 }
195
196 dbus::ObjectProxy* dbus_proxy_;
197 // Used for sending D-Bus command to Upstart to start the media analytics
198 // process.
199 dbus::ObjectProxy* upstart_proxy_;
200
201 // Stores a handler registered for receiving the media_perception.proto byte
202 // array.
203 MediaPerceptionSignalHandler media_perception_signal_handler_;
204
205 // For providing a pointer from an object of this class to bind to callbacks.
206 base::WeakPtrFactory<MediaAnalyticsClientImpl> weak_ptr_factory_;
207 };
208
209 MediaAnalyticsClient::~MediaAnalyticsClient() {}
210
211 MediaAnalyticsClient* MediaAnalyticsClient::Create() {
212 return new MediaAnalyticsClientImpl;
213 }
214
215 MediaAnalyticsClient::MediaAnalyticsClient() {}
216
217 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698