1 // Copyright 2018 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_FUCHSIA_MEM_BUFFER_UTIL_H_ 6 #define BASE_FUCHSIA_MEM_BUFFER_UTIL_H_ 7 8 #include <fuchsia/mem/cpp/fidl.h> 9 #include <string> 10 11 #include "base/base_export.h" 12 #include "base/files/file.h" 13 #include "base/strings/string_piece_forward.h" 14 #include "third_party/abseil-cpp/absl/types/optional.h" 15 16 namespace base { 17 18 // Returns the contents of `buffer` (which must be a valid UTF-8 string), or 19 // null in case of a conversion error. 20 BASE_EXPORT absl::optional<std::u16string> ReadUTF8FromVMOAsUTF16( 21 const fuchsia::mem::Buffer& buffer); 22 23 // Creates a Fuchsia VMO from `data`. The size of the resulting virtual memory 24 // object will be set to the size of the string, and it will be given the name 25 // `name`. 26 BASE_EXPORT zx::vmo VmoFromString(StringPiece data, StringPiece name); 27 28 // Creates a Fuchsia memory buffer from `data`. The resulting virtual memory 29 // object will be given the name `name`. 30 // `fuchsia::mem::Buffer` is deprecated: for new interfaces, prefer using 31 // a VMO object directly (see `VmoFromString`). 32 BASE_EXPORT fuchsia::mem::Buffer MemBufferFromString(StringPiece data, 33 StringPiece name); 34 35 // Creates a Fuchsia memory buffer from the UTF-16 string `data`. The resulting 36 // virtual memory object will be given the name `name`. 37 BASE_EXPORT fuchsia::mem::Buffer MemBufferFromString16(StringPiece16 data, 38 StringPiece name); 39 40 // Returns the contents of `data`, or null if the read operation fails. 41 BASE_EXPORT absl::optional<std::string> StringFromVmo(const zx::vmo& vmo); 42 43 // Returns the contents of `buffer`, or null if the read operation fails. 44 // `fuchsia::mem::Buffer` is deprecated: for new interfaces, prefer using 45 // a VMO object directly (see `StringFromVmo`). 46 BASE_EXPORT absl::optional<std::string> StringFromMemBuffer( 47 const fuchsia::mem::Buffer& buffer); 48 49 // Returns the contents of `data`, or null if the read operation fails. 50 BASE_EXPORT absl::optional<std::string> StringFromMemData( 51 const fuchsia::mem::Data& data); 52 53 // Creates a memory-mapped, read-only Buffer with the contents of `file`. Will 54 // return an empty Buffer if the file could not be opened. 55 BASE_EXPORT fuchsia::mem::Buffer MemBufferFromFile(File file); 56 57 // Creates a non-resizeable, copy-on-write shared memory clone of `buffer`. The 58 // resulting virtual memory object will be given the name `name`. 59 BASE_EXPORT fuchsia::mem::Buffer CloneBuffer(const fuchsia::mem::Buffer& buffer, 60 StringPiece name); 61 62 } // namespace base 63 64 #endif // BASE_FUCHSIA_MEM_BUFFER_UTIL_H_ 65