2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-05-02 12:09:19 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/electron_browser_main_parts.h"
|
2013-05-02 12:09:19 +00:00
|
|
|
|
2022-09-23 18:50:46 +00:00
|
|
|
#include <string>
|
|
|
|
|
2023-05-23 19:58:58 +00:00
|
|
|
#include "base/apple/bundle_locations.h"
|
2019-10-21 21:11:09 +00:00
|
|
|
#include "base/mac/foundation_util.h"
|
|
|
|
#include "base/path_service.h"
|
2023-06-09 23:08:36 +00:00
|
|
|
#include "services/device/public/cpp/geolocation/geolocation_manager.h"
|
2023-03-10 16:07:42 +00:00
|
|
|
#include "services/device/public/cpp/geolocation/system_geolocation_source_mac.h"
|
2023-03-16 10:03:53 +00:00
|
|
|
#include "shell/browser/browser_process_impl.h"
|
2023-08-23 13:54:21 +00:00
|
|
|
#include "shell/browser/mac/electron_application.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/mac/electron_application_delegate.h"
|
2020-05-07 20:31:26 +00:00
|
|
|
#include "shell/common/electron_paths.h"
|
2019-10-21 21:11:09 +00:00
|
|
|
#include "ui/base/l10n/l10n_util_mac.h"
|
2013-05-02 12:09:19 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2023-07-18 22:26:27 +00:00
|
|
|
static ElectronApplicationDelegate* __strong delegate_;
|
|
|
|
|
2021-05-19 23:15:47 +00:00
|
|
|
void ElectronBrowserMainParts::PreCreateMainMessageLoop() {
|
2016-01-08 04:06:06 +00:00
|
|
|
// Set our own application delegate.
|
2023-07-18 22:26:27 +00:00
|
|
|
delegate_ = [[ElectronApplicationDelegate alloc] init];
|
|
|
|
[NSApp setDelegate:delegate_];
|
2013-05-02 12:09:19 +00:00
|
|
|
|
2021-05-19 23:15:47 +00:00
|
|
|
PreCreateMainMessageLoopCommon();
|
2013-05-31 02:17:40 +00:00
|
|
|
|
|
|
|
// Prevent Cocoa from turning command-line arguments into
|
|
|
|
// |-application:openFiles:|, since we already handle them directly.
|
|
|
|
[[NSUserDefaults standardUserDefaults]
|
2018-04-20 18:47:04 +00:00
|
|
|
setObject:@"NO"
|
|
|
|
forKey:@"NSTreatUnknownArgumentsAsOpen"];
|
2021-06-29 03:26:57 +00:00
|
|
|
|
2023-06-09 23:08:36 +00:00
|
|
|
if (!device::GeolocationManager::GetInstance()) {
|
|
|
|
device::GeolocationManager::SetInstance(
|
2023-03-16 10:03:53 +00:00
|
|
|
device::SystemGeolocationSourceMac::CreateGeolocationManagerOnMac());
|
|
|
|
}
|
2013-05-02 12:09:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 09:23:27 +00:00
|
|
|
void ElectronBrowserMainParts::FreeAppDelegate() {
|
2023-07-18 22:26:27 +00:00
|
|
|
delegate_ = nil;
|
2014-11-17 01:52:24 +00:00
|
|
|
[NSApp setDelegate:nil];
|
2013-05-30 08:03:10 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 15:54:03 +00:00
|
|
|
void ElectronBrowserMainParts::RegisterURLHandler() {
|
2020-02-21 19:05:03 +00:00
|
|
|
[[AtomApplication sharedApplication] registerURLHandler];
|
2019-10-10 15:54:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 21:11:09 +00:00
|
|
|
// Replicates NSApplicationMain, but doesn't start a run loop.
|
|
|
|
void ElectronBrowserMainParts::InitializeMainNib() {
|
2023-05-23 19:58:58 +00:00
|
|
|
auto infoDictionary = base::apple::OuterBundle().infoDictionary;
|
2019-10-21 21:11:09 +00:00
|
|
|
|
|
|
|
auto principalClass =
|
|
|
|
NSClassFromString([infoDictionary objectForKey:@"NSPrincipalClass"]);
|
|
|
|
auto application = [principalClass sharedApplication];
|
|
|
|
|
|
|
|
NSString* mainNibName = [infoDictionary objectForKey:@"NSMainNibFile"];
|
|
|
|
|
|
|
|
NSNib* mainNib;
|
|
|
|
|
|
|
|
@try {
|
|
|
|
mainNib = [[NSNib alloc] initWithNibNamed:mainNibName
|
2023-05-23 19:58:58 +00:00
|
|
|
bundle:base::apple::FrameworkBundle()];
|
2019-10-21 21:11:09 +00:00
|
|
|
// Handle failure of initWithNibNamed on SMB shares
|
|
|
|
// TODO(codebytere): Remove when
|
|
|
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=932935 is fixed
|
|
|
|
} @catch (NSException* exception) {
|
|
|
|
NSString* nibPath =
|
|
|
|
[NSString stringWithFormat:@"Resources/%@.nib", mainNibName];
|
2023-05-23 19:58:58 +00:00
|
|
|
nibPath = [base::apple::FrameworkBundle().bundlePath
|
2019-10-21 21:11:09 +00:00
|
|
|
stringByAppendingPathComponent:nibPath];
|
|
|
|
|
|
|
|
NSData* data = [NSData dataWithContentsOfFile:nibPath];
|
|
|
|
mainNib = [[NSNib alloc] initWithNibData:data
|
2023-05-23 19:58:58 +00:00
|
|
|
bundle:base::apple::FrameworkBundle()];
|
2019-10-21 21:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[mainNib instantiateWithOwner:application topLevelObjects:nil];
|
2018-10-23 20:15:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 18:50:46 +00:00
|
|
|
std::string ElectronBrowserMainParts::GetCurrentSystemLocale() {
|
|
|
|
NSString* systemLocaleIdentifier =
|
|
|
|
[[NSLocale currentLocale] localeIdentifier];
|
|
|
|
|
|
|
|
// Mac OS X uses "_" instead of "-", so swap to get a real locale value.
|
|
|
|
std::string locale_value = [[systemLocaleIdentifier
|
|
|
|
stringByReplacingOccurrencesOfString:@"_"
|
|
|
|
withString:@"-"] UTF8String];
|
|
|
|
|
|
|
|
return locale_value;
|
|
|
|
}
|
|
|
|
|
2013-05-02 12:09:19 +00:00
|
|
|
} // namespace electron
|