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

Commit 25fadba

Browse files
committed
Simplified shell spawn down to just take run configuration
1 parent 015974e commit 25fadba

File tree

6 files changed

+76
-61
lines changed

6 files changed

+76
-61
lines changed

shell/common/shell.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,12 @@ Shell::~Shell() {
475475
}
476476

477477
std::unique_ptr<Shell> Shell::Spawn(
478-
Settings settings,
479478
RunConfiguration run_configuration,
480479
const CreateCallback<PlatformView>& on_create_platform_view,
481480
const CreateCallback<Rasterizer>& on_create_rasterizer) const {
482481
FML_DCHECK(task_runners_.IsValid());
483482
std::unique_ptr<Shell> result(Shell::Create(
484-
task_runners_, PlatformData{}, settings,
483+
task_runners_, PlatformData{}, GetSettings(),
485484
vm_->GetVMData()->GetIsolateSnapshot(), on_create_platform_view,
486485
on_create_rasterizer, vm_,
487486
[engine = this->engine_.get()](

shell/common/shell.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,13 @@ class Shell final : public PlatformView::Delegate,
218218
/// This results is a Shell that has a smaller startup time cost
219219
/// and a smaller memory footprint than an Shell created with a
220220
/// Create function.
221+
/// @param[in] run_configuration A RunConfiguration used to run the Isolate
222+
/// associated with this new Shell. It doesn't have to be the same
223+
/// configuration as the current Shell but it needs to be in the
224+
/// same snapshot or AOT.
221225
///
222226
/// @see http://flutter.dev/go/multiple-engines
223227
std::unique_ptr<Shell> Spawn(
224-
Settings settings,
225228
RunConfiguration run_configuration,
226229
const CreateCallback<PlatformView>& on_create_platform_view,
227230
const CreateCallback<Rasterizer>& on_create_rasterizer) const;

shell/common/shell_unittests.cc

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,39 +2438,57 @@ TEST_F(ShellTest, Spawn) {
24382438
ASSERT_TRUE(configuration.IsValid());
24392439
configuration.SetEntrypoint("fixturesAreFunctionalMain");
24402440

2441+
auto second_configuration = RunConfiguration::InferFromSettings(settings);
2442+
ASSERT_TRUE(second_configuration.IsValid());
2443+
second_configuration.SetEntrypoint("testCanLaunchSecondaryIsolate");
2444+
24412445
fml::AutoResetWaitableEvent main_latch;
2446+
std::string last_entry_point;
24422447
AddNativeCallback(
2443-
"SayHiFromFixturesAreFunctionalMain",
2444-
CREATE_NATIVE_ENTRY([&main_latch](auto args) { main_latch.Signal(); }));
2448+
"SayHiFromFixturesAreFunctionalMain", CREATE_NATIVE_ENTRY([&](auto args) {
2449+
last_entry_point = shell->GetEngine()->GetLastEntrypoint();
2450+
main_latch.Signal();
2451+
}));
2452+
AddNativeCallback(
2453+
// The Dart native function names aren't very consistent but this is just
2454+
// the native function name of the second vm entrypoint in the fixture.
2455+
"NotifyNative", CREATE_NATIVE_ENTRY([&](auto args) {}));
24452456

24462457
RunEngine(shell.get(), std::move(configuration));
24472458
main_latch.Wait();
24482459
ASSERT_TRUE(DartVMRef::IsInstanceRunning());
2460+
ASSERT_EQ("fixturesAreFunctionalMain", last_entry_point);
24492461

2450-
PostSync(shell->GetTaskRunners().GetPlatformTaskRunner(), [this,
2451-
&spawner = shell,
2452-
settings]() {
2453-
MockPlatformViewDelegate platform_view_delegate;
2454-
auto spawn = spawner->Spawn(
2455-
settings,
2456-
[&platform_view_delegate](Shell& shell) {
2457-
auto result = std::make_unique<MockPlatformView>(
2458-
platform_view_delegate, shell.GetTaskRunners());
2459-
ON_CALL(*result, CreateRenderingSurface())
2460-
.WillByDefault(::testing::Invoke(
2461-
[] { return std::make_unique<MockSurface>(); }));
2462-
return result;
2463-
},
2464-
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
2465-
ASSERT_NE(nullptr, spawn.get());
2466-
ASSERT_TRUE(ValidateShell(spawn.get()));
2467-
2468-
PostSync(spawner->GetTaskRunners().GetIOTaskRunner(), [&spawner, &spawn] {
2469-
ASSERT_EQ(spawner->GetIOManager()->GetResourceContext().get(),
2470-
spawn->GetIOManager()->GetResourceContext().get());
2471-
});
2472-
DestroyShell(std::move(spawn));
2473-
});
2462+
PostSync(
2463+
shell->GetTaskRunners().GetPlatformTaskRunner(),
2464+
[this, &spawner = shell, &second_configuration]() {
2465+
MockPlatformViewDelegate platform_view_delegate;
2466+
auto spawn = spawner->Spawn(
2467+
std::move(second_configuration),
2468+
[&platform_view_delegate](Shell& shell) {
2469+
auto result = std::make_unique<MockPlatformView>(
2470+
platform_view_delegate, shell.GetTaskRunners());
2471+
ON_CALL(*result, CreateRenderingSurface())
2472+
.WillByDefault(::testing::Invoke(
2473+
[] { return std::make_unique<MockSurface>(); }));
2474+
return result;
2475+
},
2476+
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
2477+
ASSERT_NE(nullptr, spawn.get());
2478+
ASSERT_TRUE(ValidateShell(spawn.get()));
2479+
2480+
PostSync(spawner->GetTaskRunners().GetUITaskRunner(), [&spawn] {
2481+
ASSERT_EQ("testCanLaunchSecondaryIsolate",
2482+
spawn->GetEngine()->GetLastEntrypoint());
2483+
});
2484+
2485+
PostSync(
2486+
spawner->GetTaskRunners().GetIOTaskRunner(), [&spawner, &spawn] {
2487+
ASSERT_EQ(spawner->GetIOManager()->GetResourceContext().get(),
2488+
spawn->GetIOManager()->GetResourceContext().get());
2489+
});
2490+
DestroyShell(std::move(spawn));
2491+
});
24742492

24752493
DestroyShell(std::move(shell));
24762494
ASSERT_FALSE(DartVMRef::IsInstanceRunning());

shell/platform/android/android_shell_holder.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#include <cstddef>
6-
#include <optional>
7-
#include "shell/common/run_configuration.h"
85
#define FML_USED_ON_EMBEDDER
96

107
#include "flutter/shell/platform/android/android_shell_holder.h"
118

129
#include <pthread.h>
1310
#include <sys/resource.h>
1411
#include <sys/time.h>
12+
#include <cstddef>
1513
#include <memory>
14+
#include <optional>
1615

1716
#include <sstream>
1817
#include <string>
@@ -23,6 +22,7 @@
2322
#include "flutter/fml/message_loop.h"
2423
#include "flutter/fml/platform/android/jni_util.h"
2524
#include "flutter/shell/common/rasterizer.h"
25+
#include "flutter/shell/common/run_configuration.h"
2626
#include "flutter/shell/common/thread_host.h"
2727
#include "flutter/shell/platform/android/context/android_context.h"
2828
#include "flutter/shell/platform/android/platform_view_android.h"
@@ -212,16 +212,16 @@ std::unique_ptr<AndroidShellHolder> AndroidShellHolder::Spawn(
212212
}
213213

214214
std::unique_ptr<flutter::Shell> shell = shell_->Spawn(
215-
std::move(GetSettings()), std::move(config.value()), on_create_platform_view, on_create_rasterizer);
215+
std::move(config.value()), on_create_platform_view, on_create_rasterizer);
216216

217217
return std::make_unique<AndroidShellHolder>(GetSettings(), jni_facade,
218218
thread_host_, std::move(shell),
219219
weak_platform_view);
220220
}
221221

222222
void AndroidShellHolder::Launch(std::shared_ptr<AssetManager> asset_manager,
223-
std::string entrypoint,
224-
std::string libraryUrl) {
223+
std::string entrypoint,
224+
std::string libraryUrl) {
225225
if (!IsValid()) {
226226
return;
227227
}

shell/platform/android/android_shell_holder.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ class AndroidShellHolder {
8686
std::string libraryUrl) const;
8787

8888
void Launch(std::shared_ptr<AssetManager> asset_manager,
89-
std::string entrypoint,
90-
std::string libraryUrl);
89+
std::string entrypoint,
90+
std::string libraryUrl);
9191

9292
const flutter::Settings& GetSettings() const;
9393

@@ -112,9 +112,9 @@ class AndroidShellHolder {
112112

113113
static void ThreadDestructCallback(void* value);
114114
std::optional<RunConfiguration> BuildRunConfiguration(
115-
std::shared_ptr<flutter::AssetManager> asset_manager,
116-
std::string entrypoint,
117-
std::string libraryUrl) const;
115+
std::shared_ptr<flutter::AssetManager> asset_manager,
116+
std::string entrypoint,
117+
std::string libraryUrl) const;
118118

119119
FML_DISALLOW_COPY_AND_ASSIGN(AndroidShellHolder);
120120
};

shell/platform/darwin/ios/framework/Source/FlutterEngine.mm

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -557,20 +557,6 @@ + (NSString*)generateThreadLabel:(NSString*)labelPrefix {
557557
threadHostType};
558558
}
559559

560-
static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSString* libraryURI) {
561-
if (libraryURI) {
562-
FML_DCHECK(entrypoint) << "Must specify entrypoint if specifying library";
563-
settings->advisory_script_entrypoint = entrypoint.UTF8String;
564-
settings->advisory_script_uri = libraryURI.UTF8String;
565-
} else if (entrypoint) {
566-
settings->advisory_script_entrypoint = entrypoint.UTF8String;
567-
settings->advisory_script_uri = std::string("main.dart");
568-
} else {
569-
settings->advisory_script_entrypoint = std::string("main");
570-
settings->advisory_script_uri = std::string("main.dart");
571-
}
572-
}
573-
574560
- (BOOL)createShell:(NSString*)entrypoint
575561
libraryURI:(NSString*)libraryURI
576562
initialRoute:(NSString*)initialRoute {
@@ -586,7 +572,17 @@ - (BOOL)createShell:(NSString*)entrypoint
586572

587573
auto platformData = [_dartProject.get() defaultPlatformData];
588574

589-
SetEntryPoint(&settings, entrypoint, libraryURI);
575+
if (libraryURI) {
576+
FML_DCHECK(entrypoint) << "Must specify entrypoint if specifying library";
577+
settings->advisory_script_entrypoint = entrypoint.UTF8String;
578+
settings->advisory_script_uri = libraryURI.UTF8String;
579+
} else if (entrypoint) {
580+
settings->advisory_script_entrypoint = entrypoint.UTF8String;
581+
settings->advisory_script_uri = std::string("main.dart");
582+
} else {
583+
settings->advisory_script_entrypoint = std::string("main");
584+
settings->advisory_script_uri = std::string("main.dart");
585+
}
590586

591587
NSString* threadLabel = [FlutterEngine generateThreadLabel:_labelPrefix];
592588
_threadHost = std::make_shared<flutter::ThreadHost>();
@@ -968,10 +964,11 @@ - (FlutterEngine*)spawnWithEntrypoint:(/*nullable*/ NSString*)entrypoint
968964
project:_dartProject.get()
969965
allowHeadlessExecution:_allowHeadlessExecution];
970966

971-
flutter::Settings settings = _shell->GetSettings();
972-
SetEntryPoint(&settings, entrypoint, libraryURI);
967+
RunConfiguration configuration =
968+
[_dartProject.get() runConfigurationForEntrypoint:entrypoint libraryOrNil:libraryOrNil]
973969

974-
fml::WeakPtr<flutter::PlatformView> platform_view = _shell->GetPlatformView();
970+
fml::WeakPtr<flutter::PlatformView>
971+
platform_view = _shell->GetPlatformView();
975972
FML_DCHECK(platform_view);
976973
// Static-cast safe since this class always creates PlatformViewIOS instances.
977974
flutter::PlatformViewIOS* ios_platform_view =
@@ -991,10 +988,8 @@ - (FlutterEngine*)spawnWithEntrypoint:(/*nullable*/ NSString*)entrypoint
991988
flutter::Shell::CreateCallback<flutter::Rasterizer> on_create_rasterizer =
992989
[](flutter::Shell& shell) { return std::make_unique<flutter::Rasterizer>(shell); };
993990

994-
RunConfiguration configuration = RunConfiguration::InferFromSettings(settings);
995-
996991
std::unique_ptr<flutter::Shell> shell =
997-
_shell->Spawn(std::move(settings), std::move(configuration), on_create_platform_view, on_create_rasterizer);
992+
_shell->Spawn(std::move(configuration), on_create_platform_view, on_create_rasterizer);
998993

999994
result->_threadHost = _threadHost;
1000995
result->_profiler = _profiler;

0 commit comments

Comments
 (0)