Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | List of all members
flutter::DirectoryAssetBundle Class Reference

#include <directory_asset_bundle.h>

Inheritance diagram for flutter::DirectoryAssetBundle:
flutter::AssetResolver

Public Member Functions

 DirectoryAssetBundle (fml::UniqueFD descriptor, bool is_valid_after_asset_manager_change)
 
 ~DirectoryAssetBundle () override
 
- Public Member Functions inherited from flutter::AssetResolver
 AssetResolver ()=default
 
virtual ~AssetResolver ()=default
 
virtual const AssetManageras_asset_manager () const
 
virtual const APKAssetProvideras_apk_asset_provider () const
 
bool operator!= (const AssetResolver &other) const
 

Private Member Functions

bool IsValid () const override
 
bool IsValidAfterAssetManagerChange () const override
 Certain asset resolvers are still valid after the asset manager is replaced before a hot reload, or after a new run configuration is created during a hot restart. By preserving these resolvers and re-inserting them into the new resolver or run configuration, the tooling can avoid needing to sync all application assets through the Dart devFS upon connecting to the VM Service. Besides improving the startup performance of running a Flutter application, it also reduces the occurrence of tool failures due to repeated network flakes caused by damaged cables or hereto unknown bugs in the Dart HTTP server implementation.
 
AssetResolver::AssetResolverType GetType () const override
 Gets the type of AssetResolver this is. Types are defined in AssetResolverType.
 
std::unique_ptr< fml::MappingGetAsMapping (const std::string &asset_name) const override
 
std::vector< std::unique_ptr< fml::Mapping > > GetAsMappings (const std::string &asset_pattern, const std::optional< std::string > &subdir) const override
 Same as GetAsMapping() but returns mappings for all files who's name matches a given pattern. Returns empty vector if no matching assets are found.
 
bool operator== (const AssetResolver &other) const override
 
const DirectoryAssetBundleas_directory_asset_bundle () const override
 

Additional Inherited Members

- Public Types inherited from flutter::AssetResolver
enum  AssetResolverType { kAssetManager , kApkAssetProvider , kDirectoryAssetBundle }
 Identifies the type of AssetResolver an instance is. More...
 

Detailed Description

Definition at line 16 of file directory_asset_bundle.h.

Constructor & Destructor Documentation

◆ DirectoryAssetBundle()

flutter::DirectoryAssetBundle::DirectoryAssetBundle ( fml::UniqueFD  descriptor,
bool  is_valid_after_asset_manager_change 
)

Definition at line 17 of file directory_asset_bundle.cc.

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}
bool IsDirectory(const fml::UniqueFD &directory)

◆ ~DirectoryAssetBundle()

flutter::DirectoryAssetBundle::~DirectoryAssetBundle ( )
overridedefault

Member Function Documentation

◆ as_directory_asset_bundle()

const DirectoryAssetBundle * flutter::DirectoryAssetBundle::as_directory_asset_bundle ( ) const
inlineoverrideprivatevirtual

Reimplemented from flutter::AssetResolver.

Definition at line 50 of file directory_asset_bundle.h.

50 {
51 return this;
52 }

◆ GetAsMapping()

std::unique_ptr< fml::Mapping > flutter::DirectoryAssetBundle::GetAsMapping ( const std::string &  asset_name) const
overrideprivatevirtual

Implements flutter::AssetResolver.

Definition at line 46 of file directory_asset_bundle.cc.

47 {
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}
#define FML_DLOG(severity)
Definition logging.h:102
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:66

◆ GetAsMappings()

std::vector< std::unique_ptr< fml::Mapping > > flutter::DirectoryAssetBundle::GetAsMappings ( const std::string &  asset_pattern,
const std::optional< std::string > &  subdir 
) const
overrideprivatevirtual

Same as GetAsMapping() but returns mappings for all files who's name matches a given pattern. Returns empty vector if no matching assets are found.

Parameters
[in]asset_patternThe pattern to match file names against.
[in]subdirOptional subdirectory in which to search for files. If supplied this function does a flat search within the subdirectory instead of a recursive search through the entire assets directory.
Returns
Returns a vector of mappings of files which match the search parameters.

Reimplemented from flutter::AssetResolver.

Definition at line 63 of file directory_asset_bundle.cc.

65 {
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}
#define FML_LOG(severity)
Definition logging.h:82
fml::UniqueFD OpenFileReadOnly(const fml::UniqueFD &base_directory, const char *path)
Definition file.cc:92
bool VisitFiles(const fml::UniqueFD &directory, const FileVisitor &visitor)
bool VisitFilesRecursively(const fml::UniqueFD &directory, const FileVisitor &visitor)
Definition file.cc:71
std::function< bool(const fml::UniqueFD &directory, const std::string &filename)> FileVisitor
Definition file.h:98
#define ERROR(message)
#define TRACE_EVENT0(category_group, name)

◆ GetType()

AssetResolver::AssetResolverType flutter::DirectoryAssetBundle::GetType ( ) const
overrideprivatevirtual

Gets the type of AssetResolver this is. Types are defined in AssetResolverType.

Returns
Returns the AssetResolverType that this resolver is.

Implements flutter::AssetResolver.

Definition at line 41 of file directory_asset_bundle.cc.

◆ IsValid()

bool flutter::DirectoryAssetBundle::IsValid ( ) const
overrideprivatevirtual

Implements flutter::AssetResolver.

Definition at line 31 of file directory_asset_bundle.cc.

31 {
32 return is_valid_;
33}

◆ IsValidAfterAssetManagerChange()

bool flutter::DirectoryAssetBundle::IsValidAfterAssetManagerChange ( ) const
overrideprivatevirtual

Certain asset resolvers are still valid after the asset manager is replaced before a hot reload, or after a new run configuration is created during a hot restart. By preserving these resolvers and re-inserting them into the new resolver or run configuration, the tooling can avoid needing to sync all application assets through the Dart devFS upon connecting to the VM Service. Besides improving the startup performance of running a Flutter application, it also reduces the occurrence of tool failures due to repeated network flakes caused by damaged cables or hereto unknown bugs in the Dart HTTP server implementation.

Returns
Returns whether this resolver is valid after the asset manager or run configuration is updated.

Implements flutter::AssetResolver.

Definition at line 36 of file directory_asset_bundle.cc.

36 {
37 return is_valid_after_asset_manager_change_;
38}

◆ operator==()

bool flutter::DirectoryAssetBundle::operator== ( const AssetResolver other) const
overrideprivatevirtual

Implements flutter::AssetResolver.

Definition at line 113 of file directory_asset_bundle.cc.

113 {
114 auto other_bundle = other.as_directory_asset_bundle();
115 if (!other_bundle) {
116 return false;
117 }
118 return is_valid_after_asset_manager_change_ ==
119 other_bundle->is_valid_after_asset_manager_change_ &&
120 descriptor_.get() == other_bundle->descriptor_.get();
121}
const T & get() const

The documentation for this class was generated from the following files: