Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
flutter::AssetManager Class Referencefinal

#include <asset_manager.h>

Inheritance diagram for flutter::AssetManager:
flutter::AssetResolver

Public Member Functions

 AssetManager ()
 
 ~AssetManager () override
 
bool PushFront (std::unique_ptr< AssetResolver > resolver)
 Adds an asset resolver to the front of the resolver queue. Assets would be loaded from this resolver before any follwing resolvers. More...
 
bool PushBack (std::unique_ptr< AssetResolver > resolver)
 Adds an asset resolver to the end of the resolver queue. Assets would be loaded from this resolver after any previous resolvers. More...
 
void UpdateResolverByType (std::unique_ptr< AssetResolver > updated_asset_resolver, AssetResolver::AssetResolverType type)
 Replaces an asset resolver of the specified type with updated_asset_resolver. The matching AssetResolver is removed and replaced with updated_asset_resolvers. More...
 
std::deque< std::unique_ptr< AssetResolver > > TakeResolvers ()
 
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. More...
 
AssetResolver::AssetResolverType GetType () const override
 Gets the type of AssetResolver this is. Types are defined in AssetResolverType. More...
 
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. More...
 
bool operator== (const AssetResolver &other) const override
 
const AssetManageras_asset_manager () const 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
 
virtual const DirectoryAssetBundleas_directory_asset_bundle () const
 
virtual bool IsValid () const =0
 
virtual bool IsValidAfterAssetManagerChange () const =0
 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. More...
 
virtual AssetResolverType GetType () const =0
 Gets the type of AssetResolver this is. Types are defined in AssetResolverType. More...
 
virtual std::unique_ptr< fml::MappingGetAsMapping (const std::string &asset_name) const =0
 
virtual std::vector< std::unique_ptr< fml::Mapping > > GetAsMappings (const std::string &asset_pattern, const std::optional< std::string > &subdir) const
 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. More...
 
virtual bool operator== (const AssetResolver &other) const =0
 
bool operator!= (const AssetResolver &other) const
 

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 19 of file asset_manager.h.

Constructor & Destructor Documentation

◆ AssetManager()

flutter::AssetManager::AssetManager ( )
default

◆ ~AssetManager()

flutter::AssetManager::~AssetManager ( )
overridedefault

Member Function Documentation

◆ as_asset_manager()

const AssetManager * flutter::AssetManager::as_asset_manager ( ) const
inlineoverridevirtual

Reimplemented from flutter::AssetResolver.

Definition at line 95 of file asset_manager.h.

95{ return this; }

◆ GetAsMapping()

std::unique_ptr< fml::Mapping > flutter::AssetManager::GetAsMapping ( const std::string &  asset_name) const
overridevirtual

Implements flutter::AssetResolver.

Definition at line 63 of file asset_manager.cc.

64 {
65 if (asset_name.empty()) {
66 return nullptr;
67 }
68 TRACE_EVENT1("flutter", "AssetManager::GetAsMapping", "name",
69 asset_name.c_str());
70 for (const auto& resolver : resolvers_) {
71 auto mapping = resolver->GetAsMapping(asset_name);
72 if (mapping != nullptr) {
73 return mapping;
74 }
75 }
76 FML_DLOG(WARNING) << "Could not find asset: " << asset_name;
77 return nullptr;
78}
#define FML_DLOG(severity)
Definition: logging.h:102
#define TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
Definition: trace_event.h:141

◆ GetAsMappings()

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

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 81 of file asset_manager.cc.

83 {
84 std::vector<std::unique_ptr<fml::Mapping>> mappings;
85 if (asset_pattern.empty()) {
86 return mappings;
87 }
88 TRACE_EVENT1("flutter", "AssetManager::GetAsMappings", "pattern",
89 asset_pattern.c_str());
90 for (const auto& resolver : resolvers_) {
91 auto resolver_mappings = resolver->GetAsMappings(asset_pattern, subdir);
92 mappings.insert(mappings.end(),
93 std::make_move_iterator(resolver_mappings.begin()),
94 std::make_move_iterator(resolver_mappings.end()));
95 }
96 return mappings;
97}

◆ GetType()

AssetResolver::AssetResolverType flutter::AssetManager::GetType ( ) const
overridevirtual

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 110 of file asset_manager.cc.

110 {
111 return AssetResolverType::kAssetManager;
112}

◆ IsValid()

bool flutter::AssetManager::IsValid ( ) const
overridevirtual

Implements flutter::AssetResolver.

Definition at line 100 of file asset_manager.cc.

100 {
101 return !resolvers_.empty();
102}

◆ IsValidAfterAssetManagerChange()

bool flutter::AssetManager::IsValidAfterAssetManagerChange ( ) const
overridevirtual

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 105 of file asset_manager.cc.

105 {
106 return false;
107}

◆ operator==()

bool flutter::AssetManager::operator== ( const AssetResolver other) const
overridevirtual

Implements flutter::AssetResolver.

Definition at line 114 of file asset_manager.cc.

114 {
115 const AssetManager* other_manager = other.as_asset_manager();
116 if (!other_manager) {
117 return false;
118 }
119 if (resolvers_.size() != other_manager->resolvers_.size()) {
120 return false;
121 }
122
123 for (size_t i = 0; i < resolvers_.size(); i++) {
124 if (*resolvers_[i] != *other_manager->resolvers_[i]) {
125 return false;
126 }
127 }
128 return true;
129}

◆ PushBack()

bool flutter::AssetManager::PushBack ( std::unique_ptr< AssetResolver resolver)

Adds an asset resolver to the end of the resolver queue. Assets would be loaded from this resolver after any previous resolvers.

Returns
Returns whether this resolver is valid and has been added to the resolver queue.

Definition at line 25 of file asset_manager.cc.

25 {
26 if (resolver == nullptr || !resolver->IsValid()) {
27 return false;
28 }
29
30 resolvers_.push_back(std::move(resolver));
31 return true;
32}

◆ PushFront()

bool flutter::AssetManager::PushFront ( std::unique_ptr< AssetResolver resolver)

Adds an asset resolver to the front of the resolver queue. Assets would be loaded from this resolver before any follwing resolvers.

Returns
Returns whether this resolver is valid and has been added to the resolver queue.

Definition at line 16 of file asset_manager.cc.

16 {
17 if (resolver == nullptr || !resolver->IsValid()) {
18 return false;
19 }
20
21 resolvers_.push_front(std::move(resolver));
22 return true;
23}

◆ TakeResolvers()

std::deque< std::unique_ptr< AssetResolver > > flutter::AssetManager::TakeResolvers ( )

Definition at line 58 of file asset_manager.cc.

58 {
59 return std::move(resolvers_);
60}

◆ UpdateResolverByType()

void flutter::AssetManager::UpdateResolverByType ( std::unique_ptr< AssetResolver updated_asset_resolver,
AssetResolver::AssetResolverType  type 
)

Replaces an asset resolver of the specified type with updated_asset_resolver. The matching AssetResolver is removed and replaced with updated_asset_resolvers.

AssetResolvers should be updated when the existing resolver becomes obsolete and a newer one becomes available that provides updated access to the same type of assets as the existing one. This update process is meant to be performed at runtime.

If a null resolver is provided, nothing will be done. If no matching resolver is found, the provided resolver will be added to the end of the AssetManager resolvers queue. The replacement only occurs with the first matching resolver. Any additional matching resolvers are untouched.

Parameters
[in]updated_asset_resolverThe asset resolver to replace the resolver of matching type with.
[in]typeThe type of AssetResolver to update. Only resolvers of the specified type will be replaced by the updated resolver.

Definition at line 34 of file asset_manager.cc.

36 {
37 if (updated_asset_resolver == nullptr) {
38 return;
39 }
40 bool updated = false;
41 std::deque<std::unique_ptr<AssetResolver>> new_resolvers;
42 for (auto& old_resolver : resolvers_) {
43 if (!updated && old_resolver->GetType() == type) {
44 // Push the replacement updated resolver in place of the old_resolver.
45 new_resolvers.push_back(std::move(updated_asset_resolver));
46 updated = true;
47 } else {
48 new_resolvers.push_back(std::move(old_resolver));
49 }
50 }
51 // Append resolver to the end if not used as a replacement.
52 if (!updated) {
53 new_resolvers.push_back(std::move(updated_asset_resolver));
54 }
55 resolvers_.swap(new_resolvers);
56}
GLenum type

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