Commit c3d46ef6 authored by Dmitry Gozman's avatar Dmitry Gozman Committed by Commit Bot

Introduce mojo_base.mojom.BigString

BigString uses BigBuffer internally to accomodate
large strings.

It maps to std::string and WTF::String and internally
converts data to UTF8 similary to regular string.

Bug: 776009
Change-Id: I05547f6f55f0a20dafd6a3d99e26b5cc2cbe2eed
Reviewed-on: https://chromium-review.googlesource.com/891980
Commit-Queue: Dmitry Gozman <dgozman@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@chromium.org>
Cr-Commit-Position: refs/heads/master@{#533182}
parent c81c0cc3
......@@ -261,6 +261,7 @@ jumbo_source_set("common_sources") {
"//ipc",
"//media/capture:capture_base",
"//mojo/edk/system",
"//mojo/public/cpp/base:mojom_traits",
"//mojo/public/cpp/bindings",
"//net",
"//services/network/public/cpp",
......
......@@ -41,15 +41,29 @@ component("shared_typemap_traits") {
]
}
source_set("mojom_traits") {
sources = [
"big_string_mojom_traits.cc",
"big_string_mojom_traits.h",
]
deps = [
":base",
":shared_typemap_traits",
"//mojo/public/mojom/base",
]
}
source_set("tests") {
testonly = true
sources = [
"big_buffer_unittest.cc",
"big_string_unittest.cc",
]
public_deps = [
":base",
":mojom_traits",
":shared_typemap_traits",
"//base",
"//mojo/public/cpp/test_support:test_utils",
......
per-file *.typemap=set noparent
per-file *.typemap=file://ipc/SECURITY_OWNERS
per-file *_mojom_traits*.*=set noparent
per-file *_mojom_traits*.*=file://ipc/SECURITY_OWNERS
per-file *_struct_traits*.*=set noparent
per-file *_struct_traits*.*=file://ipc/SECURITY_OWNERS
# 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.
mojom = "//mojo/public/mojom/base/big_string.mojom"
public_headers = []
traits_headers = [ "//mojo/public/cpp/base/big_string_mojom_traits.h" ]
public_deps = [
"//mojo/public/cpp/base",
"//mojo/public/cpp/base:shared_typemap_traits",
]
type_mappings = [ "mojo_base.mojom.BigString=std::string" ]
// 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 "mojo/public/cpp/base/big_string_mojom_traits.h"
#include "mojo/public/cpp/base/big_buffer_struct_traits.h"
namespace mojo {
// static
mojo_base::BigBuffer StructTraits<mojo_base::mojom::BigStringDataView,
std::string>::data(const std::string& str) {
const auto* bytes = reinterpret_cast<const uint8_t*>(str.data());
return mojo_base::BigBuffer(
base::make_span(bytes, str.size() * sizeof(char)));
}
// static
bool StructTraits<mojo_base::mojom::BigStringDataView, std::string>::Read(
mojo_base::mojom::BigStringDataView data,
std::string* out) {
mojo_base::BigBuffer buffer;
if (!data.ReadData(&buffer))
return false;
if (buffer.size() % sizeof(char))
return false;
*out = std::string(reinterpret_cast<const char*>(buffer.data()),
buffer.size() / sizeof(char));
return true;
}
} // namespace mojo
// 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.
#ifndef MOJO_PUBLIC_CPP_BASE_BIG_STRING_MOJOM_TRAITS_H_
#define MOJO_PUBLIC_CPP_BASE_BIG_STRING_MOJOM_TRAITS_H_
#include <string>
#include "mojo/public/cpp/base/big_buffer.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
#include "mojo/public/mojom/base/big_string.mojom-shared.h"
namespace mojo {
template <>
struct StructTraits<mojo_base::mojom::BigStringDataView, std::string> {
static mojo_base::BigBuffer data(const std::string& str);
static bool Read(mojo_base::mojom::BigStringDataView data, std::string* out);
};
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_BASE_BIG_STRING_MOJOM_TRAITS_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 <string>
#include "base/rand_util.h"
#include "mojo/public/cpp/base/big_buffer_struct_traits.h"
#include "mojo/public/cpp/base/big_string_mojom_traits.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "mojo/public/mojom/base/big_string.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo_base {
namespace big_string_unittest {
TEST(BigStringTest, Empty) {
std::string in;
std::string out;
ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::BigString>(&in, &out));
EXPECT_EQ(in, out);
}
TEST(BigStringTest, Short) {
std::string in("hello world");
std::string out;
ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::BigString>(&in, &out));
EXPECT_EQ(in, out);
}
TEST(BigStringTest, Long) {
constexpr size_t kLargeStringSize = 1024 * 1024;
std::string in(kLargeStringSize, 0);
base::RandBytes(&in[0], kLargeStringSize);
std::string out;
ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::BigString>(&in, &out));
EXPECT_EQ(in, out);
}
} // namespace big_string_unittest
} // namespace mojo_base
......@@ -2,4 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
typemaps = [ "//mojo/public/cpp/base/big_buffer.typemap" ]
typemaps = [
"//mojo/public/cpp/base/big_buffer.typemap",
"//mojo/public/cpp/base/big_string.typemap",
]
......@@ -7,6 +7,7 @@ import("//mojo/public/tools/bindings/mojom.gni")
mojom_component("base") {
sources = [
"big_buffer.mojom",
"big_string.mojom",
]
output_prefix = "mojo_base_mojom"
......
// 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.
module mojo_base.mojom;
import "mojo/public/mojom/base/big_buffer.mojom";
// Corresponds to |std::string| in <string>.
// Corresponds to |WTF::String| in
// third_party/WebKit/Source/platform/wtf/text/WTFString.h.
// This type should be used in place of string whenever the contents of the
// string may realistically exceed 64 kB in size. This string is safe to store
// an arbitrarily large amount of data (available memory permitting) without
// negatively impacting IPC performance or hitting hard message size
// boundaries.
struct BigString {
mojo_base.mojom.BigBuffer data;
};
......@@ -1312,6 +1312,8 @@ jumbo_component("platform") {
"mhtml/MHTMLArchive.h",
"mhtml/MHTMLParser.cpp",
"mhtml/MHTMLParser.h",
"mojo/BigStringMojomTraits.cpp",
"mojo/BigStringMojomTraits.h",
"mojo/BluetoothStructTraits.cpp",
"mojo/BluetoothStructTraits.h",
"mojo/CommonCustomTypesStructTraits.cpp",
......@@ -1909,6 +1911,7 @@ jumbo_source_set("blink_platform_unittests_sources") {
"json/JSONValuesTest.cpp",
"mac/GraphicsContextCanvasTest.mm",
"mac/VersionUtilMacTest.mm",
"mojo/BigStringMojomTraitsTest.cpp",
"mojo/CommonCustomTypesStructTraitsTest.cpp",
"mojo/GeometryStructTraitsTest.cpp",
"mojo/InterfaceInvalidatorTest.cpp",
......
# 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.
mojom = "//mojo/public/mojom/base/big_string.mojom"
public_headers = [ "//third_party/WebKit/Source/platform/wtf/text/WTFString.h" ]
traits_headers =
[ "//third_party/WebKit/Source/platform/mojo/BigStringMojomTraits.h" ]
type_mappings =
[ "mojo_base.mojom.BigString=WTF::String[nullable_is_same_type]" ]
// Copyright 2016 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 "platform/mojo/BigStringMojomTraits.h"
#include <cstring>
#include "base/containers/span.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "mojo/public/cpp/base/big_buffer_struct_traits.h"
#include "platform/wtf/text/StringUTF8Adaptor.h"
namespace mojo {
// static
mojo_base::BigBuffer StructTraits<mojo_base::mojom::BigStringDataView,
WTF::String>::data(const WTF::String& input) {
WTF::StringUTF8Adaptor adaptor(input);
return mojo_base::BigBuffer(
base::make_span(reinterpret_cast<const uint8_t*>(adaptor.Data()),
adaptor.length() * sizeof(char)));
}
// static
bool StructTraits<mojo_base::mojom::BigStringDataView, WTF::String>::Read(
mojo_base::mojom::BigStringDataView data,
WTF::String* out) {
mojo_base::BigBuffer buffer;
if (!data.ReadData(&buffer))
return false;
size_t size = buffer.size();
if (size % sizeof(char))
return false;
// An empty |mojo_base::BigBuffer| may have a null |data()| if empty.
if (!size) {
*out = g_empty_string;
} else {
*out = WTF::String::FromUTF8(reinterpret_cast<const char*>(buffer.data()),
size / sizeof(char));
}
return true;
}
} // namespace mojo
// 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.
#ifndef BigStringMojomTraits_h
#define BigStringMojomTraits_h
#include "mojo/public/cpp/bindings/struct_traits.h"
#include "mojo/public/mojom/base/big_string.mojom-blink.h"
#include "platform/PlatformExport.h"
namespace mojo_base {
class BigBuffer;
}
namespace mojo {
template <>
struct PLATFORM_EXPORT
StructTraits<mojo_base::mojom::BigStringDataView, WTF::String> {
static bool IsNull(const WTF::String& input) { return input.IsNull(); }
static void SetToNull(WTF::String* output) { *output = WTF::String(); }
static mojo_base::BigBuffer data(const WTF::String& input);
static bool Read(mojo_base::mojom::BigStringDataView, WTF::String* out);
};
} // namespace mojo
#endif // BigStringMojomTraits_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 "base/rand_util.h"
#include "mojo/common/test_common_custom_types.mojom-blink.h"
#include "mojo/public/cpp/base/big_buffer_struct_traits.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "mojo/public/mojom/base/big_string.mojom-blink.h"
#include "platform/wtf/text/WTFString.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/platform/mojo/BigStringMojomTraits.h"
#include "third_party/WebKit/Source/platform/wtf/text/WTFString.h"
namespace blink {
TEST(BigStringMojomTraitsTest, BigString_Null) {
String str;
String output;
ASSERT_TRUE(
mojo::test::SerializeAndDeserialize<mojo_base::mojom::blink::BigString>(
&str, &output));
ASSERT_EQ(str, output);
}
TEST(BigStringMojomTraitsTest, BigString_Empty) {
String str = String::FromUTF8("");
String output;
ASSERT_TRUE(
mojo::test::SerializeAndDeserialize<mojo_base::mojom::blink::BigString>(
&str, &output));
ASSERT_EQ(str, output);
}
TEST(BigStringMojomTraitsTest, BigString_Short) {
String str = String::FromUTF8("hello world");
ASSERT_TRUE(str.Is8Bit());
String output;
ASSERT_TRUE(
mojo::test::SerializeAndDeserialize<mojo_base::mojom::blink::BigString>(
&str, &output));
ASSERT_EQ(str, output);
// Replace the "o"s in "hello world" with "o"s with acute, so that |str| is
// 16-bit.
str = String::FromUTF8("hell\xC3\xB3 w\xC3\xB3rld");
ASSERT_FALSE(str.Is8Bit());
ASSERT_TRUE(
mojo::test::SerializeAndDeserialize<mojo_base::mojom::blink::BigString>(
&str, &output));
ASSERT_EQ(str, output);
}
TEST(BigStringMojomTraitsTest, BigString_Long) {
WTF::Vector<char> random_latin1_string(1024 * 1024);
base::RandBytes(random_latin1_string.data(), random_latin1_string.size());
String str(random_latin1_string.data(), random_latin1_string.size());
String output;
ASSERT_TRUE(
mojo::test::SerializeAndDeserialize<mojo_base::mojom::blink::BigString>(
&str, &output));
ASSERT_EQ(str, output);
}
} // namespace blink
......@@ -7,6 +7,7 @@ include_rules = [
"+base/observer_list.h",
"+base/strings/string16.h",
"+mojo/public/cpp/bindings/binding.h",
"+mojo/common/big_string.mojom-blink.h",
"+mojo/common/string16.mojom-blink.h",
"+mojo/common/test_common_custom_types.mojom-blink.h",
"+services/network/public/interfaces/fetch_api.mojom-blink.h",
......
per-file *MojomTraits*.*=set noparent
per-file *MojomTraits*.*=file://ipc/SECURITY_OWNERS
per-file *StructTraits*.*=set noparent
per-file *StructTraits*.*=file://ipc/SECURITY_OWNERS
per-file *.typemap=set noparent
......
......@@ -9,6 +9,7 @@ typemaps = [
"//third_party/WebKit/Source/core/messaging/BlinkTransferableMessage.typemap",
"//third_party/WebKit/Source/platform/blob/SerializedBlob.typemap",
"//third_party/WebKit/Source/platform/mojo/BigBuffer.typemap",
"//third_party/WebKit/Source/platform/mojo/BigString.typemap",
"//third_party/WebKit/Source/platform/mojo/File.typemap",
"//third_party/WebKit/Source/platform/mojo/Geometry.typemap",
"//third_party/WebKit/Source/platform/mojo/KURL.typemap",
......
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