Alex Vakulenko | b381365 | 2014-08-27 08:04:22 -0700 | [diff] [blame] | 1 | // Copyright 2014 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 | |
| 5 | #include <chromeos/dbus/utils.h> |
| 6 | |
Alex Vakulenko | 00ceab8 | 2014-11-04 14:21:59 -0800 | [diff] [blame] | 7 | #include <tuple> |
| 8 | #include <vector> |
| 9 | |
Alex Vakulenko | b381365 | 2014-08-27 08:04:22 -0700 | [diff] [blame] | 10 | #include <base/bind.h> |
| 11 | #include <base/memory/scoped_ptr.h> |
Alex Vakulenko | 1510466 | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 12 | #include <chromeos/errors/error_codes.h> |
Alex Vakulenko | 00ceab8 | 2014-11-04 14:21:59 -0800 | [diff] [blame] | 13 | #include <chromeos/strings/string_utils.h> |
Alex Vakulenko | b381365 | 2014-08-27 08:04:22 -0700 | [diff] [blame] | 14 | |
| 15 | namespace chromeos { |
| 16 | namespace dbus_utils { |
| 17 | |
Alex Vakulenko | b381365 | 2014-08-27 08:04:22 -0700 | [diff] [blame] | 18 | std::unique_ptr<dbus::Response> CreateDBusErrorResponse( |
| 19 | dbus::MethodCall* method_call, |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 20 | const std::string& error_name, |
| 21 | const std::string& error_message) { |
| 22 | auto resp = dbus::ErrorResponse::FromMethodCall(method_call, error_name, |
| 23 | error_message); |
Alex Vakulenko | b381365 | 2014-08-27 08:04:22 -0700 | [diff] [blame] | 24 | return std::unique_ptr<dbus::Response>(resp.release()); |
| 25 | } |
| 26 | |
| 27 | std::unique_ptr<dbus::Response> GetDBusError(dbus::MethodCall* method_call, |
| 28 | const chromeos::Error* error) { |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 29 | CHECK(error) << "Error object must be specified"; |
| 30 | std::string error_name = DBUS_ERROR_FAILED; // Default error code. |
Alex Vakulenko | b381365 | 2014-08-27 08:04:22 -0700 | [diff] [blame] | 31 | std::string error_message; |
| 32 | |
| 33 | // Special case for "dbus" error domain. |
| 34 | // Pop the error code and message from the error chain and use them as the |
| 35 | // actual D-Bus error message. |
| 36 | if (error->GetDomain() == errors::dbus::kDomain) { |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 37 | error_name = error->GetCode(); |
Alex Vakulenko | b381365 | 2014-08-27 08:04:22 -0700 | [diff] [blame] | 38 | error_message = error->GetMessage(); |
| 39 | error = error->GetInnerError(); |
| 40 | } |
| 41 | |
| 42 | // Append any inner errors to the error message. |
| 43 | while (error) { |
| 44 | // Format error string as "domain/code:message". |
| 45 | if (!error_message.empty()) |
| 46 | error_message += ';'; |
| 47 | error_message += error->GetDomain() + '/' + error->GetCode() + ':' + |
| 48 | error->GetMessage(); |
| 49 | error = error->GetInnerError(); |
| 50 | } |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 51 | return CreateDBusErrorResponse(method_call, error_name, error_message); |
Alex Vakulenko | b381365 | 2014-08-27 08:04:22 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Alex Vakulenko | 00ceab8 | 2014-11-04 14:21:59 -0800 | [diff] [blame] | 54 | void AddDBusError(chromeos::ErrorPtr* error, |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 55 | const std::string& dbus_error_name, |
Alex Vakulenko | 00ceab8 | 2014-11-04 14:21:59 -0800 | [diff] [blame] | 56 | const std::string& dbus_error_message) { |
| 57 | std::vector<std::string> parts = string_utils::Split(dbus_error_message, ';'); |
| 58 | std::vector<std::tuple<std::string, std::string, std::string>> errors; |
| 59 | for (const std::string& part : parts) { |
| 60 | // Each part should be in format of "domain/code:message" |
| 61 | size_t slash_pos = part.find('/'); |
| 62 | size_t colon_pos = part.find(':'); |
| 63 | if (slash_pos != std::string::npos && |
| 64 | colon_pos != std::string::npos && |
| 65 | slash_pos < colon_pos) { |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 66 | // If we have both '/' and ':' and in proper order, then we have a |
Alex Vakulenko | 00ceab8 | 2014-11-04 14:21:59 -0800 | [diff] [blame] | 67 | // correctly encoded error object. |
| 68 | std::string domain = part.substr(0, slash_pos); |
| 69 | std::string code = part.substr(slash_pos + 1, colon_pos - slash_pos - 1); |
| 70 | std::string message = part.substr(colon_pos + 1); |
| 71 | errors.emplace_back(domain, code, message); |
| 72 | } else if (slash_pos == std::string::npos && |
| 73 | colon_pos == std::string::npos && |
| 74 | errors.empty()) { |
| 75 | // If we don't have both '/' and ':' and this is the first error object, |
| 76 | // then we had a D-Bus error at the top of the error chain. |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 77 | errors.emplace_back(errors::dbus::kDomain, dbus_error_name, part); |
Alex Vakulenko | 00ceab8 | 2014-11-04 14:21:59 -0800 | [diff] [blame] | 78 | } else { |
| 79 | // We have a malformed part. The whole D-Bus error was most likely |
| 80 | // not generated by GetDBusError(). To be safe, stop parsing it |
| 81 | // and return the error as received from D-Bus. |
| 82 | errors.clear(); // Remove any errors accumulated so far. |
| 83 | errors.emplace_back(errors::dbus::kDomain, |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 84 | dbus_error_name, |
Alex Vakulenko | 00ceab8 | 2014-11-04 14:21:59 -0800 | [diff] [blame] | 85 | dbus_error_message); |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Go backwards and add the parsed errors to the error chain. |
| 91 | for (auto it = errors.crbegin(); it != errors.crend(); ++it) { |
Alex Vakulenko | 8f815f5 | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 92 | Error::AddTo(error, FROM_HERE, |
| 93 | std::get<0>(*it), std::get<1>(*it), std::get<2>(*it)); |
Alex Vakulenko | 00ceab8 | 2014-11-04 14:21:59 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Alex Vakulenko | b381365 | 2014-08-27 08:04:22 -0700 | [diff] [blame] | 97 | } // namespace dbus_utils |
| 98 | } // namespace chromeos |