Flutter Engine
The 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
10namespace flutter {
11
13
15
16bool AssetManager::PushFront(std::unique_ptr<AssetResolver> resolver) {
17 if (resolver == nullptr || !resolver->IsValid()) {
18 return false;
19 }
20
21 resolvers_.push_front(std::move(resolver));
22 return true;
23}
24
25bool AssetManager::PushBack(std::unique_ptr<AssetResolver> resolver) {
26 if (resolver == nullptr || !resolver->IsValid()) {
27 return false;
28 }
29
30 resolvers_.push_back(std::move(resolver));
31 return true;
32}
33
35 std::unique_ptr<AssetResolver> updated_asset_resolver,
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}
57
58std::deque<std::unique_ptr<AssetResolver>> AssetManager::TakeResolvers() {
59 return std::move(resolvers_);
60}
61
62// |AssetResolver|
63std::unique_ptr<fml::Mapping> AssetManager::GetAsMapping(
64 const std::string& asset_name) const {
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}
79
80// |AssetResolver|
81std::vector<std::unique_ptr<fml::Mapping>> AssetManager::GetAsMappings(
82 const std::string& asset_pattern,
83 const std::optional<std::string>& subdir) const {
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}
98
99// |AssetResolver|
101 return !resolvers_.empty();
102}
103
104// |AssetResolver|
106 return false;
107}
108
109// |AssetResolver|
111 return AssetResolverType::kAssetManager;
112}
113
114bool AssetManager::operator==(const AssetResolver& other) const {
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}
130
131} // namespace flutter
GLenum type
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...
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....
std::unique_ptr< fml::Mapping > GetAsMapping(const std::string &asset_name) const override
std::deque< std::unique_ptr< AssetResolver > > TakeResolvers()
bool IsValid() const override
bool operator==(const AssetResolver &other) const override
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 af...
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 ...
~AssetManager() override
AssetResolver::AssetResolverType GetType() const override
Gets the type of AssetResolver this is. Types are defined in AssetResolverType.
bool IsValidAfterAssetManagerChange() const override
Certain asset resolvers are still valid after the asset manager is replaced before a hot reload,...
virtual const AssetManager * as_asset_manager() const
AssetResolverType
Identifies the type of AssetResolver an instance is.
#define FML_DLOG(severity)
Definition: logging.h:102
#define TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
Definition: trace_event.h:141