Flutter Engine
flutter::AssetManager Class Referencefinal

#include <asset_manager.h>

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

Public Member Functions

 AssetManager ()
 
 ~AssetManager () override
 
void PushFront (std::unique_ptr< AssetResolver > resolver)
 
void PushBack (std::unique_ptr< AssetResolver > resolver)
 
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...
 
- Public Member Functions inherited from flutter::AssetResolver
 AssetResolver ()=default
 
virtual ~AssetResolver ()=default
 

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

◆ GetAsMapping()

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

Implements flutter::AssetResolver.

Definition at line 61 of file asset_manager.cc.

References FML_DLOG, and TRACE_EVENT1.

Referenced by flutter::testing::TEST_F().

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

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

References TRACE_EVENT1.

Referenced by flutter::testing::TEST_F().

81  {
82  std::vector<std::unique_ptr<fml::Mapping>> mappings;
83  if (asset_pattern.size() == 0) {
84  return mappings;
85  }
86  TRACE_EVENT1("flutter", "AssetManager::GetAsMappings", "pattern",
87  asset_pattern.c_str());
88  for (const auto& resolver : resolvers_) {
89  auto resolver_mappings = resolver->GetAsMappings(asset_pattern, subdir);
90  mappings.insert(mappings.end(),
91  std::make_move_iterator(resolver_mappings.begin()),
92  std::make_move_iterator(resolver_mappings.end()));
93  }
94  return mappings;
95 }
#define TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
Definition: trace_event.h:94

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

108  {
109  return AssetResolverType::kAssetManager;
110 }

◆ IsValid()

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

Implements flutter::AssetResolver.

Definition at line 98 of file asset_manager.cc.

98  {
99  return resolvers_.size() > 0;
100 }

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

103  {
104  return false;
105 }

◆ PushBack()

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

Definition at line 24 of file asset_manager.cc.

Referenced by flutter::testing::TEST_F().

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

◆ PushFront()

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

Definition at line 16 of file asset_manager.cc.

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

◆ TakeResolvers()

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

Definition at line 56 of file asset_manager.cc.

56  {
57  return std::move(resolvers_);
58 }

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

References type.

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

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