Flutter Engine
directory_asset_bundle.cc
Go to the documentation of this file.
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 #include "flutter/assets/directory_asset_bundle.h"
6 
7 #include <regex>
8 #include <utility>
9 
10 #include "flutter/fml/eintr_wrapper.h"
11 #include "flutter/fml/file.h"
12 #include "flutter/fml/mapping.h"
13 #include "flutter/fml/trace_event.h"
14 
15 namespace flutter {
16 
18  fml::UniqueFD descriptor,
19  bool is_valid_after_asset_manager_change)
20  : descriptor_(std::move(descriptor)) {
21  if (!fml::IsDirectory(descriptor_)) {
22  return;
23  }
24  is_valid_after_asset_manager_change_ = is_valid_after_asset_manager_change;
25  is_valid_ = true;
26 }
27 
29 
30 // |AssetResolver|
31 bool DirectoryAssetBundle::IsValid() const {
32  return is_valid_;
33 }
34 
35 // |AssetResolver|
36 bool DirectoryAssetBundle::IsValidAfterAssetManagerChange() const {
37  return is_valid_after_asset_manager_change_;
38 }
39 
40 // |AssetResolver|
41 AssetResolver::AssetResolverType DirectoryAssetBundle::GetType() const {
42  return AssetResolver::AssetResolverType::kDirectoryAssetBundle;
43 }
44 
45 // |AssetResolver|
46 std::unique_ptr<fml::Mapping> DirectoryAssetBundle::GetAsMapping(
47  const std::string& asset_name) const {
48  if (!is_valid_) {
49  FML_DLOG(WARNING) << "Asset bundle was not valid.";
50  return nullptr;
51  }
52 
53  auto mapping = std::make_unique<fml::FileMapping>(fml::OpenFile(
54  descriptor_, asset_name.c_str(), false, fml::FilePermission::kRead));
55 
56  if (!mapping->IsValid()) {
57  return nullptr;
58  }
59 
60  return mapping;
61 }
62 
63 std::vector<std::unique_ptr<fml::Mapping>> DirectoryAssetBundle::GetAsMappings(
64  const std::string& asset_pattern,
65  const std::optional<std::string>& subdir) const {
66  std::vector<std::unique_ptr<fml::Mapping>> mappings;
67  if (!is_valid_) {
68  FML_DLOG(WARNING) << "Asset bundle was not valid.";
69  return mappings;
70  }
71 
72  std::regex asset_regex(asset_pattern);
73  fml::FileVisitor visitor = [&](const fml::UniqueFD& directory,
74  const std::string& filename) {
75  TRACE_EVENT0("flutter", "DirectoryAssetBundle::GetAsMappings FileVisitor");
76 
77  if (std::regex_match(filename, asset_regex)) {
78  TRACE_EVENT0("flutter", "Matched File");
79 
80  fml::UniqueFD fd = fml::OpenFile(directory, filename.c_str(), false,
82 
83  if (fml::IsDirectory(fd)) {
84  return true;
85  }
86 
87  auto mapping = std::make_unique<fml::FileMapping>(fd);
88 
89  if (mapping && mapping->IsValid()) {
90  mappings.push_back(std::move(mapping));
91  } else {
92  FML_LOG(ERROR) << "Mapping " << filename << " failed";
93  }
94  }
95  return true;
96  };
97  if (!subdir) {
98  fml::VisitFilesRecursively(descriptor_, visitor);
99  } else {
100  fml::UniqueFD subdir_fd =
101  fml::OpenFileReadOnly(descriptor_, subdir.value().c_str());
102  if (!fml::IsDirectory(subdir_fd)) {
103  FML_LOG(ERROR) << "Subdirectory path " << subdir.value()
104  << " is not a directory";
105  return mappings;
106  }
107  fml::VisitFiles(subdir_fd, visitor);
108  }
109 
110  return mappings;
111 }
112 
113 } // namespace flutter
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:90
std::function< bool(const fml::UniqueFD &directory, const std::string &filename)> FileVisitor
Definition: file.h:98
Definition: ref_ptr.h:255
#define FML_LOG(severity)
Definition: logging.h:65
DirectoryAssetBundle(fml::UniqueFD descriptor, bool is_valid_after_asset_manager_change)
AssetResolverType
Identifies the type of AssetResolver an instance is.
bool VisitFiles(const fml::UniqueFD &directory, const FileVisitor &visitor)
Definition: file_posix.cc:235
bool IsDirectory(const fml::UniqueFD &directory)
Definition: file_posix.cc:126
bool VisitFilesRecursively(const fml::UniqueFD &directory, const FileVisitor &visitor)
Definition: file.cc:71
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition: file_posix.cc:65
#define FML_DLOG(severity)
Definition: logging.h:85
fml::UniqueFD OpenFileReadOnly(const fml::UniqueFD &base_directory, const char *path)
Definition: file.cc:92