Skip to content

Commit

Permalink
Add FlutterProjectArgs::root_isolate_create_callback (flutter#7651)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cbracken authored Feb 6, 2019
1 parent fdc699b commit ce07399
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions shell/platform/embedder/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions shell/platform/embedder/embedder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion shell/platform/embedder/tests/embedder_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ce07399

Please sign in to comment.