• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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/base/big_string_mojom_traits.h"
6 
7 #include "mojo/public/cpp/base/big_buffer_mojom_traits.h"
8 
9 namespace mojo {
10 
11 // static
12 mojo_base::BigBuffer StructTraits<mojo_base::mojom::BigStringDataView,
data(const std::string & str)13                                   std::string>::data(const std::string& str) {
14   const auto* bytes = reinterpret_cast<const uint8_t*>(str.data());
15   return mojo_base::BigBuffer(
16       base::make_span(bytes, str.size() * sizeof(char)));
17 }
18 
19 // static
Read(mojo_base::mojom::BigStringDataView data,std::string * out)20 bool StructTraits<mojo_base::mojom::BigStringDataView, std::string>::Read(
21     mojo_base::mojom::BigStringDataView data,
22     std::string* out) {
23   mojo_base::BigBuffer buffer;
24   if (!data.ReadData(&buffer))
25     return false;
26   if (buffer.size() % sizeof(char))
27     return false;
28   *out = std::string(reinterpret_cast<const char*>(buffer.data()),
29                      buffer.size() / sizeof(char));
30   return true;
31 }
32 
33 }  // namespace mojo
34