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

Commit e695a51

Browse files
Add FLEDartProject for macOS embedding (#9672)
Adds an initial version of FLEDartProject, which contains the configuration for running the engine. In the future this will align with FlutterDartProject, and likely share a significant portion of the implementation. This is a breaking change for macOS runners.
1 parent 44f2f07 commit e695a51

File tree

10 files changed

+192
-48
lines changed

10 files changed

+192
-48
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_software.mm
740740
FILE: ../../../flutter/shell/platform/darwin/ios/platform_view_ios.h
741741
FILE: ../../../flutter/shell/platform/darwin/ios/platform_view_ios.mm
742742
FILE: ../../../flutter/shell/platform/darwin/macos/framework/FlutterMacOS.podspec
743+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FLEDartProject.h
743744
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FLEEngine.h
744745
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FLEOpenGLContextHandling.h
745746
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FLEReshapeListener.h
@@ -749,6 +750,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterMacO
749750
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPluginMacOS.h
750751
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPluginRegistrarMacOS.h
751752
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Info.plist
753+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLEDartProject.mm
754+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLEDartProject_Internal.h
752755
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLEEngine.mm
753756
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLEEngine_Internal.h
754757
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLETextInputModel.h

shell/platform/darwin/macos/BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ _flutter_framework_headers = [
3333
"framework/Headers/FlutterMacOS.h",
3434
"framework/Headers/FlutterPluginMacOS.h",
3535
"framework/Headers/FlutterPluginRegistrarMacOS.h",
36+
"framework/Headers/FLEDartProject.h",
3637
"framework/Headers/FLEEngine.h",
3738
"framework/Headers/FLEOpenGLContextHandling.h",
3839
"framework/Headers/FLEReshapeListener.h",
@@ -49,6 +50,8 @@ shared_library("create_flutter_framework_dylib") {
4950
output_name = "$_flutter_framework_name"
5051

5152
sources = [
53+
"framework/Source/FLEDartProject.mm",
54+
"framework/Source/FLEDartProject_Internal.h",
5255
"framework/Source/FLEEngine.mm",
5356
"framework/Source/FLEEngine_Internal.h",
5457
"framework/Source/FLETextInputModel.h",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_FLEDARTPROJECT_H_
6+
#define FLUTTER_FLEDARTPROJECT_H_
7+
8+
#import <Foundation/Foundation.h>
9+
10+
#include "FlutterMacros.h"
11+
12+
/**
13+
* A set of Flutter and Dart assets used by a `FlutterEngine` to initialize execution.
14+
*
15+
* TODO(stuartmorgan): Align API with FlutterDartProject.
16+
*/
17+
FLUTTER_EXPORT
18+
@interface FLEDartProject : NSObject
19+
20+
/**
21+
* Initializes a Flutter Dart project from a bundle.
22+
*
23+
* The bundle must either contain a flutter_assets resource directory, or set the Info.plist key
24+
* FLTAssetsPath to override that name (if you are doing a custom build using a different name).
25+
*
26+
* @param bundle The bundle containing the Flutter assets directory. If nil, the main bundle is
27+
* used.
28+
*/
29+
- (nonnull instancetype)initWithBundle:(nullable NSBundle*)bundle NS_DESIGNATED_INITIALIZER;
30+
31+
/**
32+
* Switches to pass to the Flutter engine. See
33+
* https://github.com/flutter/engine/blob/master/shell/common/switches.h
34+
* for details. Not all switches will apply to embedding mode.
35+
*
36+
* Note: This property is likely to be removed in the future in favor of exposing specific switches
37+
* via their own APIs.
38+
*/
39+
@property(nullable) NSArray<NSString*>* engineSwitches;
40+
41+
@end
42+
43+
#endif // FLUTTER_FLEDARTPROJECT_H_

shell/platform/darwin/macos/framework/Headers/FLEEngine.h

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#import <Foundation/Foundation.h>
99

10+
#include "FLEDartProject.h"
1011
#include "FlutterBinaryMessenger.h"
1112
#include "FlutterMacros.h"
1213
#include "FlutterPluginRegistrarMacOS.h"
@@ -27,22 +28,27 @@ FLUTTER_EXPORT
2728
*
2829
* @param viewController The view controller associated with this engine. If nil, the engine
2930
* will be run headless.
31+
* @param project The project configuration. If nil, a default FLEDartProject will be used.
3032
*/
31-
- (nonnull instancetype)initWithViewController:(nullable FLEViewController*)viewController;
33+
- (nonnull instancetype)initWithViewController:(nullable FLEViewController*)viewController
34+
project:(nullable FLEDartProject*)project
35+
NS_DESIGNATED_INITIALIZER;
3236

3337
/**
34-
* Launches the Flutter engine with the provided configuration.
38+
* Runs `main()` from this engine's project.
3539
*
36-
* @param assets The path to the flutter_assets folder for the Flutter application to be run.
37-
* @param arguments Arguments to pass to the Flutter engine. See
38-
* https://github.com/flutter/engine/blob/master/shell/common/switches.h
39-
* for details. Not all arguments will apply to embedding mode.
40-
* Note: This API layer will abstract arguments in the future, instead of
41-
* providing a direct passthrough.
4240
* @return YES if the engine launched successfully.
4341
*/
44-
- (BOOL)launchEngineWithAssetsPath:(nonnull NSURL*)assets
45-
commandLineArguments:(nullable NSArray<NSString*>*)arguments;
42+
- (BOOL)run;
43+
44+
/**
45+
* The `FLEDartProject` associated with this engine. If nil, a default will be used for `run`.
46+
*
47+
* TODO(stuartmorgan): Remove this once FLEViewController takes the project as an initializer
48+
* argument. Blocked on currently needing to create it from a XIB due to the view issues
49+
* described in https://github.com/google/flutter-desktop-embedding/issues/10.
50+
*/
51+
@property(nonatomic, nullable) FLEDartProject* project;
4652

4753
/**
4854
* The `FLEViewController` associated with this engine, if any.

shell/platform/darwin/macos/framework/Headers/FLEViewController.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,12 @@ FLUTTER_EXPORT
4949
@property(nonatomic) FlutterMouseTrackingMode mouseTrackingMode;
5050

5151
/**
52-
* Launches the Flutter engine with the provided configuration.
52+
* Launches the Flutter engine with the provided project.
5353
*
54-
* @param assets The path to the flutter_assets folder for the Flutter application to be run.
55-
* @param arguments Arguments to pass to the Flutter engine. See
56-
* https://github.com/flutter/engine/blob/master/shell/common/switches.h
57-
* for details. Not all arguments will apply to embedding mode.
58-
* Note: This API layer will abstract in the future, instead of providing a direct
59-
* passthrough.
54+
* @param project The project to run in this view controller. If nil, a default `FLEDartProject`
55+
* will be used.
6056
* @return YES if the engine launched successfully.
6157
*/
62-
- (BOOL)launchEngineWithAssetsPath:(nonnull NSURL*)assets
63-
commandLineArguments:(nullable NSArray<NSString*>*)arguments;
58+
- (BOOL)launchEngineWithProject:(nullable FLEDartProject*)project;
6459

6560
@end

shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h

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

5+
#import "FLEDartProject.h"
56
#import "FLEEngine.h"
67
#import "FLEOpenGLContextHandling.h"
78
#import "FLEReshapeListener.h"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import "flutter/shell/platform/darwin/macos/framework/Headers/FLEDartProject.h"
6+
#import "flutter/shell/platform/darwin/macos/framework/Source/FLEDartProject_Internal.h"
7+
8+
#include <vector>
9+
10+
static NSString* const kICUBundlePath = @"icudtl.dat";
11+
12+
@implementation FLEDartProject {
13+
NSBundle* _bundle;
14+
}
15+
16+
- (instancetype)init {
17+
return [self initWithBundle:nil];
18+
}
19+
20+
- (instancetype)initWithBundle:(NSBundle*)bundle {
21+
self = [super init];
22+
NSAssert(self, @"Super init cannot be nil");
23+
24+
_bundle = bundle ?: [NSBundle mainBundle];
25+
return self;
26+
}
27+
28+
- (NSString*)assetsPath {
29+
NSString* flutterAssetsName = [_bundle objectForInfoDictionaryKey:@"FLTAssetsPath"];
30+
if (flutterAssetsName == nil) {
31+
flutterAssetsName = @"flutter_assets";
32+
}
33+
NSString* path = [_bundle pathForResource:flutterAssetsName ofType:@""];
34+
if (!path) {
35+
NSLog(@"Failed to find path for \"%@\"", flutterAssetsName);
36+
}
37+
return path;
38+
}
39+
40+
- (NSString*)ICUDataPath {
41+
NSString* path = [[NSBundle bundleForClass:[self class]] pathForResource:kICUBundlePath
42+
ofType:nil];
43+
if (!path) {
44+
NSLog(@"Failed to find path for \"%@\"", kICUBundlePath);
45+
}
46+
return path;
47+
}
48+
49+
- (std::vector<const char*>)argv {
50+
// FlutterProjectArgs expects a full argv, so when processing it for flags the first item is
51+
// treated as the executable and ignored. Add a dummy value so that all provided arguments
52+
// are used.
53+
std::vector<const char*> arguments = {"placeholder"};
54+
for (NSUInteger i = 0; i < _engineSwitches.count; ++i) {
55+
arguments.push_back([_engineSwitches[i] UTF8String]);
56+
}
57+
return arguments;
58+
}
59+
60+
@end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLEDARTPROJECT_INTERNAL_H_
6+
#define SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLEDARTPROJECT_INTERNAL_H_
7+
8+
#import "flutter/shell/platform/darwin/macos/framework/Headers/FLEDartProject.h"
9+
10+
#include <vector>
11+
12+
/**
13+
* Provides access to data needed to construct a FlutterProjectArgs for the project.
14+
*/
15+
@interface FLEDartProject ()
16+
17+
/**
18+
* The path to the Flutter assets directory.
19+
*/
20+
@property(nonatomic, readonly, nullable) NSString* assetsPath;
21+
22+
/**
23+
* The path to the ICU data file.
24+
*/
25+
@property(nonatomic, readonly, nullable) NSString* ICUDataPath;
26+
27+
/**
28+
* The command line arguments array for the engine.
29+
*
30+
* WARNING: The pointers in this array are valid only until the next call to set `engineSwitches`.
31+
* The returned vector should be used immediately, then discarded. It is returned this way for
32+
* ease of use with FlutterProjectArgs.
33+
*/
34+
@property(nonatomic, readonly) std::vector<const char*> argv;
35+
36+
@end
37+
38+
#endif // SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLEDARTPROJECT_INTERNAL_H_

shell/platform/darwin/macos/framework/Source/FLEEngine.mm

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77

88
#include <vector>
99

10+
#import "flutter/shell/platform/darwin/macos/framework/Source/FLEDartProject_Internal.h"
1011
#import "flutter/shell/platform/darwin/macos/framework/Source/FLEViewController_Internal.h"
1112
#import "flutter/shell/platform/embedder/embedder.h"
1213

13-
static NSString* const kICUBundlePath = @"icudtl.dat";
14-
1514
/**
1615
* Private interface declaration for FLEEngine.
1716
*/
@@ -126,15 +125,18 @@ @implementation FLEEngine {
126125
}
127126

128127
- (instancetype)init {
129-
return [self initWithViewController:nil];
128+
return [self initWithViewController:nil project:nil];
130129
}
131130

132-
- (instancetype)initWithViewController:(FLEViewController*)viewController {
131+
- (instancetype)initWithViewController:(FLEViewController*)viewController
132+
project:(FLEDartProject*)project {
133133
self = [super init];
134-
if (self != nil) {
135-
_viewController = viewController;
136-
_messageHandlers = [[NSMutableDictionary alloc] init];
137-
}
134+
NSAssert(self, @"Super init cannot be nil");
135+
136+
_viewController = viewController;
137+
_project = project ?: [[FLEDartProject alloc] init];
138+
_messageHandlers = [[NSMutableDictionary alloc] init];
139+
138140
return self;
139141
}
140142

@@ -144,8 +146,11 @@ - (void)dealloc {
144146
}
145147
}
146148

147-
- (BOOL)launchEngineWithAssetsPath:(NSURL*)assets
148-
commandLineArguments:(NSArray<NSString*>*)arguments {
149+
- (void)setProject:(FLEDartProject*)project {
150+
_project = project ?: [[FLEDartProject alloc] init];
151+
}
152+
153+
- (BOOL)run {
149154
if (_engine != NULL) {
150155
return NO;
151156
}
@@ -162,23 +167,13 @@ - (BOOL)launchEngineWithAssetsPath:(NSURL*)assets
162167

163168
// TODO(stuartmorgan): Move internal channel registration from FLEViewController to here.
164169

165-
// FlutterProjectArgs is expecting a full argv, so when processing it for flags the first
166-
// item is treated as the executable and ignored. Add a dummy value so that all provided arguments
167-
// are used.
168-
std::vector<const char*> argv = {"placeholder"};
169-
for (NSUInteger i = 0; i < arguments.count; ++i) {
170-
argv.push_back([arguments[i] UTF8String]);
171-
}
172-
173-
NSString* icuData = [[NSBundle bundleForClass:[self class]] pathForResource:kICUBundlePath
174-
ofType:nil];
175-
176170
FlutterProjectArgs flutterArguments = {};
177171
flutterArguments.struct_size = sizeof(FlutterProjectArgs);
178-
flutterArguments.assets_path = assets.fileSystemRepresentation;
179-
flutterArguments.icu_data_path = icuData.UTF8String;
180-
flutterArguments.command_line_argc = static_cast<int>(argv.size());
181-
flutterArguments.command_line_argv = &argv[0];
172+
flutterArguments.assets_path = _project.assetsPath.UTF8String;
173+
flutterArguments.icu_data_path = _project.ICUDataPath.UTF8String;
174+
std::vector<const char*> arguments = _project.argv;
175+
flutterArguments.command_line_argc = static_cast<int>(arguments.size());
176+
flutterArguments.command_line_argv = &arguments[0];
182177
flutterArguments.platform_message_callback = (FlutterPlatformMessageCallback)OnPlatformMessage;
183178

184179
FlutterEngineResult result = FlutterEngineRun(

shell/platform/darwin/macos/framework/Source/FLEViewController.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ @implementation FLEViewController {
174174
* Performs initialization that's common between the different init paths.
175175
*/
176176
static void CommonInit(FLEViewController* controller) {
177-
controller->_engine = [[FLEEngine alloc] initWithViewController:controller];
177+
controller->_engine = [[FLEEngine alloc] initWithViewController:controller project:nil];
178178
controller->_additionalKeyResponders = [[NSMutableOrderedSet alloc] init];
179179
controller->_mouseTrackingMode = FlutterMouseTrackingModeInKeyWindow;
180180
}
@@ -217,8 +217,7 @@ - (void)setMouseTrackingMode:(FlutterMouseTrackingMode)mode {
217217
[self configureTrackingArea];
218218
}
219219

220-
- (BOOL)launchEngineWithAssetsPath:(NSURL*)assets
221-
commandLineArguments:(NSArray<NSString*>*)arguments {
220+
- (BOOL)launchEngineWithProject:(nullable FLEDartProject*)project {
222221
// Set up the resource context. This is done here rather than in viewDidLoad as there's no
223222
// guarantee that viewDidLoad will be called before the engine is started, and the context must
224223
// be valid by that point.
@@ -227,7 +226,8 @@ - (BOOL)launchEngineWithAssetsPath:(NSURL*)assets
227226
// Register internal plugins before starting the engine.
228227
[self addInternalPlugins];
229228

230-
if (![_engine launchEngineWithAssetsPath:assets commandLineArguments:arguments]) {
229+
_engine.project = project;
230+
if (![_engine run]) {
231231
return NO;
232232
}
233233
// Send the initial user settings such as brightness and text scale factor

0 commit comments

Comments
 (0)