1 // Copyright 2016 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.
4
5 #include "mojo/public/cpp/bindings/string_traits_string16.h"
6
7 #include <string>
8
9 #include "base/strings/utf_string_conversions.h"
10
11 namespace mojo {
12
13 // static
SetUpContext(const base::string16 & input)14 void* StringTraits<base::string16>::SetUpContext(const base::string16& input) {
15 return new std::string(base::UTF16ToUTF8(input));
16 }
17
18 // static
TearDownContext(const base::string16 & input,void * context)19 void StringTraits<base::string16>::TearDownContext(const base::string16& input,
20 void* context) {
21 delete static_cast<std::string*>(context);
22 }
23
24 // static
GetSize(const base::string16 & input,void * context)25 size_t StringTraits<base::string16>::GetSize(const base::string16& input,
26 void* context) {
27 return static_cast<std::string*>(context)->size();
28 }
29
30 // static
GetData(const base::string16 & input,void * context)31 const char* StringTraits<base::string16>::GetData(const base::string16& input,
32 void* context) {
33 return static_cast<std::string*>(context)->data();
34 }
35
36 // static
Read(StringDataView input,base::string16 * output)37 bool StringTraits<base::string16>::Read(StringDataView input,
38 base::string16* output) {
39 return base::UTF8ToUTF16(input.storage(), input.size(), output);
40 }
41
42 } // namespace mojo
43