1 // Copyright 2013 The Flutter 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 #ifndef FLUTTER_FML_MAPPING_H_ 6 #define FLUTTER_FML_MAPPING_H_ 7 8 #include <initializer_list> 9 #include <memory> 10 #include <string> 11 #include <vector> 12 13 #include "flutter/fml/build_config.h" 14 #include "flutter/fml/file.h" 15 #include "flutter/fml/macros.h" 16 #include "flutter/fml/native_library.h" 17 #include "flutter/fml/unique_fd.h" 18 19 namespace fml { 20 21 class Mapping { 22 public: 23 Mapping(); 24 25 virtual ~Mapping(); 26 27 virtual size_t GetSize() const = 0; 28 29 virtual const uint8_t* GetMapping() const = 0; 30 31 private: 32 FML_DISALLOW_COPY_AND_ASSIGN(Mapping); 33 }; 34 35 class FileMapping final : public Mapping { 36 public: 37 enum class Protection { 38 kRead, 39 kWrite, 40 kExecute, 41 }; 42 43 FileMapping(const fml::UniqueFD& fd, 44 std::initializer_list<Protection> protection = { 45 Protection::kRead}); 46 47 ~FileMapping() override; 48 49 static std::unique_ptr<FileMapping> CreateReadOnly(const std::string& path); 50 51 static std::unique_ptr<FileMapping> CreateReadOnly( 52 const fml::UniqueFD& base_fd, 53 const std::string& sub_path = ""); 54 55 static std::unique_ptr<FileMapping> CreateReadExecute( 56 const std::string& path); 57 58 static std::unique_ptr<FileMapping> CreateReadExecute( 59 const fml::UniqueFD& base_fd, 60 const std::string& sub_path = ""); 61 62 // |Mapping| 63 size_t GetSize() const override; 64 65 // |Mapping| 66 const uint8_t* GetMapping() const override; 67 68 uint8_t* GetMutableMapping(); 69 70 bool IsValid() const; 71 72 private: 73 bool valid_ = false; 74 size_t size_ = 0; 75 uint8_t* mapping_ = nullptr; 76 uint8_t* mutable_mapping_ = nullptr; 77 78 #if OS_WIN 79 fml::UniqueFD mapping_handle_; 80 #endif 81 82 FML_DISALLOW_COPY_AND_ASSIGN(FileMapping); 83 }; 84 85 class DataMapping final : public Mapping { 86 public: 87 DataMapping(std::vector<uint8_t> data); 88 89 ~DataMapping() override; 90 91 // |Mapping| 92 size_t GetSize() const override; 93 94 // |Mapping| 95 const uint8_t* GetMapping() const override; 96 97 private: 98 std::vector<uint8_t> data_; 99 100 FML_DISALLOW_COPY_AND_ASSIGN(DataMapping); 101 }; 102 103 class NonOwnedMapping final : public Mapping { 104 public: 105 using ReleaseProc = std::function<void(const uint8_t* data, size_t size)>; 106 NonOwnedMapping(const uint8_t* data, 107 size_t size, 108 ReleaseProc release_proc = nullptr); 109 110 ~NonOwnedMapping() override; 111 112 // |Mapping| 113 size_t GetSize() const override; 114 115 // |Mapping| 116 const uint8_t* GetMapping() const override; 117 118 private: 119 const uint8_t* const data_; 120 const size_t size_; 121 const ReleaseProc release_proc_; 122 123 FML_DISALLOW_COPY_AND_ASSIGN(NonOwnedMapping); 124 }; 125 126 class SymbolMapping final : public Mapping { 127 public: 128 SymbolMapping(fml::RefPtr<fml::NativeLibrary> native_library, 129 const char* symbol_name); 130 131 ~SymbolMapping() override; 132 133 // |Mapping| 134 size_t GetSize() const override; 135 136 // |Mapping| 137 const uint8_t* GetMapping() const override; 138 139 private: 140 fml::RefPtr<fml::NativeLibrary> native_library_; 141 const uint8_t* mapping_ = nullptr; 142 143 FML_DISALLOW_COPY_AND_ASSIGN(SymbolMapping); 144 }; 145 146 } // namespace fml 147 148 #endif // FLUTTER_FML_MAPPING_H_ 149