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 APPS_DART_RUNNER_MAPPED_RESOURCE_H_ 6 #define APPS_DART_RUNNER_MAPPED_RESOURCE_H_ 7 8 #include <fuchsia/mem/cpp/fidl.h> 9 #include <lib/fdio/namespace.h> 10 11 namespace dart_runner { 12 13 class MappedResource { 14 public: MappedResource()15 MappedResource() : address_(nullptr), size_(0) {} MappedResource(MappedResource && other)16 MappedResource(MappedResource&& other) 17 : address_(other.address_), size_(other.size_) { 18 other.address_ = nullptr; 19 other.size_ = 0; 20 } 21 MappedResource& operator=(MappedResource&& other) { 22 address_ = other.address_; 23 size_ = other.size_; 24 other.address_ = nullptr; 25 other.size_ = 0; 26 return *this; 27 } 28 ~MappedResource(); 29 30 // Loads the content of a file from the given namespace and maps it into the 31 // current process's address space. If namespace is null, the fdio "global" 32 // namespace is used (in which case, ./pkg means the dart_runner's package). 33 // The content is unmapped when the MappedResource goes out of scope. Returns 34 // true on success. 35 static bool LoadFromNamespace(fdio_ns_t* namespc, 36 const std::string& path, 37 MappedResource& resource, 38 bool executable = false); 39 40 // Maps a VMO into the current process's address space. The content is 41 // unmapped when the MappedResource goes out of scope. Returns true on 42 // success. The path is used only for error messages. 43 static bool LoadFromVmo(const std::string& path, 44 fuchsia::mem::Buffer resource_vmo, 45 MappedResource& resource, 46 bool executable = false); 47 address()48 const uint8_t* address() const { 49 return reinterpret_cast<const uint8_t*>(address_); 50 } size()51 size_t size() const { return size_; } 52 53 private: 54 void* address_; 55 size_t size_; 56 57 // Disallow copy and assignment. 58 MappedResource(const MappedResource&) = delete; 59 MappedResource& operator=(const MappedResource&) = delete; 60 }; 61 62 } // namespace dart_runner 63 64 #endif // APPS_DART_RUNNER_MAPPED_RESOURCE_H_ 65