2013-03-13 19:12:05 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE-CHROMIUM file.
|
|
|
|
|
2017-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/browser_main_parts.h"
|
2013-03-13 19:12:05 +00:00
|
|
|
|
2018-02-08 14:26:37 +00:00
|
|
|
#if defined(OS_POSIX)
|
2017-08-09 15:55:46 +00:00
|
|
|
#include <stdlib.h>
|
2017-09-29 13:32:30 +00:00
|
|
|
#endif
|
|
|
|
|
2017-08-09 17:33:27 +00:00
|
|
|
#include <sys/stat.h>
|
2017-08-14 15:47:37 +00:00
|
|
|
#include <string>
|
2017-08-08 17:17:03 +00:00
|
|
|
|
2018-02-08 14:26:37 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
#include <glib.h> // for g_setenv()
|
|
|
|
#endif
|
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
#include "base/command_line.h"
|
2016-05-23 03:34:09 +00:00
|
|
|
#include "base/feature_list.h"
|
2017-08-07 22:38:17 +00:00
|
|
|
#include "base/message_loop/message_loop.h"
|
2015-01-10 01:14:52 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2016-04-07 10:55:30 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2017-12-16 09:21:29 +00:00
|
|
|
#include "brightray/browser/browser_client.h"
|
2017-05-18 22:59:41 +00:00
|
|
|
#include "brightray/browser/browser_context.h"
|
|
|
|
#include "brightray/browser/devtools_manager_delegate.h"
|
2017-12-07 04:15:17 +00:00
|
|
|
#include "brightray/browser/media/media_capture_devices_dispatcher.h"
|
2017-05-18 22:59:41 +00:00
|
|
|
#include "brightray/browser/web_ui_controller_factory.h"
|
2017-08-07 20:54:15 +00:00
|
|
|
#include "brightray/common/application_info.h"
|
2017-05-18 22:59:41 +00:00
|
|
|
#include "brightray/common/main_delegate.h"
|
2015-10-04 07:40:51 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
#include "content/public/common/content_switches.h"
|
2018-04-05 21:09:37 +00:00
|
|
|
#include "content/public/common/result_codes.h"
|
2017-04-04 04:43:49 +00:00
|
|
|
#include "media/base/localized_strings.h"
|
2018-04-04 12:58:09 +00:00
|
|
|
#include "net/proxy_resolution/proxy_resolver_v8.h"
|
2016-01-08 04:04:23 +00:00
|
|
|
#include "ui/base/l10n/l10n_util.h"
|
2016-07-05 00:41:05 +00:00
|
|
|
#include "ui/base/material_design/material_design_controller.h"
|
2018-02-08 14:26:37 +00:00
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
|
|
|
#include "ui/base/ui_base_switches.h"
|
2013-03-14 13:03:50 +00:00
|
|
|
|
2014-07-03 09:04:03 +00:00
|
|
|
#if defined(USE_AURA)
|
2016-07-04 07:40:46 +00:00
|
|
|
#include "ui/display/display.h"
|
2016-07-04 06:06:05 +00:00
|
|
|
#include "ui/display/screen.h"
|
2014-07-03 09:04:03 +00:00
|
|
|
#include "ui/views/widget/desktop_aura/desktop_screen.h"
|
2017-04-16 14:27:36 +00:00
|
|
|
#include "ui/wm/core/wm_state.h"
|
2014-07-03 09:04:03 +00:00
|
|
|
#endif
|
|
|
|
|
2015-10-04 07:40:51 +00:00
|
|
|
#if defined(USE_X11)
|
2015-01-19 01:34:36 +00:00
|
|
|
#include "base/environment.h"
|
|
|
|
#include "base/nix/xdg_util.h"
|
2017-05-18 22:29:22 +00:00
|
|
|
#include "base/path_service.h"
|
2016-07-04 07:40:46 +00:00
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2017-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/brightray_paths.h"
|
2017-01-26 10:54:24 +00:00
|
|
|
#include "chrome/browser/ui/libgtkui/gtk_ui.h"
|
2015-10-04 07:40:51 +00:00
|
|
|
#include "ui/base/x/x11_util.h"
|
|
|
|
#include "ui/base/x/x11_util_internal.h"
|
|
|
|
#include "ui/views/linux_ui/linux_ui.h"
|
2015-01-19 01:34:36 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-12 11:46:26 +00:00
|
|
|
#if defined(OS_WIN)
|
2015-07-06 05:48:31 +00:00
|
|
|
#include "ui/base/cursor/cursor_loader_win.h"
|
2014-07-12 11:46:26 +00:00
|
|
|
#include "ui/base/l10n/l10n_util_win.h"
|
|
|
|
#include "ui/gfx/platform_font_win.h"
|
|
|
|
#endif
|
|
|
|
|
2016-05-31 07:06:51 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
#include "device/bluetooth/bluetooth_adapter_factory.h"
|
|
|
|
#include "device/bluetooth/dbus/dbus_bluez_manager_wrapper_linux.h"
|
|
|
|
#endif
|
|
|
|
|
2013-03-13 19:12:05 +00:00
|
|
|
namespace brightray {
|
|
|
|
|
2014-07-12 11:46:26 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-01-19 01:34:36 +00:00
|
|
|
#if defined(OS_WIN)
|
2014-07-12 11:46:26 +00:00
|
|
|
// gfx::Font callbacks
|
|
|
|
void AdjustUIFont(LOGFONT* logfont) {
|
|
|
|
l10n_util::AdjustUIFont(logfont);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetMinimumFontSize() {
|
|
|
|
return 10;
|
|
|
|
}
|
2015-01-19 01:34:36 +00:00
|
|
|
#endif
|
2014-07-12 11:46:26 +00:00
|
|
|
|
2015-10-04 07:40:51 +00:00
|
|
|
#if defined(USE_X11)
|
|
|
|
// Indicates that we're currently responding to an IO error (by shutting down).
|
|
|
|
bool g_in_x11_io_error_handler = false;
|
|
|
|
|
|
|
|
// Number of seconds to wait for UI thread to get an IO error if we get it on
|
|
|
|
// the background thread.
|
|
|
|
const int kWaitForUIThreadSeconds = 10;
|
|
|
|
|
2015-01-19 01:34:36 +00:00
|
|
|
void OverrideLinuxAppDataPath() {
|
|
|
|
base::FilePath path;
|
|
|
|
if (PathService::Get(DIR_APP_DATA, &path))
|
|
|
|
return;
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<base::Environment> env(base::Environment::Create());
|
2018-04-18 01:56:12 +00:00
|
|
|
path = base::nix::GetXDGDirectory(env.get(), base::nix::kXdgConfigHomeEnvVar,
|
2015-01-19 01:34:36 +00:00
|
|
|
base::nix::kDotConfigDir);
|
|
|
|
PathService::Override(DIR_APP_DATA, path);
|
|
|
|
}
|
2015-10-04 07:40:51 +00:00
|
|
|
|
|
|
|
int BrowserX11ErrorHandler(Display* d, XErrorEvent* error) {
|
2018-01-31 07:37:32 +00:00
|
|
|
if (!g_in_x11_io_error_handler && base::ThreadTaskRunnerHandle::IsSet()) {
|
2015-10-04 07:40:51 +00:00
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostTask(
|
|
|
|
FROM_HERE, base::Bind(&ui::LogErrorEventDescription, d, *error));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is used to help us diagnose crash dumps that happen
|
|
|
|
// during the shutdown process.
|
|
|
|
NOINLINE void WaitingForUIThreadToHandleIOError() {
|
|
|
|
// Ensure function isn't optimized away.
|
|
|
|
asm("");
|
|
|
|
sleep(kWaitForUIThreadSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
int BrowserX11IOErrorHandler(Display* d) {
|
2017-03-30 19:44:15 +00:00
|
|
|
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
2015-10-04 07:40:51 +00:00
|
|
|
// Wait for the UI thread (which has a different connection to the X server)
|
|
|
|
// to get the error. We can't call shutdown from this thread without
|
|
|
|
// tripping an error. Doing it through a function so that we'll be able
|
2017-09-24 01:44:08 +00:00
|
|
|
// to see it in any crash dumps.
|
2015-10-04 07:40:51 +00:00
|
|
|
WaitingForUIThreadToHandleIOError();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's an IO error it likely means the X server has gone away.
|
|
|
|
// If this CHECK fails, then that means SessionEnding() below triggered some
|
|
|
|
// code that tried to talk to the X server, resulting in yet another error.
|
|
|
|
CHECK(!g_in_x11_io_error_handler);
|
|
|
|
|
|
|
|
g_in_x11_io_error_handler = true;
|
|
|
|
LOG(ERROR) << "X IO error received (X server probably went away)";
|
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostTask(
|
|
|
|
FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int X11EmptyErrorHandler(Display* d, XErrorEvent* error) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int X11EmptyIOErrorHandler(Display* d) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-12 11:46:26 +00:00
|
|
|
#endif
|
|
|
|
|
2016-04-07 10:55:30 +00:00
|
|
|
base::string16 MediaStringProvider(media::MessageId id) {
|
|
|
|
switch (id) {
|
|
|
|
case media::DEFAULT_AUDIO_DEVICE_NAME:
|
|
|
|
return base::ASCIIToUTF16("Default");
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
case media::COMMUNICATIONS_AUDIO_DEVICE_NAME:
|
|
|
|
return base::ASCIIToUTF16("Communications");
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return base::string16();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 01:34:36 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-04-18 01:56:12 +00:00
|
|
|
BrowserMainParts::BrowserMainParts() {}
|
2013-03-13 19:12:05 +00:00
|
|
|
|
2018-04-18 01:56:12 +00:00
|
|
|
BrowserMainParts::~BrowserMainParts() {}
|
2013-03-13 19:12:05 +00:00
|
|
|
|
2017-09-29 13:32:30 +00:00
|
|
|
#if defined(OS_WIN) || defined(OS_LINUX)
|
|
|
|
void OverrideAppLogsPath() {
|
2017-10-26 04:18:55 +00:00
|
|
|
base::FilePath path;
|
2017-10-26 04:21:52 +00:00
|
|
|
if (PathService::Get(brightray::DIR_APP_DATA, &path)) {
|
2017-10-26 04:27:27 +00:00
|
|
|
path = path.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
|
|
|
|
path = path.Append(base::FilePath::FromUTF8Unsafe("logs"));
|
|
|
|
PathService::Override(DIR_APP_LOGS, path);
|
2017-10-26 04:18:55 +00:00
|
|
|
}
|
2017-09-29 03:03:01 +00:00
|
|
|
}
|
2017-09-29 13:32:30 +00:00
|
|
|
#endif
|
2017-09-29 03:03:01 +00:00
|
|
|
|
2018-04-05 21:09:37 +00:00
|
|
|
int BrowserMainParts::PreEarlyInitialization() {
|
2016-05-23 03:34:09 +00:00
|
|
|
std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
|
2018-01-02 06:40:25 +00:00
|
|
|
// TODO(deepak1556): Disable guest webcontents based on OOPIF feature.
|
|
|
|
feature_list->InitializeFromCommandLine("", "GuestViewCrossProcessFrames");
|
2016-05-23 03:34:09 +00:00
|
|
|
base::FeatureList::SetInstance(std::move(feature_list));
|
2017-09-29 13:32:30 +00:00
|
|
|
OverrideAppLogsPath();
|
2015-10-04 07:40:51 +00:00
|
|
|
#if defined(USE_X11)
|
2017-04-17 07:52:24 +00:00
|
|
|
views::LinuxUI::SetInstance(BuildGtkUi());
|
2015-01-19 01:34:36 +00:00
|
|
|
OverrideLinuxAppDataPath();
|
2015-10-04 07:40:51 +00:00
|
|
|
|
|
|
|
// Installs the X11 error handlers for the browser process used during
|
|
|
|
// startup. They simply print error messages and exit because
|
|
|
|
// we can't shutdown properly while creating and initializing services.
|
|
|
|
ui::SetX11ErrorHandlers(nullptr, nullptr);
|
2015-01-19 01:34:36 +00:00
|
|
|
#endif
|
2018-04-05 21:09:37 +00:00
|
|
|
|
|
|
|
return content::RESULT_CODE_NORMAL_EXIT;
|
2014-07-03 08:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserMainParts::ToolkitInitialized() {
|
2016-07-05 00:41:05 +00:00
|
|
|
ui::MaterialDesignController::Initialize();
|
|
|
|
|
2014-07-03 08:28:14 +00:00
|
|
|
#if defined(USE_AURA) && defined(USE_X11)
|
|
|
|
views::LinuxUI::instance()->Initialize();
|
2017-04-16 14:27:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_AURA)
|
2014-07-09 12:05:02 +00:00
|
|
|
wm_state_.reset(new wm::WMState);
|
2014-07-03 08:28:14 +00:00
|
|
|
#endif
|
2014-07-03 08:44:58 +00:00
|
|
|
|
2014-07-12 11:46:26 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
gfx::PlatformFontWin::adjust_font_callback = &AdjustUIFont;
|
|
|
|
gfx::PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize;
|
2015-07-06 05:48:31 +00:00
|
|
|
|
2018-04-18 01:56:12 +00:00
|
|
|
wchar_t module_name[MAX_PATH] = {0};
|
2015-07-06 05:48:31 +00:00
|
|
|
if (GetModuleFileName(NULL, module_name, MAX_PATH))
|
|
|
|
ui::CursorLoaderWin::SetCursorResourceModule(module_name);
|
2014-07-12 11:46:26 +00:00
|
|
|
#endif
|
2014-07-03 08:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserMainParts::PreMainMessageLoopStart() {
|
2018-02-08 14:26:37 +00:00
|
|
|
// Initialize ui::ResourceBundle.
|
|
|
|
ui::ResourceBundle::InitSharedInstanceWithLocale(
|
|
|
|
"", nullptr, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* cmd_line = base::CommandLine::ForCurrentProcess();
|
2018-02-08 14:26:37 +00:00
|
|
|
if (cmd_line->HasSwitch(switches::kLang)) {
|
|
|
|
const std::string locale = cmd_line->GetSwitchValueASCII(switches::kLang);
|
|
|
|
const base::FilePath locale_file_path =
|
|
|
|
ui::ResourceBundle::GetSharedInstance().GetLocaleFilePath(locale, true);
|
|
|
|
if (!locale_file_path.empty()) {
|
|
|
|
custom_locale_ = locale;
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
/* When built with USE_GLIB, libcc's GetApplicationLocaleInternal() uses
|
|
|
|
* glib's g_get_language_names(), which keys off of getenv("LC_ALL") */
|
|
|
|
g_setenv("LC_ALL", custom_locale_.c_str(), TRUE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:04:23 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2018-02-08 14:26:37 +00:00
|
|
|
if (custom_locale_.empty())
|
|
|
|
l10n_util::OverrideLocaleWithCocoaLocale();
|
2016-01-08 04:04:23 +00:00
|
|
|
#endif
|
2018-02-08 14:26:37 +00:00
|
|
|
LoadResourceBundle(custom_locale_);
|
2014-07-03 08:12:12 +00:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
InitializeMainNib();
|
|
|
|
#endif
|
2016-04-07 10:55:30 +00:00
|
|
|
media::SetLocalizedStringProvider(MediaStringProvider);
|
2014-07-03 08:12:12 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 19:12:05 +00:00
|
|
|
void BrowserMainParts::PreMainMessageLoopRun() {
|
2013-11-17 22:51:19 +00:00
|
|
|
content::WebUIControllerFactory::RegisterFactory(
|
2015-08-27 19:54:05 +00:00
|
|
|
WebUIControllerFactory::GetInstance());
|
2015-01-10 01:14:52 +00:00
|
|
|
|
|
|
|
// --remote-debugging-port
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* command_line = base::CommandLine::ForCurrentProcess();
|
2015-03-09 02:53:37 +00:00
|
|
|
if (command_line->HasSwitch(switches::kRemoteDebuggingPort))
|
2017-01-23 06:07:18 +00:00
|
|
|
DevToolsManagerDelegate::StartHttpHandler();
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 07:40:51 +00:00
|
|
|
void BrowserMainParts::PostMainMessageLoopStart() {
|
|
|
|
#if defined(USE_X11)
|
|
|
|
// Installs the X11 error handlers for the browser process after the
|
|
|
|
// main message loop has started. This will allow us to exit cleanly
|
|
|
|
// if X exits before us.
|
|
|
|
ui::SetX11ErrorHandlers(BrowserX11ErrorHandler, BrowserX11IOErrorHandler);
|
|
|
|
#endif
|
2016-05-31 07:06:51 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
bluez::DBusBluezManagerWrapperLinux::Initialize();
|
|
|
|
#endif
|
2015-10-04 07:40:51 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 16:13:24 +00:00
|
|
|
void BrowserMainParts::PostMainMessageLoopRun() {
|
2015-10-04 07:40:51 +00:00
|
|
|
#if defined(USE_X11)
|
|
|
|
// Unset the X11 error handlers. The X11 error handlers log the errors using a
|
|
|
|
// |PostTask()| on the message-loop. But since the message-loop is in the
|
|
|
|
// process of terminating, this can cause errors.
|
|
|
|
ui::SetX11ErrorHandlers(X11EmptyErrorHandler, X11EmptyIOErrorHandler);
|
|
|
|
#endif
|
2013-10-23 16:13:24 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 10:56:28 +00:00
|
|
|
int BrowserMainParts::PreCreateThreads() {
|
2014-07-03 09:04:03 +00:00
|
|
|
#if defined(USE_AURA)
|
2016-07-04 06:06:05 +00:00
|
|
|
display::Screen* screen = views::CreateDesktopScreen();
|
|
|
|
display::Screen::SetScreenInstance(screen);
|
2015-10-04 08:52:14 +00:00
|
|
|
#if defined(USE_X11)
|
2017-01-26 10:54:24 +00:00
|
|
|
views::LinuxUI::instance()->UpdateDeviceScaleFactor();
|
2015-10-04 08:52:14 +00:00
|
|
|
#endif
|
2014-07-03 09:04:03 +00:00
|
|
|
#endif
|
2017-07-03 16:23:47 +00:00
|
|
|
|
2017-12-07 04:15:17 +00:00
|
|
|
// Force MediaCaptureDevicesDispatcher to be created on UI thread.
|
|
|
|
MediaCaptureDevicesDispatcher::GetInstance();
|
|
|
|
|
2017-07-03 16:23:47 +00:00
|
|
|
if (!views::LayoutProvider::Get())
|
|
|
|
layout_provider_.reset(new views::LayoutProvider());
|
|
|
|
|
2017-12-16 09:21:29 +00:00
|
|
|
// Initialize the app locale.
|
2018-02-08 14:26:37 +00:00
|
|
|
BrowserClient::SetApplicationLocale(
|
2018-04-18 01:56:12 +00:00
|
|
|
l10n_util::GetApplicationLocale(custom_locale_));
|
2017-12-16 09:21:29 +00:00
|
|
|
|
2017-12-18 11:57:20 +00:00
|
|
|
// Manage global state of net and other IO thread related.
|
2018-04-12 12:48:32 +00:00
|
|
|
io_thread_ = std::make_unique<IOThread>();
|
2017-12-18 11:57:20 +00:00
|
|
|
|
2013-12-05 10:56:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-31 07:06:51 +00:00
|
|
|
void BrowserMainParts::PostDestroyThreads() {
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
device::BluetoothAdapterFactory::Shutdown();
|
|
|
|
bluez::DBusBluezManagerWrapperLinux::Shutdown();
|
|
|
|
#endif
|
2017-12-18 11:57:20 +00:00
|
|
|
|
|
|
|
io_thread_.reset();
|
2016-05-31 07:06:51 +00:00
|
|
|
}
|
|
|
|
|
2013-11-17 22:51:19 +00:00
|
|
|
} // namespace brightray
|