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(const std::string& string); |
90 | |
91 | ~DataMapping() override; |
92 | |
93 | // |Mapping| |
94 | size_t GetSize() const override; |
95 | |
96 | // |Mapping| |
97 | const uint8_t* GetMapping() const override; |
98 | |
99 | private: |
100 | std::vector<uint8_t> data_; |
101 | |
102 | FML_DISALLOW_COPY_AND_ASSIGN(DataMapping); |
103 | }; |
104 | |
105 | class NonOwnedMapping final : public Mapping { |
106 | public: |
107 | using ReleaseProc = std::function<void(const uint8_t* data, size_t size)>; |
108 | NonOwnedMapping(const uint8_t* data, |
109 | size_t size, |
110 | const ReleaseProc& release_proc = nullptr); |
111 | |
112 | ~NonOwnedMapping() override; |
113 | |
114 | // |Mapping| |
115 | size_t GetSize() const override; |
116 | |
117 | // |Mapping| |
118 | const uint8_t* GetMapping() const override; |
119 | |
120 | private: |
121 | const uint8_t* const data_; |
122 | const size_t size_; |
123 | const ReleaseProc release_proc_; |
124 | |
125 | FML_DISALLOW_COPY_AND_ASSIGN(NonOwnedMapping); |
126 | }; |
127 | |
128 | class SymbolMapping final : public Mapping { |
129 | public: |
130 | SymbolMapping(fml::RefPtr<fml::NativeLibrary> native_library, |
131 | const char* symbol_name); |
132 | |
133 | ~SymbolMapping() override; |
134 | |
135 | // |Mapping| |
136 | size_t GetSize() const override; |
137 | |
138 | // |Mapping| |
139 | const uint8_t* GetMapping() const override; |
140 | |
141 | private: |
142 | fml::RefPtr<fml::NativeLibrary> native_library_; |
143 | const uint8_t* mapping_ = nullptr; |
144 | |
145 | FML_DISALLOW_COPY_AND_ASSIGN(SymbolMapping); |
146 | }; |
147 | |
148 | } // namespace fml |
149 | |
150 | #endif // FLUTTER_FML_MAPPING_H_ |
151 | |