Commit 261ab646 authored by Elliot Glaysher's avatar Elliot Glaysher Committed by Commit Bot

Add mojo support for base::RefCountedMemory.

RefCountedMemory is an arbitrary wrapper around memory. It is used
extensively in code which passes encoded PNG files around, such as the
history system and the screenshotting code.

Bug: 706246
Change-Id: I9788aaba9dcfe38bd05418c4cc3a9e2737a5985c
Reviewed-on: https://chromium-review.googlesource.com/917198Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@chromium.org>
Commit-Queue: Elliot Glaysher <erg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#536914}
parent 858fc730
......@@ -46,6 +46,7 @@ source_set("tests") {
sources = [
"big_buffer_unittest.cc",
"big_string_unittest.cc",
"ref_counted_memory_unittest.cc",
"string16_unittest.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.
mojom = "//mojo/public/mojom/base/ref_counted_memory.mojom"
public_headers = [ "//base/memory/ref_counted_memory.h" ]
traits_headers = [ "//mojo/public/cpp/base/ref_counted_memory_mojom_traits.h" ]
sources = [
"//mojo/public/cpp/base/ref_counted_memory_mojom_traits.cc",
"//mojo/public/cpp/base/ref_counted_memory_mojom_traits.h",
]
public_deps = [
"//base",
"//mojo/public/cpp/base",
"//mojo/public/cpp/base:shared_typemap_traits",
]
type_mappings = [ "mojo.common.mojom.RefCountedMemory=scoped_refptr<base::RefCountedMemory>[copyable_pass_by_value,nullable_is_same_type]" ]
// 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/ref_counted_memory_mojom_traits.h"
#include "mojo/public/cpp/base/big_buffer_mojom_traits.h"
namespace mojo {
// static
mojo_base::BigBuffer StructTraits<mojo_base::mojom::RefCountedMemoryDataView,
scoped_refptr<base::RefCountedMemory>>::
data(const scoped_refptr<base::RefCountedMemory>& in) {
return mojo_base::BigBuffer(base::make_span(in->front(), in->size()));
}
// static
bool StructTraits<mojo_base::mojom::RefCountedMemoryDataView,
scoped_refptr<base::RefCountedMemory>>::
IsNull(const scoped_refptr<base::RefCountedMemory>& input) {
return !input.get();
}
// static
void StructTraits<mojo_base::mojom::RefCountedMemoryDataView,
scoped_refptr<base::RefCountedMemory>>::
SetToNull(scoped_refptr<base::RefCountedMemory>* out) {
*out = scoped_refptr<base::RefCountedMemory>();
}
// static
bool StructTraits<mojo_base::mojom::RefCountedMemoryDataView,
scoped_refptr<base::RefCountedMemory>>::
Read(mojo_base::mojom::RefCountedMemoryDataView data,
scoped_refptr<base::RefCountedMemory>* out) {
mojo_base::BigBuffer buffer;
if (!data.ReadData(&buffer))
return false;
*out =
base::MakeRefCounted<base::RefCountedBytes>(buffer.data(), buffer.size());
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_REF_COUNTED_MEMORY_MOJOM_TRAITS_H_
#define MOJO_PUBLIC_CPP_BASE_REF_COUNTED_MEMORY_MOJOM_TRAITS_H_
#include <string>
#include "base/component_export.h"
#include "base/memory/ref_counted_memory.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
#include "mojo/public/mojom/base/ref_counted_memory.mojom-shared.h"
namespace mojo {
template <>
struct COMPONENT_EXPORT(MOJO_BASE_MOJOM)
StructTraits<mojo_base::mojom::RefCountedMemoryDataView,
scoped_refptr<base::RefCountedMemory>> {
static mojo_base::BigBuffer data(
const scoped_refptr<base::RefCountedMemory>& in);
static bool IsNull(const scoped_refptr<base::RefCountedMemory>& input);
static void SetToNull(scoped_refptr<base::RefCountedMemory>* out);
static bool Read(mojo_base::mojom::RefCountedMemoryDataView data,
scoped_refptr<base::RefCountedMemory>* out);
};
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_BASE_REF_COUNTED_MEMORY_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 "mojo/public/cpp/base/big_buffer_mojom_traits.h"
#include "mojo/public/cpp/base/ref_counted_memory_mojom_traits.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "mojo/public/mojom/base/ref_counted_memory.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo_base {
TEST(RefCountedMemoryTest, Data) {
uint8_t data[] = {'a', 'b', 'c', 'd', 'e'};
scoped_refptr<base::RefCountedMemory> in =
new base::RefCountedStaticMemory(&data, arraysize(data));
scoped_refptr<base::RefCountedMemory> out;
ASSERT_TRUE(
mojo::test::SerializeAndDeserialize<mojom::RefCountedMemory>(&in, &out));
ASSERT_EQ(out->size(), in->size());
for (size_t i = 0; i < out->size(); ++i)
EXPECT_EQ(in->front()[i], out->front()[i]);
}
TEST(RefCountedMemoryTest, Null) {
// Stuff real data in out to ensure it gets overwritten with a null.
uint8_t data[] = {'a', 'b', 'c', 'd', 'e'};
scoped_refptr<base::RefCountedMemory> out =
new base::RefCountedStaticMemory(&data, arraysize(data));
scoped_refptr<base::RefCountedMemory> in;
ASSERT_TRUE(
mojo::test::SerializeAndDeserialize<mojom::RefCountedMemory>(&in, &out));
ASSERT_FALSE(out.get());
}
} // namespace mojo_base
......@@ -5,5 +5,6 @@
typemaps = [
"//mojo/public/cpp/base/big_buffer.typemap",
"//mojo/public/cpp/base/big_string.typemap",
"//mojo/public/cpp/base/ref_counted_memory.typemap",
"//mojo/public/cpp/base/string16.typemap",
]
......@@ -8,6 +8,7 @@ mojom_component("base") {
sources = [
"big_buffer.mojom",
"big_string.mojom",
"ref_counted_memory.mojom",
"string16.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";
// Chrome internally uses base::RefCountedMemory to pass data around. These
// data segments can be fairly large, to the point where we don't want them
// to be stuffed raw through the mojo ipc layer.
struct RefCountedMemory {
mojo_base.mojom.BigBuffer data;
};
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