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.
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
#include "browser/browser_main_parts.h"
|
2013-03-13 19:12:05 +00:00
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
#include "browser/browser_context.h"
|
2015-03-09 02:53:37 +00:00
|
|
|
#include "browser/devtools_manager_delegate.h"
|
2013-05-14 06:45:34 +00:00
|
|
|
#include "browser/web_ui_controller_factory.h"
|
2015-01-10 01:14:52 +00:00
|
|
|
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/strings/string_number_conversions.h"
|
2015-07-23 07:04:47 +00:00
|
|
|
#include "components/devtools_http_handler/devtools_http_handler.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"
|
2013-12-05 10:56:28 +00:00
|
|
|
#include "net/proxy/proxy_resolver_v8.h"
|
2013-03-14 13:03:50 +00:00
|
|
|
|
2014-07-03 09:04:03 +00:00
|
|
|
#if defined(USE_AURA)
|
|
|
|
#include "ui/gfx/screen.h"
|
|
|
|
#include "ui/views/widget/desktop_aura/desktop_screen.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 08:44:58 +00:00
|
|
|
#if defined(TOOLKIT_VIEWS)
|
|
|
|
#include "browser/views/views_delegate.h"
|
|
|
|
#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/path_service.h"
|
|
|
|
#include "base/nix/xdg_util.h"
|
2015-10-04 07:40:51 +00:00
|
|
|
#include "base/thread_task_runner_handle.h"
|
2015-01-19 01:34:36 +00:00
|
|
|
#include "browser/brightray_paths.h"
|
2015-10-04 07:40:51 +00:00
|
|
|
#include "chrome/browser/ui/libgtk2ui/gtk2_ui.h"
|
|
|
|
#include "ui/base/x/x11_util.h"
|
|
|
|
#include "ui/base/x/x11_util_internal.h"
|
|
|
|
#include "ui/views/linux_ui/linux_ui.h"
|
|
|
|
#include "ui/wm/core/wm_state.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.h"
|
|
|
|
#include "ui/base/l10n/l10n_util_win.h"
|
|
|
|
#include "ui/gfx/platform_font_win.h"
|
|
|
|
#endif
|
|
|
|
|
2015-10-04 07:40:51 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
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;
|
|
|
|
scoped_ptr<base::Environment> env(base::Environment::Create());
|
|
|
|
path = base::nix::GetXDGDirectory(env.get(),
|
|
|
|
base::nix::kXdgConfigHomeEnvVar,
|
|
|
|
base::nix::kDotConfigDir);
|
|
|
|
PathService::Override(DIR_APP_DATA, path);
|
|
|
|
}
|
2015-10-04 07:40:51 +00:00
|
|
|
|
|
|
|
int BrowserX11ErrorHandler(Display* d, XErrorEvent* error) {
|
|
|
|
if (!g_in_x11_io_error_handler) {
|
|
|
|
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) {
|
|
|
|
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
|
|
|
|
// 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
|
|
|
|
// to see it in any crash dumps.
|
|
|
|
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
|
|
|
|
|
2015-01-19 01:34:36 +00:00
|
|
|
} // namespace
|
|
|
|
|
2013-03-13 19:12:05 +00:00
|
|
|
BrowserMainParts::BrowserMainParts() {
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserMainParts::~BrowserMainParts() {
|
|
|
|
}
|
|
|
|
|
2014-07-03 08:12:12 +00:00
|
|
|
void BrowserMainParts::PreEarlyInitialization() {
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
IncreaseFileDescriptorLimit();
|
|
|
|
#endif
|
2014-07-03 08:28:14 +00:00
|
|
|
|
2015-10-04 07:40:51 +00:00
|
|
|
#if defined(USE_X11)
|
2014-07-03 08:28:14 +00:00
|
|
|
views::LinuxUI::SetInstance(BuildGtk2UI());
|
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
|
2014-07-03 08:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserMainParts::ToolkitInitialized() {
|
2014-07-03 08:28:14 +00:00
|
|
|
#if defined(USE_AURA) && defined(USE_X11)
|
|
|
|
views::LinuxUI::instance()->Initialize();
|
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
|
|
|
|
|
|
|
#if defined(TOOLKIT_VIEWS)
|
2014-07-04 13:41:22 +00:00
|
|
|
views_delegate_.reset(new ViewsDelegate);
|
2014-07-03 08:44:58 +00:00
|
|
|
#endif
|
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
|
|
|
|
|
|
|
wchar_t module_name[MAX_PATH] = { 0 };
|
|
|
|
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() {
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
InitializeMainNib();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-03-13 19:12:05 +00:00
|
|
|
void BrowserMainParts::PreMainMessageLoopRun() {
|
2015-09-05 14:34:42 +00:00
|
|
|
browser_context_ = BrowserContext::From("", false);
|
2013-03-14 13:03:50 +00:00
|
|
|
|
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
|
2015-03-09 02:53:37 +00:00
|
|
|
auto command_line = base::CommandLine::ForCurrentProcess();
|
|
|
|
if (command_line->HasSwitch(switches::kRemoteDebuggingPort))
|
|
|
|
devtools_http_handler_.reset(DevToolsManagerDelegate::CreateHttpHandler());
|
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
|
|
|
|
}
|
|
|
|
|
2013-10-23 16:13:24 +00:00
|
|
|
void BrowserMainParts::PostMainMessageLoopRun() {
|
2015-09-05 12:03:18 +00:00
|
|
|
browser_context_ = nullptr;
|
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)
|
|
|
|
gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
|
|
|
|
views::CreateDesktopScreen());
|
|
|
|
#endif
|
|
|
|
|
2013-12-05 10:56:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-17 22:51:19 +00:00
|
|
|
} // namespace brightray
|