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