Print Preview: Changing displayed error message when PDF Viewer is missing.
[chromium-blink-merge.git] / chrome / browser / browser_main_gtk.cc
blob3701a57f3111f92662dc566b84ca3ee48e4a5b6d
1 // Copyright (c) 2011 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/browser_main_gtk.h"
7 #include <gtk/gtk.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #include <unistd.h>
12 #include "base/command_line.h"
13 #include "base/debug/debugger.h"
14 #include "chrome/browser/browser_main_win.h"
15 #include "chrome/browser/metrics/metrics_service.h"
16 #include "chrome/browser/ui/browser_list.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "content/browser/renderer_host/render_sandbox_host_linux.h"
19 #include "content/browser/zygote_host_linux.h"
20 #include "content/common/result_codes.h"
21 #include "grit/chromium_strings.h"
22 #include "grit/generated_resources.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/base/x/x11_util.h"
26 #include "ui/base/x/x11_util_internal.h"
27 #include "ui/gfx/gtk_util.h"
29 #if defined(USE_NSS)
30 #include "crypto/nss_util.h"
31 #endif
33 #if defined(USE_LINUX_BREAKPAD)
34 #include "chrome/app/breakpad_linux.h"
35 #endif
37 namespace {
39 // Indicates that we're currently responding to an IO error (by shutting down).
40 bool g_in_x11_io_error_handler = false;
42 // Number of seconds to wait for UI thread to get an IO error if we get it on
43 // the background thread.
44 const int kWaitForUIThreadSeconds = 10;
46 int BrowserX11ErrorHandler(Display* d, XErrorEvent* error) {
47 if (!g_in_x11_io_error_handler)
48 MessageLoop::current()->PostTask(
49 FROM_HERE,
50 NewRunnableFunction(ui::LogErrorEventDescription, d, *error));
51 return 0;
55 // This function is used to help us diagnose crash dumps that happen
56 // during the shutdown process.
57 NOINLINE void WaitingForUIThreadToHandleIOError() {
58 // Ensure function isn't optimized away.
59 asm("");
60 sleep(kWaitForUIThreadSeconds);
63 int BrowserX11IOErrorHandler(Display* d) {
64 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
65 // Wait for the UI thread (which has a different connection to the X server)
66 // to get the error. We can't call shutdown from this thread without
67 // tripping an error. Doing it through a function so that we'll be able
68 // to see it in any crash dumps.
69 WaitingForUIThreadToHandleIOError();
70 return 0;
72 // If there's an IO error it likely means the X server has gone away
73 if (!g_in_x11_io_error_handler) {
74 g_in_x11_io_error_handler = true;
75 LOG(ERROR) << "X IO Error detected";
76 BrowserList::SessionEnding();
79 return 0;
82 } // namespace
84 void BrowserMainPartsGtk::PreEarlyInitialization() {
85 DetectRunningAsRoot();
87 BrowserMainPartsPosix::PreEarlyInitialization();
89 SetupSandbox();
91 #if defined(USE_NSS)
92 // We want to be sure to init NSPR on the main thread.
93 crypto::EnsureNSPRInit();
94 #endif
97 void BrowserMainPartsGtk::DetectRunningAsRoot() {
98 if (geteuid() == 0) {
99 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
100 if (parsed_command_line().HasSwitch(switches::kUserDataDir))
101 return;
103 gfx::GtkInitFromCommandLine(command_line);
105 // Get just enough of our resource machinery up so we can extract the
106 // locale appropriate string. Note that the GTK implementation ignores the
107 // passed in parameter and checks the LANG environment variables instead.
108 ResourceBundle::InitSharedInstance("");
110 std::string message = l10n_util::GetStringFUTF8(
111 IDS_REFUSE_TO_RUN_AS_ROOT,
112 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
113 GtkWidget* dialog = gtk_message_dialog_new(
114 NULL,
115 static_cast<GtkDialogFlags>(0),
116 GTK_MESSAGE_ERROR,
117 GTK_BUTTONS_CLOSE,
118 "%s",
119 message.c_str());
121 LOG(ERROR) << "Startup refusing to run as root.";
122 message = l10n_util::GetStringFUTF8(
123 IDS_REFUSE_TO_RUN_AS_ROOT_2,
124 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
125 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
126 "%s",
127 message.c_str());
129 message = l10n_util::GetStringUTF8(IDS_PRODUCT_NAME);
130 gtk_window_set_title(GTK_WINDOW(dialog), message.c_str());
132 gtk_dialog_run(GTK_DIALOG(dialog));
133 gtk_widget_destroy(dialog);
134 exit(EXIT_FAILURE);
138 void BrowserMainPartsGtk::SetupSandbox() {
139 // TODO(evanm): move this into SandboxWrapper; I'm just trying to move this
140 // code en masse out of chrome_main for now.
141 const char* sandbox_binary = NULL;
142 struct stat st;
144 // In Chromium branded builds, developers can set an environment variable to
145 // use the development sandbox. See
146 // http://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment
147 if (stat("/proc/self/exe", &st) == 0 && st.st_uid == getuid())
148 sandbox_binary = getenv("CHROME_DEVEL_SANDBOX");
150 #if defined(LINUX_SANDBOX_PATH)
151 if (!sandbox_binary)
152 sandbox_binary = LINUX_SANDBOX_PATH;
153 #endif
155 std::string sandbox_cmd;
156 if (sandbox_binary && !parsed_command_line().HasSwitch(switches::kNoSandbox))
157 sandbox_cmd = sandbox_binary;
159 // Tickle the sandbox host and zygote host so they fork now.
160 RenderSandboxHostLinux* shost = RenderSandboxHostLinux::GetInstance();
161 shost->Init(sandbox_cmd);
162 ZygoteHost* zhost = ZygoteHost::GetInstance();
163 zhost->Init(sandbox_cmd);
166 void DidEndMainMessageLoop() {
169 void RecordBreakpadStatusUMA(MetricsService* metrics) {
170 #if defined(USE_LINUX_BREAKPAD)
171 metrics->RecordBreakpadRegistration(IsCrashReporterEnabled());
172 #else
173 metrics->RecordBreakpadRegistration(false);
174 #endif
175 metrics->RecordBreakpadHasDebugger(base::debug::BeingDebugged());
178 void WarnAboutMinimumSystemRequirements() {
179 // Nothing to warn about on GTK right now.
182 void RecordBrowserStartupTime() {
183 // Not implemented on GTK for now.
186 // From browser_main_win.h, stubs until we figure out the right thing...
188 int DoUninstallTasks(bool chrome_still_running) {
189 return ResultCodes::NORMAL_EXIT;
192 int HandleIconsCommands(const CommandLine &parsed_command_line) {
193 return 0;
196 bool CheckMachineLevelInstall() {
197 return false;
200 void PrepareRestartOnCrashEnviroment(const CommandLine &parsed_command_line) {
203 void SetBrowserX11ErrorHandlers() {
204 // Set up error handlers to make sure profile gets written if X server
205 // goes away.
206 ui::SetX11ErrorHandlers(BrowserX11ErrorHandler, BrowserX11IOErrorHandler);
209 #if !defined(OS_CHROMEOS)
210 // static
211 BrowserMainParts* BrowserMainParts::CreateBrowserMainParts(
212 const MainFunctionParams& parameters) {
213 return new BrowserMainPartsGtk(parameters);
215 #endif