Add FlutterProjectArgs::root_isolate_create_callback (flutter/engine#7651)

Allows embedders to specify a callback to be invoked in isolate scope
once root isolate has been created and marked runnable.

As an example of where this is useful, embedder unit test fixtures may
want to include Dart functions backed by a native implementation. On
isolate creation, this patch allows the unit test author to call
Dart_SetNativeResolver in root isolate scope.
This commit is contained in:
Chris Bracken 2019-02-06 14:16:47 -08:00 committed by GitHub
parent 03b9ff5721
commit 7ff50beb59
3 changed files with 17 additions and 1 deletions

View File

@ -361,6 +361,13 @@ FlutterEngineResult FlutterEngineRun(size_t version,
settings.task_observer_remove = [](intptr_t key) {
fml::MessageLoop::GetCurrent().RemoveTaskObserver(key);
};
if (SAFE_ACCESS(args, root_isolate_create_callback, nullptr) != nullptr) {
VoidCallback callback =
SAFE_ACCESS(args, root_isolate_create_callback, nullptr);
settings.root_isolate_create_callback = [callback, user_data]() {
callback(user_data);
};
}
// Create a thread host with the current thread as the platform thread and all
// other threads managed.

View File

@ -251,6 +251,9 @@ typedef struct {
const uint8_t* isolate_snapshot_instructions;
// The size of the isolate snapshot instructions buffer.
size_t isolate_snapshot_instructions_size;
// The callback invoked by the engine in root isolate scope. Called
// immediately after the root isolate has been created and marked runnable.
VoidCallback root_isolate_create_callback;
} FlutterProjectArgs;
FLUTTER_EXPORT

View File

@ -29,10 +29,16 @@ TEST(EmbedderTest, CanLaunchAndShutdownWithValidProjectArgs) {
FlutterProjectArgs args = {};
args.struct_size = sizeof(FlutterProjectArgs);
args.assets_path = testing::GetFixturesPath();
args.root_isolate_create_callback = [](void* data) {
std::string str_data = reinterpret_cast<char*>(data);
ASSERT_EQ(str_data, "Data");
};
std::string str_data = "Data";
void* user_data = const_cast<char*>(str_data.c_str());
FlutterEngine engine = nullptr;
FlutterEngineResult result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config,
&args, nullptr, &engine);
&args, user_data, &engine);
ASSERT_EQ(result, FlutterEngineResult::kSuccess);
result = FlutterEngineShutdown(engine);