[email protected] | 6653c19 | 2012-04-10 22:52:44 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 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.h" | ||||
6 | |||||
[email protected] | 401abeb6 | 2011-10-26 22:03:44 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 8 | #include "base/debug/debugger.h" |
[email protected] | b8ddb05 | 2012-04-19 02:36:06 | [diff] [blame] | 9 | #include "base/message_loop.h" |
[email protected] | 2b89cb91 | 2011-10-24 22:16:31 | [diff] [blame] | 10 | #include "chrome/browser/browser_shutdown.h" |
[email protected] | 2e6389f | 2012-05-18 19:41:25 | [diff] [blame] | 11 | #include "chrome/browser/lifetime/application_lifetime.h" |
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 12 | #include "chrome/browser/metrics/metrics_service.h" |
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 13 | #include "chrome/common/chrome_result_codes.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 15 | #include "ui/base/x/x11_util.h" |
16 | #include "ui/base/x/x11_util_internal.h" | ||||
17 | |||||
18 | #if defined(USE_LINUX_BREAKPAD) | ||||
[email protected] | 1602cde | 2012-06-26 15:09:12 | [diff] [blame] | 19 | #include "chrome/app/breakpad_linux.h" |
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 20 | #endif |
21 | |||||
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 22 | using content::BrowserThread; |
23 | |||||
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 24 | namespace { |
25 | |||||
26 | // Indicates that we're currently responding to an IO error (by shutting down). | ||||
27 | bool g_in_x11_io_error_handler = false; | ||||
28 | |||||
29 | // Number of seconds to wait for UI thread to get an IO error if we get it on | ||||
30 | // the background thread. | ||||
31 | const int kWaitForUIThreadSeconds = 10; | ||||
32 | |||||
33 | int BrowserX11ErrorHandler(Display* d, XErrorEvent* error) { | ||||
34 | if (!g_in_x11_io_error_handler) | ||||
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 35 | base::MessageLoop::current()->PostTask( |
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 36 | FROM_HERE, |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 37 | base::Bind(&ui::LogErrorEventDescription, d, *error)); |
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 38 | return 0; |
39 | } | ||||
40 | |||||
41 | |||||
42 | // This function is used to help us diagnose crash dumps that happen | ||||
43 | // during the shutdown process. | ||||
44 | NOINLINE void WaitingForUIThreadToHandleIOError() { | ||||
45 | // Ensure function isn't optimized away. | ||||
46 | asm(""); | ||||
47 | sleep(kWaitForUIThreadSeconds); | ||||
48 | } | ||||
49 | |||||
50 | int BrowserX11IOErrorHandler(Display* d) { | ||||
51 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | ||||
52 | // Wait for the UI thread (which has a different connection to the X server) | ||||
53 | // to get the error. We can't call shutdown from this thread without | ||||
54 | // tripping an error. Doing it through a function so that we'll be able | ||||
55 | // to see it in any crash dumps. | ||||
56 | WaitingForUIThreadToHandleIOError(); | ||||
57 | return 0; | ||||
58 | } | ||||
59 | // If there's an IO error it likely means the X server has gone away | ||||
60 | if (!g_in_x11_io_error_handler) { | ||||
61 | g_in_x11_io_error_handler = true; | ||||
[email protected] | bb8ecee | 2012-10-10 19:14:47 | [diff] [blame] | 62 | LOG(ERROR) << "X IO error received (X server probably went away)"; |
[email protected] | 2b89cb91 | 2011-10-24 22:16:31 | [diff] [blame] | 63 | browser_shutdown::SetShuttingDownWithoutClosingBrowsers(true); |
[email protected] | 0c98ab65 | 2013-02-18 00:39:37 | [diff] [blame] | 64 | chrome::SessionEnding(); |
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 65 | } |
66 | |||||
67 | return 0; | ||||
68 | } | ||||
69 | |||||
[email protected] | 1dd7a8e | 2012-09-16 18:49:29 | [diff] [blame] | 70 | int X11EmptyErrorHandler(Display* d, XErrorEvent* error) { |
71 | return 0; | ||||
72 | } | ||||
73 | |||||
74 | int X11EmptyIOErrorHandler(Display* d) { | ||||
75 | return 0; | ||||
76 | } | ||||
77 | |||||
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 78 | } // namespace |
79 | |||||
80 | void RecordBreakpadStatusUMA(MetricsService* metrics) { | ||||
81 | #if defined(USE_LINUX_BREAKPAD) | ||||
82 | metrics->RecordBreakpadRegistration(IsCrashReporterEnabled()); | ||||
83 | #else | ||||
84 | metrics->RecordBreakpadRegistration(false); | ||||
85 | #endif | ||||
86 | metrics->RecordBreakpadHasDebugger(base::debug::BeingDebugged()); | ||||
87 | } | ||||
88 | |||||
89 | void WarnAboutMinimumSystemRequirements() { | ||||
90 | // Nothing to warn about on X11 right now. | ||||
91 | } | ||||
92 | |||||
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 93 | // From browser_main_win.h, stubs until we figure out the right thing... |
94 | |||||
95 | int DoUninstallTasks(bool chrome_still_running) { | ||||
96 | return content::RESULT_CODE_NORMAL_EXIT; | ||||
97 | } | ||||
98 | |||||
[email protected] | 17d4f97 | 2013-06-15 05:03:01 | [diff] [blame] | 99 | void SetBrowserX11ErrorHandlersPreEarlyInitialization() { |
100 | // Use default error handlers during startup. | ||||
101 | ui::SetX11ErrorHandlers(NULL, NULL); | ||||
102 | } | ||||
103 | |||||
104 | void SetBrowserX11ErrorHandlersPostMainMessageLoopStart() { | ||||
[email protected] | 378e1fa | 2011-10-06 02:37:28 | [diff] [blame] | 105 | // Set up error handlers to make sure profile gets written if X server |
106 | // goes away. | ||||
107 | ui::SetX11ErrorHandlers(BrowserX11ErrorHandler, BrowserX11IOErrorHandler); | ||||
108 | } | ||||
[email protected] | 1dd7a8e | 2012-09-16 18:49:29 | [diff] [blame] | 109 | |
110 | void UnsetBrowserX11ErrorHandlers() { | ||||
111 | ui::SetX11ErrorHandlers(X11EmptyErrorHandler, X11EmptyIOErrorHandler); | ||||
112 | } |