Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
directory_asset_bundle.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/directory_asset_bundle.h"
6
7#include <regex>
8#include <utility>
9
10#include "flutter/fml/eintr_wrapper.h"
11#include "flutter/fml/file.h"
12#include "flutter/fml/mapping.h"
13#include "flutter/fml/trace_event.h"
14
15namespace flutter {
16
18 fml::UniqueFD descriptor,
19 bool is_valid_after_asset_manager_change)
20 : descriptor_(std::move(descriptor)) {
21 if (!fml::IsDirectory(descriptor_)) {
22 return;
23 }
24 is_valid_after_asset_manager_change_ = is_valid_after_asset_manager_change;
25 is_valid_ = true;
26}
27
29
30// |AssetResolver|
32 return is_valid_;
33}
34
35// |AssetResolver|
37 return is_valid_after_asset_manager_change_;
38}
39
40// |AssetResolver|
44
45// |AssetResolver|
46std::unique_ptr<fml::Mapping> DirectoryAssetBundle::GetAsMapping(
47 const std::string& asset_name) const {
48 if (!is_valid_) {
49 FML_DLOG(WARNING) << "Asset bundle was not valid.";
50 return nullptr;
51 }
52
53 auto mapping = std::make_unique<fml::FileMapping>(fml::OpenFile(
54 descriptor_, asset_name.c_str(), false, fml::FilePermission::kRead));
55
56 if (!mapping->IsValid()) {
57 return nullptr;
58 }
59
60 return mapping;
61}
62
63std::vector<std::unique_ptr<fml::Mapping>> DirectoryAssetBundle::GetAsMappings(
64 const std::string& asset_pattern,
65 const std::optional<std::string>& subdir) const {
66 std::vector<std::unique_ptr<fml::Mapping>> mappings;
67 if (!is_valid_) {
68 FML_DLOG(WARNING) << "Asset bundle was not valid.";
69 return mappings;
70 }
71
72 std::regex asset_regex(asset_pattern);
73 fml::FileVisitor visitor = [&](const fml::UniqueFD& directory,
74 const std::string& filename) {
75 TRACE_EVENT0("flutter", "DirectoryAssetBundle::GetAsMappings FileVisitor");
76
77 if (std::regex_match(filename, asset_regex)) {
78 TRACE_EVENT0("flutter", "Matched File");
79
80 fml::UniqueFD fd = fml::OpenFile(directory, filename.c_str(), false,
82
83 if (fml::IsDirectory(fd)) {
84 return true;
85 }
86
87 auto mapping = std::make_unique<fml::FileMapping>(fd);
88
89 if (mapping && mapping->IsValid()) {
90 mappings.push_back(std::move(mapping));
91 } else {
92 FML_LOG(ERROR) << "Mapping " << filename << " failed";
93 }
94 }
95 return true;
96 };
97 if (!subdir) {
98 fml::VisitFilesRecursively(descriptor_, visitor);
99 } else {
100 fml::UniqueFD subdir_fd =
101 fml::OpenFileReadOnly(descriptor_, subdir.value().c_str());
102 if (!fml::IsDirectory(subdir_fd)) {
103 FML_LOG(ERROR) << "Subdirectory path " << subdir.value()
104 << " is not a directory";
105 return mappings;
106 }
107 fml::VisitFiles(subdir_fd, visitor);
108 }
109
110 return mappings;
111}
112
114 auto other_bundle = other.as_directory_asset_bundle();
115 if (!other_bundle) {
116 return false;
117 }
118 return is_valid_after_asset_manager_change_ ==
119 other_bundle->is_valid_after_asset_manager_change_ &&
120 descriptor_.get() == other_bundle->descriptor_.get();
121}
122
123} // namespace flutter
AssetResolverType
Identifies the type of AssetResolver an instance is.
virtual const DirectoryAssetBundle * as_directory_asset_bundle() const
DirectoryAssetBundle(fml::UniqueFD descriptor, bool is_valid_after_asset_manager_change)
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
bool operator==(const AssetResolver &other) const override
bool IsValidAfterAssetManagerChange() const override
Certain asset resolvers are still valid after the asset manager is replaced before a hot reload,...
AssetResolver::AssetResolverType GetType() const override
Gets the type of AssetResolver this is. Types are defined in AssetResolverType.
const T & get() const
#define FML_DLOG(severity)
Definition logging.h:102
#define FML_LOG(severity)
Definition logging.h:82
fml::UniqueFD OpenFileReadOnly(const fml::UniqueFD &base_directory, const char *path)
Definition file.cc:92
bool VisitFiles(const fml::UniqueFD &directory, const FileVisitor &visitor)
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition file_posix.cc:66
bool VisitFilesRecursively(const fml::UniqueFD &directory, const FileVisitor &visitor)
Definition file.cc:71
std::function< bool(const fml::UniqueFD &directory, const std::string &filename)> FileVisitor
Definition file.h:98
bool IsDirectory(const fml::UniqueFD &directory)
Definition ref_ptr.h:256
#define ERROR(message)
#define TRACE_EVENT0(category_group, name)