Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / chrome_browser_main_extra_parts_x11.cc
blob3a068fb882e700062cd74ef1b5ae9fd39e8afd35
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"
7 #include "base/bind.h"
8 #include "base/debug/debugger.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/lifetime/application_lifetime.h"
13 #include "chrome/common/chrome_result_codes.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "ui/base/x/x11_util.h"
16 #include "ui/base/x/x11_util_internal.h"
18 using content::BrowserThread;
20 namespace {
22 // Indicates that we're currently responding to an IO error (by shutting down).
23 bool g_in_x11_io_error_handler = false;
25 // Number of seconds to wait for UI thread to get an IO error if we get it on
26 // the background thread.
27 const int kWaitForUIThreadSeconds = 10;
29 int BrowserX11ErrorHandler(Display* d, XErrorEvent* error) {
30 if (!g_in_x11_io_error_handler) {
31 base::ThreadTaskRunnerHandle::Get()->PostTask(
32 FROM_HERE, base::Bind(&ui::LogErrorEventDescription, d, *error));
34 return 0;
38 // This function is used to help us diagnose crash dumps that happen
39 // during the shutdown process.
40 NOINLINE void WaitingForUIThreadToHandleIOError() {
41 // Ensure function isn't optimized away.
42 asm("");
43 sleep(kWaitForUIThreadSeconds);
46 int BrowserX11IOErrorHandler(Display* d) {
47 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
48 // Wait for the UI thread (which has a different connection to the X server)
49 // to get the error. We can't call shutdown from this thread without
50 // tripping an error. Doing it through a function so that we'll be able
51 // to see it in any crash dumps.
52 WaitingForUIThreadToHandleIOError();
53 return 0;
56 // If there's an IO error it likely means the X server has gone away.
57 // If this CHECK fails, then that means SessionEnding() below triggered some
58 // code that tried to talk to the X server, resulting in yet another error.
59 CHECK(!g_in_x11_io_error_handler);
61 g_in_x11_io_error_handler = true;
62 LOG(ERROR) << "X IO error received (X server probably went away)";
63 chrome::SessionEnding();
65 return 0;
68 int X11EmptyErrorHandler(Display* d, XErrorEvent* error) {
69 return 0;
72 int X11EmptyIOErrorHandler(Display* d) {
73 return 0;
76 } // namespace
78 ChromeBrowserMainExtraPartsX11::ChromeBrowserMainExtraPartsX11() {
81 ChromeBrowserMainExtraPartsX11::~ChromeBrowserMainExtraPartsX11() {
84 void ChromeBrowserMainExtraPartsX11::PreEarlyInitialization() {
85 // Installs the X11 error handlers for the browser process used during
86 // startup. They simply print error messages and exit because
87 // we can't shutdown properly while creating and initializing services.
88 ui::SetX11ErrorHandlers(NULL, NULL);
91 void ChromeBrowserMainExtraPartsX11::PostMainMessageLoopStart() {
92 // Installs the X11 error handlers for the browser process after the
93 // main message loop has started. This will allow us to exit cleanly
94 // if X exits before us.
95 ui::SetX11ErrorHandlers(BrowserX11ErrorHandler, BrowserX11IOErrorHandler);
98 void ChromeBrowserMainExtraPartsX11::PostMainMessageLoopRun() {
99 // Unset the X11 error handlers. The X11 error handlers log the errors using a
100 // |PostTask()| on the message-loop. But since the message-loop is in the
101 // process of terminating, this can cause errors.
102 ui::SetX11ErrorHandlers(X11EmptyErrorHandler, X11EmptyIOErrorHandler);