electron_main_delegate_mac.mm 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/mac/foundation_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. #if !defined(__has_feature) || !__has_feature(objc_arc)
  18. #error "This file requires ARC support."
  19. #endif
  20. namespace electron {
  21. namespace {
  22. base::FilePath GetFrameworksPath() {
  23. return MainApplicationBundlePath().Append("Contents").Append("Frameworks");
  24. }
  25. base::FilePath GetHelperAppPath(const base::FilePath& frameworks_path,
  26. const std::string& name) {
  27. // Figure out what helper we are running
  28. base::FilePath path;
  29. base::PathService::Get(base::FILE_EXE, &path);
  30. std::string helper_name = "Helper";
  31. if (base::EndsWith(path.value(), content::kMacHelperSuffix_renderer,
  32. base::CompareCase::SENSITIVE)) {
  33. helper_name += content::kMacHelperSuffix_renderer;
  34. } else if (base::EndsWith(path.value(), content::kMacHelperSuffix_gpu,
  35. base::CompareCase::SENSITIVE)) {
  36. helper_name += content::kMacHelperSuffix_gpu;
  37. } else if (base::EndsWith(path.value(), content::kMacHelperSuffix_plugin,
  38. base::CompareCase::SENSITIVE)) {
  39. helper_name += content::kMacHelperSuffix_plugin;
  40. }
  41. return frameworks_path.Append(name + " " + helper_name + ".app")
  42. .Append("Contents")
  43. .Append("MacOS")
  44. .Append(name + " " + helper_name);
  45. }
  46. } // namespace
  47. void ElectronMainDelegate::OverrideFrameworkBundlePath() {
  48. base::apple::SetOverrideFrameworkBundlePath(
  49. GetFrameworksPath().Append(ELECTRON_PRODUCT_NAME " Framework.framework"));
  50. }
  51. void ElectronMainDelegate::OverrideChildProcessPath() {
  52. base::FilePath frameworks_path = GetFrameworksPath();
  53. base::FilePath helper_path =
  54. GetHelperAppPath(frameworks_path, ELECTRON_PRODUCT_NAME);
  55. if (!base::PathExists(helper_path))
  56. helper_path = GetHelperAppPath(frameworks_path, GetApplicationName());
  57. if (!base::PathExists(helper_path))
  58. LOG(FATAL) << "Unable to find helper app";
  59. base::PathService::OverrideAndCreateIfNeeded(
  60. content::CHILD_PROCESS_EXE, helper_path, /*is_absolute=*/true,
  61. /*create=*/false);
  62. }
  63. void ElectronMainDelegate::SetUpBundleOverrides() {
  64. @autoreleasepool {
  65. NSBundle* bundle = MainApplicationBundle();
  66. std::string base_bundle_id =
  67. base::SysNSStringToUTF8([bundle bundleIdentifier]);
  68. NSString* team_id = [bundle objectForInfoDictionaryKey:@"ElectronTeamID"];
  69. if (team_id)
  70. base_bundle_id = base::SysNSStringToUTF8(team_id) + "." + base_bundle_id;
  71. base::mac::SetBaseBundleID(base_bundle_id.c_str());
  72. }
  73. }
  74. void RegisterAtomCrApp() {
  75. // Force the NSApplication subclass to be used.
  76. [AtomApplication sharedApplication];
  77. }
  78. } // namespace electron