Add factory methods to FileMapping that make it easy to create common mappings. (#8652)

The GetMapping calls removed in this patch had the same code and had to be repeated across different test harnesses as well as in dart_snapshot.cc. Just make this a factory method so the code is less verbose.
This commit is contained in:
Chinmay Garde 2019-04-19 12:48:53 -07:00 committed by GitHub
parent 5fed725202
commit 6257dfeb53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 116 deletions

View File

@ -14,6 +14,55 @@ uint8_t* FileMapping::GetMutableMapping() {
return mutable_mapping_;
}
std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
const std::string& path) {
return CreateReadOnly(OpenFile(path.c_str(), false, FilePermission::kRead),
"");
}
std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
const fml::UniqueFD& base_fd,
const std::string& sub_path) {
if (sub_path.size() != 0) {
return CreateReadOnly(
OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
}
auto mapping = std::make_unique<FileMapping>(
base_fd, std::initializer_list<Protection>{Protection::kRead});
if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}
return mapping;
}
std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
const std::string& path) {
return CreateReadExecute(
OpenFile(path.c_str(), false, FilePermission::kRead));
}
std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
const fml::UniqueFD& base_fd,
const std::string& sub_path) {
if (sub_path.size() != 0) {
return CreateReadExecute(
OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
}
auto mapping = std::make_unique<FileMapping>(
base_fd, std::initializer_list<Protection>{Protection::kRead,
Protection::kExecute});
if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}
return mapping;
}
// Data Mapping
DataMapping::DataMapping(std::vector<uint8_t> data) : data_(std::move(data)) {}

View File

@ -46,6 +46,19 @@ class FileMapping final : public Mapping {
~FileMapping() override;
static std::unique_ptr<FileMapping> CreateReadOnly(const std::string& path);
static std::unique_ptr<FileMapping> CreateReadOnly(
const fml::UniqueFD& base_fd,
const std::string& sub_path = "");
static std::unique_ptr<FileMapping> CreateReadExecute(
const std::string& path);
static std::unique_ptr<FileMapping> CreateReadExecute(
const fml::UniqueFD& base_fd,
const std::string& sub_path = "");
// |Mapping|
size_t GetSize() const override;

View File

@ -21,33 +21,13 @@ const char* DartSnapshot::kIsolateInstructionsSymbol =
"kDartIsolateSnapshotInstructions";
static std::unique_ptr<const fml::Mapping> GetFileMapping(
const std::string path,
const std::string& path,
bool executable) {
fml::UniqueFD file =
fml::OpenFile(path.c_str(), // file path
false, // create file if necessary
fml::FilePermission::kRead // file permissions
);
if (!file.is_valid()) {
return nullptr;
}
using Prot = fml::FileMapping::Protection;
std::unique_ptr<fml::FileMapping> mapping;
if (executable) {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
return fml::FileMapping::CreateReadExecute(path);
} else {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead});
return fml::FileMapping::CreateReadOnly(path);
}
if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}
return mapping;
}
// The first party embedders don't yet use the stable embedder API and depend on

View File

@ -15,32 +15,6 @@ RuntimeTest::RuntimeTest()
RuntimeTest::~RuntimeTest() = default;
static std::unique_ptr<fml::Mapping> GetMapping(const fml::UniqueFD& directory,
const char* path,
bool executable) {
fml::UniqueFD file = fml::OpenFile(directory, path, false /* create */,
fml::FilePermission::kRead);
if (!file.is_valid()) {
return nullptr;
}
using Prot = fml::FileMapping::Protection;
std::unique_ptr<fml::FileMapping> mapping;
if (executable) {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
} else {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead});
}
if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}
return mapping;
}
void RuntimeTest::SetSnapshotsAndAssets(Settings& settings) {
if (!assets_dir_.is_valid()) {
return;
@ -52,27 +26,30 @@ void RuntimeTest::SetSnapshotsAndAssets(Settings& settings) {
// don't need to be explicitly suppiled by the embedder.
if (DartVM::IsRunningPrecompiledCode()) {
settings.vm_snapshot_data = [this]() {
return GetMapping(assets_dir_, "vm_snapshot_data", false);
return fml::FileMapping::CreateReadOnly(assets_dir_, "vm_snapshot_data");
};
settings.isolate_snapshot_data = [this]() {
return GetMapping(assets_dir_, "isolate_snapshot_data", false);
return fml::FileMapping::CreateReadOnly(assets_dir_,
"isolate_snapshot_data");
};
if (DartVM::IsRunningPrecompiledCode()) {
settings.vm_snapshot_instr = [this]() {
return GetMapping(assets_dir_, "vm_snapshot_instr", true);
return fml::FileMapping::CreateReadExecute(assets_dir_,
"vm_snapshot_instr");
};
settings.isolate_snapshot_instr = [this]() {
return GetMapping(assets_dir_, "isolate_snapshot_instr", true);
return fml::FileMapping::CreateReadExecute(assets_dir_,
"isolate_snapshot_instr");
};
}
} else {
settings.application_kernels = [this]() {
std::vector<std::unique_ptr<const fml::Mapping>> kernel_mappings;
kernel_mappings.emplace_back(
GetMapping(assets_dir_, "kernel_blob.bin", false));
fml::FileMapping::CreateReadOnly(assets_dir_, "kernel_blob.bin"));
return kernel_mappings;
};
}

View File

@ -18,32 +18,6 @@ ShellTest::ShellTest()
ShellTest::~ShellTest() = default;
static std::unique_ptr<fml::Mapping> GetMapping(const fml::UniqueFD& directory,
const char* path,
bool executable) {
fml::UniqueFD file = fml::OpenFile(directory, path, false /* create */,
fml::FilePermission::kRead);
if (!file.is_valid()) {
return nullptr;
}
using Prot = fml::FileMapping::Protection;
std::unique_ptr<fml::FileMapping> mapping;
if (executable) {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
} else {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead});
}
if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}
return mapping;
}
void ShellTest::SetSnapshotsAndAssets(Settings& settings) {
if (!assets_dir_.is_valid()) {
return;
@ -55,27 +29,30 @@ void ShellTest::SetSnapshotsAndAssets(Settings& settings) {
// don't need to be explicitly suppiled by the embedder.
if (DartVM::IsRunningPrecompiledCode()) {
settings.vm_snapshot_data = [this]() {
return GetMapping(assets_dir_, "vm_snapshot_data", false);
return fml::FileMapping::CreateReadOnly(assets_dir_, "vm_snapshot_data");
};
settings.isolate_snapshot_data = [this]() {
return GetMapping(assets_dir_, "isolate_snapshot_data", false);
return fml::FileMapping::CreateReadOnly(assets_dir_,
"isolate_snapshot_data");
};
if (DartVM::IsRunningPrecompiledCode()) {
settings.vm_snapshot_instr = [this]() {
return GetMapping(assets_dir_, "vm_snapshot_instr", true);
return fml::FileMapping::CreateReadExecute(assets_dir_,
"vm_snapshot_instr");
};
settings.isolate_snapshot_instr = [this]() {
return GetMapping(assets_dir_, "isolate_snapshot_instr", true);
return fml::FileMapping::CreateReadExecute(assets_dir_,
"isolate_snapshot_instr");
};
}
} else {
settings.application_kernels = [this]() {
std::vector<std::unique_ptr<const fml::Mapping>> kernel_mappings;
kernel_mappings.emplace_back(
GetMapping(assets_dir_, "kernel_blob.bin", false));
fml::FileMapping::CreateReadOnly(assets_dir_, "kernel_blob.bin"));
return kernel_mappings;
};
}

View File

@ -9,46 +9,21 @@
namespace flutter {
namespace testing {
static std::unique_ptr<fml::Mapping> GetMapping(const fml::UniqueFD& directory,
const char* path,
bool executable) {
fml::UniqueFD file = fml::OpenFile(directory, path, false /* create */,
fml::FilePermission::kRead);
if (!file.is_valid()) {
return nullptr;
}
using Prot = fml::FileMapping::Protection;
std::unique_ptr<fml::FileMapping> mapping;
if (executable) {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
} else {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead});
}
if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}
return mapping;
}
EmbedderContext::EmbedderContext(std::string assets_path)
: assets_path_(std::move(assets_path)),
native_resolver_(std::make_shared<::testing::TestDartNativeResolver>()) {
auto assets_dir = fml::OpenDirectory(assets_path_.c_str(), false,
fml::FilePermission::kRead);
vm_snapshot_data_ = GetMapping(assets_dir, "vm_snapshot_data", false);
vm_snapshot_data_ =
fml::FileMapping::CreateReadOnly(assets_dir, "vm_snapshot_data");
isolate_snapshot_data_ =
GetMapping(assets_dir, "isolate_snapshot_data", false);
fml::FileMapping::CreateReadOnly(assets_dir, "isolate_snapshot_data");
if (flutter::DartVM::IsRunningPrecompiledCode()) {
vm_snapshot_instructions_ =
GetMapping(assets_dir, "vm_snapshot_instr", true);
isolate_snapshot_instructions_ =
GetMapping(assets_dir, "isolate_snapshot_instr", true);
fml::FileMapping::CreateReadExecute(assets_dir, "vm_snapshot_instr");
isolate_snapshot_instructions_ = fml::FileMapping::CreateReadExecute(
assets_dir, "isolate_snapshot_instr");
}
isolate_create_callbacks_.push_back(