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