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"
32 #if defined(USE_X11) && !defined(OS_CHROMEOS)
33 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h"
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"
46 #if defined(USE_X11) && !defined(OS_CHROMEOS)
47 ui::NativeTheme
* GetNativeThemeForWindow(aura::Window
* window
) {
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();
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
;
82 return chrome::HOST_DESKTOP_TYPE_NATIVE
;
84 #endif // !defined(OS_CHROMEOS) && defined(USE_ASH)
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
);
102 // TODO(erg): Eventually, we'll need to somehow support IMEs in ash on
104 ui::InitializeInputMethodForTesting();
109 void ChromeBrowserMainExtraPartsAura::ToolkitInitialized() {
110 #if !defined(OS_CHROMEOS)
112 CHECK(aura::Env::GetInstance());
113 active_desktop_monitor_
.reset(new ActiveDesktopMonitor(GetInitialDesktop()));
117 #if defined(USE_X11) && !defined(OS_CHROMEOS)
118 if (GetInitialDesktop() != chrome::HOST_DESKTOP_TYPE_ASH
)
119 views::LinuxUI::instance()->Initialize();
123 void ChromeBrowserMainExtraPartsAura::PreCreateThreads() {
124 #if !defined(OS_CHROMEOS)
126 if (!chrome::ShouldOpenAshOnStartup())
129 gfx::Screen
* screen
= views::CreateDesktopScreen();
130 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, screen
);
132 views::LinuxUI::instance()->UpdateDeviceScaleFactor(
133 screen
->GetPrimaryDisplay().device_scale_factor());
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();
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() {
157 const base::CommandLine
& command_line
=
158 *base::CommandLine::ForCurrentProcess();
159 if (command_line
.HasSwitch(switches::kUserDataDir
))
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
,
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();