OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_STRING_PIECE_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_STRING_PIECE_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_STRING_PIECE_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_STRING_PIECE_H_ |
7 | 7 |
8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
9 #include "mojo/public/cpp/bindings/string_traits.h" | 9 #include "mojo/public/cpp/bindings/string_traits.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 | 12 |
13 template <> | 13 template <> |
14 struct StringTraits<base::StringPiece> { | 14 struct StringTraits<base::StringPiece> { |
15 // No IsNull() function, which means that base::StringPiece is always | 15 // No IsNull() function, which means that base::StringPiece is always |
16 // considered as non-null mojom string. We could have let StringPiece | 16 // considered as non-null mojom string. We could have let StringPiece |
17 // containing a null data pointer map to null mojom string, but | 17 // containing a null data pointer map to null mojom string, but |
18 // StringPiece::empty() returns true in this case. It seems confusing to mix | 18 // StringPiece::empty() returns true in this case. It seems confusing to mix |
19 // the concept of empty and null strings, especially because they mean | 19 // the concept of empty and null strings, especially because they mean |
20 // different things in mojom. | 20 // different things in mojom. |
21 | 21 |
22 static size_t GetSize(const base::StringPiece& input) { return input.size(); } | 22 static size_t GetSize(const base::StringPiece& input) { return input.size(); } |
23 | 23 |
24 static const char* GetData(const base::StringPiece& input) { | 24 static const char* GetData(const base::StringPiece& input) { |
25 return input.data(); | 25 return input.data(); |
26 } | 26 } |
27 | 27 |
28 // TODO(yzshen): Use a public type, such as mojo::String::DataView, for | 28 static bool Read(StringDataView input, base::StringPiece* output) { |
29 // |input|. | 29 if (!input.is_null()) |
30 static bool Read(internal::String_Data* input, base::StringPiece* output) { | 30 output->set(input.storage(), input.size()); |
31 if (input) | |
32 output->set(input->storage(), input->size()); | |
33 else | 31 else |
34 output->set(nullptr, 0); | 32 output->set(nullptr, 0); |
35 return true; | 33 return true; |
36 } | 34 } |
37 }; | 35 }; |
38 | 36 |
39 } // namespace mojo | 37 } // namespace mojo |
40 | 38 |
41 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_STRING_PIECE_H_ | 39 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_STRING_PIECE_H_ |
OLD | NEW |