blob: 98f5643280914b279854656240bdb9a326183b94 [file] [log] [blame]
Avi Drissmanea1be232022-09-14 23:29:061// Copyright 2012 The Chromium Authors
sdefresne34e9bd42016-12-19 12:45:402// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import <UIKit/UIKit.h>
6
Justin Cohen28f8f9c2024-03-04 20:17:397#import "base/allocator/partition_alloc_support.h"
Gauthier Ambard999088c2022-09-13 08:36:578#import "base/at_exit.h"
9#import "base/debug/crash_logging.h"
10#import "base/strings/sys_string_conversions.h"
Dave Tapuskac625d8b2023-08-14 20:26:1111#import "build/blink_buildflags.h"
ginnnnnnnyb88db3e22022-11-04 17:15:3312#import "components/component_updater/component_updater_paths.h"
Gauthier Ambard999088c2022-09-13 08:36:5713#import "ios/chrome/app/startup/ios_chrome_main.h"
14#import "ios/chrome/app/startup/ios_enable_sandbox_dump_buildflags.h"
Justin Cohen7b1e0df2024-10-17 22:25:0715#import "ios/chrome/app/tests_hook.h"
Aman Vermab6f55572023-09-28 11:40:5316#import "ios/chrome/browser/crash_report/model/crash_helper.h"
Gauthier Ambardc4177472023-06-16 16:03:0817#import "ios/chrome/browser/shared/model/paths/paths.h"
Max Lid2deea412022-10-12 21:56:4218#import "ios/public/provider/chrome/browser/primes/primes_api.h"
sdefresne34e9bd42016-12-19 12:45:4019
Olivier Robind6dd04d2020-07-30 06:21:5220#if BUILDFLAG(IOS_ENABLE_SANDBOX_DUMP)
Gauthier Ambard999088c2022-09-13 08:36:5721#import "ios/chrome/app/startup/sandbox_dump.h" // nogncheck
Olivier Robind6dd04d2020-07-30 06:21:5222#endif // BUILDFLAG(IOS_ENABLE_SANDBOX_DUMP)
23
Dave Tapuska8cda9b42024-05-15 14:30:4824#if BUILDFLAG(USE_BLINK)
25extern "C" {
26// This function must be marked with NO_STACK_PROTECTOR or it may crash on
27// return, see the --change-stack-guard-on-fork command line flag.
Peter Kasting7bf5e3f92024-09-05 17:25:1928NO_STACK_PROTECTOR __attribute__((visibility("default"))) int ChromeMain(
29 int argc,
30 char* argv[]);
Dave Tapuska8cda9b42024-05-15 14:30:4831}
32#endif
33
sdefresne34e9bd42016-12-19 12:45:4034namespace {
35
36NSString* const kUIApplicationDelegateInfoKey = @"UIApplicationDelegate";
37
38void StartCrashController() {
Yuke Liaod795cd032017-06-22 20:57:3239 @autoreleasepool {
Justin Cohenb08ae482021-02-05 20:19:1840 crash_helper::Start();
Yuke Liaod795cd032017-06-22 20:57:3241 }
42}
43
44void SetTextDirectionIfPseudoRTLEnabled() {
45 @autoreleasepool {
46 NSUserDefaults* standard_defaults = [NSUserDefaults standardUserDefaults];
47 if ([standard_defaults boolForKey:@"EnablePseudoRTL"]) {
Mark Coganf31b0da2017-09-01 14:23:5048 NSDictionary* pseudoDict = @{
49 @"AppleTextDirection" : @"YES",
50 @"NSForceRightToLeftWritingDirection" : @"YES"
51 };
Yuke Liaod795cd032017-06-22 20:57:3252 [standard_defaults registerDefaults:pseudoDict];
53 }
54 }
55}
56
57int RunUIApplicationMain(int argc, char* argv[]) {
58 @autoreleasepool {
59 // Fetch the name of the UIApplication delegate stored in the application
60 // Info.plist under the "UIApplicationDelegate" key.
61 NSString* delegate_class_name = [[NSBundle mainBundle]
62 objectForInfoDictionaryKey:kUIApplicationDelegateInfoKey];
63 CHECK(delegate_class_name);
64
65 return UIApplicationMain(argc, argv, nil, delegate_class_name);
66 }
sdefresne34e9bd42016-12-19 12:45:4067}
68
ginnnnnnnyb88db3e22022-11-04 17:15:3369void RegisterPathProviders() {
70 @autoreleasepool {
71 ios::RegisterPathProvider();
72
73 // Bundled components are not supported on ios, so DIR_USER_DATA is passed
74 // for all three arguments.
75 component_updater::RegisterPathProvider(
76 ios::DIR_USER_DATA, ios::DIR_USER_DATA, ios::DIR_USER_DATA);
77 }
78}
79
sdefresne34e9bd42016-12-19 12:45:4080} // namespace
81
Dave Tapuska8cda9b42024-05-15 14:30:4882int ChromeMain(int argc, char* argv[]) {
sdefresne34e9bd42016-12-19 12:45:4083 IOSChromeMain::InitStartTime();
sdefresne34e9bd42016-12-19 12:45:4084
Olivier Robind6dd04d2020-07-30 06:21:5285#if BUILDFLAG(IOS_ENABLE_SANDBOX_DUMP)
86 // Dumps the sandbox if needed. This must be called as soon as possible,
87 // before actions are done on the sandbox.
88 // This is a blocking call.
89 DumpSandboxIfRequested();
90#endif // BUILDFLAG(IOS_ENABLE_SANDBOX_DUMP)
91
Justin Cohen7b1e0df2024-10-17 22:25:0792 tests_hook::WipeProfileIfRequested(argc, argv);
93
Yuke Liao1764ee12017-06-15 19:36:1094 // Set NSUserDefaults keys to force pseudo-RTL if needed.
Yuke Liaod795cd032017-06-22 20:57:3295 SetTextDirectionIfPseudoRTLEnabled();
sdefresne34e9bd42016-12-19 12:45:4096
Yuke Liao1764ee12017-06-15 19:36:1097 // Create this here since it's needed to start the crash handler.
98 base::AtExitManager at_exit;
sdefresne34e9bd42016-12-19 12:45:4099
Max Lid2deea412022-10-12 21:56:42100 // Start Primes logging if it's supported.
101 if (ios::provider::IsPrimesSupported()) {
102 ios::provider::PrimesStartLogging();
103 }
104
Yuke Liao1764ee12017-06-15 19:36:10105 // The Crash Controller is started here even if the user opted out since we
106 // don't have yet preferences. Later on it is stopped if the user opted out.
107 // In any case reports are not sent if the user opted out.
108 StartCrashController();
109
110 // Always ignore SIGPIPE. We check the return value of write().
111 CHECK_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN));
112
ginnnnnnnyb88db3e22022-11-04 17:15:33113 // Register Chrome path providers.
114 RegisterPathProviders();
115
Arthur Sonzogni62e877a2024-04-30 16:09:43116#if PA_BUILDFLAG(USE_PARTITION_ALLOC) && !BUILDFLAG(USE_BLINK)
Justinb4d29072024-03-19 17:41:00117 // ContentMainRunnerImpl::Initialize calls this when USE_BLINK is true.
Justin Cohen28f8f9c2024-03-04 20:17:39118 base::allocator::PartitionAllocSupport::Get()->ReconfigureEarlyish("");
Arthur Sonzogni62e877a2024-04-30 16:09:43119#endif // PA_BUILDFLAG(USE_PARTITION_ALLOC) && !BUILDFLAG(USE_BLINK)
Justin Cohen28f8f9c2024-03-04 20:17:39120
Yuke Liaod795cd032017-06-22 20:57:32121 return RunUIApplicationMain(argc, argv);
sdefresne34e9bd42016-12-19 12:45:40122}
Dave Tapuska8cda9b42024-05-15 14:30:48123
124#if !BUILDFLAG(USE_BLINK)
125int main(int argc, char* argv[]) {
126 return ChromeMain(argc, argv);
127}
128#endif