Flutter Engine
FlutterDartProject.mm
Go to the documentation of this file.
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/FlutterDartProject.h"
6 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
7 
8 #include <vector>
9 
10 #include "flutter/shell/platform/common/engine_switches.h"
11 
12 static NSString* const kICUBundlePath = @"icudtl.dat";
13 static NSString* const kAppBundleIdentifier = @"io.flutter.flutter.app";
14 
15 @implementation FlutterDartProject {
16  NSBundle* _dartBundle;
17  NSString* _assetsPath;
18  NSString* _ICUDataPath;
19 }
20 
21 - (instancetype)init {
22  return [self initWithPrecompiledDartBundle:nil];
23 }
24 
25 - (instancetype)initWithPrecompiledDartBundle:(NSBundle*)bundle {
26  self = [super init];
27  NSAssert(self, @"Super init cannot be nil");
28 
29  _dartBundle = bundle ?: [NSBundle bundleWithIdentifier:kAppBundleIdentifier];
30  if (_dartBundle == nil) {
31  // The bundle isn't loaded and can't be found by bundle ID. Find it by path.
32  _dartBundle = [NSBundle bundleWithURL:[NSBundle.mainBundle.privateFrameworksURL
33  URLByAppendingPathComponent:@"App.framework"]];
34  }
35  if (!_dartBundle.isLoaded) {
36  [_dartBundle load];
37  }
38  _dartEntrypointArguments = [[NSProcessInfo processInfo] arguments];
39  // Remove the first element as it's the binary name
40  _dartEntrypointArguments = [_dartEntrypointArguments
41  subarrayWithRange:NSMakeRange(1, _dartEntrypointArguments.count - 1)];
42  return self;
43 }
44 
45 - (instancetype)initWithAssetsPath:(NSString*)assets ICUDataPath:(NSString*)icuPath {
46  self = [super init];
47  NSAssert(self, @"Super init cannot be nil");
48  _assetsPath = assets;
49  _ICUDataPath = icuPath;
50  return self;
51 }
52 
53 - (NSString*)assetsPath {
54  if (_assetsPath) {
55  return _assetsPath;
56  }
57 
58  // If there's no App.framework, fall back to checking the main bundle for assets.
59  NSBundle* assetBundle = _dartBundle ?: [NSBundle mainBundle];
60  NSString* flutterAssetsName = [assetBundle objectForInfoDictionaryKey:@"FLTAssetsPath"];
61  if (flutterAssetsName == nil) {
62  flutterAssetsName = @"flutter_assets";
63  }
64  NSString* path = [assetBundle pathForResource:flutterAssetsName ofType:@""];
65  if (!path) {
66  NSLog(@"Failed to find path for \"%@\"", flutterAssetsName);
67  }
68  return path;
69 }
70 
71 - (NSString*)ICUDataPath {
72  if (_ICUDataPath) {
73  return _ICUDataPath;
74  }
75 
76  NSString* path = [[NSBundle bundleForClass:[self class]] pathForResource:kICUBundlePath
77  ofType:nil];
78  if (!path) {
79  NSLog(@"Failed to find path for \"%@\"", kICUBundlePath);
80  }
81  return path;
82 }
83 
84 - (std::vector<std::string>)switches {
85  std::vector<std::string> arguments = flutter::GetSwitchesFromEnvironment();
86  if (self.enableMirrors) {
87  arguments.push_back("--dart-flags=--enable_mirrors=true");
88  }
89  return arguments;
90 }
91 
92 @end
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
static NSString *const kAppBundleIdentifier
NSString * _ICUDataPath
std::vector< std::string > GetSwitchesFromEnvironment()
NSString * _assetsPath
static NSString *const kICUBundlePath