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

Commit c4d8d32

Browse files
committed
Allow additional expose_dirs in flutter_runner
Users can add an expose_dirs entry to the program field in a component's cml file to optionally expose extra directories from their out/ directory. BUG: fxbug.dev/89690
1 parent 2516ea6 commit c4d8d32

File tree

4 files changed

+125
-27
lines changed

4 files changed

+125
-27
lines changed

shell/platform/fuchsia/flutter/component_v2.cc

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ constexpr char kAssetsKey[] = "assets";
5151
// "args" are how the component specifies arguments to the runner.
5252
constexpr char kArgsKey[] = "args";
5353
constexpr char kOldGenHeapSizeKey[] = "old_gen_heap_size";
54+
constexpr char kExposeDirsKey[] = "expose_dirs";
5455

5556
constexpr char kTmpPath[] = "/tmp";
5657
constexpr char kServiceRootPath[] = "/svc";
@@ -88,6 +89,18 @@ void ParseArgs(std::vector<std::string>& args, ProgramMetadata* metadata) {
8889
<< old_gen_heap_size_option;
8990
}
9091
}
92+
93+
std::string expose_dirs_option;
94+
if (parsed_args.GetOptionValue(kExposeDirsKey, &expose_dirs_option)) {
95+
// Parse the comma delimited string
96+
std::vector<std::string> expose_dirs;
97+
std::stringstream s(expose_dirs_option);
98+
while (s.good()) {
99+
std::string dir;
100+
getline(s, dir, ','); // get first string delimited by comma
101+
metadata->expose_dirs.push_back(dir);
102+
}
103+
}
91104
}
92105

93106
} // namespace
@@ -258,32 +271,41 @@ ComponentV2::ComponentV2(
258271
fuchsia::io::OPEN_RIGHT_WRITABLE,
259272
cloned_directory_ptr_.NewRequest());
260273

261-
cloned_directory_ptr_.events().OnOpen =
262-
[this](zx_status_t status, std::unique_ptr<fuchsia::io::NodeInfo> info) {
263-
cloned_directory_ptr_.Unbind();
264-
if (status != ZX_OK) {
265-
FML_LOG(ERROR)
266-
<< "could not bind out directory for flutter component("
267-
<< debug_label_ << "): " << zx_status_get_string(status);
268-
return;
269-
}
270-
const char* other_dirs[] = {"debug", "ctrl", "diagnostics"};
271-
// add other directories as RemoteDirs.
272-
for (auto& dir_str : other_dirs) {
273-
fuchsia::io::DirectoryHandle dir;
274-
auto request = dir.NewRequest().TakeChannel();
275-
auto status = fdio_service_connect_at(directory_ptr_.channel().get(),
276-
dir_str, request.release());
277-
if (status == ZX_OK) {
278-
outgoing_dir_->AddEntry(
279-
dir_str, std::make_unique<vfs::RemoteDir>(dir.TakeChannel()));
280-
} else {
281-
FML_LOG(ERROR) << "could not add out directory entry(" << dir_str
282-
<< ") for flutter component(" << debug_label_
283-
<< "): " << zx_status_get_string(status);
284-
}
285-
}
286-
};
274+
// Collect our standard set of directories along with directories that are
275+
// included in the cml file to expose.
276+
std::vector<std::string> other_dirs = {"debug", "ctrl", "diagnostics"};
277+
for (auto dir : metadata.expose_dirs) {
278+
other_dirs.push_back(dir);
279+
}
280+
281+
cloned_directory_ptr_.events()
282+
.OnOpen = [this, other_dirs](
283+
zx_status_t status,
284+
std::unique_ptr<fuchsia::io::NodeInfo> info) {
285+
cloned_directory_ptr_.Unbind();
286+
if (status != ZX_OK) {
287+
FML_LOG(ERROR) << "could not bind out directory for flutter component("
288+
<< debug_label_ << "): " << zx_status_get_string(status);
289+
return;
290+
}
291+
292+
// add other directories as RemoteDirs.
293+
for (auto& dir_str : other_dirs) {
294+
fuchsia::io::DirectoryHandle dir;
295+
auto request = dir.NewRequest().TakeChannel();
296+
auto status = fdio_service_connect_at(directory_ptr_.channel().get(),
297+
dir_str.c_str(), request.release());
298+
if (status == ZX_OK) {
299+
outgoing_dir_->AddEntry(
300+
dir_str.c_str(),
301+
std::make_unique<vfs::RemoteDir>(dir.TakeChannel()));
302+
} else {
303+
FML_LOG(ERROR) << "could not add out directory entry(" << dir_str
304+
<< ") for flutter component(" << debug_label_
305+
<< "): " << zx_status_get_string(status);
306+
}
307+
}
308+
};
287309

288310
cloned_directory_ptr_.set_error_handler(
289311
[this](zx_status_t status) { cloned_directory_ptr_.Unbind(); });

shell/platform/fuchsia/flutter/component_v2_unittest.cc

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,78 @@ TEST(ComponentV2, ParseProgramMetadataOldGenHeapSizeInvalid) {
103103
EXPECT_EQ(result.old_gen_heap_size, std::nullopt);
104104
}
105105

106+
TEST(ComponentV2, ParseProgramMetadataNoExposeDirs) {
107+
// Should always have a valid expose_dirs entry
108+
std::vector<fuchsia::data::DictionaryEntry> entries;
109+
110+
fuchsia::data::Dictionary program_metadata;
111+
program_metadata.set_entries(std::move(entries));
112+
113+
ProgramMetadata result = ComponentV2::ParseProgramMetadata(program_metadata);
114+
115+
EXPECT_EQ(result.expose_dirs.empty(), true);
116+
}
117+
118+
TEST(ComponentV2, ParseProgramMetadataWithSingleExposeDirs) {
119+
// Can parse a single exposed dir
120+
std::vector<fuchsia::data::DictionaryEntry> entries;
121+
122+
fuchsia::data::DictionaryEntry args_entry;
123+
args_entry.key = "args";
124+
args_entry.value = std::make_unique<fuchsia::data::DictionaryValue>(
125+
fuchsia::data::DictionaryValue::WithStrVec({"--expose_dirs=foo"}));
126+
entries.push_back(std::move(args_entry));
127+
128+
fuchsia::data::Dictionary program_metadata;
129+
program_metadata.set_entries(std::move(entries));
130+
131+
ProgramMetadata result = ComponentV2::ParseProgramMetadata(program_metadata);
132+
133+
ASSERT_EQ(result.expose_dirs.size(), 1u);
134+
EXPECT_EQ(result.expose_dirs[0], "foo");
135+
}
136+
137+
TEST(ComponentV2, ParseProgramMetadataWithMultipleExposeDirs) {
138+
// Can parse a multiple exposed dirs
139+
std::vector<fuchsia::data::DictionaryEntry> entries;
140+
141+
fuchsia::data::DictionaryEntry args_entry;
142+
args_entry.key = "args";
143+
args_entry.value = std::make_unique<fuchsia::data::DictionaryValue>(
144+
fuchsia::data::DictionaryValue::WithStrVec(
145+
{"--expose_dirs=foo,bar,baz"}));
146+
entries.push_back(std::move(args_entry));
147+
148+
fuchsia::data::Dictionary program_metadata;
149+
program_metadata.set_entries(std::move(entries));
150+
151+
ProgramMetadata result = ComponentV2::ParseProgramMetadata(program_metadata);
152+
153+
ASSERT_EQ(result.expose_dirs.size(), 3u);
154+
EXPECT_EQ(result.expose_dirs[0], "foo");
155+
EXPECT_EQ(result.expose_dirs[1], "bar");
156+
EXPECT_EQ(result.expose_dirs[2], "baz");
157+
}
158+
159+
TEST(ComponentV2, ParseProgramMetadataWithSingleExposeDirsAndOtherArgs) {
160+
// Can parse a single exposed dir when other args are present
161+
std::vector<fuchsia::data::DictionaryEntry> entries;
162+
163+
fuchsia::data::DictionaryEntry args_entry;
164+
args_entry.key = "args";
165+
args_entry.value = std::make_unique<fuchsia::data::DictionaryValue>(
166+
fuchsia::data::DictionaryValue::WithStrVec(
167+
{"--expose_dirs=foo", "--foo=bar"}));
168+
entries.push_back(std::move(args_entry));
169+
170+
fuchsia::data::Dictionary program_metadata;
171+
program_metadata.set_entries(std::move(entries));
172+
173+
ProgramMetadata result = ComponentV2::ParseProgramMetadata(program_metadata);
174+
175+
ASSERT_EQ(result.expose_dirs.size(), 1u);
176+
EXPECT_EQ(result.expose_dirs[0], "foo");
177+
}
178+
106179
} // anonymous namespace
107180
} // namespace flutter_runner

shell/platform/fuchsia/flutter/program_metadata.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ struct ProgramMetadata {
2626

2727
/// The preferred heap size for the Flutter component in megabytes.
2828
std::optional<int64_t> old_gen_heap_size = std::nullopt;
29+
30+
/// A list of additional directories that we will expose in out/
31+
std::vector<std::string> expose_dirs;
2932
};
3033

3134
} // namespace flutter_runner

tools/fuchsia/devshell/serve.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ while true; do
247247
config_url="http://${addr}:${port}/config.json"
248248

249249
if [[ ${component_framework_version} == 2 ]]; then
250-
run_ssh_command pkgctl repo add \
250+
run_ssh_command pkgctl repo add url \
251251
-n "engine" \
252252
"${config_url}"
253253
else

0 commit comments

Comments
 (0)