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

Unified Diff: chromeos/dbus/media_analytics_client.cc

Issue 2791983004: DBus MediaAnalyticsClient and media_perception pb. (Closed)
Patch Set: Upstart process management Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
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..ba0c98ad7e86fb2f10d61c036ed5af3ba592b923
--- /dev/null
+++ b/chromeos/dbus/media_analytics_client.cc
@@ -0,0 +1,205 @@
+// 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"
+
+namespace chromeos {
+
+namespace {
+
+const char kUpstartServiceName[] = "com.ubuntu.Upstart";
+const char kUpstartJobInterface[] = "com.ubuntu.Upstart0_6.Job";
+const char kUpstartStartMethod[] = "Start";
+const char kUpstartRestartMethod[] = "Restart";
+const char kUpstartMediaAnalyticsPath[] =
+ "/com/ubuntu/Upstart/jobs/rtanalytics";
+
+const char kMediaPerceptionServiceName[] = "org.chromium.MediaPerception";
+const char kMediaPerceptionServicePath[] = "/org/chromium/MediaPerception";
+const char kMediaPerceptionInterface[] = "org.chromium.MediaPerception";
tbarzic 2017/04/27 20:37:35 these should probably go to third_party/cros_syste
Luke Sorenson 2017/05/03 23:56:06 Done. Moved here: https://chromium-review.googleso
+const char kState[] = "State";
+const char kDetectionSignal[] = "MediaPerceptionDetection";
+const char kGetDiagnostics[] = "GetDiagnostics";
+
+} // namespace
+
+// The MediaAnalyticsCleint implementation used in production.
+class MediaAnalyticsClientImpl : public MediaAnalyticsClient {
+ public:
+ MediaAnalyticsClientImpl() : dbus_proxy_(nullptr), weak_ptr_factory_(this) {}
+
+ ~MediaAnalyticsClientImpl() override {}
+
+ void StartMediaAnalytics(const UpstartCallback& callback) override {
+ dbus::MethodCall method_call(kUpstartJobInterface, kUpstartStartMethod);
+ dbus::MessageWriter writer(&method_call);
+ writer.AppendArrayOfStrings(std::vector<std::string>());
+ writer.AppendBool(true); // Wait for response.
+ upstart_proxy_->CallMethod(
+ &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
+ base::Bind(&MediaAnalyticsClientImpl::HandleUpstartResponse,
+ weak_ptr_factory_.GetWeakPtr(), callback));
+ }
+
+ void RestartMediaAnalytics(const UpstartCallback& callback) override {
+ dbus::MethodCall method_call(kUpstartJobInterface, kUpstartRestartMethod);
+ dbus::MessageWriter writer(&method_call);
+ writer.AppendArrayOfStrings(std::vector<std::string>());
+ writer.AppendBool(true); // Wait for response.
+ upstart_proxy_->CallMethod(
+ &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
+ base::Bind(&MediaAnalyticsClientImpl::HandleUpstartResponse,
+ weak_ptr_factory_.GetWeakPtr(), callback));
+ }
+
+ void SetMediaPerceptionSignalHandler(
+ const MediaPerceptionSignalHandler& handler) override {
+ media_perception_signal_handler_ = handler;
+ // Connect to the MediaPerception proto signal.
+ dbus_proxy_->ConnectToSignal(
+ kMediaPerceptionInterface, kDetectionSignal,
+ base::Bind(&MediaAnalyticsClientImpl::OnDetectionSignalReceived,
+ weak_ptr_factory_.GetWeakPtr()),
+ base::Bind(&MediaAnalyticsClientImpl::OnSignalConnected,
+ weak_ptr_factory_.GetWeakPtr()));
+ }
+
+ void State(const uint8_t* bytes,
+ size_t length,
+ const StateCallback& callback) override {
+ dbus::MethodCall method_call(kMediaPerceptionServiceName, kState);
+ dbus::MessageWriter writer(&method_call);
+ if (bytes == nullptr || length == 0) {
+ 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 DiagnoticsCallback& callback) override {
+ dbus::MethodCall method_call(kMediaPerceptionServiceName, 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 {
+ upstart_proxy_ = bus->GetObjectProxy(
+ kUpstartServiceName, dbus::ObjectPath(kUpstartMediaAnalyticsPath));
+ dbus_proxy_ =
+ bus->GetObjectProxy(kMediaPerceptionServiceName,
+ dbus::ObjectPath(kMediaPerceptionServicePath));
+ }
+
+ private:
+ void HandleUpstartResponse(const UpstartCallback& callback,
+ dbus::Response* response) {
+ if (!response) {
+ LOG(ERROR) << "Failed to signal Upstart, response is null.";
+ callback.Run(false);
+ }
+ callback.Run(true);
+ }
+
+ 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;
+ }
+
+ if (!media_perception_signal_handler_.is_null()) {
+ media_perception_signal_handler_.Run(bytes, length);
+ }
+ }
+
+ void OnState(const StateCallback& callback, dbus::Response* response) {
+ if (!response) {
+ LOG(ERROR) << "Call to State failed to get response.";
+ callback.Run(false, nullptr, 0);
+ return;
+ }
+
+ const uint8_t* bytes = nullptr;
+ size_t length = 0;
+
+ dbus::MessageReader reader(response);
+ if (!reader.PopArrayOfBytes(&bytes, &length)) {
+ LOG(ERROR) << "Invalid State response: " << response->ToString();
+ callback.Run(false, nullptr, 0);
+ }
+
+ callback.Run(true, bytes, length);
+ }
+
+ void OnGetDiagnostics(const DiagnoticsCallback& callback,
+ dbus::Response* response) {
+ if (!response) {
+ LOG(ERROR) << "Call to GetDiagnostics failed to get response.";
+ callback.Run(false, nullptr, 0);
+ 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, nullptr, 0);
+ }
+
+ callback.Run(true, bytes, length);
+ }
+
+ dbus::ObjectProxy* dbus_proxy_;
+ // Used for sending D-Bus command to Upstart to start the media analytics
+ // process.
+ dbus::ObjectProxy* upstart_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

Powered by Google App Engine
This is Rietveld 408576698