electron_main_delegate_mac.mm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 (base::EndsWith(path.value(), content::kMacHelperSuffix_renderer,
  29. base::CompareCase::SENSITIVE)) {
  30. helper_name += content::kMacHelperSuffix_renderer;
  31. } else if (base::EndsWith(path.value(), content::kMacHelperSuffix_gpu,
  32. base::CompareCase::SENSITIVE)) {
  33. helper_name += content::kMacHelperSuffix_gpu;
  34. } else if (base::EndsWith(path.value(), content::kMacHelperSuffix_plugin,
  35. base::CompareCase::SENSITIVE)) {
  36. helper_name += content::kMacHelperSuffix_plugin;
  37. }
  38. return frameworks_path.Append(name + " " + helper_name + ".app")
  39. .Append("Contents")
  40. .Append("MacOS")
  41. .Append(name + " " + helper_name);
  42. }
  43. } // namespace
  44. void ElectronMainDelegate::OverrideFrameworkBundlePath() {
  45. base::apple::SetOverrideFrameworkBundlePath(
  46. GetFrameworksPath().Append(ELECTRON_PRODUCT_NAME " Framework.framework"));
  47. }
  48. void ElectronMainDelegate::OverrideChildProcessPath() {
  49. base::FilePath frameworks_path = GetFrameworksPath();
  50. base::FilePath helper_path =
  51. GetHelperAppPath(frameworks_path, ELECTRON_PRODUCT_NAME);
  52. if (!base::PathExists(helper_path))
  53. helper_path = GetHelperAppPath(frameworks_path, GetApplicationName());
  54. if (!base::PathExists(helper_path))
  55. LOG(FATAL) << "Unable to find helper app";
  56. base::PathService::OverrideAndCreateIfNeeded(
  57. content::CHILD_PROCESS_EXE, helper_path, /*is_absolute=*/true,
  58. /*create=*/false);
  59. }
  60. void ElectronMainDelegate::SetUpBundleOverrides() {
  61. @autoreleasepool {
  62. NSBundle* bundle = MainApplicationBundle();
  63. std::string base_bundle_id =
  64. base::SysNSStringToUTF8([bundle bundleIdentifier]);
  65. NSString* team_id = [bundle objectForInfoDictionaryKey:@"ElectronTeamID"];
  66. if (team_id)
  67. base_bundle_id = base::SysNSStringToUTF8(team_id) + "." + base_bundle_id;
  68. base::apple::SetBaseBundleID(base_bundle_id.c_str());
  69. }
  70. }
  71. void RegisterAtomCrApp() {
  72. // Force the NSApplication subclass to be used.
  73. [AtomApplication sharedApplication];
  74. }
  75. } // namespace electron