Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS 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 | #include "system-proxy/system_proxy_adaptor.h" |
| 5 | |
| 6 | #include <string> |
| 7 | #include <utility> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <base/location.h> |
| 11 | #include <brillo/dbus/dbus_object.h> |
| 12 | |
| 13 | #include "system_proxy/proto_bindings/system_proxy_service.pb.h" |
| 14 | |
| 15 | namespace system_proxy { |
| 16 | namespace { |
| 17 | // Serializes |proto| to a vector of bytes. |
| 18 | std::vector<uint8_t> SerializeProto( |
| 19 | const google::protobuf::MessageLite& proto) { |
| 20 | std::vector<uint8_t> proto_blob(proto.ByteSizeLong()); |
| 21 | DCHECK(proto.SerializeToArray(proto_blob.data(), proto_blob.size())); |
| 22 | return proto_blob; |
| 23 | } |
| 24 | |
| 25 | // Parses a proto from an array of bytes |proto_blob|. Returns |
| 26 | // ERROR_PARSE_REQUEST_FAILED on error. |
| 27 | std::string DeserializeProto(const base::Location& from_here, |
| 28 | google::protobuf::MessageLite* proto, |
| 29 | const std::vector<uint8_t>& proto_blob) { |
| 30 | if (!proto->ParseFromArray(proto_blob.data(), proto_blob.size())) { |
| 31 | const std::string error_message = "Failed to parse proto message."; |
| 32 | LOG(ERROR) << from_here.ToString() << error_message; |
| 33 | return error_message; |
| 34 | } |
| 35 | return ""; |
| 36 | } |
| 37 | } // namespace |
| 38 | |
| 39 | SystemProxyAdaptor::SystemProxyAdaptor( |
| 40 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object) |
| 41 | : org::chromium::SystemProxyAdaptor(this), |
| 42 | dbus_object_(std::move(dbus_object)) {} |
| 43 | |
| 44 | SystemProxyAdaptor::~SystemProxyAdaptor() = default; |
| 45 | |
| 46 | void SystemProxyAdaptor::RegisterAsync( |
| 47 | const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& |
| 48 | completion_callback) { |
| 49 | RegisterWithDBusObject(dbus_object_.get()); |
| 50 | dbus_object_->RegisterAsync(completion_callback); |
| 51 | } |
| 52 | |
| 53 | std::vector<uint8_t> SystemProxyAdaptor::SetSystemTrafficCredentials( |
| 54 | const std::vector<uint8_t>& request_blob) { |
| 55 | LOG(INFO) << "Received set credentials request."; |
| 56 | SetSystemTrafficCredentialsRequest request; |
| 57 | const std::string error_message = |
| 58 | DeserializeProto(FROM_HERE, &request, request_blob); |
| 59 | SetSystemTrafficCredentialsResponse response; |
| 60 | if (!error_message.empty()) |
| 61 | response.set_error_message(error_message); |
| 62 | return SerializeProto(response); |
| 63 | } |
| 64 | |
| 65 | std::vector<uint8_t> SystemProxyAdaptor::ShutDown() { |
| 66 | LOG(INFO) << "Received shutdown request."; |
| 67 | ShutDownResponse response; |
| 68 | return SerializeProto(response); |
| 69 | } |
| 70 | |
| 71 | } // namespace system_proxy |