From 7ff50beb59951d305944b96abcbc59dc65759373 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Wed, 6 Feb 2019 14:16:47 -0800 Subject: [PATCH] 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. --- engine/src/flutter/shell/platform/embedder/embedder.cc | 7 +++++++ engine/src/flutter/shell/platform/embedder/embedder.h | 3 +++ .../shell/platform/embedder/tests/embedder_unittests.cc | 8 +++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/shell/platform/embedder/embedder.cc b/engine/src/flutter/shell/platform/embedder/embedder.cc index 807ebe772b7..512f1183153 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder.cc @@ -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. diff --git a/engine/src/flutter/shell/platform/embedder/embedder.h b/engine/src/flutter/shell/platform/embedder/embedder.h index c2cc35669ca..e0603644f44 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.h +++ b/engine/src/flutter/shell/platform/embedder/embedder.h @@ -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 diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc index e2062f6ee95..7ddff1e96aa 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc @@ -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(data); + ASSERT_EQ(str_data, "Data"); + }; + std::string str_data = "Data"; + void* user_data = const_cast(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);