electron_main_delegate_mac.mm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (c) 2014 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/app/electron_main_delegate.h"
  5. #include <string>
  6. #include "base/apple/bundle_locations.h"
  7. #include "base/apple/foundation_util.h"
  8. #include "base/files/file_path.h"
  9. #include "base/files/file_util.h"
  10. #include "base/path_service.h"
  11. #include "base/strings/sys_string_conversions.h"
  12. #include "content/browser/mac_helpers.h"
  13. #include "content/public/common/content_paths.h"
  14. #include "shell/browser/mac/electron_application.h"
  15. #include "shell/common/application_info.h"
  16. #include "shell/common/mac/main_application_bundle.h"
  17. namespace electron {
  18. namespace {
  19. base::FilePath GetFrameworksPath() {
  20. return MainApplicationBundlePath().Append("Contents").Append("Frameworks");
  21. }
  22. base::FilePath GetHelperAppPath(const base::FilePath& frameworks_path,
  23. const std::string& name) {
  24. // Figure out what helper we are running
  25. base::FilePath path;
  26. base::PathService::Get(base::FILE_EXE, &path);
  27. std::string helper_name = "Helper";
  28. if (const auto& val = path.value();
  29. val.ends_with(content::kMacHelperSuffix_renderer)) {
  30. helper_name += content::kMacHelperSuffix_renderer;
  31. } else if (val.ends_with(content::kMacHelperSuffix_gpu)) {
  32. helper_name += content::kMacHelperSuffix_gpu;
  33. } else if (val.ends_with(content::kMacHelperSuffix_plugin)) {
  34. helper_name += content::kMacHelperSuffix_plugin;
  35. }
  36. return frameworks_path.Append(name + " " + helper_name + ".app")
  37. .Append("Contents")
  38. .Append("MacOS")
  39. .Append(name + " " + helper_name);
  40. }
  41. } // namespace
  42. void ElectronMainDelegate::OverrideFrameworkBundlePath() {
  43. base::apple::SetOverrideFrameworkBundlePath(
  44. GetFrameworksPath().Append(ELECTRON_PRODUCT_NAME " Framework.framework"));
  45. }
  46. void ElectronMainDelegate::OverrideChildProcessPath() {
  47. base::FilePath frameworks_path = GetFrameworksPath();
  48. base::FilePath helper_path =
  49. GetHelperAppPath(frameworks_path, ELECTRON_PRODUCT_NAME);
  50. if (!base::PathExists(helper_path))
  51. helper_path = GetHelperAppPath(frameworks_path, GetApplicationName());
  52. if (!base::PathExists(helper_path))
  53. LOG(FATAL) << "Unable to find helper app";
  54. base::PathService::OverrideAndCreateIfNeeded(
  55. content::CHILD_PROCESS_EXE, helper_path, /*is_absolute=*/true,
  56. /*create=*/false);
  57. }
  58. void ElectronMainDelegate::SetUpBundleOverrides() {
  59. @autoreleasepool {
  60. NSBundle* bundle = MainApplicationBundle();
  61. std::string base_bundle_id =
  62. base::SysNSStringToUTF8([bundle bundleIdentifier]);
  63. NSString* team_id = [bundle objectForInfoDictionaryKey:@"ElectronTeamID"];
  64. if (team_id)
  65. base_bundle_id = base::SysNSStringToUTF8(team_id) + "." + base_bundle_id;
  66. base::apple::SetBaseBundleID(base_bundle_id.c_str());
  67. }
  68. }
  69. void RegisterAtomCrApp() {
  70. // Force the NSApplication subclass to be used.
  71. [AtomApplication sharedApplication];
  72. }
  73. } // namespace electron