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

Make FlutterEngineGroup support initialRoute #28884

Merged

Conversation

ColdPaleLight
Copy link
Member

@ColdPaleLight ColdPaleLight commented Sep 27, 2021

fixes: flutter/flutter#78931

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides].
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See [testing the engine] for instructions on
    writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the [CLA].
  • All existing and new tests are passing.

@ColdPaleLight ColdPaleLight changed the title [WIP] Make FlutterEngineGroup support initialRoute Make FlutterEngineGroup support initialRoute Sep 28, 2021
@gaaclarke
Copy link
Member

The way initial route typically works is through the navigation platform channel: https://github.com/gaaclarke/engine/blob/0e76836ea8409e87b553341bcb16f98028faba90/shell/common/engine.cc#L361:L361

Why are creating c++ plumbing to handle initial route instead of using the navigation platform channel?

@ColdPaleLight
Copy link
Member Author

ColdPaleLight commented Sep 28, 2021

The way initial route typically works is through the navigation platform channel: https://github.com/gaaclarke/engine/blob/0e76836ea8409e87b553341bcb16f98028faba90/shell/common/engine.cc#L361:L361

Why are creating c++ plumbing to handle initial route instead of using the navigation platform channel?

Because I want to make sure that the initialRoute has been set up before Shell::RunEngine

result->RunEngine(std::move(run_configuration));

@ColdPaleLight
Copy link
Member Author

The initialization and running of the heavyweight engine are separate. So after it is initialized and before running, you have the opportunity to set up the initialRoute through the navigation platform channel.

However, the initialization and running of the lightweight engine are all done in Shell::Spawn, and there is no good time to set the initialRoute through the navigation platform channel.

@gaaclarke
Copy link
Member

Because I want to make sure that the initialRoute has been set up before Shell::RunEngine

Ahh I see.

Here is where the message is sent:
https://github.com/gaaclarke/engine/blob/9f99d99628ee4a658f4fff206f588592278ed6b0/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm#L468:L468

The flow for typical usage is:

[FlutterEngine runWithEntrypoint:libraryURI:initialRoute:]
  [FlutterEngine createShell:libraryURI:initialRoute:]
    flutter::Shell::Create()
    [FlutterEngine setupShell:withObservatoryPublication:]
      [FlutterEngine setupChannels]
        <send initial route message>
  [FlutterEngine launchEngine:libraryURI:]
    flutter::Shell::RunEngine()

Because spawn is create+launch together you had to insert the ability to specify it in spawn to sandwich it between create and launch. I think that sounds good, I'll look at this again.

Copy link
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, another great contribution @ColdPaleLight, thanks. I just have a few nitpicks.

@@ -133,6 +134,7 @@ std::unique_ptr<Engine> Engine::Spawn(
settings_.isolate_shutdown_callback, // isolate shutdown callback
settings_.persistent_isolate_data // persistent isolate data
);
result->initial_route_ = std::move(initial_route);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you want an std::move here. This has to initiate a copy anyways, I think it is just needlessly invalidating the reference.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -488,7 +489,8 @@ std::unique_ptr<Shell> Shell::Spawn(
PlatformData{}, task_runners_, rasterizer_->GetRasterThreadMerger(),
GetSettings(), vm_, vm_->GetVMData()->GetIsolateSnapshot(),
on_create_platform_view, on_create_rasterizer,
[engine = this->engine_.get()](
[engine = this->engine_.get(),
initial_route = std::move(initial_route)](
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here for std::move. Since this is executing on a separate thread, just explicitly make a copy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -501,7 +503,8 @@ std::unique_ptr<Shell> Shell::Spawn(
return engine->Spawn(/*delegate=*/delegate,
/*dispatcher_maker=*/dispatcher_maker,
/*settings=*/settings,
/*animator=*/std::move(animator));
/*animator=*/std::move(animator),
/*initial_route*/ initial_route);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/initial_route/initial_route=

I wish the linter worked for that, doesn't seem to be.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -2740,6 +2743,7 @@ TEST_F(ShellTest, Spawn) {
// Check second shell ran the second entrypoint.
ASSERT_EQ("testCanLaunchSecondaryIsolate",
spawn->GetEngine()->GetLastEntrypoint());
ASSERT_EQ("/foo", spawn->GetEngine()->InitialRoute());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/"/foo"/initial_route

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -1027,8 +1028,13 @@ - (FlutterEngine*)spawnWithEntrypoint:(/*nullable*/ NSString*)entrypoint
flutter::Shell::CreateCallback<flutter::Rasterizer> on_create_rasterizer =
[](flutter::Shell& shell) { return std::make_unique<flutter::Rasterizer>(shell); };

std::unique_ptr<flutter::Shell> shell =
_shell->Spawn(std::move(configuration), on_create_platform_view, on_create_rasterizer);
std::string initial_route;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming convention for objc is camel case. It's confusing having initialRoute and initial_route in the same context. I'm not sure if we have an established convention for this but how about initialRoute and cppInitialRoute?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@gaaclarke gaaclarke added the waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land. label Sep 29, 2021
@fluttergithubbot
Copy link
Contributor

This pull request is not suitable for automatic merging in its current state.

@fluttergithubbot fluttergithubbot removed the waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land. label Sep 29, 2021
@gaaclarke gaaclarke merged commit 7be0c36 into flutter:master Sep 29, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Sep 29, 2021
akbiggs pushed a commit to akbiggs/engine that referenced this pull request Oct 1, 2021
dnfield pushed a commit to dnfield/engine that referenced this pull request Oct 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FlutterEngineGroup does not support initialRoute
3 participants