From 04bf8791feb9c8b56f3fa03ddea3d5286d74a18f Mon Sep 17 00:00:00 2001 From: George Wright Date: Fri, 16 Oct 2020 16:07:02 -0700 Subject: [PATCH] Add plumbing to grab dart entrypoint args on macOS (#21789) --- .../macos/framework/Headers/FlutterDartProject.h | 10 ++++++++++ .../macos/framework/Source/FlutterDartProject.mm | 4 ++++ .../darwin/macos/framework/Source/FlutterEngine.mm | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h b/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h index ab4f0d9e60b..e87ddf54ead 100644 --- a/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h +++ b/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h @@ -36,6 +36,16 @@ FLUTTER_EXPORT */ @property(nonatomic) bool enableMirrors; +/** + * An NSArray of NSStrings to be passed as command line arguments to the Dart entrypoint. + * + * If this is not explicitly set, this will default to the contents of + * [NSProcessInfo arguments], without the binary name. + * + * Set this to nil to pass no arguments to the Dart entrypoint. + */ +@property(nonatomic, nullable) NSArray* dartEntrypointArguments; + @end #endif // FLUTTER_FLUTTERDARTPROJECT_H_ diff --git a/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm b/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm index a1648b332a1..6343729d09a 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm @@ -27,6 +27,10 @@ static NSString* const kAppBundleIdentifier = @"io.flutter.flutter.app"; NSAssert(self, @"Super init cannot be nil"); _dartBundle = bundle ?: [NSBundle bundleWithIdentifier:kAppBundleIdentifier]; + _dartEntrypointArguments = [[NSProcessInfo processInfo] arguments]; + // Remove the first element as it's the binary name + _dartEntrypointArguments = [_dartEntrypointArguments + subarrayWithRange:NSMakeRange(1, _dartEntrypointArguments.count - 1)]; return self; } diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index f6a0027dc3d..60968ceec00 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -263,6 +263,11 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, std::transform(switches.begin(), switches.end(), std::back_inserter(argv), [](const std::string& arg) -> const char* { return arg.c_str(); }); + std::vector dartEntrypointArgs; + for (NSString* argument in [_project dartEntrypointArguments]) { + dartEntrypointArgs.push_back([argument UTF8String]); + } + FlutterProjectArgs flutterArguments = {}; flutterArguments.struct_size = sizeof(FlutterProjectArgs); flutterArguments.assets_path = _project.assetsPath.UTF8String; @@ -272,6 +277,9 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, flutterArguments.platform_message_callback = (FlutterPlatformMessageCallback)OnPlatformMessage; flutterArguments.custom_dart_entrypoint = entrypoint.UTF8String; flutterArguments.shutdown_dart_vm_when_done = true; + flutterArguments.dart_entrypoint_argc = dartEntrypointArgs.size(); + flutterArguments.dart_entrypoint_argv = dartEntrypointArgs.data(); + static size_t sTaskRunnerIdentifiers = 0; const FlutterTaskRunnerDescription cocoa_task_runner_description = { .struct_size = sizeof(FlutterTaskRunnerDescription),