Commit 35ba7448 authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

[DevTools] Add blink::protocol::Binary to v8_inspector_string{.cc,.h}.

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: I74836b74df4a3009670d8193c6b6bbd179c22fb8
Reviewed-on: https://chromium-review.googlesource.com/c/1280210Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#602062}
parent b1299b5f
......@@ -1974,6 +1974,7 @@ jumbo_source_set("unit_tests") {
"inspector/inspector_session_state_test.cc",
"inspector/main_thread_debugger_test.cc",
"inspector/protocol_parser_test.cc",
"inspector/protocol_unittest.cc",
"intersection_observer/intersection_observer_test.cc",
"layout/api/selection_state_test.cc",
"layout/collapsed_border_value_test.cc",
......
// 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 "third_party/blink/renderer/core/inspector/v8_inspector_string.h"
#include <vector>
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
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) {
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);
Vector<uint8_t> decoded_bytes;
decoded_bytes.Append(decoded.data(), decoded.size());
EXPECT_EQ(all_bytes, decoded_bytes);
}
TEST(ProtocolStringTest, HelloWorldBase64Roundtrip) {
const char* kMsg = "Hello, world.";
Vector<uint8_t> msg;
msg.Append(reinterpret_cast<const uint8_t*>(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);
Vector<uint8_t> decoded;
decoded.Append(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 blink
......@@ -4,7 +4,9 @@
#include "third_party/blink/renderer/core/inspector/v8_inspector_string.h"
#include <utility>
#include "third_party/blink/renderer/core/inspector/protocol/Protocol.h"
#include "third_party/blink/renderer/platform/wtf/text/base64.h"
namespace blink {
......@@ -74,6 +76,84 @@ void StringUtil::builderAppendQuotedString(StringBuilder& builder,
builder.Append('"');
}
} // namespace protocol
namespace {
class BinaryBasedOnSharedBuffer : public Binary::Impl {
public:
explicit BinaryBasedOnSharedBuffer(scoped_refptr<SharedBuffer> buffer)
: buffer_(buffer) {}
const uint8_t* data() const override {
return reinterpret_cast<const uint8_t*>(buffer_->Data());
}
size_t size() const override { return buffer_->size(); }
private:
// buffer_ is mutable so we can call SharedBuffer::Data(),
// which flattens the segments of the buffer.
mutable scoped_refptr<SharedBuffer> buffer_;
};
class BinaryBasedOnVector : public Binary::Impl {
public:
explicit BinaryBasedOnVector(Vector<uint8_t> values)
: values_(std::move(values)) {}
const uint8_t* data() const override { return values_.data(); }
size_t size() const override { return values_.size(); }
private:
Vector<uint8_t> values_;
};
class BinaryBasedOnCachedData : public Binary::Impl {
public:
explicit BinaryBasedOnCachedData(
std::unique_ptr<v8::ScriptCompiler::CachedData> data)
: data_(std::move(data)) {}
const uint8_t* data() const override { return data_->data; }
size_t size() const override { return data_->length; }
private:
std::unique_ptr<v8::ScriptCompiler::CachedData> data_;
};
} // namespace
String Binary::toBase64() const {
return impl_ ? WTF::Base64Encode(reinterpret_cast<const char*>(impl_->data()),
impl_->size())
: String();
}
// static
Binary Binary::fromBase64(const String& base64, bool* success) {
Vector<char> out;
*success = WTF::Base64Decode(base64, out);
return Binary(base::AdoptRef(
new BinaryBasedOnSharedBuffer(SharedBuffer::AdoptVector(out))));
}
// static
Binary Binary::fromSharedBuffer(scoped_refptr<SharedBuffer> buffer) {
return Binary(base::AdoptRef(new BinaryBasedOnSharedBuffer(buffer)));
}
// static
Binary Binary::fromVector(Vector<uint8_t>&& in) {
return Binary(base::AdoptRef(new BinaryBasedOnVector(std::move(in))));
}
// static
Binary Binary::fromVector(const Vector<uint8_t>& in) {
return Binary(base::AdoptRef(new BinaryBasedOnVector(in)));
}
// static
Binary Binary::fromCachedData(
std::unique_ptr<v8::ScriptCompiler::CachedData> data) {
CHECK_EQ(data->buffer_policy, v8::ScriptCompiler::CachedData::BufferOwned);
return Binary(base::AdoptRef(new BinaryBasedOnCachedData(std::move(data))));
}
} // namespace protocol
} // namespace blink
......@@ -8,9 +8,11 @@
#include <memory>
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/shared_buffer.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"
#include "third_party/blink/renderer/platform/wtf/decimal.h"
#include "third_party/blink/renderer/platform/wtf/ref_counted.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
#include "third_party/blink/renderer/platform/wtf/text/string_to_number.h"
......@@ -76,6 +78,37 @@ class CORE_EXPORT StringUtil {
static std::unique_ptr<protocol::Value> parseJSON(const String&);
};
// A read-only sequence of uninterpreted bytes with reference-counted storage.
class CORE_EXPORT Binary {
public:
class Impl : public RefCounted<Impl> {
public:
Impl() = default;
virtual ~Impl() = default;
virtual const uint8_t* data() const = 0;
virtual size_t size() const = 0;
};
Binary() = default;
const uint8_t* data() const { return impl_->data(); }
size_t size() const { return impl_->size(); }
String toBase64() const;
static Binary fromBase64(const String& base64, bool* success);
static Binary fromSharedBuffer(scoped_refptr<SharedBuffer> buffer);
static Binary fromVector(Vector<uint8_t>&& in);
static Binary fromVector(const Vector<uint8_t>& in);
// Note: |data.buffer_policy| must be
// ScriptCompiler::ScriptCompiler::CachedData::BufferOwned.
static Binary fromCachedData(
std::unique_ptr<v8::ScriptCompiler::CachedData> data);
private:
explicit Binary(scoped_refptr<Impl> impl) : impl_(impl) {}
scoped_refptr<Impl> impl_;
};
} // namespace protocol
} // namespace blink
......
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