main_delegate.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "ui/base/resource/resource_bundle.h"
  13. #include "ui/base/ui_base_switches.h"
  14. namespace brightray {
  15. namespace {
  16. // Returns true if this subprocess type needs the ResourceBundle initialized
  17. // and resources loaded.
  18. bool SubprocessNeedsResourceBundle(const std::string& process_type) {
  19. return
  20. #if defined(OS_POSIX) && !defined(OS_MACOSX)
  21. // The zygote process opens the resources for the renderers.
  22. process_type == switches::kZygoteProcess ||
  23. #endif
  24. #if defined(OS_MACOSX)
  25. // Mac needs them too for scrollbar related images and for sandbox
  26. // profiles.
  27. #if !defined(DISABLE_NACL)
  28. process_type == switches::kNaClLoaderProcess ||
  29. #endif
  30. process_type == switches::kPpapiPluginProcess ||
  31. process_type == switches::kPpapiBrokerProcess ||
  32. process_type == switches::kGpuProcess ||
  33. #endif
  34. process_type == switches::kRendererProcess ||
  35. process_type == switches::kUtilityProcess;
  36. }
  37. } // namespace
  38. void LoadResourceBundle(const std::string& locale) {
  39. const bool initialized = ui::ResourceBundle::HasSharedInstance();
  40. if (initialized)
  41. ui::ResourceBundle::CleanupSharedInstance();
  42. ui::ResourceBundle::InitSharedInstanceWithLocale(
  43. locale, nullptr, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
  44. ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  45. bundle.ReloadLocaleResources(locale);
  46. // Load other resource files.
  47. base::FilePath pak_dir;
  48. #if defined(OS_MACOSX)
  49. pak_dir = base::mac::FrameworkBundlePath().Append("Resources");
  50. #else
  51. PathService::Get(base::DIR_MODULE, &pak_dir);
  52. #endif
  53. bundle.AddDataPackFromPath(
  54. pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak")),
  55. ui::GetSupportedScaleFactors()[0]);
  56. #if defined(ENABLE_PDF_VIEWER)
  57. bundle.AddDataPackFromPath(
  58. pak_dir.Append(FILE_PATH_LITERAL("pdf_viewer_resources.pak")),
  59. ui::GetSupportedScaleFactors()[0]);
  60. #endif // defined(ENABLE_PDF_VIEWER)
  61. bundle.AddDataPackFromPath(pak_dir.Append(FILE_PATH_LITERAL(
  62. "blink_image_resources_200_percent.pak")),
  63. ui::SCALE_FACTOR_200P);
  64. bundle.AddDataPackFromPath(
  65. pak_dir.Append(FILE_PATH_LITERAL("content_resources_200_percent.pak")),
  66. ui::SCALE_FACTOR_200P);
  67. bundle.AddDataPackFromPath(
  68. pak_dir.Append(FILE_PATH_LITERAL("ui_resources_200_percent.pak")),
  69. ui::SCALE_FACTOR_200P);
  70. bundle.AddDataPackFromPath(
  71. pak_dir.Append(FILE_PATH_LITERAL("views_resources_200_percent.pak")),
  72. ui::SCALE_FACTOR_200P);
  73. }
  74. MainDelegate::MainDelegate() {}
  75. MainDelegate::~MainDelegate() {}
  76. std::unique_ptr<ContentClient> MainDelegate::CreateContentClient() {
  77. return std::unique_ptr<ContentClient>(new ContentClient);
  78. }
  79. bool MainDelegate::BasicStartupComplete(int* exit_code) {
  80. content_client_ = CreateContentClient();
  81. SetContentClient(content_client_.get());
  82. #if defined(OS_MACOSX)
  83. OverrideChildProcessPath();
  84. OverrideFrameworkBundlePath();
  85. #endif
  86. return false;
  87. }
  88. void MainDelegate::PreSandboxStartup() {
  89. auto cmd = *base::CommandLine::ForCurrentProcess();
  90. std::string process_type = cmd.GetSwitchValueASCII(switches::kProcessType);
  91. // Initialize ResourceBundle which handles files loaded from external
  92. // sources. The language should have been passed in to us from the
  93. // browser process as a command line flag.
  94. if (SubprocessNeedsResourceBundle(process_type)) {
  95. std::string locale = cmd.GetSwitchValueASCII(switches::kLang);
  96. LoadResourceBundle(locale);
  97. }
  98. }
  99. content::ContentBrowserClient* MainDelegate::CreateContentBrowserClient() {
  100. browser_client_ = CreateBrowserClient();
  101. return browser_client_.get();
  102. }
  103. std::unique_ptr<BrowserClient> MainDelegate::CreateBrowserClient() {
  104. return std::unique_ptr<BrowserClient>(new BrowserClient);
  105. }
  106. } // namespace brightray