Index: chromeos/dbus/media_analytics_client.cc |
diff --git a/chromeos/dbus/media_analytics_client.cc b/chromeos/dbus/media_analytics_client.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..42839a7c91f700a8d7abe2f7041b40260a23839f |
--- /dev/null |
+++ b/chromeos/dbus/media_analytics_client.cc |
@@ -0,0 +1,178 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chromeos/dbus/media_analytics_client.h" |
+ |
+#include <cstdint> |
+ |
+#include "base/bind.h" |
+#include "base/logging.h" |
+#include "base/memory/weak_ptr.h" |
+#include "dbus/bus.h" |
+#include "dbus/message.h" |
+#include "dbus/object_path.h" |
+#include "dbus/object_proxy.h" |
+#include "third_party/cros_system_api/dbus/service_constants.h" |
+ |
+namespace chromeos { |
+ |
+// The MediaAnalyticsCleint implementation used in production. |
+class MediaAnalyticsClientImpl : public MediaAnalyticsClient { |
+ public: |
+ MediaAnalyticsClientImpl() : dbus_proxy_(nullptr), weak_ptr_factory_(this) {} |
+ |
+ ~MediaAnalyticsClientImpl() override {} |
+ |
+ void SetMediaPerceptionSignalHandler( |
+ const MediaPerceptionSignalHandler& handler) override { |
+ media_perception_signal_handler_ = handler; |
+ // Connect to the MediaPerception proto signal. |
+ dbus_proxy_->ConnectToSignal( |
+ media_perception::kMediaPerceptionInterface, |
+ media_perception::kDetectionSignal, |
+ base::Bind(&MediaAnalyticsClientImpl::OnDetectionSignalReceived, |
+ weak_ptr_factory_.GetWeakPtr()), |
+ base::Bind(&MediaAnalyticsClientImpl::OnSignalConnected, |
+ weak_ptr_factory_.GetWeakPtr())); |
+ } |
+ |
+ void UnsetMediaPerceptionSignalHandler() override { |
+ media_perception_signal_handler_.Reset(); |
+ } |
+ |
+ void State(const mri::State& state, const StateCallback& callback) override { |
+ dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName, |
+ media_perception::kState); |
+ if (state.has_status()) { // Check that a new state is requested. |
+ int length = state.ByteSize(); |
+ uint8_t bytes[length]; |
+ state.SerializeToArray(bytes, length); |
+ dbus::MessageWriter writer(&method_call); |
+ writer.AppendArrayOfBytes(bytes, length); |
+ } |
+ dbus_proxy_->CallMethod( |
+ &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
+ base::Bind(&MediaAnalyticsClientImpl::OnState, |
+ weak_ptr_factory_.GetWeakPtr(), callback)); |
+ } |
+ |
+ void GetDiagnostics(const DiagnosticsCallback& callback) override { |
+ dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName, |
+ media_perception::kGetDiagnostics); |
+ // TODO(lasoren): Verify that this timeout setting is sufficient. |
+ dbus_proxy_->CallMethod( |
+ &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
+ base::Bind(&MediaAnalyticsClientImpl::OnGetDiagnostics, |
+ weak_ptr_factory_.GetWeakPtr(), callback)); |
+ } |
+ |
+ protected: |
+ void Init(dbus::Bus* bus) override { |
+ dbus_proxy_ = bus->GetObjectProxy( |
+ media_perception::kMediaPerceptionServiceName, |
+ dbus::ObjectPath(media_perception::kMediaPerceptionServicePath)); |
+ } |
+ |
+ private: |
+ void OnSignalConnected(const std::string& interface, |
+ const std::string& signal, |
+ bool succeeded) { |
+ LOG_IF(ERROR, !succeeded) |
+ << "Connect to " << interface << " " << signal << " failed."; |
+ } |
+ |
+ // Handler that is triggered when a MediaPerception proto is received from |
+ // the media analytics process. |
+ void OnDetectionSignalReceived(dbus::Signal* signal) { |
+ const uint8_t* bytes = nullptr; |
+ size_t length = 0; |
+ |
+ dbus::MessageReader reader(signal); |
+ |
+ if (!reader.PopArrayOfBytes(&bytes, &length)) { |
+ LOG(ERROR) << "Invalid detection signal: " << signal->ToString(); |
+ return; |
+ } |
+ |
+ mri::MediaPerception media_perception; |
+ if (!media_perception.ParseFromArray(bytes, length)) { |
+ LOG(ERROR) << "Failed to parse MediaPerception message."; |
+ return; |
+ } |
+ |
+ if (!media_perception_signal_handler_.is_null()) { |
+ media_perception_signal_handler_.Run(media_perception); |
+ } |
+ } |
+ |
+ void OnState(const StateCallback& callback, dbus::Response* response) { |
+ mri::State state; |
+ if (!response) { |
+ LOG(ERROR) << "Call to State failed to get response."; |
+ callback.Run(false, state); |
+ return; |
+ } |
+ |
+ const uint8_t* bytes = nullptr; |
+ size_t length = 0; |
+ |
+ dbus::MessageReader reader(response); |
+ if (!reader.PopArrayOfBytes(&bytes, &length)) { |
+ LOG(ERROR) << "Invalid D-Bus response: " << response->ToString(); |
+ callback.Run(false, state); |
+ } |
+ |
+ if (!state.ParseFromArray(bytes, length)) { |
+ LOG(ERROR) << "Failed to parse State message."; |
+ callback.Run(false, state); |
+ } |
+ |
+ callback.Run(true, state); |
+ } |
+ |
+ void OnGetDiagnostics(const DiagnosticsCallback& callback, |
+ dbus::Response* response) { |
+ mri::Diagnostics diagnostics; |
+ if (!response) { |
+ LOG(ERROR) << "Call to GetDiagnostics failed to get response."; |
+ callback.Run(false, diagnostics); |
+ return; |
+ } |
+ |
+ const uint8_t* bytes = nullptr; |
+ size_t length = 0; |
+ |
+ dbus::MessageReader reader(response); |
+ if (!reader.PopArrayOfBytes(&bytes, &length)) { |
+ LOG(ERROR) << "Invalid GetDiagnostics response: " << response->ToString(); |
+ callback.Run(false, diagnostics); |
+ } |
+ |
+ if (!diagnostics.ParseFromArray(bytes, length)) { |
+ LOG(ERROR) << "Failed to parse Diagnostics message."; |
+ callback.Run(false, diagnostics); |
+ } |
+ |
+ callback.Run(true, diagnostics); |
+ } |
+ |
+ dbus::ObjectProxy* dbus_proxy_; |
+ |
+ // Stores a handler registered for receiving the media_perception.proto byte |
+ // array. |
+ MediaPerceptionSignalHandler media_perception_signal_handler_; |
+ |
+ // For providing a pointer from an object of this class to bind to callbacks. |
+ base::WeakPtrFactory<MediaAnalyticsClientImpl> weak_ptr_factory_; |
+}; |
+ |
+MediaAnalyticsClient::~MediaAnalyticsClient() {} |
+ |
+MediaAnalyticsClient* MediaAnalyticsClient::Create() { |
+ return new MediaAnalyticsClientImpl; |
+} |
+ |
+MediaAnalyticsClient::MediaAnalyticsClient() {} |
+ |
+} // namespace chromeos |