Roll src/third_party/WebKit 0c3f21f:4f9ce20 (svn 202223:202224)
[chromium-blink-merge.git] / mojo / common / common_type_converters.cc
blob16c477d192f1f476cded29558e3ab5eef0f5235b
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/common/common_type_converters.h"
7 #include <string>
9 #include "base/strings/utf_string_conversions.h"
11 namespace mojo {
13 // static
14 String TypeConverter<String, base::StringPiece>::Convert(
15 const base::StringPiece& input) {
16 if (input.empty()) {
17 char c = 0;
18 return String(&c, 0);
20 return String(input.data(), input.size());
22 // static
23 base::StringPiece TypeConverter<base::StringPiece, String>::Convert(
24 const String& input) {
25 return input.get();
28 // static
29 String TypeConverter<String, base::string16>::Convert(
30 const base::string16& input) {
31 return TypeConverter<String, base::StringPiece>::Convert(
32 base::UTF16ToUTF8(input));
34 // static
35 base::string16 TypeConverter<base::string16, String>::Convert(
36 const String& input) {
37 return base::UTF8ToUTF16(input.To<base::StringPiece>());
40 std::string TypeConverter<std::string, Array<uint8_t> >::Convert(
41 const Array<uint8_t>& input) {
42 if (input.is_null() || input.size() == 0u)
43 return std::string();
45 return std::string(reinterpret_cast<const char*>(&input.front()),
46 input.size());
49 Array<uint8_t> TypeConverter<Array<uint8_t>, std::string>::Convert(
50 const std::string& input) {
51 Array<uint8_t> result(input.size());
52 if (input.size() > 0)
53 memcpy(&result.front(), input.c_str(), input.size());
54 return result.Pass();
57 } // namespace mojo