Johannes Henkel | 11e6cb3 | 2019-04-24 22:32:12 | [diff] [blame] | 1 | // Copyright 2019 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 "content/browser/devtools/devtools_protocol_encoding.h" |
| 6 | |
| 7 | #include "base/strings/string_number_conversions.h" |
| 8 | |
| 9 | namespace content { |
| 10 | namespace { |
| 11 | using ::inspector_protocol_encoding::span; |
Johannes Henkel | 11e6cb3 | 2019-04-24 22:32:12 | [diff] [blame] | 12 | using ::inspector_protocol_encoding::json::ConvertCBORToJSON; |
| 13 | using ::inspector_protocol_encoding::json::ConvertJSONToCBOR; |
Johannes Henkel | 8128446e | 2019-04-30 01:03:16 | [diff] [blame] | 14 | using IPEStatus = ::inspector_protocol_encoding::Status; |
Johannes Henkel | 11e6cb3 | 2019-04-24 22:32:12 | [diff] [blame] | 15 | |
| 16 | // Platform allows us to inject the string<->double conversion |
| 17 | // routines from base:: into the inspector_protocol JSON parser / serializer. |
| 18 | class Platform : public ::inspector_protocol_encoding::json::Platform { |
| 19 | public: |
| 20 | bool StrToD(const char* str, double* result) const override { |
| 21 | return base::StringToDouble(str, result); |
| 22 | } |
| 23 | |
| 24 | // Prints |value| in a format suitable for JSON. |
| 25 | std::unique_ptr<char[]> DToStr(double value) const override { |
| 26 | std::string str = base::NumberToString(value); |
| 27 | std::unique_ptr<char[]> result(new char[str.size() + 1]); |
| 28 | memcpy(result.get(), str.c_str(), str.size() + 1); |
| 29 | return result; |
| 30 | } |
| 31 | }; |
| 32 | } // namespace |
| 33 | |
Johannes Henkel | 8128446e | 2019-04-30 01:03:16 | [diff] [blame] | 34 | IPEStatus ConvertCBORToJSON(span<uint8_t> cbor, std::string* json) { |
Johannes Henkel | 11e6cb3 | 2019-04-24 22:32:12 | [diff] [blame] | 35 | Platform platform; |
| 36 | return ConvertCBORToJSON(platform, cbor, json); |
| 37 | } |
| 38 | |
Johannes Henkel | 8128446e | 2019-04-30 01:03:16 | [diff] [blame] | 39 | IPEStatus ConvertJSONToCBOR(span<uint8_t> json, std::string* cbor) { |
Johannes Henkel | 11e6cb3 | 2019-04-24 22:32:12 | [diff] [blame] | 40 | Platform platform; |
| 41 | return ConvertJSONToCBOR(platform, json, cbor); |
| 42 | } |
| 43 | |
Johannes Henkel | 8128446e | 2019-04-30 01:03:16 | [diff] [blame] | 44 | IPEStatus ConvertJSONToCBOR(span<uint8_t> json, std::vector<uint8_t>* cbor) { |
Johannes Henkel | 11e6cb3 | 2019-04-24 22:32:12 | [diff] [blame] | 45 | Platform platform; |
| 46 | return ConvertJSONToCBOR(platform, json, cbor); |
| 47 | } |
| 48 | } // namespace content |