Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1c975f1

Browse files
authored
AssetResolver updating in AssetManager for Dynamic features (#23130)
1 parent 596bae2 commit 1c975f1

22 files changed

+387
-56
lines changed

assets/asset_manager.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ void AssetManager::PushBack(std::unique_ptr<AssetResolver> resolver) {
2929
resolvers_.push_back(std::move(resolver));
3030
}
3131

32+
void AssetManager::UpdateResolverByType(
33+
std::unique_ptr<AssetResolver> updated_asset_resolver,
34+
AssetResolver::AssetResolverType type) {
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+
3256
std::deque<std::unique_ptr<AssetResolver>> AssetManager::TakeResolvers() {
3357
return std::move(resolvers_);
3458
}
@@ -79,4 +103,9 @@ bool AssetManager::IsValidAfterAssetManagerChange() const {
79103
return false;
80104
}
81105

106+
// |AssetResolver|
107+
AssetResolver::AssetResolverType AssetManager::GetType() const {
108+
return AssetResolverType::kAssetManager;
109+
}
110+
82111
} // namespace flutter

assets/asset_manager.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,34 @@ class AssetManager final : public AssetResolver {
2525

2626
void PushBack(std::unique_ptr<AssetResolver> resolver);
2727

28+
//--------------------------------------------------------------------------
29+
/// @brief Replaces an asset resolver of the specified `type` with
30+
/// `updated_asset_resolver`. The matching AssetResolver is
31+
/// removed and replaced with `updated_asset_resolvers`.
32+
///
33+
/// AssetResolvers should be updated when the existing resolver
34+
/// becomes obsolete and a newer one becomes available that
35+
/// provides updated access to the same type of assets as the
36+
/// existing one. This update process is meant to be performed
37+
/// at runtime.
38+
///
39+
/// If a null resolver is provided, nothing will be done. If no
40+
/// matching resolver is found, the provided resolver will be
41+
/// added to the end of the AssetManager resolvers queue. The
42+
/// replacement only occurs with the first matching resolver.
43+
/// Any additional matching resolvers are untouched.
44+
///
45+
/// @param[in] updated_asset_resolver The asset resolver to replace the
46+
/// resolver of matching type with.
47+
///
48+
/// @param[in] type The type of AssetResolver to update. Only resolvers of
49+
/// the specified type will be replaced by the updated
50+
/// resolver.
51+
///
52+
void UpdateResolverByType(
53+
std::unique_ptr<AssetResolver> updated_asset_resolver,
54+
AssetResolver::AssetResolverType type);
55+
2856
std::deque<std::unique_ptr<AssetResolver>> TakeResolvers();
2957

3058
// |AssetResolver|
@@ -33,6 +61,9 @@ class AssetManager final : public AssetResolver {
3361
// |AssetResolver|
3462
bool IsValidAfterAssetManagerChange() const override;
3563

64+
// |AssetResolver|
65+
AssetResolver::AssetResolverType GetType() const override;
66+
3667
// |AssetResolver|
3768
std::unique_ptr<fml::Mapping> GetAsMapping(
3869
const std::string& asset_name) const override;

assets/asset_resolver.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ class AssetResolver {
1919

2020
virtual ~AssetResolver() = default;
2121

22+
//----------------------------------------------------------------------------
23+
/// @brief Identifies the type of AssetResolver an instance is.
24+
///
25+
enum AssetResolverType {
26+
kAssetManager,
27+
kApkAssetProvider,
28+
kDirectoryAssetBundle
29+
};
30+
2231
virtual bool IsValid() const = 0;
2332

2433
//----------------------------------------------------------------------------
@@ -39,6 +48,14 @@ class AssetResolver {
3948
///
4049
virtual bool IsValidAfterAssetManagerChange() const = 0;
4150

51+
//----------------------------------------------------------------------------
52+
/// @brief Gets the type of AssetResolver this is. Types are defined in
53+
/// AssetResolverType.
54+
///
55+
/// @return Returns the AssetResolverType that this resolver is.
56+
///
57+
virtual AssetResolverType GetType() const = 0;
58+
4259
[[nodiscard]] virtual std::unique_ptr<fml::Mapping> GetAsMapping(
4360
const std::string& asset_name) const = 0;
4461

assets/directory_asset_bundle.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ bool DirectoryAssetBundle::IsValidAfterAssetManagerChange() const {
3636
return is_valid_after_asset_manager_change_;
3737
}
3838

39+
// |AssetResolver|
40+
AssetResolver::AssetResolverType DirectoryAssetBundle::GetType() const {
41+
return AssetResolver::AssetResolverType::kDirectoryAssetBundle;
42+
}
43+
3944
// |AssetResolver|
4045
std::unique_ptr<fml::Mapping> DirectoryAssetBundle::GetAsMapping(
4146
const std::string& asset_name) const {

assets/directory_asset_bundle.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class DirectoryAssetBundle : public AssetResolver {
3030
// |AssetResolver|
3131
bool IsValidAfterAssetManagerChange() const override;
3232

33+
// |AssetResolver|
34+
AssetResolver::AssetResolverType GetType() const override;
35+
3336
// |AssetResolver|
3437
std::unique_ptr<fml::Mapping> GetAsMapping(
3538
const std::string& asset_name) const override;

shell/common/platform_view.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ void PlatformView::LoadDartDeferredLibraryError(intptr_t loading_unit_id,
170170
const std::string error_message,
171171
bool transient) {}
172172

173-
void PlatformView::UpdateAssetManager(
174-
std::shared_ptr<AssetManager> asset_manager) {}
173+
void PlatformView::UpdateAssetResolverByType(
174+
std::unique_ptr<AssetResolver> updated_asset_resolver,
175+
AssetResolver::AssetResolverType type) {
176+
delegate_.UpdateAssetResolverByType(std::move(updated_asset_resolver), type);
177+
}
175178

176179
} // namespace flutter

shell/common/platform_view.h

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,34 @@ class PlatformView {
273273
const std::string error_message,
274274
bool transient) = 0;
275275

276-
// TODO(garyq): Implement a proper asset_resolver replacement instead of
277-
// overwriting the entire asset manager.
278276
//--------------------------------------------------------------------------
279-
/// @brief Sets the asset manager of the engine to asset_manager
280-
///
281-
/// @param[in] asset_manager The asset manager to use.
282-
///
283-
virtual void UpdateAssetManager(
284-
std::shared_ptr<AssetManager> asset_manager) = 0;
277+
/// @brief Replaces the asset resolver handled by the engine's
278+
/// AssetManager of the specified `type` with
279+
/// `updated_asset_resolver`. The matching AssetResolver is
280+
/// removed and replaced with `updated_asset_resolvers`.
281+
///
282+
/// AssetResolvers should be updated when the existing resolver
283+
/// becomes obsolete and a newer one becomes available that
284+
/// provides updated access to the same type of assets as the
285+
/// existing one. This update process is meant to be performed
286+
/// at runtime.
287+
///
288+
/// If a null resolver is provided, nothing will be done. If no
289+
/// matching resolver is found, the provided resolver will be
290+
/// added to the end of the AssetManager resolvers queue. The
291+
/// replacement only occurs with the first matching resolver.
292+
/// Any additional matching resolvers are untouched.
293+
///
294+
/// @param[in] updated_asset_resolver The asset resolver to replace the
295+
/// resolver of matching type with.
296+
///
297+
/// @param[in] type The type of AssetResolver to update. Only resolvers of
298+
/// the specified type will be replaced by the updated
299+
/// resolver.
300+
///
301+
virtual void UpdateAssetResolverByType(
302+
std::unique_ptr<AssetResolver> updated_asset_resolver,
303+
AssetResolver::AssetResolverType type) = 0;
285304
};
286305

287306
//----------------------------------------------------------------------------
@@ -720,14 +739,34 @@ class PlatformView {
720739
const std::string error_message,
721740
bool transient);
722741

723-
// TODO(garyq): Implement a proper asset_resolver replacement instead of
724-
// overwriting the entire asset manager.
725742
//--------------------------------------------------------------------------
726-
/// @brief Sets the asset manager of the engine to asset_manager
727-
///
728-
/// @param[in] asset_manager The asset manager to use.
729-
///
730-
virtual void UpdateAssetManager(std::shared_ptr<AssetManager> asset_manager);
743+
/// @brief Replaces the asset resolver handled by the engine's
744+
/// AssetManager of the specified `type` with
745+
/// `updated_asset_resolver`. The matching AssetResolver is
746+
/// removed and replaced with `updated_asset_resolvers`.
747+
///
748+
/// AssetResolvers should be updated when the existing resolver
749+
/// becomes obsolete and a newer one becomes available that
750+
/// provides updated access to the same type of assets as the
751+
/// existing one. This update process is meant to be performed
752+
/// at runtime.
753+
///
754+
/// If a null resolver is provided, nothing will be done. If no
755+
/// matching resolver is found, the provided resolver will be
756+
/// added to the end of the AssetManager resolvers queue. The
757+
/// replacement only occurs with the first matching resolver.
758+
/// Any additional matching resolvers are untouched.
759+
///
760+
/// @param[in] updated_asset_resolver The asset resolver to replace the
761+
/// resolver of matching type with.
762+
///
763+
/// @param[in] type The type of AssetResolver to update. Only resolvers of
764+
/// the specified type will be replaced by the updated
765+
/// resolver.
766+
///
767+
virtual void UpdateAssetResolverByType(
768+
std::unique_ptr<AssetResolver> updated_asset_resolver,
769+
AssetResolver::AssetResolverType type);
731770

732771
protected:
733772
PlatformView::Delegate& delegate_;

shell/common/shell.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,8 +1225,11 @@ void Shell::LoadDartDeferredLibraryError(intptr_t loading_unit_id,
12251225
transient);
12261226
}
12271227

1228-
void Shell::UpdateAssetManager(std::shared_ptr<AssetManager> asset_manager) {
1229-
engine_->UpdateAssetManager(std::move(asset_manager));
1228+
void Shell::UpdateAssetResolverByType(
1229+
std::unique_ptr<AssetResolver> updated_asset_resolver,
1230+
AssetResolver::AssetResolverType type) {
1231+
engine_->GetAssetManager()->UpdateResolverByType(
1232+
std::move(updated_asset_resolver), type);
12301233
}
12311234

12321235
// |Engine::Delegate|

shell/common/shell.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,9 @@ class Shell final : public PlatformView::Delegate,
536536
bool transient) override;
537537

538538
// |PlatformView::Delegate|
539-
void UpdateAssetManager(std::shared_ptr<AssetManager> asset_manager) override;
539+
void UpdateAssetResolverByType(
540+
std::unique_ptr<AssetResolver> updated_asset_resolver,
541+
AssetResolver::AssetResolverType type) override;
540542

541543
// |Animator::Delegate|
542544
void OnAnimatorBeginFrame(fml::TimePoint frame_target_time) override;

0 commit comments

Comments
 (0)