main_delegate.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE-CHROMIUM file.
  4. #include "brightray/common/main_delegate.h"
  5. #include <memory>
  6. #include "base/command_line.h"
  7. #include "base/mac/bundle_locations.h"
  8. #include "base/path_service.h"
  9. #include "brightray/browser/browser_client.h"
  10. #include "brightray/common/content_client.h"
  11. #include "content/public/common/content_switches.h"
  12. #include "electron/buildflags/buildflags.h"
  13. #include "services/service_manager/embedder/switches.h"
  14. #include "ui/base/resource/resource_bundle.h"
  15. #include "ui/base/ui_base_switches.h"
  16. namespace brightray {
  17. namespace {
  18. // Returns true if this subprocess type needs the ResourceBundle initialized
  19. // and resources loaded.
  20. bool SubprocessNeedsResourceBundle(const std::string& process_type) {
  21. return
  22. #if defined(OS_POSIX) && !defined(OS_MACOSX)
  23. // The zygote process opens the resources for the renderers.
  24. process_type == service_manager::switches::kZygoteProcess ||
  25. #endif
  26. #if defined(OS_MACOSX)
  27. // Mac needs them too for scrollbar related images and for sandbox
  28. // profiles.
  29. process_type == switches::kPpapiPluginProcess ||
  30. process_type == switches::kPpapiBrokerProcess ||
  31. process_type == switches::kGpuProcess ||
  32. #endif
  33. process_type == switches::kRendererProcess ||
  34. process_type == switches::kUtilityProcess;
  35. }
  36. } // namespace
  37. void LoadResourceBundle(const std::string& locale) {
  38. const bool initialized = ui::ResourceBundle::HasSharedInstance();
  39. if (initialized)
  40. ui::ResourceBundle::CleanupSharedInstance();
  41. // Load other resource files.
  42. base::FilePath pak_dir;
  43. #if defined(OS_MACOSX)
  44. pak_dir =
  45. base::mac::FrameworkBundlePath().Append(FILE_PATH_LITERAL("Resources"));
  46. #else
  47. base::PathService::Get(base::DIR_MODULE, &pak_dir);
  48. #endif
  49. ui::ResourceBundle::InitSharedInstanceWithLocale(
  50. locale, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
  51. ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  52. bundle.ReloadLocaleResources(locale);
  53. bundle.AddDataPackFromPath(pak_dir.Append(FILE_PATH_LITERAL("resources.pak")),
  54. ui::SCALE_FACTOR_NONE);
  55. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  56. NOTIMPLEMENTED()
  57. << "Hi, whoever's fixing PDF support! Thanks! The pdf "
  58. "viewer resources haven't been ported over to the GN build yet, so "
  59. "you'll probably need to change this bit of code.";
  60. bundle.AddDataPackFromPath(
  61. pak_dir.Append(FILE_PATH_LITERAL("pdf_viewer_resources.pak")),
  62. ui::GetSupportedScaleFactors()[0]);
  63. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  64. }
  65. MainDelegate::MainDelegate() {}
  66. MainDelegate::~MainDelegate() {}
  67. std::unique_ptr<ContentClient> MainDelegate::CreateContentClient() {
  68. return std::unique_ptr<ContentClient>(new ContentClient);
  69. }
  70. bool MainDelegate::BasicStartupComplete(int* exit_code) {
  71. content_client_ = CreateContentClient();
  72. SetContentClient(content_client_.get());
  73. #if defined(OS_MACOSX)
  74. OverrideChildProcessPath();
  75. OverrideFrameworkBundlePath();
  76. #endif
  77. return false;
  78. }
  79. void MainDelegate::PreSandboxStartup() {
  80. auto cmd = *base::CommandLine::ForCurrentProcess();
  81. std::string process_type = cmd.GetSwitchValueASCII(switches::kProcessType);
  82. // Initialize ResourceBundle which handles files loaded from external
  83. // sources. The language should have been passed in to us from the
  84. // browser process as a command line flag.
  85. if (SubprocessNeedsResourceBundle(process_type)) {
  86. std::string locale = cmd.GetSwitchValueASCII(switches::kLang);
  87. LoadResourceBundle(locale);
  88. }
  89. }
  90. content::ContentBrowserClient* MainDelegate::CreateContentBrowserClient() {
  91. browser_client_ = CreateBrowserClient();
  92. return browser_client_.get();
  93. }
  94. std::unique_ptr<BrowserClient> MainDelegate::CreateBrowserClient() {
  95. return std::unique_ptr<BrowserClient>(new BrowserClient);
  96. }
  97. } // namespace brightray