electron_main_delegate_mac.mm 3.2 KB

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