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 file.
5 #include "chrome/browser/chrome_browser_main_extra_parts_x11.h"
8 #include "base/debug/debugger.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/browser_shutdown.h"
11 #include "chrome/browser/lifetime/application_lifetime.h"
12 #include "chrome/common/chrome_result_codes.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "ui/base/x/x11_util.h"
15 #include "ui/base/x/x11_util_internal.h"
17 using content::BrowserThread
;
21 // Indicates that we're currently responding to an IO error (by shutting down).
22 bool g_in_x11_io_error_handler
= false;
24 // Number of seconds to wait for UI thread to get an IO error if we get it on
25 // the background thread.
26 const int kWaitForUIThreadSeconds
= 10;
28 int BrowserX11ErrorHandler(Display
* d
, XErrorEvent
* error
) {
29 if (!g_in_x11_io_error_handler
)
30 base::MessageLoop::current()->PostTask(
32 base::Bind(&ui::LogErrorEventDescription
, d
, *error
));
37 // This function is used to help us diagnose crash dumps that happen
38 // during the shutdown process.
39 NOINLINE
void WaitingForUIThreadToHandleIOError() {
40 // Ensure function isn't optimized away.
42 sleep(kWaitForUIThreadSeconds
);
45 int BrowserX11IOErrorHandler(Display
* d
) {
46 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
47 // Wait for the UI thread (which has a different connection to the X server)
48 // to get the error. We can't call shutdown from this thread without
49 // tripping an error. Doing it through a function so that we'll be able
50 // to see it in any crash dumps.
51 WaitingForUIThreadToHandleIOError();
54 // If there's an IO error it likely means the X server has gone away
55 if (!g_in_x11_io_error_handler
) {
56 g_in_x11_io_error_handler
= true;
57 LOG(ERROR
) << "X IO error received (X server probably went away)";
58 browser_shutdown::SetShuttingDownWithoutClosingBrowsers(true);
59 chrome::SessionEnding();
65 int X11EmptyErrorHandler(Display
* d
, XErrorEvent
* error
) {
69 int X11EmptyIOErrorHandler(Display
* d
) {
75 ChromeBrowserMainExtraPartsX11::ChromeBrowserMainExtraPartsX11() {
78 ChromeBrowserMainExtraPartsX11::~ChromeBrowserMainExtraPartsX11() {
81 void ChromeBrowserMainExtraPartsX11::PreEarlyInitialization() {
82 // Installs the X11 error handlers for the browser process used during
83 // startup. They simply print error messages and exit because
84 // we can't shutdown properly while creating and initializing services.
85 ui::SetX11ErrorHandlers(NULL
, NULL
);
88 void ChromeBrowserMainExtraPartsX11::PostMainMessageLoopStart() {
89 // Installs the X11 error handlers for the browser process after the
90 // main message loop has started. This will allow us to exit cleanly
91 // if X exits before us.
92 ui::SetX11ErrorHandlers(BrowserX11ErrorHandler
, BrowserX11IOErrorHandler
);
95 void ChromeBrowserMainExtraPartsX11::PostMainMessageLoopRun() {
96 // Unset the X11 error handlers. The X11 error handlers log the errors using a
97 // |PostTask()| on the message-loop. But since the message-loop is in the
98 // process of terminating, this can cause errors.
99 ui::SetX11ErrorHandlers(X11EmptyErrorHandler
, X11EmptyIOErrorHandler
);