• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_wtf.h"
6 
7 #include <string.h>
8 
9 #include "base/logging.h"
10 #include "mojo/public/cpp/bindings/lib/array_internal.h"
11 #include "mojo/public/cpp/bindings/string_data_view.h"
12 #include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h"
13 
14 namespace mojo {
15 namespace {
16 
17 struct UTF8AdaptorInfo {
UTF8AdaptorInfomojo::__anon1bc12ed60111::UTF8AdaptorInfo18   explicit UTF8AdaptorInfo(const WTF::String& input) : utf8_adaptor(input) {
19 #if DCHECK_IS_ON()
20     original_size_in_bytes = input.CharactersSizeInBytes();
21 #endif
22   }
23 
~UTF8AdaptorInfomojo::__anon1bc12ed60111::UTF8AdaptorInfo24   ~UTF8AdaptorInfo() {}
25 
26   WTF::StringUTF8Adaptor utf8_adaptor;
27 
28 #if DCHECK_IS_ON()
29   // For sanity check only.
30   size_t original_size_in_bytes;
31 #endif
32 };
33 
ToAdaptor(const WTF::String & input,void * context)34 UTF8AdaptorInfo* ToAdaptor(const WTF::String& input, void* context) {
35   UTF8AdaptorInfo* adaptor = static_cast<UTF8AdaptorInfo*>(context);
36 
37 #if DCHECK_IS_ON()
38   DCHECK_EQ(adaptor->original_size_in_bytes, input.CharactersSizeInBytes());
39 #endif
40   return adaptor;
41 }
42 
43 }  // namespace
44 
45 // static
SetToNull(WTF::String * output)46 void StringTraits<WTF::String>::SetToNull(WTF::String* output) {
47   if (output->IsNull())
48     return;
49 
50   WTF::String result;
51   output->swap(result);
52 }
53 
54 // static
SetUpContext(const WTF::String & input)55 void* StringTraits<WTF::String>::SetUpContext(const WTF::String& input) {
56   return new UTF8AdaptorInfo(input);
57 }
58 
59 // static
TearDownContext(const WTF::String & input,void * context)60 void StringTraits<WTF::String>::TearDownContext(const WTF::String& input,
61                                                 void* context) {
62   delete ToAdaptor(input, context);
63 }
64 
65 // static
GetSize(const WTF::String & input,void * context)66 size_t StringTraits<WTF::String>::GetSize(const WTF::String& input,
67                                           void* context) {
68   return ToAdaptor(input, context)->utf8_adaptor.length();
69 }
70 
71 // static
GetData(const WTF::String & input,void * context)72 const char* StringTraits<WTF::String>::GetData(const WTF::String& input,
73                                                void* context) {
74   return ToAdaptor(input, context)->utf8_adaptor.Data();
75 }
76 
77 // static
Read(StringDataView input,WTF::String * output)78 bool StringTraits<WTF::String>::Read(StringDataView input,
79                                      WTF::String* output) {
80   WTF::String result = WTF::String::FromUTF8(input.storage(), input.size());
81   output->swap(result);
82   return true;
83 }
84 
85 }  // namespace mojo
86