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

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

Issue 2791983004: DBus MediaAnalyticsClient and media_perception pb. (Closed)
Patch Set: Addressing comments on D-Bus code. 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
« no previous file with comments | « chromeos/dbus/media_analytics_client.h ('k') | chromeos/dbus/proto/media_perception.proto » ('j') | 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 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 UnsetMediaPerceptionSignalHandler() override {
35 media_perception_signal_handler_.Reset();
36 }
37
38 void State(const mri::State& state, const StateCallback& callback) override {
39 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName,
40 media_perception::kState);
41 // Check that a new state is requested.
42 if (state.has_status()) {
43 int length = state.ByteSize();
44 uint8_t bytes[length];
45 state.SerializeToArray(bytes, length);
46 dbus::MessageWriter writer(&method_call);
47 writer.AppendArrayOfBytes(bytes, length);
48 }
49 dbus_proxy_->CallMethod(
50 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
51 base::Bind(&MediaAnalyticsClientImpl::OnState,
52 weak_ptr_factory_.GetWeakPtr(), callback));
53 }
54
55 void GetDiagnostics(const DiagnosticsCallback& callback) override {
56 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName,
57 media_perception::kGetDiagnostics);
58 // TODO(lasoren): Verify that this timeout setting is sufficient.
59 dbus_proxy_->CallMethod(
60 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
61 base::Bind(&MediaAnalyticsClientImpl::OnGetDiagnostics,
62 weak_ptr_factory_.GetWeakPtr(), callback));
63 }
64
65 protected:
66 void Init(dbus::Bus* bus) override {
67 dbus_proxy_ = bus->GetObjectProxy(
68 media_perception::kMediaPerceptionServiceName,
69 dbus::ObjectPath(media_perception::kMediaPerceptionServicePath));
70 // Connect to the MediaPerception proto signal.
tbarzic 2017/05/08 23:04:55 nit: drop "proto"
Luke Sorenson 2017/05/09 17:39:45 Done.
71 dbus_proxy_->ConnectToSignal(
72 media_perception::kMediaPerceptionInterface,
73 media_perception::kDetectionSignal,
74 base::Bind(&MediaAnalyticsClientImpl::OnDetectionSignalReceived,
75 weak_ptr_factory_.GetWeakPtr()),
76 base::Bind(&MediaAnalyticsClientImpl::OnSignalConnected,
77 weak_ptr_factory_.GetWeakPtr()));
78 }
79
80 private:
81 void OnSignalConnected(const std::string& interface,
82 const std::string& signal,
83 bool succeeded) {
84 LOG_IF(ERROR, !succeeded)
85 << "Connect to " << interface << " " << signal << " failed.";
86 }
87
88 // Handler that is triggered when a MediaPerception proto is received from
89 // the media analytics process.
90 void OnDetectionSignalReceived(dbus::Signal* signal) {
91 if (media_perception_signal_handler_.is_null()) {
92 LOG(WARNING) << "Received DetectionSignal but handler is null.";
93 return;
94 }
95 const uint8_t* bytes = nullptr;
96 size_t length = 0;
97
98 dbus::MessageReader reader(signal);
99
100 if (!reader.PopArrayOfBytes(&bytes, &length)) {
101 LOG(ERROR) << "Invalid detection signal: " << signal->ToString();
102 return;
103 }
104
105 mri::MediaPerception media_perception;
106 if (!media_perception.ParseFromArray(bytes, length)) {
107 LOG(ERROR) << "Failed to parse MediaPerception message.";
108 return;
109 }
110
111 media_perception_signal_handler_.Run(media_perception);
112 }
113
114 void OnState(const StateCallback& callback, dbus::Response* response) {
115 mri::State state;
116 if (!response) {
117 LOG(ERROR) << "Call to State failed to get response.";
118 callback.Run(false, state);
119 return;
120 }
121
122 const uint8_t* bytes = nullptr;
123 size_t length = 0;
124
125 dbus::MessageReader reader(response);
126 if (!reader.PopArrayOfBytes(&bytes, &length)) {
127 LOG(ERROR) << "Invalid D-Bus response: " << response->ToString();
128 callback.Run(false, state);
129 return;
130 }
131
132 if (!state.ParseFromArray(bytes, length)) {
133 LOG(ERROR) << "Failed to parse State message.";
134 callback.Run(false, state);
135 return;
136 }
137
138 callback.Run(true, state);
139 }
140
141 void OnGetDiagnostics(const DiagnosticsCallback& callback,
142 dbus::Response* response) {
143 mri::Diagnostics diagnostics;
144 if (!response) {
145 LOG(ERROR) << "Call to GetDiagnostics failed to get response.";
146 callback.Run(false, diagnostics);
147 return;
148 }
149
150 const uint8_t* bytes = nullptr;
151 size_t length = 0;
152
153 dbus::MessageReader reader(response);
154 if (!reader.PopArrayOfBytes(&bytes, &length)) {
155 LOG(ERROR) << "Invalid GetDiagnostics response: " << response->ToString();
156 callback.Run(false, diagnostics);
157 return;
158 }
159
160 if (!diagnostics.ParseFromArray(bytes, length)) {
161 LOG(ERROR) << "Failed to parse Diagnostics message.";
162 callback.Run(false, diagnostics);
163 return;
164 }
165
166 callback.Run(true, diagnostics);
167 }
168
169 dbus::ObjectProxy* dbus_proxy_;
170
171 // Stores a handler registered for receiving the media_perception.proto byte
172 // array.
173 MediaPerceptionSignalHandler media_perception_signal_handler_;
174
175 base::WeakPtrFactory<MediaAnalyticsClientImpl> weak_ptr_factory_;
176
177 DISALLOW_COPY_AND_ASSIGN(MediaAnalyticsClientImpl);
178 };
179
180 MediaAnalyticsClient::~MediaAnalyticsClient() {}
181
182 MediaAnalyticsClient* MediaAnalyticsClient::Create() {
183 return new MediaAnalyticsClientImpl;
184 }
185
186 MediaAnalyticsClient::MediaAnalyticsClient() {}
187
188 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/media_analytics_client.h ('k') | chromeos/dbus/proto/media_perception.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698