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 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 17 | |
| 18 namespace chromeos { | |
| 19 | |
| 20 // The MediaAnalyticsCleint implementation used in production. | |
|
tbarzic
2017/05/05 21:10:42
nit:s/Cleint/Client
Luke Sorenson
2017/05/08 19:06:07
Done.
| |
| 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 media_perception_signal_handler_ = handler; | |
|
tbarzic
2017/05/05 21:10:42
DCHECK that the handler was not set before this?
Luke Sorenson
2017/05/08 19:06:07
Done.
| |
| 30 // Connect to the MediaPerception proto signal. | |
| 31 dbus_proxy_->ConnectToSignal( | |
|
tbarzic
2017/05/05 21:10:42
should this be done in init instead (or at least o
Luke Sorenson
2017/05/08 19:06:07
Done.
| |
| 32 media_perception::kMediaPerceptionInterface, | |
| 33 media_perception::kDetectionSignal, | |
| 34 base::Bind(&MediaAnalyticsClientImpl::OnDetectionSignalReceived, | |
| 35 weak_ptr_factory_.GetWeakPtr()), | |
| 36 base::Bind(&MediaAnalyticsClientImpl::OnSignalConnected, | |
| 37 weak_ptr_factory_.GetWeakPtr())); | |
| 38 } | |
| 39 | |
| 40 void UnsetMediaPerceptionSignalHandler() override { | |
| 41 media_perception_signal_handler_.Reset(); | |
| 42 } | |
| 43 | |
| 44 void State(const mri::State& state, const StateCallback& callback) override { | |
| 45 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName, | |
| 46 media_perception::kState); | |
| 47 if (state.has_status()) { // Check that a new state is requested. | |
|
tbarzic
2017/05/05 21:10:42
nit: put the comment above if
Luke Sorenson
2017/05/08 19:06:07
Done.
| |
| 48 int length = state.ByteSize(); | |
| 49 uint8_t bytes[length]; | |
| 50 state.SerializeToArray(bytes, length); | |
| 51 dbus::MessageWriter writer(&method_call); | |
| 52 writer.AppendArrayOfBytes(bytes, length); | |
| 53 } | |
| 54 dbus_proxy_->CallMethod( | |
| 55 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 56 base::Bind(&MediaAnalyticsClientImpl::OnState, | |
| 57 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 58 } | |
| 59 | |
| 60 void GetDiagnostics(const DiagnosticsCallback& callback) override { | |
| 61 dbus::MethodCall method_call(media_perception::kMediaPerceptionServiceName, | |
| 62 media_perception::kGetDiagnostics); | |
| 63 // TODO(lasoren): Verify that this timeout setting is sufficient. | |
| 64 dbus_proxy_->CallMethod( | |
| 65 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 66 base::Bind(&MediaAnalyticsClientImpl::OnGetDiagnostics, | |
| 67 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 68 } | |
| 69 | |
| 70 protected: | |
| 71 void Init(dbus::Bus* bus) override { | |
| 72 dbus_proxy_ = bus->GetObjectProxy( | |
| 73 media_perception::kMediaPerceptionServiceName, | |
| 74 dbus::ObjectPath(media_perception::kMediaPerceptionServicePath)); | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 void OnSignalConnected(const std::string& interface, | |
| 79 const std::string& signal, | |
| 80 bool succeeded) { | |
| 81 LOG_IF(ERROR, !succeeded) | |
| 82 << "Connect to " << interface << " " << signal << " failed."; | |
| 83 } | |
| 84 | |
| 85 // Handler that is triggered when a MediaPerception proto is received from | |
| 86 // the media analytics process. | |
| 87 void OnDetectionSignalReceived(dbus::Signal* signal) { | |
| 88 const uint8_t* bytes = nullptr; | |
| 89 size_t length = 0; | |
| 90 | |
| 91 dbus::MessageReader reader(signal); | |
| 92 | |
| 93 if (!reader.PopArrayOfBytes(&bytes, &length)) { | |
| 94 LOG(ERROR) << "Invalid detection signal: " << signal->ToString(); | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 mri::MediaPerception media_perception; | |
| 99 if (!media_perception.ParseFromArray(bytes, length)) { | |
| 100 LOG(ERROR) << "Failed to parse MediaPerception message."; | |
| 101 return; | |
| 102 } | |
| 103 | |
| 104 if (!media_perception_signal_handler_.is_null()) { | |
|
tbarzic
2017/05/05 21:10:42
maybe do
if (media_perception_signal_handler_.is_n
Luke Sorenson
2017/05/08 19:06:07
Done.
| |
| 105 media_perception_signal_handler_.Run(media_perception); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 void OnState(const StateCallback& callback, dbus::Response* response) { | |
| 110 mri::State state; | |
| 111 if (!response) { | |
| 112 LOG(ERROR) << "Call to State failed to get response."; | |
| 113 callback.Run(false, state); | |
| 114 return; | |
| 115 } | |
| 116 | |
| 117 const uint8_t* bytes = nullptr; | |
| 118 size_t length = 0; | |
| 119 | |
| 120 dbus::MessageReader reader(response); | |
| 121 if (!reader.PopArrayOfBytes(&bytes, &length)) { | |
| 122 LOG(ERROR) << "Invalid D-Bus response: " << response->ToString(); | |
| 123 callback.Run(false, state); | |
|
tbarzic
2017/05/05 21:10:42
return;
(here and other places you run error call
Luke Sorenson
2017/05/08 19:06:07
Done.
| |
| 124 } | |
| 125 | |
| 126 if (!state.ParseFromArray(bytes, length)) { | |
| 127 LOG(ERROR) << "Failed to parse State message."; | |
| 128 callback.Run(false, state); | |
| 129 } | |
| 130 | |
| 131 callback.Run(true, state); | |
| 132 } | |
| 133 | |
| 134 void OnGetDiagnostics(const DiagnosticsCallback& callback, | |
| 135 dbus::Response* response) { | |
| 136 mri::Diagnostics diagnostics; | |
| 137 if (!response) { | |
| 138 LOG(ERROR) << "Call to GetDiagnostics failed to get response."; | |
| 139 callback.Run(false, diagnostics); | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 const uint8_t* bytes = nullptr; | |
| 144 size_t length = 0; | |
| 145 | |
| 146 dbus::MessageReader reader(response); | |
| 147 if (!reader.PopArrayOfBytes(&bytes, &length)) { | |
| 148 LOG(ERROR) << "Invalid GetDiagnostics response: " << response->ToString(); | |
| 149 callback.Run(false, diagnostics); | |
| 150 } | |
| 151 | |
| 152 if (!diagnostics.ParseFromArray(bytes, length)) { | |
| 153 LOG(ERROR) << "Failed to parse Diagnostics message."; | |
| 154 callback.Run(false, diagnostics); | |
| 155 } | |
| 156 | |
| 157 callback.Run(true, diagnostics); | |
| 158 } | |
| 159 | |
| 160 dbus::ObjectProxy* dbus_proxy_; | |
| 161 | |
| 162 // Stores a handler registered for receiving the media_perception.proto byte | |
| 163 // array. | |
| 164 MediaPerceptionSignalHandler media_perception_signal_handler_; | |
| 165 | |
| 166 // For providing a pointer from an object of this class to bind to callbacks. | |
|
tbarzic
2017/05/05 21:10:42
I don't think this comment is needed.
Luke Sorenson
2017/05/08 19:06:07
Done.
| |
| 167 base::WeakPtrFactory<MediaAnalyticsClientImpl> weak_ptr_factory_; | |
|
tbarzic
2017/05/05 21:10:42
DISALLOW_COPY_AND_ASSIGN?
Luke Sorenson
2017/05/08 19:06:07
Done.
| |
| 168 }; | |
| 169 | |
| 170 MediaAnalyticsClient::~MediaAnalyticsClient() {} | |
| 171 | |
| 172 MediaAnalyticsClient* MediaAnalyticsClient::Create() { | |
| 173 return new MediaAnalyticsClientImpl; | |
| 174 } | |
| 175 | |
| 176 MediaAnalyticsClient::MediaAnalyticsClient() {} | |
| 177 | |
| 178 } // namespace chromeos | |
| OLD | NEW |