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/cpp/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  _dartEntrypointArguments = [[NSProcessInfo processInfo] arguments];
31  // Remove the first element as it's the binary name
32  _dartEntrypointArguments = [_dartEntrypointArguments
33  subarrayWithRange:NSMakeRange(1, _dartEntrypointArguments.count - 1)];
34  return self;
35 }
36 
37 - (instancetype)initWithAssetsPath:(NSString*)assets ICUDataPath:(NSString*)icuPath {
38  self = [super init];
39  NSAssert(self, @"Super init cannot be nil");
40  _assetsPath = assets;
41  _ICUDataPath = icuPath;
42  return self;
43 }
44 
45 - (NSString*)assetsPath {
46  if (_assetsPath) {
47  return _assetsPath;
48  }
49 
50  // If there's no App.framework, fall back to checking the main bundle for assets.
51  NSBundle* assetBundle = _dartBundle ?: [NSBundle mainBundle];
52  NSString* flutterAssetsName = [assetBundle objectForInfoDictionaryKey:@"FLTAssetsPath"];
53  if (flutterAssetsName == nil) {
54  flutterAssetsName = @"flutter_assets";
55  }
56  NSString* path = [assetBundle pathForResource:flutterAssetsName ofType:@""];
57  if (!path) {
58  NSLog(@"Failed to find path for \"%@\"", flutterAssetsName);
59  }
60  return path;
61 }
62 
63 - (NSString*)ICUDataPath {
64  if (_ICUDataPath) {
65  return _ICUDataPath;
66  }
67 
68  NSString* path = [[NSBundle bundleForClass:[self class]] pathForResource:kICUBundlePath
69  ofType:nil];
70  if (!path) {
71  NSLog(@"Failed to find path for \"%@\"", kICUBundlePath);
72  }
73  return path;
74 }
75 
76 - (std::vector<std::string>)switches {
77  std::vector<std::string> arguments = flutter::GetSwitchesFromEnvironment();
78  if (self.enableMirrors) {
79  arguments.push_back("--dart-flags=--enable_mirrors=true");
80  }
81  return arguments;
82 }
83 
84 @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