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

Make FlutterEngineGroup support dart entrypoint args #29096

Merged

Conversation

ColdPaleLight
Copy link
Member

@ColdPaleLight ColdPaleLight commented Oct 9, 2021

This PR try to add a new parameter named initialArguments to pass the startup arguments from platform to Flutter app

fixes: flutter/flutter#91547 flutter/flutter#23786 flutter/flutter#34170

design doc: https://docs.google.com/document/d/1ufsbXdhCm1qTAY2eXsRrVrfxED5WCZgqQ-Tm2LzScoE/edit

sample: https://github.com/ColdPaleLight/multiple_flutters/

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.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@google-cla google-cla bot added the cla: yes label Oct 9, 2021
@ColdPaleLight ColdPaleLight force-pushed the engine_group_with_initial_arguments branch from 97c7562 to 8a29f14 Compare October 9, 2021 12:49
@ColdPaleLight
Copy link
Member Author

(Remove "need tests" label because unit tests has been added)

@ColdPaleLight ColdPaleLight changed the title [WIP] Make FlutterEngineGroup support initialArguments Make FlutterEngineGroup support initialArguments Oct 9, 2021
@gaaclarke
Copy link
Member

Why can't you use platform channels for this?

Copy link
Member

@chinmaygarde chinmaygarde left a comment

Choose a reason for hiding this comment

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

The underlying issue warrants further discussion of the use-case and alternatives (in a design doc perhaps) before jumping into an implementation. This data blob is persistent across the lifecycle of the engine and cannot be reclaimed. It has the possibility of growing as the application grows more complex.

@ColdPaleLight ColdPaleLight force-pushed the engine_group_with_initial_arguments branch 4 times, most recently from e684b21 to e893583 Compare October 15, 2021 08:51
@ColdPaleLight ColdPaleLight force-pushed the engine_group_with_initial_arguments branch from 834be03 to 58bb313 Compare October 18, 2021 17:27
Comment on lines 574 to 577
dynamic getInitialArguments({required List<String> rawArgs}) {
if (rawArgs.isEmpty) return null;
return json.decode(rawArgs[0]);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We should avoid using dynamic, and avoid using or assuming this is JSON.

Copy link
Member Author

@ColdPaleLight ColdPaleLight Oct 19, 2021

Choose a reason for hiding this comment

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

1 We should avoid using dynamic
Done. Using Object? now

2 and avoid using or assuming this is JSON.
Done.
The current solution cannot avoid using json, but some changes can be made here.
We can change the dart entrypoint args passed by the platform into the following format

['--initial-arguments','{\"foo\":\"bar\"}']

Then we change the code to the following

Object? getInitialArguments({required List<String> rawArgs}) {
  if (rawArgs.isEmpty) return null;
  final int initialArgumentsIndex = rawArgs.indexOf('--initial-arguments') + 1;
  if (initialArgumentsIndex <= 0 || initialArgumentsIndex == rawArgs.length) return null;
  return json.decode(rawArgs[initialArgumentsIndex]);
}

void canRecieveEmptyArguments(List<String> args) {
final dynamic initialArguments = PlatformDispatcher.instance.getInitialArguments(rawArgs: args);
notifyNativeWhenEngineRun(args.isEmpty && initialArguments == null);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: newline at EOF

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
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

One high level concern: the args get stuck in memory forever even with this patch.

If we do this for debug to support restart it's probably acceptable, but we should not be doing this for profile/release where restart isn't possible.

@ColdPaleLight
Copy link
Member Author

One high level concern: the args get stuck in memory forever even with this patch.

If we do this for debug to support restart it's probably acceptable, but we should not be doing this for profile/release where restart isn't possible.

I have considered this problem, and currently I have two solutions:

  1. Use the macro FLUTTER_RELEASE to do conditional compilation, do not store arguments in last_entry_point_args_ in engine in release mode.
  2. Use DartVM::IsRunningPrecompiledCode(). If it is AOT mode, do not store arguments in last_entry_point_args_ in engine.

Hi, @dnfield , do you think these two options are feasible? If feasible, which one do you prefer?

@dnfield
Copy link
Contributor

dnfield commented Oct 19, 2021

I'd say just guard it with conditional compilation, but probably on DEBUG rather than RELEASE, and make sure both branches are covered by tests :)

@ColdPaleLight
Copy link
Member Author

I'd say just guard it with conditional compilation, but probably on DEBUG rather than RELEASE, and make sure both branches are covered by tests :)

Ok, I'll do that :)

@CaseyHillers CaseyHillers changed the base branch from master to main November 15, 2021 18:14
* must be decorated with `@pragma(vm:entry-point)` to ensure themethod is not tree-shaken by the
* Dart compiler.
*/
@property(nonatomic, copy) NSString* entrypoint;
Copy link
Member

Choose a reason for hiding this comment

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

These properties are nullable right? You should specify that @property(nonatomic, copy, nullable)

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

@ColdPaleLight ColdPaleLight changed the base branch from master to main November 16, 2021 04:34
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.

I read through everything again. This looks close. The only important thing I noticed is that one test that I think should make sure the arguments match on the ui thread with the ones that were supplied.

Comment on lines 187 to 190
private Context context;
private DartEntrypoint dartEntrypoint;
private String initialRoute;
private List<String> dartEntrypointArgs;
Copy link
Member

Choose a reason for hiding this comment

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

Nullable annotations would be helpful here too.

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

}

private native void nativeRunBundleAndSnapshotFromLibrary(
long nativeShellHolderId,
@NonNull String bundlePath,
@Nullable String entrypointFunctionName,
@Nullable String pathToEntrypointFunction,
@NonNull AssetManager manager);
@NonNull AssetManager manager,
@NonNull List<String> entrypointArgs);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be nullable?

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

@@ -42,16 +54,32 @@ - (FlutterEngine*)makeEngineWithEntrypoint:(nullable NSString*)entrypoint
- (FlutterEngine*)makeEngineWithEntrypoint:(nullable NSString*)entrypoint
libraryURI:(nullable NSString*)libraryURI
initialRoute:(nullable NSString*)initialRoute {
NSString* engineName = [NSString stringWithFormat:@"%@.%d", self.name, ++_enginesCreatedCount];
FlutterEngineGroupOptions* options = [FlutterEngineGroupOptions new];
Copy link
Member

Choose a reason for hiding this comment

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

There are new analyzer rules coming, r/[FlutterEngineGroupOptions new]/[[FlutterEngineGroupOptions alloc] init]

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
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

Please add a TODO on settings.h to remove the duplicated entry, and link it to the existing issue that has context.

This LGTM once it LGT @gaaclarke

@dnfield dnfield dismissed chinmaygarde’s stale review November 18, 2021 00:42

Chinmay says his concerns have been addressed

@ColdPaleLight
Copy link
Member Author

Please add a TODO on settings.h to remove the duplicated entry, and link it to the existing issue that has context.

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 @ColdPaleLight =)

@dnfield dnfield 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 Nov 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes customer: alibaba platform-android platform-ios platform-web Code specifically for the web engine waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to send structured data to Flutter app at startup when using FlutterEngineGroup
7 participants