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 10 #include <optional> 11 #include <string> 12 #include <string_view> 13 14 #include "base/base_export.h" 15 #include "base/files/file.h" 16 17 namespace base { 18 19 // Returns the contents of `buffer` (which must be a valid UTF-8 string), or 20 // null in case of a conversion error. 21 BASE_EXPORT std::optional<std::u16string> ReadUTF8FromVMOAsUTF16( 22 const fuchsia::mem::Buffer& buffer); 23 24 // Creates a Fuchsia VMO from `data`. The size of the resulting virtual memory 25 // object will be set to the size of the string, and it will be given the name 26 // `name`. 27 BASE_EXPORT zx::vmo VmoFromString(std::string_view data, std::string_view name); 28 29 // Creates a Fuchsia memory buffer from `data`. The resulting virtual memory 30 // object will be given the name `name`. 31 // `fuchsia::mem::Buffer` is deprecated: for new interfaces, prefer using 32 // a VMO object directly (see `VmoFromString`). 33 BASE_EXPORT fuchsia::mem::Buffer MemBufferFromString(std::string_view data, 34 std::string_view name); 35 36 // Creates a Fuchsia memory buffer from the UTF-16 string `data`. The resulting 37 // virtual memory object will be given the name `name`. 38 BASE_EXPORT fuchsia::mem::Buffer MemBufferFromString16(std::u16string_view data, 39 std::string_view name); 40 41 // Returns the contents of `data`, or null if the read operation fails. 42 BASE_EXPORT std::optional<std::string> StringFromVmo(const zx::vmo& vmo); 43 44 // Returns the contents of `buffer`, or null if the read operation fails. 45 // `fuchsia::mem::Buffer` is deprecated: for new interfaces, prefer using 46 // a VMO object directly (see `StringFromVmo`). 47 BASE_EXPORT std::optional<std::string> StringFromMemBuffer( 48 const fuchsia::mem::Buffer& buffer); 49 50 // Returns the contents of `data`, or null if the read operation fails. 51 BASE_EXPORT std::optional<std::string> StringFromMemData( 52 const fuchsia::mem::Data& data); 53 54 // Creates a memory-mapped, read-only Buffer with the contents of `file`. Will 55 // return an empty Buffer if the file could not be opened. 56 BASE_EXPORT fuchsia::mem::Buffer MemBufferFromFile(File file); 57 58 // Creates a non-resizeable, copy-on-write shared memory clone of `buffer`. The 59 // resulting virtual memory object will be given the name `name`. 60 BASE_EXPORT fuchsia::mem::Buffer CloneBuffer(const fuchsia::mem::Buffer& buffer, 61 std::string_view name); 62 63 } // namespace base 64 65 #endif // BASE_FUCHSIA_MEM_BUFFER_UTIL_H_ 66