OLD | NEW |
---|---|
(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::kState); | |
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::kState); | |
51 // Check that a new state is requested. | |
52 if (state.has_status()) { | |
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 } else { | |
59 LOG(WARNING) << "Attempting to SetState without status set."; | |
tbarzic
2017/05/09 20:28:41
should this case cause an error callback to be inv
Luke Sorenson
2017/05/09 22:21:25
Done.
| |
60 } | |
61 dbus_proxy_->CallMethod( | |
62 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
63 base::Bind(&MediaAnalyticsClientImpl::OnState, | |
64 weak_ptr_factory_.GetWeakPtr(), callback)); | |
65 } | |
66 | |
67 void GetDiagnostics(const DiagnosticsCallback& callback) override { | |
68 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName, | |
69 media_perception::kGetDiagnostics); | |
70 // TODO(lasoren): Verify that this timeout setting is sufficient. | |
71 dbus_proxy_->CallMethod( | |
72 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
73 base::Bind(&MediaAnalyticsClientImpl::OnGetDiagnostics, | |
74 weak_ptr_factory_.GetWeakPtr(), callback)); | |
75 } | |
76 | |
77 protected: | |
78 void Init(dbus::Bus* bus) override { | |
79 dbus_proxy_ = bus->GetObjectProxy( | |
80 media_perception::kMediaPerceptionServiceName, | |
81 dbus::ObjectPath(media_perception::kMediaPerceptionServicePath)); | |
82 // Connect to the MediaPerception signal. | |
83 dbus_proxy_->ConnectToSignal( | |
84 media_perception::kMediaPerceptionInterface, | |
85 media_perception::kDetectionSignal, | |
86 base::Bind(&MediaAnalyticsClientImpl::OnDetectionSignalReceived, | |
87 weak_ptr_factory_.GetWeakPtr()), | |
88 base::Bind(&MediaAnalyticsClientImpl::OnSignalConnected, | |
89 weak_ptr_factory_.GetWeakPtr())); | |
90 } | |
91 | |
92 private: | |
93 void OnSignalConnected(const std::string& interface, | |
94 const std::string& signal, | |
95 bool succeeded) { | |
96 LOG_IF(ERROR, !succeeded) | |
97 << "Connect to " << interface << " " << signal << " failed."; | |
98 } | |
99 | |
100 // Handler that is triggered when a MediaPerception proto is received from | |
101 // the media analytics process. | |
102 void OnDetectionSignalReceived(dbus::Signal* signal) { | |
103 if (media_perception_signal_handler_.is_null()) { | |
104 LOG(WARNING) << "Received DetectionSignal but handler is null."; | |
105 return; | |
106 } | |
107 const uint8_t* bytes = nullptr; | |
108 size_t length = 0; | |
109 | |
110 dbus::MessageReader reader(signal); | |
111 | |
112 if (!reader.PopArrayOfBytes(&bytes, &length)) { | |
113 LOG(ERROR) << "Invalid detection signal: " << signal->ToString(); | |
114 return; | |
115 } | |
116 | |
117 mri::MediaPerception media_perception; | |
118 if (!media_perception.ParseFromArray(bytes, length)) { | |
119 LOG(ERROR) << "Failed to parse MediaPerception message."; | |
120 return; | |
121 } | |
122 | |
123 media_perception_signal_handler_.Run(media_perception); | |
124 } | |
125 | |
126 void OnState(const StateCallback& callback, dbus::Response* response) { | |
127 mri::State state; | |
128 if (!response) { | |
129 LOG(ERROR) << "Call to State failed to get response."; | |
130 callback.Run(false, state); | |
131 return; | |
132 } | |
133 | |
134 const uint8_t* bytes = nullptr; | |
135 size_t length = 0; | |
136 | |
137 dbus::MessageReader reader(response); | |
138 if (!reader.PopArrayOfBytes(&bytes, &length)) { | |
139 LOG(ERROR) << "Invalid D-Bus response: " << response->ToString(); | |
140 callback.Run(false, state); | |
141 return; | |
142 } | |
143 | |
144 if (!state.ParseFromArray(bytes, length)) { | |
145 LOG(ERROR) << "Failed to parse State message."; | |
146 callback.Run(false, state); | |
147 return; | |
148 } | |
149 | |
150 callback.Run(true, state); | |
151 } | |
152 | |
153 void OnGetDiagnostics(const DiagnosticsCallback& callback, | |
154 dbus::Response* response) { | |
155 mri::Diagnostics diagnostics; | |
156 if (!response) { | |
157 LOG(ERROR) << "Call to GetDiagnostics failed to get response."; | |
158 callback.Run(false, diagnostics); | |
159 return; | |
160 } | |
161 | |
162 const uint8_t* bytes = nullptr; | |
163 size_t length = 0; | |
164 | |
165 dbus::MessageReader reader(response); | |
166 if (!reader.PopArrayOfBytes(&bytes, &length)) { | |
167 LOG(ERROR) << "Invalid GetDiagnostics response: " << response->ToString(); | |
168 callback.Run(false, diagnostics); | |
169 return; | |
170 } | |
171 | |
172 if (!diagnostics.ParseFromArray(bytes, length)) { | |
173 LOG(ERROR) << "Failed to parse Diagnostics message."; | |
174 callback.Run(false, diagnostics); | |
175 return; | |
176 } | |
177 | |
178 callback.Run(true, diagnostics); | |
179 } | |
180 | |
181 dbus::ObjectProxy* dbus_proxy_; | |
182 | |
183 // Stores a handler registered for receiving the media_perception.proto byte | |
184 // array. | |
185 MediaPerceptionSignalHandler media_perception_signal_handler_; | |
186 | |
187 base::WeakPtrFactory<MediaAnalyticsClientImpl> weak_ptr_factory_; | |
188 | |
189 DISALLOW_COPY_AND_ASSIGN(MediaAnalyticsClientImpl); | |
190 }; | |
191 | |
192 MediaAnalyticsClient::~MediaAnalyticsClient() {} | |
193 | |
194 MediaAnalyticsClient* MediaAnalyticsClient::Create() { | |
195 return new MediaAnalyticsClientImpl; | |
196 } | |
197 | |
198 MediaAnalyticsClient::MediaAnalyticsClient() {} | |
199 | |
200 } // namespace chromeos | |
OLD | NEW |