browser_main_parts.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/browser/browser_main_parts.h"
  5. #if defined(OS_POSIX)
  6. #include <stdlib.h>
  7. #endif
  8. #include <sys/stat.h>
  9. #include <string>
  10. #if defined(OS_LINUX)
  11. #include <glib.h> // for g_setenv()
  12. #endif
  13. #include "base/command_line.h"
  14. #include "base/feature_list.h"
  15. #include "base/message_loop/message_loop.h"
  16. #include "base/strings/string_number_conversions.h"
  17. #include "base/strings/utf_string_conversions.h"
  18. #include "brightray/browser/browser_client.h"
  19. #include "brightray/browser/browser_context.h"
  20. #include "brightray/browser/devtools_manager_delegate.h"
  21. #include "brightray/browser/media/media_capture_devices_dispatcher.h"
  22. #include "brightray/browser/web_ui_controller_factory.h"
  23. #include "brightray/common/application_info.h"
  24. #include "brightray/common/main_delegate.h"
  25. #include "content/public/browser/browser_thread.h"
  26. #include "content/public/common/content_switches.h"
  27. #include "media/base/localized_strings.h"
  28. #include "net/proxy/proxy_resolver_v8.h"
  29. #include "ui/base/l10n/l10n_util.h"
  30. #include "ui/base/material_design/material_design_controller.h"
  31. #include "ui/base/resource/resource_bundle.h"
  32. #include "ui/base/ui_base_switches.h"
  33. #if defined(USE_AURA)
  34. #include "ui/display/display.h"
  35. #include "ui/display/screen.h"
  36. #include "ui/views/widget/desktop_aura/desktop_screen.h"
  37. #include "ui/wm/core/wm_state.h"
  38. #endif
  39. #if defined(USE_X11)
  40. #include "base/environment.h"
  41. #include "base/nix/xdg_util.h"
  42. #include "base/path_service.h"
  43. #include "base/threading/thread_task_runner_handle.h"
  44. #include "brightray/browser/brightray_paths.h"
  45. #include "chrome/browser/ui/libgtkui/gtk_ui.h"
  46. #include "ui/base/x/x11_util.h"
  47. #include "ui/base/x/x11_util_internal.h"
  48. #include "ui/views/linux_ui/linux_ui.h"
  49. #endif
  50. #if defined(OS_WIN)
  51. #include "ui/base/cursor/cursor_loader_win.h"
  52. #include "ui/base/l10n/l10n_util_win.h"
  53. #include "ui/gfx/platform_font_win.h"
  54. #endif
  55. #if defined(OS_LINUX)
  56. #include "device/bluetooth/bluetooth_adapter_factory.h"
  57. #include "device/bluetooth/dbus/dbus_bluez_manager_wrapper_linux.h"
  58. #endif
  59. namespace brightray {
  60. namespace {
  61. #if defined(OS_WIN)
  62. // gfx::Font callbacks
  63. void AdjustUIFont(LOGFONT* logfont) {
  64. l10n_util::AdjustUIFont(logfont);
  65. }
  66. int GetMinimumFontSize() {
  67. return 10;
  68. }
  69. #endif
  70. #if defined(USE_X11)
  71. // Indicates that we're currently responding to an IO error (by shutting down).
  72. bool g_in_x11_io_error_handler = false;
  73. // Number of seconds to wait for UI thread to get an IO error if we get it on
  74. // the background thread.
  75. const int kWaitForUIThreadSeconds = 10;
  76. void OverrideLinuxAppDataPath() {
  77. base::FilePath path;
  78. if (PathService::Get(DIR_APP_DATA, &path))
  79. return;
  80. std::unique_ptr<base::Environment> env(base::Environment::Create());
  81. path = base::nix::GetXDGDirectory(env.get(), base::nix::kXdgConfigHomeEnvVar,
  82. base::nix::kDotConfigDir);
  83. PathService::Override(DIR_APP_DATA, path);
  84. }
  85. int BrowserX11ErrorHandler(Display* d, XErrorEvent* error) {
  86. if (!g_in_x11_io_error_handler && base::ThreadTaskRunnerHandle::IsSet()) {
  87. base::ThreadTaskRunnerHandle::Get()->PostTask(
  88. FROM_HERE, base::Bind(&ui::LogErrorEventDescription, d, *error));
  89. }
  90. return 0;
  91. }
  92. // This function is used to help us diagnose crash dumps that happen
  93. // during the shutdown process.
  94. NOINLINE void WaitingForUIThreadToHandleIOError() {
  95. // Ensure function isn't optimized away.
  96. asm("");
  97. sleep(kWaitForUIThreadSeconds);
  98. }
  99. int BrowserX11IOErrorHandler(Display* d) {
  100. if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
  101. // Wait for the UI thread (which has a different connection to the X server)
  102. // to get the error. We can't call shutdown from this thread without
  103. // tripping an error. Doing it through a function so that we'll be able
  104. // to see it in any crash dumps.
  105. WaitingForUIThreadToHandleIOError();
  106. return 0;
  107. }
  108. // If there's an IO error it likely means the X server has gone away.
  109. // If this CHECK fails, then that means SessionEnding() below triggered some
  110. // code that tried to talk to the X server, resulting in yet another error.
  111. CHECK(!g_in_x11_io_error_handler);
  112. g_in_x11_io_error_handler = true;
  113. LOG(ERROR) << "X IO error received (X server probably went away)";
  114. base::ThreadTaskRunnerHandle::Get()->PostTask(
  115. FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
  116. return 0;
  117. }
  118. int X11EmptyErrorHandler(Display* d, XErrorEvent* error) {
  119. return 0;
  120. }
  121. int X11EmptyIOErrorHandler(Display* d) {
  122. return 0;
  123. }
  124. #endif
  125. base::string16 MediaStringProvider(media::MessageId id) {
  126. switch (id) {
  127. case media::DEFAULT_AUDIO_DEVICE_NAME:
  128. return base::ASCIIToUTF16("Default");
  129. #if defined(OS_WIN)
  130. case media::COMMUNICATIONS_AUDIO_DEVICE_NAME:
  131. return base::ASCIIToUTF16("Communications");
  132. #endif
  133. default:
  134. return base::string16();
  135. }
  136. }
  137. } // namespace
  138. BrowserMainParts::BrowserMainParts() {}
  139. BrowserMainParts::~BrowserMainParts() {}
  140. #if defined(OS_WIN) || defined(OS_LINUX)
  141. void OverrideAppLogsPath() {
  142. base::FilePath path;
  143. if (PathService::Get(brightray::DIR_APP_DATA, &path)) {
  144. path = path.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
  145. path = path.Append(base::FilePath::FromUTF8Unsafe("logs"));
  146. PathService::Override(DIR_APP_LOGS, path);
  147. }
  148. }
  149. #endif
  150. void BrowserMainParts::PreEarlyInitialization() {
  151. std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
  152. // TODO(deepak1556): Disable guest webcontents based on OOPIF feature.
  153. feature_list->InitializeFromCommandLine("", "GuestViewCrossProcessFrames");
  154. base::FeatureList::SetInstance(std::move(feature_list));
  155. OverrideAppLogsPath();
  156. #if defined(USE_X11)
  157. views::LinuxUI::SetInstance(BuildGtkUi());
  158. OverrideLinuxAppDataPath();
  159. // Installs the X11 error handlers for the browser process used during
  160. // startup. They simply print error messages and exit because
  161. // we can't shutdown properly while creating and initializing services.
  162. ui::SetX11ErrorHandlers(nullptr, nullptr);
  163. #endif
  164. }
  165. void BrowserMainParts::ToolkitInitialized() {
  166. ui::MaterialDesignController::Initialize();
  167. #if defined(USE_AURA) && defined(USE_X11)
  168. views::LinuxUI::instance()->Initialize();
  169. #endif
  170. #if defined(USE_AURA)
  171. wm_state_.reset(new wm::WMState);
  172. #endif
  173. #if defined(OS_WIN)
  174. gfx::PlatformFontWin::adjust_font_callback = &AdjustUIFont;
  175. gfx::PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize;
  176. wchar_t module_name[MAX_PATH] = {0};
  177. if (GetModuleFileName(NULL, module_name, MAX_PATH))
  178. ui::CursorLoaderWin::SetCursorResourceModule(module_name);
  179. #endif
  180. }
  181. void BrowserMainParts::PreMainMessageLoopStart() {
  182. // Initialize ui::ResourceBundle.
  183. ui::ResourceBundle::InitSharedInstanceWithLocale(
  184. "", nullptr, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
  185. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  186. if (cmd_line->HasSwitch(switches::kLang)) {
  187. const std::string locale = cmd_line->GetSwitchValueASCII(switches::kLang);
  188. const base::FilePath locale_file_path =
  189. ui::ResourceBundle::GetSharedInstance().GetLocaleFilePath(locale, true);
  190. if (!locale_file_path.empty()) {
  191. custom_locale_ = locale;
  192. #if defined(OS_LINUX)
  193. /* When built with USE_GLIB, libcc's GetApplicationLocaleInternal() uses
  194. * glib's g_get_language_names(), which keys off of getenv("LC_ALL") */
  195. g_setenv("LC_ALL", custom_locale_.c_str(), TRUE);
  196. #endif
  197. }
  198. }
  199. #if defined(OS_MACOSX)
  200. if (custom_locale_.empty())
  201. l10n_util::OverrideLocaleWithCocoaLocale();
  202. #endif
  203. LoadResourceBundle(custom_locale_);
  204. #if defined(OS_MACOSX)
  205. InitializeMainNib();
  206. #endif
  207. media::SetLocalizedStringProvider(MediaStringProvider);
  208. }
  209. void BrowserMainParts::PreMainMessageLoopRun() {
  210. content::WebUIControllerFactory::RegisterFactory(
  211. WebUIControllerFactory::GetInstance());
  212. // --remote-debugging-port
  213. auto* command_line = base::CommandLine::ForCurrentProcess();
  214. if (command_line->HasSwitch(switches::kRemoteDebuggingPort))
  215. DevToolsManagerDelegate::StartHttpHandler();
  216. }
  217. void BrowserMainParts::PostMainMessageLoopStart() {
  218. #if defined(USE_X11)
  219. // Installs the X11 error handlers for the browser process after the
  220. // main message loop has started. This will allow us to exit cleanly
  221. // if X exits before us.
  222. ui::SetX11ErrorHandlers(BrowserX11ErrorHandler, BrowserX11IOErrorHandler);
  223. #endif
  224. #if defined(OS_LINUX)
  225. bluez::DBusBluezManagerWrapperLinux::Initialize();
  226. #endif
  227. }
  228. void BrowserMainParts::PostMainMessageLoopRun() {
  229. #if defined(USE_X11)
  230. // Unset the X11 error handlers. The X11 error handlers log the errors using a
  231. // |PostTask()| on the message-loop. But since the message-loop is in the
  232. // process of terminating, this can cause errors.
  233. ui::SetX11ErrorHandlers(X11EmptyErrorHandler, X11EmptyIOErrorHandler);
  234. #endif
  235. }
  236. int BrowserMainParts::PreCreateThreads() {
  237. #if defined(USE_AURA)
  238. display::Screen* screen = views::CreateDesktopScreen();
  239. display::Screen::SetScreenInstance(screen);
  240. #if defined(USE_X11)
  241. views::LinuxUI::instance()->UpdateDeviceScaleFactor();
  242. #endif
  243. #endif
  244. // Force MediaCaptureDevicesDispatcher to be created on UI thread.
  245. MediaCaptureDevicesDispatcher::GetInstance();
  246. if (!views::LayoutProvider::Get())
  247. layout_provider_.reset(new views::LayoutProvider());
  248. // Initialize the app locale.
  249. BrowserClient::SetApplicationLocale(
  250. l10n_util::GetApplicationLocale(custom_locale_));
  251. // Manage global state of net and other IO thread related.
  252. io_thread_ = std::make_unique<IOThread>();
  253. return 0;
  254. }
  255. void BrowserMainParts::PostDestroyThreads() {
  256. #if defined(OS_LINUX)
  257. device::BluetoothAdapterFactory::Shutdown();
  258. bluez::DBusBluezManagerWrapperLinux::Shutdown();
  259. #endif
  260. io_thread_.reset();
  261. }
  262. } // namespace brightray