Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / aura / chrome_browser_main_extra_parts_aura.cc
blob9e9476b0e075084089994f3ac68e3b83ec6a7bf3
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/ui/aura/chrome_browser_main_extra_parts_aura.h"
7 #include "base/command_line.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/chrome_browser_main.h"
10 #include "chrome/browser/ui/aura/active_desktop_monitor.h"
11 #include "chrome/browser/ui/host_desktop.h"
12 #include "chrome/browser/ui/simple_message_box.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/grit/chromium_strings.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "ui/aura/env.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/ui_base_switches.h"
19 #include "ui/gfx/screen.h"
20 #include "ui/views/widget/native_widget_aura.h"
22 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
23 #include "base/prefs/pref_service.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/common/pref_names.h"
26 #include "ui/aura/window.h"
27 #include "ui/base/ime/input_method_initializer.h"
28 #include "ui/native_theme/native_theme_aura.h"
29 #include "ui/views/linux_ui/linux_ui.h"
30 #endif
32 #if defined(USE_X11) && !defined(OS_CHROMEOS)
33 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h"
34 #endif
36 #if defined(USE_ASH)
37 #include "chrome/browser/ui/ash/ash_util.h"
38 #endif // defined(USE_ASH)
40 #if !defined(OS_CHROMEOS)
41 #include "ui/views/widget/desktop_aura/desktop_screen.h"
42 #endif
44 namespace {
46 #if defined(USE_X11) && !defined(OS_CHROMEOS)
47 ui::NativeTheme* GetNativeThemeForWindow(aura::Window* window) {
48 if (!window)
49 return NULL;
51 Profile* profile = NULL;
52 if (window->type() == ui::wm::WINDOW_TYPE_NORMAL ||
53 window->type() == ui::wm::WINDOW_TYPE_POPUP) {
54 profile = reinterpret_cast<Profile*>(
55 window->GetNativeWindowProperty(Profile::kProfileKey));
58 if (profile && !profile->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme))
59 return ui::NativeThemeAura::instance();
61 return NULL;
63 #endif
65 #if !defined(OS_CHROMEOS) && defined(USE_ASH)
66 // Returns the desktop this process was initially launched in.
67 chrome::HostDesktopType GetInitialDesktop() {
68 #if defined(OS_WIN) && defined(USE_ASH)
69 const base::CommandLine* command_line =
70 base::CommandLine::ForCurrentProcess();
71 if (command_line->HasSwitch(switches::kViewerConnect) ||
72 command_line->HasSwitch(switches::kViewerLaunchViaAppId)) {
73 return chrome::HOST_DESKTOP_TYPE_ASH;
75 #elif defined(OS_LINUX)
76 const base::CommandLine* command_line =
77 base::CommandLine::ForCurrentProcess();
78 if (command_line->HasSwitch(switches::kOpenAsh))
79 return chrome::HOST_DESKTOP_TYPE_ASH;
80 #endif
82 return chrome::HOST_DESKTOP_TYPE_NATIVE;
84 #endif // !defined(OS_CHROMEOS) && defined(USE_ASH)
86 } // namespace
88 ChromeBrowserMainExtraPartsAura::ChromeBrowserMainExtraPartsAura() {
91 ChromeBrowserMainExtraPartsAura::~ChromeBrowserMainExtraPartsAura() {
94 void ChromeBrowserMainExtraPartsAura::PreEarlyInitialization() {
95 #if defined(USE_X11) && !defined(OS_CHROMEOS)
96 if (GetInitialDesktop() != chrome::HOST_DESKTOP_TYPE_ASH) {
97 // TODO(erg): Refactor this into a dlopen call when we add a GTK3 port.
98 views::LinuxUI* gtk2_ui = BuildGtk2UI();
99 gtk2_ui->SetNativeThemeOverride(base::Bind(&GetNativeThemeForWindow));
100 views::LinuxUI::SetInstance(gtk2_ui);
101 } else {
102 // TODO(erg): Eventually, we'll need to somehow support IMEs in ash on
103 // Linux.
104 ui::InitializeInputMethodForTesting();
106 #endif
109 void ChromeBrowserMainExtraPartsAura::ToolkitInitialized() {
110 #if !defined(OS_CHROMEOS)
111 #if defined(USE_ASH)
112 CHECK(aura::Env::GetInstance());
113 active_desktop_monitor_.reset(new ActiveDesktopMonitor(GetInitialDesktop()));
114 #endif
115 #endif
117 #if defined(USE_X11) && !defined(OS_CHROMEOS)
118 if (GetInitialDesktop() != chrome::HOST_DESKTOP_TYPE_ASH)
119 views::LinuxUI::instance()->Initialize();
120 #endif
123 void ChromeBrowserMainExtraPartsAura::PreCreateThreads() {
124 #if !defined(OS_CHROMEOS)
125 #if defined(USE_ASH)
126 if (!chrome::ShouldOpenAshOnStartup())
127 #endif
129 gfx::Screen* screen = views::CreateDesktopScreen();
130 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen);
131 #if defined(USE_X11)
132 views::LinuxUI::instance()->UpdateDeviceScaleFactor(
133 screen->GetPrimaryDisplay().device_scale_factor());
134 #endif
136 #endif
139 void ChromeBrowserMainExtraPartsAura::PreProfileInit() {
140 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
141 // Now that we have some minimal ui initialized, check to see if we're
142 // running as root and bail if we are.
143 DetectRunningAsRoot();
144 #endif
147 void ChromeBrowserMainExtraPartsAura::PostMainMessageLoopRun() {
148 active_desktop_monitor_.reset();
150 // aura::Env instance is deleted in BrowserProcessImpl::StartTearDown
151 // after the metrics service is deleted.
154 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
155 void ChromeBrowserMainExtraPartsAura::DetectRunningAsRoot() {
156 if (getuid() == 0) {
157 const base::CommandLine& command_line =
158 *base::CommandLine::ForCurrentProcess();
159 if (command_line.HasSwitch(switches::kUserDataDir))
160 return;
162 base::string16 title = l10n_util::GetStringFUTF16(
163 IDS_REFUSE_TO_RUN_AS_ROOT,
164 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
165 base::string16 message = l10n_util::GetStringFUTF16(
166 IDS_REFUSE_TO_RUN_AS_ROOT_2,
167 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
169 chrome::ShowMessageBox(NULL,
170 title,
171 message,
172 chrome::MESSAGE_BOX_TYPE_WARNING);
174 // Avoids gpu_process_transport_factory.cc(153)] Check failed:
175 // per_compositor_data_.empty() when quit is chosen.
176 base::RunLoop().RunUntilIdle();
178 exit(EXIT_FAILURE);
181 #endif