electron_browser_main_parts_mac.mm 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/electron_browser_main_parts.h"
  5. #include <string>
  6. #include "base/apple/bundle_locations.h"
  7. #include "base/apple/foundation_util.h"
  8. #include "services/device/public/cpp/geolocation/geolocation_system_permission_manager.h"
  9. #include "services/device/public/cpp/geolocation/system_geolocation_source_apple.h"
  10. #include "shell/browser/browser_process_impl.h"
  11. #include "shell/browser/mac/electron_application.h"
  12. #include "shell/browser/mac/electron_application_delegate.h"
  13. #include "ui/base/l10n/l10n_util_mac.h"
  14. namespace electron {
  15. static ElectronApplicationDelegate* __strong delegate_;
  16. void ElectronBrowserMainParts::PreCreateMainMessageLoop() {
  17. // Set our own application delegate.
  18. delegate_ = [[ElectronApplicationDelegate alloc] init];
  19. [NSApp setDelegate:delegate_];
  20. PreCreateMainMessageLoopCommon();
  21. // Prevent Cocoa from turning command-line arguments into
  22. // |-application:openFiles:|, since we already handle them directly.
  23. [[NSUserDefaults standardUserDefaults]
  24. setObject:@"NO"
  25. forKey:@"NSTreatUnknownArgumentsAsOpen"];
  26. if (!device::GeolocationSystemPermissionManager::GetInstance()) {
  27. device::GeolocationSystemPermissionManager::SetInstance(
  28. device::SystemGeolocationSourceApple::
  29. CreateGeolocationSystemPermissionManager());
  30. }
  31. }
  32. void ElectronBrowserMainParts::FreeAppDelegate() {
  33. delegate_ = nil;
  34. [NSApp setDelegate:nil];
  35. }
  36. void ElectronBrowserMainParts::RegisterURLHandler() {
  37. [[AtomApplication sharedApplication] registerURLHandler];
  38. }
  39. // Replicates NSApplicationMain, but doesn't start a run loop.
  40. void ElectronBrowserMainParts::InitializeMainNib() {
  41. auto infoDictionary = base::apple::OuterBundle().infoDictionary;
  42. auto principalClass =
  43. NSClassFromString([infoDictionary objectForKey:@"NSPrincipalClass"]);
  44. auto application = [principalClass sharedApplication];
  45. NSString* mainNibName = [infoDictionary objectForKey:@"NSMainNibFile"];
  46. NSNib* mainNib;
  47. @try {
  48. mainNib = [[NSNib alloc] initWithNibNamed:mainNibName
  49. bundle:base::apple::FrameworkBundle()];
  50. // Handle failure of initWithNibNamed on SMB shares
  51. // TODO(codebytere): Remove when
  52. // https://bugs.chromium.org/p/chromium/issues/detail?id=932935 is fixed
  53. } @catch (NSException* exception) {
  54. NSString* nibPath =
  55. [NSString stringWithFormat:@"Resources/%@.nib", mainNibName];
  56. nibPath = [base::apple::FrameworkBundle().bundlePath
  57. stringByAppendingPathComponent:nibPath];
  58. NSData* data = [NSData dataWithContentsOfFile:nibPath];
  59. mainNib = [[NSNib alloc] initWithNibData:data
  60. bundle:base::apple::FrameworkBundle()];
  61. }
  62. [mainNib instantiateWithOwner:application topLevelObjects:nil];
  63. }
  64. std::string ElectronBrowserMainParts::GetCurrentSystemLocale() {
  65. NSString* systemLocaleIdentifier =
  66. [[NSLocale currentLocale] localeIdentifier];
  67. // Mac OS X uses "_" instead of "-", so swap to get a real locale value.
  68. std::string locale_value = [[systemLocaleIdentifier
  69. stringByReplacingOccurrencesOfString:@"_"
  70. withString:@"-"] UTF8String];
  71. return locale_value;
  72. }
  73. } // namespace electron