Commit 4990e61d authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

[DevTools] Add protocol::content::Binary to protocol_string{.h,.cc}.

This is in preparation of referencing this type from the code
generated by third_party/inspector_protocol/code_generator.py.

My overall WIP change is in here, and happy to explain more:
https://chromium-review.googlesource.com/c/chromium/src/+/1244719

Bug: chromium:891377
Change-Id: I66f75e2b53f30944fecbfdc22c396799c15593b8
Reviewed-on: https://chromium-review.googlesource.com/c/1280555Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#600978}
parent b1370902
......@@ -4,6 +4,8 @@
#include "content/browser/devtools/protocol_string.h"
#include <utility>
#include "base/base64.h"
#include "base/json/json_reader.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string16.h"
......@@ -164,5 +166,55 @@ void StringBuilder::reserveCapacity(size_t capacity) {
string_.reserve(capacity);
}
Binary::Binary() : bytes_(new base::RefCountedBytes) {}
Binary::Binary(const Binary& binary) : bytes_(binary.bytes_) {}
Binary::Binary(scoped_refptr<base::RefCountedMemory> bytes) : bytes_(bytes) {}
Binary::~Binary() {}
String Binary::toBase64() const {
std::string encoded;
base::Base64Encode(
base::StringPiece(reinterpret_cast<const char*>(bytes_->front()),
bytes_->size()),
&encoded);
return encoded;
}
// static
Binary Binary::fromBase64(const String& base64, bool* success) {
std::string decoded;
*success = base::Base64Decode(base::StringPiece(base64), &decoded);
if (*success) {
return Binary::fromString(std::move(decoded));
}
return Binary();
}
// static
Binary Binary::fromRefCounted(scoped_refptr<base::RefCountedMemory> memory) {
return Binary(memory);
}
// static
Binary Binary::fromVector(std::vector<uint8_t>&& data) {
return Binary(base::RefCountedBytes::TakeVector(&data));
}
// static
Binary Binary::fromVector(const std::vector<uint8_t>& data) {
std::vector<uint8_t> copied_data(data);
return Binary(base::RefCountedBytes::TakeVector(&copied_data));
}
// static
Binary Binary::fromString(std::string&& data) {
return Binary(base::RefCountedString::TakeString(&data));
}
// static
Binary Binary::fromString(const std::string& data) {
std::string copied_data(data);
return Binary(base::RefCountedString::TakeString(&copied_data));
}
} // namespace protocol
} // namespace content
......@@ -2,14 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_DEVTOOLS_PROTOCOL_STRING_H
#define CONTENT_BROWSER_DEVTOOLS_PROTOCOL_STRING_H
#ifndef CONTENT_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_
#define CONTENT_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_
#include <memory>
#include <string>
#include <vector>
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_number_conversions.h"
#include "content/common/content_export.h"
......@@ -85,6 +87,30 @@ class CONTENT_EXPORT StringUtil {
static std::unique_ptr<protocol::Value> parseJSON(const String&);
};
// A read-only sequence of uninterpreted bytes with reference-counted storage.
class CONTENT_EXPORT Binary {
public:
Binary(const Binary&);
Binary();
~Binary();
const uint8_t* data() const { return bytes_->front(); }
size_t size() const { return bytes_->size(); }
String toBase64() const;
static Binary fromBase64(const String& base64, bool* success);
static Binary fromRefCounted(scoped_refptr<base::RefCountedMemory> memory);
static Binary fromVector(std::vector<uint8_t>&& data);
static Binary fromVector(const std::vector<uint8_t>& data);
static Binary fromString(std::string&& data);
static Binary fromString(const std::string& data);
private:
explicit Binary(scoped_refptr<base::RefCountedMemory> bytes);
scoped_refptr<base::RefCountedMemory> bytes_;
};
std::unique_ptr<protocol::Value> toProtocolValue(
const base::Value* value, int depth);
std::unique_ptr<base::Value> toBaseValue(protocol::Value* value, int depth);
......@@ -92,4 +118,4 @@ std::unique_ptr<base::Value> toBaseValue(protocol::Value* value, int depth);
} // namespace protocol
} // namespace content
#endif // CONTENT_BROWSER_DEVTOOLS_PROTOCOL_STRING_H
#endif // CONTENT_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/devtools/protocol_string.h"
#include <vector>
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace protocol {
namespace {
TEST(ProtocolBinaryTest, base64EmptyArgs) {
EXPECT_EQ(protocol::String(), Binary().toBase64());
bool success = false;
Binary decoded = Binary::fromBase64("", &success);
EXPECT_TRUE(success);
EXPECT_EQ(
std::vector<uint8_t>(),
std::vector<uint8_t>(decoded.data(), decoded.data() + decoded.size()));
}
TEST(ProtocolStringTest, AllBytesBase64Roundtrip) {
std::vector<uint8_t> all_bytes;
for (int ii = 0; ii < 255; ++ii)
all_bytes.push_back(ii);
Binary binary = Binary::fromVector(all_bytes);
bool success = false;
Binary decoded = Binary::fromBase64(binary.toBase64(), &success);
EXPECT_TRUE(success);
std::vector<uint8_t> decoded_bytes(decoded.data(),
decoded.data() + decoded.size());
EXPECT_EQ(all_bytes, decoded_bytes);
}
TEST(ProtocolStringTest, HelloWorldBase64Roundtrip) {
const char* kMsg = "Hello, world.";
std::vector<uint8_t> msg(kMsg, kMsg + strlen(kMsg));
EXPECT_EQ(strlen(kMsg), msg.size());
protocol::String encoded = Binary::fromVector(msg).toBase64();
EXPECT_EQ("SGVsbG8sIHdvcmxkLg==", encoded);
bool success = false;
Binary decoded_binary = Binary::fromBase64(encoded, &success);
EXPECT_TRUE(success);
std::vector<uint8_t> decoded(decoded_binary.data(),
decoded_binary.data() + decoded_binary.size());
EXPECT_EQ(msg, decoded);
}
TEST(ProtocolBinaryTest, InvalidBase64Decode) {
bool success = true;
Binary binary = Binary::fromBase64("This is not base64.", &success);
EXPECT_FALSE(success);
}
} // namespace
} // namespace protocol
} // namespace content
......@@ -1348,6 +1348,7 @@ test("content_unittests") {
"../browser/devtools/devtools_manager_unittest.cc",
"../browser/devtools/devtools_video_consumer_unittest.cc",
"../browser/devtools/protocol/tracing_handler_unittest.cc",
"../browser/devtools/protocol_unittest.cc",
"../browser/dom_storage/dom_storage_area_unittest.cc",
"../browser/dom_storage/dom_storage_context_impl_unittest.cc",
"../browser/dom_storage/dom_storage_database_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment