Flutter Engine
asset_manager.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/asset_manager.h"
6 
7 #include "flutter/assets/directory_asset_bundle.h"
8 #include "flutter/fml/trace_event.h"
9 
10 namespace flutter {
11 
12 AssetManager::AssetManager() = default;
13 
14 AssetManager::~AssetManager() = default;
15 
16 void AssetManager::PushFront(std::unique_ptr<AssetResolver> resolver) {
17  if (resolver == nullptr || !resolver->IsValid()) {
18  return;
19  }
20 
21  resolvers_.push_front(std::move(resolver));
22 }
23 
24 void AssetManager::PushBack(std::unique_ptr<AssetResolver> resolver) {
25  if (resolver == nullptr || !resolver->IsValid()) {
26  return;
27  }
28 
29  resolvers_.push_back(std::move(resolver));
30 }
31 
33  std::unique_ptr<AssetResolver> updated_asset_resolver,
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 }
55 
56 std::deque<std::unique_ptr<AssetResolver>> AssetManager::TakeResolvers() {
57  return std::move(resolvers_);
58 }
59 
60 // |AssetResolver|
61 std::unique_ptr<fml::Mapping> AssetManager::GetAsMapping(
62  const std::string& asset_name) const {
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 }
77 
78 // |AssetResolver|
79 std::vector<std::unique_ptr<fml::Mapping>> AssetManager::GetAsMappings(
80  const std::string& asset_pattern,
81  const std::optional<std::string>& subdir) const {
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 }
96 
97 // |AssetResolver|
98 bool AssetManager::IsValid() const {
99  return resolvers_.size() > 0;
100 }
101 
102 // |AssetResolver|
104  return false;
105 }
106 
107 // |AssetResolver|
109  return AssetResolverType::kAssetManager;
110 }
111 
112 } // namespace flutter
void PushFront(std::unique_ptr< AssetResolver > resolver)
KeyCallType type
bool IsValid() 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&#39;s name matches a given pattern...
AssetResolver::AssetResolverType GetType() const override
Gets the type of AssetResolver this is. Types are defined in AssetResolverType.
AssetResolverType
Identifies the type of AssetResolver an instance is.
std::unique_ptr< fml::Mapping > GetAsMapping(const std::string &asset_name) const override
std::deque< std::unique_ptr< AssetResolver > > TakeResolvers()
bool IsValidAfterAssetManagerChange() const override
Certain asset resolvers are still valid after the asset manager is replaced before a hot reload...
~AssetManager() override
void PushBack(std::unique_ptr< AssetResolver > resolver)
#define TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
Definition: trace_event.h:94
#define FML_DLOG(severity)
Definition: logging.h:85
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 AssetResol...