summaryrefslogtreecommitdiffstats
path: root/mojo
diff options
context:
space:
mode:
authorjam <jam@chromium.org>2016-03-18 17:29:32 -0700
committerCommit bot <commit-bot@chromium.org>2016-03-19 00:31:37 +0000
commit490e88eee68dd6907195dfeb0922d9e7cf37a693 (patch)
treeb0a3b7746a17a23c85f9a26d24ae715840e18ddc /mojo
parent60030663a474eb523aa9c958e5fe88fa844d8b79 (diff)
downloadchromium_src-490e88eee68dd6907195dfeb0922d9e7cf37a693.zip
chromium_src-490e88eee68dd6907195dfeb0922d9e7cf37a693.tar.gz
chromium_src-490e88eee68dd6907195dfeb0922d9e7cf37a693.tar.bz2
Implement the renderer side of the mojo based local storage implementation.
This is based on the existing chrome IPC based implementation. BUG=586194 Review URL: https://codereview.chromium.org/1814003002 Cr-Commit-Position: refs/heads/master@{#382138}
Diffstat (limited to 'mojo')
-rw-r--r--mojo/common/common_type_converters.cc23
-rw-r--r--mojo/common/common_type_converters.h16
-rw-r--r--mojo/common/common_type_converters_unittest.cc27
3 files changed, 58 insertions, 8 deletions
diff --git a/mojo/common/common_type_converters.cc b/mojo/common/common_type_converters.cc
index 2740853..78418f8 100644
--- a/mojo/common/common_type_converters.cc
+++ b/mojo/common/common_type_converters.cc
@@ -39,9 +39,9 @@ base::string16 TypeConverter<base::string16, String>::Convert(
return base::UTF8ToUTF16(input.To<base::StringPiece>());
}
-std::string TypeConverter<std::string, Array<uint8_t> >::Convert(
+std::string TypeConverter<std::string, Array<uint8_t>>::Convert(
const Array<uint8_t>& input) {
- if (input.is_null() || input.size() == 0u)
+ if (input.is_null() || input.empty())
return std::string();
return std::string(reinterpret_cast<const char*>(&input.front()),
@@ -51,9 +51,26 @@ std::string TypeConverter<std::string, Array<uint8_t> >::Convert(
Array<uint8_t> TypeConverter<Array<uint8_t>, std::string>::Convert(
const std::string& input) {
Array<uint8_t> result(input.size());
- if (input.size() > 0)
+ if (!input.empty())
memcpy(&result.front(), input.c_str(), input.size());
return result;
}
+base::string16 TypeConverter<base::string16, Array<uint8_t>>::Convert(
+ const Array<uint8_t>& input) {
+ if (input.is_null() || input.empty())
+ return base::string16();
+
+ return base::string16(reinterpret_cast<const base::char16*>(&input.front()),
+ input.size() / sizeof(base::char16));
+}
+
+Array<uint8_t> TypeConverter<Array<uint8_t>, base::string16>::Convert(
+ const base::string16& input) {
+ Array<uint8_t> result(input.size() * sizeof(base::char16));
+ if (!input.empty())
+ memcpy(&result.front(), input.c_str(), input.size() * sizeof(base::char16));
+ return result;
+}
+
} // namespace mojo
diff --git a/mojo/common/common_type_converters.h b/mojo/common/common_type_converters.h
index 5f6e3c5..6dc31f1 100644
--- a/mojo/common/common_type_converters.h
+++ b/mojo/common/common_type_converters.h
@@ -36,12 +36,8 @@ struct MOJO_COMMON_EXPORT TypeConverter<base::string16, String> {
static base::string16 Convert(const String& input);
};
-// TODO(erg): In the very long term, we will want to remove conversion between
-// std::strings and arrays of unsigned bytes. However, there is too much code
-// across chrome which uses std::string as a bag of bytes that we probably
-// don't want to roll this function at each callsite.
template <>
-struct MOJO_COMMON_EXPORT TypeConverter<std::string, Array<uint8_t> > {
+struct MOJO_COMMON_EXPORT TypeConverter<std::string, Array<uint8_t>> {
static std::string Convert(const Array<uint8_t>& input);
};
@@ -50,6 +46,16 @@ struct MOJO_COMMON_EXPORT TypeConverter<Array<uint8_t>, std::string> {
static Array<uint8_t> Convert(const std::string& input);
};
+template <>
+struct MOJO_COMMON_EXPORT TypeConverter<base::string16, Array<uint8_t>> {
+ static base::string16 Convert(const Array<uint8_t>& input);
+};
+
+template <>
+struct MOJO_COMMON_EXPORT TypeConverter<Array<uint8_t>, base::string16> {
+ static Array<uint8_t> Convert(const base::string16& input);
+};
+
} // namespace mojo
#endif // MOJO_COMMON_COMMON_TYPE_CONVERTERS_H_
diff --git a/mojo/common/common_type_converters_unittest.cc b/mojo/common/common_type_converters_unittest.cc
index af8f640..302bcbe 100644
--- a/mojo/common/common_type_converters_unittest.cc
+++ b/mojo/common/common_type_converters_unittest.cc
@@ -108,6 +108,33 @@ TEST(CommonTypeConvertersTest, StdStringToArrayUint8) {
EXPECT_EQ('a', data[3]);
}
+TEST(CommonTypeConvertersTest, ArrayUint8ToString16) {
+ Array<uint8_t> data(8);
+ data[0] = 'd';
+ data[2] = 'a';
+ data[4] = 't';
+ data[6] = 'a';
+
+ EXPECT_EQ(base::ASCIIToUTF16("data"), data.To<base::string16>());
+}
+
+TEST(CommonTypeConvertersTest, String16ToArrayUint8) {
+ base::string16 input(base::ASCIIToUTF16("data"));
+ Array<uint8_t> data = Array<uint8_t>::From(input);
+
+ ASSERT_EQ(8ul, data.size());
+ EXPECT_EQ('d', data[0]);
+ EXPECT_EQ('a', data[2]);
+ EXPECT_EQ('t', data[4]);
+ EXPECT_EQ('a', data[6]);
+}
+
+TEST(CommonTypeConvertersTest, String16ToArrayUint8AndBack) {
+ base::string16 input(base::ASCIIToUTF16("data"));
+ Array<uint8_t> data = Array<uint8_t>::From(input);
+ EXPECT_EQ(input, data.To<base::string16>());
+}
+
TEST(CommonTypeConvertersTest, EmptyStringToArrayUint8) {
Array<uint8_t> data = Array<uint8_t>::From(std::string());