From 26366a5001539bd70eea5328c2947fcdc13de6e5 Mon Sep 17 00:00:00 2001 From: George Wright Date: Mon, 12 Oct 2020 16:04:49 -0700 Subject: [PATCH] Add plumbing to grab dart entrypoint args on macOS --- .../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 ab4f0d9e60bab..e87ddf54eaded 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 a1648b332a156..6343729d09ac7 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm @@ -27,6 +27,10 @@ - (instancetype)initWithPrecompiledDartBundle:(NSBundle*)bundle { 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 f6a0027dc3d0f..60968ceec0031 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -263,6 +263,11 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint { 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 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint { 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),