diff options
Diffstat (limited to 'chromium/mojo/common')
-rw-r--r-- | chromium/mojo/common/BUILD.gn | 18 | ||||
-rw-r--r-- | chromium/mojo/common/OWNERS | 4 | ||||
-rw-r--r-- | chromium/mojo/common/common_custom_types.mojom | 23 | ||||
-rw-r--r-- | chromium/mojo/common/common_custom_types.typemap | 20 | ||||
-rw-r--r-- | chromium/mojo/common/common_custom_types_struct_traits.cc | 66 | ||||
-rw-r--r-- | chromium/mojo/common/common_custom_types_struct_traits.h | 71 | ||||
-rw-r--r-- | chromium/mojo/common/common_custom_types_unittest.cc | 108 | ||||
-rw-r--r-- | chromium/mojo/common/common_type_converters.h | 1 | ||||
-rw-r--r-- | chromium/mojo/common/struct_traits_unittest.cc | 57 | ||||
-rw-r--r-- | chromium/mojo/common/test_common_custom_types.mojom | 11 | ||||
-rw-r--r-- | chromium/mojo/common/traits_test_service.mojom | 14 |
11 files changed, 373 insertions, 20 deletions
diff --git a/chromium/mojo/common/BUILD.gn b/chromium/mojo/common/BUILD.gn index 2edfb6d5c83..278d9ee2fb2 100644 --- a/chromium/mojo/common/BUILD.gn +++ b/chromium/mojo/common/BUILD.gn @@ -12,14 +12,12 @@ group("common") { ] } -# GYP version: mojo/mojo_base.gyp:mojo_common_custom_types mojom("common_custom_types") { sources = [ "common_custom_types.mojom", ] } -# GYP version: mojo/mojo_base.gyp:mojo_common_lib component("common_base") { output_name = "mojo_common_lib" @@ -44,21 +42,21 @@ component("common_base") { ] } -# GYP version: mojo/mojo_base.gyp:mojo_test_common_custom_types mojom("test_common_custom_types") { sources = [ "test_common_custom_types.mojom", + "traits_test_service.mojom", ] public_deps = [ ":common_custom_types", ] } -# GYP version: mojo/mojo_base.gyp:mojo_common_unittests test("mojo_common_unittests") { deps = [ ":common", ":common_custom_types", + ":struct_traits", ":test_common_custom_types", "//base", "//base:message_loop_tests", @@ -74,6 +72,7 @@ test("mojo_common_unittests") { sources = [ "common_custom_types_unittest.cc", "common_type_converters_unittest.cc", + "struct_traits_unittest.cc", ] } @@ -86,3 +85,14 @@ test("mojo_common_perftests") { "//testing/gtest", ] } + +source_set("struct_traits") { + sources = [ + "common_custom_types_struct_traits.cc", + "common_custom_types_struct_traits.h", + ] + deps = [ + ":common_custom_types_shared_cpp_sources", + "//base:base", + ] +} diff --git a/chromium/mojo/common/OWNERS b/chromium/mojo/common/OWNERS index a1660982293..b567079e0a9 100644 --- a/chromium/mojo/common/OWNERS +++ b/chromium/mojo/common/OWNERS @@ -1,2 +1,6 @@ per-file *_type_converter*.*=set noparent per-file *_type_converter*.*=file://ipc/SECURITY_OWNERS +per-file *.mojom=set noparent +per-file *.mojom=file://ipc/SECURITY_OWNERS +per-file *_struct_traits*.*=set noparent +per-file *_struct_traits*.*=file://ipc/SECURITY_OWNERS diff --git a/chromium/mojo/common/common_custom_types.mojom b/chromium/mojo/common/common_custom_types.mojom index aa871064c42..203fa3c844d 100644 --- a/chromium/mojo/common/common_custom_types.mojom +++ b/chromium/mojo/common/common_custom_types.mojom @@ -16,8 +16,27 @@ struct DictionaryValue; [Native] struct Time; -[Native] -struct TimeDelta; +struct TimeDelta { + int64 microseconds; +}; [Native] struct TimeTicks; + +// Corresponds to |base::string16| in base/strings/string16.h +// Corresponds to |WTF::String| in +// third_party/WebKit/Source/wtf/text/WTFString.h. +struct String16 { + array<uint16> data; +}; + +// Corresponds to |base::UnguessableToken| in base/unguessable_token.h +struct UnguessableToken { + uint64 high; + uint64 low; +}; + +// Corresponds to |base::Version| in base/version.h +struct Version { + array<uint32> components; +}; diff --git a/chromium/mojo/common/common_custom_types.typemap b/chromium/mojo/common/common_custom_types.typemap index 8d88109d8c1..ecd09e87be1 100644 --- a/chromium/mojo/common/common_custom_types.typemap +++ b/chromium/mojo/common/common_custom_types.typemap @@ -5,19 +5,29 @@ mojom = "//mojo/common/common_custom_types.mojom" public_headers = [ "//base/files/file_path.h", - "//base/values.h", + "//base/strings/string16.h", "//base/time/time.h", + "//base/unguessable_token.h", + "//base/values.h", + "//base/version.h", +] +traits_headers = [ + "//ipc/ipc_message_utils.h", + "//mojo/common/common_custom_types_struct_traits.h", ] -traits_headers = [ "//ipc/ipc_message_utils.h" ] public_deps = [ "//ipc", + "//mojo/common:struct_traits", ] type_mappings = [ "mojo.common.mojom.FilePath=base::FilePath", "mojo.common.mojom.DictionaryValue=base::DictionaryValue", "mojo.common.mojom.ListValue=base::ListValue", - "mojo.common.mojom.Time=base::Time", - "mojo.common.mojom.TimeDelta=base::TimeDelta", - "mojo.common.mojom.TimeTicks=base::TimeTicks", + "mojo.common.mojom.UnguessableToken=base::UnguessableToken", + "mojo.common.mojom.String16=base::string16", + "mojo.common.mojom.Time=base::Time[copyable_pass_by_value]", + "mojo.common.mojom.TimeDelta=base::TimeDelta[copyable_pass_by_value]", + "mojo.common.mojom.TimeTicks=base::TimeTicks[copyable_pass_by_value]", + "mojo.common.mojom.Version=base::Version", ] diff --git a/chromium/mojo/common/common_custom_types_struct_traits.cc b/chromium/mojo/common/common_custom_types_struct_traits.cc new file mode 100644 index 00000000000..c18f793b793 --- /dev/null +++ b/chromium/mojo/common/common_custom_types_struct_traits.cc @@ -0,0 +1,66 @@ +// 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 "mojo/common/common_custom_types_struct_traits.h" + +#include <iterator> + +namespace mojo { + +// static +mojo::ConstCArray<uint16_t> +StructTraits<mojo::common::mojom::String16DataView, base::string16>::data( + const base::string16& str) { + return mojo::ConstCArray<uint16_t>( + str.size(), reinterpret_cast<const uint16_t*>(str.data())); +} + +// static +bool StructTraits<mojo::common::mojom::String16DataView, base::string16>::Read( + mojo::common::mojom::String16DataView data, + base::string16* out) { + mojo::ArrayDataView<uint16_t> view; + data.GetDataDataView(&view); + if (view.is_null()) + return false; + out->assign(reinterpret_cast<const base::char16*>(view.data()), view.size()); + return true; +} + +// static +const std::vector<uint32_t>& +StructTraits<mojo::common::mojom::VersionDataView, base::Version>::components( + const base::Version& version) { + return version.components(); +} + +// static +bool StructTraits<mojo::common::mojom::VersionDataView, base::Version>::Read( + mojo::common::mojom::VersionDataView data, + base::Version* out) { + std::vector<uint32_t> components; + if (!data.ReadComponents(&components)) + return false; + + *out = base::Version(base::Version(std::move(components))); + return out->IsValid(); +} + +// static +bool StructTraits<mojo::common::mojom::UnguessableTokenDataView, + base::UnguessableToken>:: + Read(mojo::common::mojom::UnguessableTokenDataView data, + base::UnguessableToken* out) { + uint64_t high = data.high(); + uint64_t low = data.low(); + + // Receiving a zeroed UnguessableToken is a security issue. + if (high == 0 && low == 0) + return false; + + *out = base::UnguessableToken::Deserialize(high, low); + return true; +} + +} // namespace mojo diff --git a/chromium/mojo/common/common_custom_types_struct_traits.h b/chromium/mojo/common/common_custom_types_struct_traits.h new file mode 100644 index 00000000000..37be5409074 --- /dev/null +++ b/chromium/mojo/common/common_custom_types_struct_traits.h @@ -0,0 +1,71 @@ +// 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. + +#ifndef MOJO_COMMON_COMMON_CUSTOM_TYPES_STRUCT_TRAITS_H_ +#define MOJO_COMMON_COMMON_CUSTOM_TYPES_STRUCT_TRAITS_H_ + +#include "base/strings/utf_string_conversions.h" +#include "base/unguessable_token.h" +#include "base/version.h" +#include "mojo/common/common_custom_types.mojom-shared.h" +#include "mojo/common/mojo_common_export.h" + +namespace mojo { + +template <> +struct StructTraits<mojo::common::mojom::String16DataView, base::string16> { + static mojo::ConstCArray<uint16_t> data(const base::string16& str); + static bool Read(mojo::common::mojom::String16DataView data, + base::string16* out); +}; + +template <> +struct StructTraits<mojo::common::mojom::VersionDataView, base::Version> { + static bool IsNull(const base::Version& version) { + return !version.IsValid(); + } + static void SetToNull(base::Version* out) { + *out = base::Version(std::string()); + } + static const std::vector<uint32_t>& components(const base::Version& version); + static bool Read(mojo::common::mojom::VersionDataView data, + base::Version* out); +}; + +// If base::UnguessableToken is no longer 128 bits, the logic below and the +// mojom::UnguessableToken type should be updated. +static_assert(sizeof(base::UnguessableToken) == 2 * sizeof(uint64_t), + "base::UnguessableToken should be of size 2 * sizeof(uint64_t)."); + +template <> +struct StructTraits<mojo::common::mojom::UnguessableTokenDataView, + base::UnguessableToken> { + static uint64_t high(const base::UnguessableToken& token) { + return token.GetHighForSerialization(); + } + + static uint64_t low(const base::UnguessableToken& token) { + return token.GetLowForSerialization(); + } + + static bool Read(mojo::common::mojom::UnguessableTokenDataView data, + base::UnguessableToken* out); +}; + +template <> +struct StructTraits<mojo::common::mojom::TimeDeltaDataView, base::TimeDelta> { + static int64_t microseconds(const base::TimeDelta& delta) { + return delta.InMicroseconds(); + } + + static bool Read(mojo::common::mojom::TimeDeltaDataView data, + base::TimeDelta* delta) { + *delta = base::TimeDelta::FromMicroseconds(data.microseconds()); + return true; + } +}; + +} // namespace mojo + +#endif // MOJO_COMMON_COMMON_CUSTOM_TYPES_STRUCT_TRAITS_H_ diff --git a/chromium/mojo/common/common_custom_types_unittest.cc b/chromium/mojo/common/common_custom_types_unittest.cc index fe6bb5da73a..520f82a9a00 100644 --- a/chromium/mojo/common/common_custom_types_unittest.cc +++ b/chromium/mojo/common/common_custom_types_unittest.cc @@ -5,6 +5,7 @@ #include "base/files/file_path.h" #include "base/message_loop/message_loop.h" #include "base/run_loop.h" +#include "base/strings/utf_string_conversions.h" #include "base/values.h" #include "mojo/common/common_custom_types.mojom.h" #include "mojo/common/test_common_custom_types.mojom.h" @@ -40,16 +41,37 @@ struct BounceTestTraits<base::ListValue> { }; template <typename T> +struct PassTraits { + using Type = const T&; +}; + +template <> +struct PassTraits<base::Time> { + using Type = base::Time; +}; + +template <> +struct PassTraits<base::TimeDelta> { + using Type = base::TimeDelta; +}; + +template <> +struct PassTraits<base::TimeTicks> { + using Type = base::TimeTicks; +}; + +template <typename T> void DoExpectResponse(T* expected_value, const base::Closure& closure, - const T& value) { + typename PassTraits<T>::Type value) { BounceTestTraits<T>::ExpectEquality(*expected_value, value); closure.Run(); } template <typename T> -base::Callback<void(const T&)> ExpectResponse(T* expected_value, - const base::Closure& closure) { +base::Callback<void(typename PassTraits<T>::Type)> ExpectResponse( + T* expected_value, + const base::Closure& closure) { return base::Bind(&DoExpectResponse<T>, expected_value, closure); } @@ -68,24 +90,38 @@ class TestFilePathImpl : public TestFilePath { mojo::Binding<TestFilePath> binding_; }; +class TestUnguessableTokenImpl : public TestUnguessableToken { + public: + explicit TestUnguessableTokenImpl(TestUnguessableTokenRequest request) + : binding_(this, std::move(request)) {} + + // TestUnguessableToken implementation: + void BounceNonce(const base::UnguessableToken& in, + const BounceNonceCallback& callback) override { + callback.Run(in); + } + + private: + mojo::Binding<TestUnguessableToken> binding_; +}; + class TestTimeImpl : public TestTime { public: explicit TestTimeImpl(TestTimeRequest request) : binding_(this, std::move(request)) {} // TestTime implementation: - void BounceTime(const base::Time& in, - const BounceTimeCallback& callback) override { + void BounceTime(base::Time in, const BounceTimeCallback& callback) override { callback.Run(in); } - void BounceTimeDelta(const base::TimeDelta& in, - const BounceTimeDeltaCallback& callback) override { + void BounceTimeDelta(base::TimeDelta in, + const BounceTimeDeltaCallback& callback) override { callback.Run(in); } - void BounceTimeTicks(const base::TimeTicks& in, - const BounceTimeTicksCallback& callback) override { + void BounceTimeTicks(base::TimeTicks in, + const BounceTimeTicksCallback& callback) override { callback.Run(in); } @@ -113,6 +149,21 @@ class TestValueImpl : public TestValue { mojo::Binding<TestValue> binding_; }; +class TestString16Impl : public TestString16 { + public: + explicit TestString16Impl(TestString16Request request) + : binding_(this, std::move(request)) {} + + // TestString16 implementation: + void BounceString16(const base::string16& in, + const BounceString16Callback& callback) override { + callback.Run(in); + } + + private: + mojo::Binding<TestString16> binding_; +}; + class CommonCustomTypesTest : public testing::Test { protected: CommonCustomTypesTest() {} @@ -140,6 +191,19 @@ TEST_F(CommonCustomTypesTest, FilePath) { run_loop.Run(); } +TEST_F(CommonCustomTypesTest, UnguessableToken) { + base::RunLoop run_loop; + + TestUnguessableTokenPtr ptr; + TestUnguessableTokenImpl impl(GetProxy(&ptr)); + + base::UnguessableToken token = base::UnguessableToken::Create(); + + ptr->BounceNonce(token, ExpectResponse(&token, run_loop.QuitClosure())); + + run_loop.Run(); +} + TEST_F(CommonCustomTypesTest, Time) { base::RunLoop run_loop; @@ -221,6 +285,32 @@ TEST_F(CommonCustomTypesTest, Value) { } } +TEST_F(CommonCustomTypesTest, String16) { + base::RunLoop run_loop; + + TestString16Ptr ptr; + TestString16Impl impl(GetProxy(&ptr)); + + base::string16 str16 = base::ASCIIToUTF16("hello world"); + + ptr->BounceString16(str16, ExpectResponse(&str16, run_loop.QuitClosure())); + + run_loop.Run(); +} + +TEST_F(CommonCustomTypesTest, EmptyString16) { + base::RunLoop run_loop; + + TestString16Ptr ptr; + TestString16Impl impl(GetProxy(&ptr)); + + base::string16 str16; + + ptr->BounceString16(str16, ExpectResponse(&str16, run_loop.QuitClosure())); + + run_loop.Run(); +} + } // namespace test } // namespace common } // namespace mojo diff --git a/chromium/mojo/common/common_type_converters.h b/chromium/mojo/common/common_type_converters.h index a065f050791..bf1e723b84d 100644 --- a/chromium/mojo/common/common_type_converters.h +++ b/chromium/mojo/common/common_type_converters.h @@ -6,6 +6,7 @@ #define MOJO_COMMON_COMMON_TYPE_CONVERTERS_H_ #include <stdint.h> +#include <vector> #include "base/strings/string16.h" #include "base/strings/string_piece.h" diff --git a/chromium/mojo/common/struct_traits_unittest.cc b/chromium/mojo/common/struct_traits_unittest.cc new file mode 100644 index 00000000000..5ac4bc9c800 --- /dev/null +++ b/chromium/mojo/common/struct_traits_unittest.cc @@ -0,0 +1,57 @@ +// 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 "base/message_loop/message_loop.h" +#include "mojo/common/traits_test_service.mojom.h" +#include "mojo/public/cpp/bindings/binding_set.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace mojo { +namespace common { +namespace { + +class StructTraitsTest : public testing::Test, public mojom::TraitsTestService { + public: + StructTraitsTest() {} + + protected: + mojom::TraitsTestServicePtr GetTraitsTestProxy() { + return traits_test_bindings_.CreateInterfacePtrAndBind(this); + } + + private: + // TraitsTestService: + void EchoVersion(const base::Optional<base::Version>& m, + const EchoVersionCallback& callback) override { + callback.Run(m); + } + + base::MessageLoop loop_; + mojo::BindingSet<TraitsTestService> traits_test_bindings_; + + DISALLOW_COPY_AND_ASSIGN(StructTraitsTest); +}; + +TEST_F(StructTraitsTest, Version) { + const std::string& version_str = "1.2.3.4"; + base::Version input(version_str); + mojom::TraitsTestServicePtr proxy = GetTraitsTestProxy(); + base::Optional<base::Version> output; + proxy->EchoVersion(input, &output); + EXPECT_TRUE(output.has_value()); + EXPECT_EQ(version_str, output->GetString()); +} + +TEST_F(StructTraitsTest, InvalidVersion) { + const std::string invalid_version_str; + base::Version input(invalid_version_str); + mojom::TraitsTestServicePtr proxy = GetTraitsTestProxy(); + base::Optional<base::Version> output; + proxy->EchoVersion(input, &output); + EXPECT_FALSE(output.has_value()); +} + +} // namespace +} // namespace common +} // namespace mojo diff --git a/chromium/mojo/common/test_common_custom_types.mojom b/chromium/mojo/common/test_common_custom_types.mojom index db91a4f53f4..ab49cce1339 100644 --- a/chromium/mojo/common/test_common_custom_types.mojom +++ b/chromium/mojo/common/test_common_custom_types.mojom @@ -11,6 +11,11 @@ interface TestFilePath { => (mojo.common.mojom.FilePath out); }; +interface TestUnguessableToken { + BounceNonce(mojo.common.mojom.UnguessableToken in) + => (mojo.common.mojom.UnguessableToken out); +}; + interface TestTime { BounceTime(mojo.common.mojom.Time time) => (mojo.common.mojom.Time time); BounceTimeDelta(mojo.common.mojom.TimeDelta time_delta) @@ -25,3 +30,9 @@ interface TestValue { BounceListValue(mojo.common.mojom.ListValue in) => (mojo.common.mojom.ListValue out); }; + +interface TestString16 { + [Sync] + BounceString16(mojo.common.mojom.String16 in) + => (mojo.common.mojom.String16 out); +}; diff --git a/chromium/mojo/common/traits_test_service.mojom b/chromium/mojo/common/traits_test_service.mojom new file mode 100644 index 00000000000..bf508cd661a --- /dev/null +++ b/chromium/mojo/common/traits_test_service.mojom @@ -0,0 +1,14 @@ +// 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. + +module mojo.common.mojom; + +import "mojo/common/common_custom_types.mojom"; + +// All functions on this interface echo their arguments to test StructTraits +// serialization and deserialization. +interface TraitsTestService { + [Sync] + EchoVersion(Version? v) => (Version? pass); +}; |