Home | History | Annotate | Download | only in aura
      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.
      4 
      5 #include "chrome/browser/ui/aura/chrome_browser_main_extra_parts_aura.h"
      6 
      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 "grit/chromium_strings.h"
     15 #include "grit/generated_resources.h"
     16 #include "ui/aura/env.h"
     17 #include "ui/base/l10n/l10n_util.h"
     18 #include "ui/gfx/screen.h"
     19 #include "ui/views/widget/native_widget_aura.h"
     20 
     21 #if defined(USE_X11) && !defined(OS_CHROMEOS)
     22 #include "base/prefs/pref_service.h"
     23 #include "chrome/browser/profiles/profile.h"
     24 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.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
     31 
     32 #if defined(USE_ASH)
     33 #include "chrome/browser/ui/ash/ash_init.h"
     34 #if defined(OS_WIN)
     35 #include "base/command_line.h"
     36 #include "chrome/common/chrome_switches.h"
     37 #endif  // defined(OS_WIN)
     38 #endif  // defined(USE_ASH)
     39 
     40 #if !defined(OS_CHROMEOS)
     41 #include "ui/views/widget/desktop_aura/desktop_screen.h"
     42 #endif
     43 
     44 namespace {
     45 
     46 #if defined(USE_X11) && !defined(OS_CHROMEOS)
     47 ui::NativeTheme* GetNativeThemeForWindow(aura::Window* window) {
     48   if (!window)
     49     return NULL;
     50 
     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));
     56   }
     57 
     58   if (profile && !profile->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme))
     59     return ui::NativeThemeAura::instance();
     60 
     61   return NULL;
     62 }
     63 #endif
     64 
     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 CommandLine* command_line = CommandLine::ForCurrentProcess();
     70   if (command_line->HasSwitch(switches::kViewerConnect) ||
     71       command_line->HasSwitch(switches::kViewerLaunchViaAppId)) {
     72     return chrome::HOST_DESKTOP_TYPE_ASH;
     73   }
     74 #elif defined(OS_LINUX)
     75   const CommandLine* command_line = CommandLine::ForCurrentProcess();
     76   if (command_line->HasSwitch(switches::kOpenAsh))
     77     return chrome::HOST_DESKTOP_TYPE_ASH;
     78 #endif
     79 
     80   return chrome::HOST_DESKTOP_TYPE_NATIVE;
     81 }
     82 #endif  // !defined(OS_CHROMEOS) && defined(USE_ASH)
     83 
     84 }  // namespace
     85 
     86 ChromeBrowserMainExtraPartsAura::ChromeBrowserMainExtraPartsAura() {
     87 }
     88 
     89 ChromeBrowserMainExtraPartsAura::~ChromeBrowserMainExtraPartsAura() {
     90 }
     91 
     92 void ChromeBrowserMainExtraPartsAura::PreEarlyInitialization() {
     93 #if defined(USE_X11) && !defined(OS_CHROMEOS)
     94   if (GetInitialDesktop() != chrome::HOST_DESKTOP_TYPE_ASH) {
     95     // TODO(erg): Refactor this into a dlopen call when we add a GTK3 port.
     96     views::LinuxUI* gtk2_ui = BuildGtk2UI();
     97     gtk2_ui->SetNativeThemeOverride(base::Bind(&GetNativeThemeForWindow));
     98     views::LinuxUI::SetInstance(gtk2_ui);
     99   } else {
    100     // TODO(erg): Eventually, we'll need to somehow support IMEs in ash on
    101     // Linux.
    102     ui::InitializeInputMethodForTesting();
    103   }
    104 #endif
    105 }
    106 
    107 void ChromeBrowserMainExtraPartsAura::ToolkitInitialized() {
    108 #if !defined(OS_CHROMEOS)
    109 #if defined(USE_ASH)
    110   CHECK(aura::Env::GetInstance());
    111   active_desktop_monitor_.reset(new ActiveDesktopMonitor(GetInitialDesktop()));
    112 #endif
    113 #endif
    114 
    115 #if defined(USE_X11) && !defined(OS_CHROMEOS)
    116   if (GetInitialDesktop() != chrome::HOST_DESKTOP_TYPE_ASH)
    117     views::LinuxUI::instance()->Initialize();
    118 #endif
    119 }
    120 
    121 void ChromeBrowserMainExtraPartsAura::PreCreateThreads() {
    122 #if !defined(OS_CHROMEOS)
    123 #if defined(USE_ASH)
    124   if (!chrome::ShouldOpenAshOnStartup())
    125 #endif
    126   {
    127     gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
    128                                    views::CreateDesktopScreen());
    129   }
    130 #endif
    131 }
    132 
    133 void ChromeBrowserMainExtraPartsAura::PreProfileInit() {
    134 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
    135   // Now that we have some minimal ui initialized, check to see if we're
    136   // running as root and bail if we are.
    137   DetectRunningAsRoot();
    138 #endif
    139 }
    140 
    141 void ChromeBrowserMainExtraPartsAura::PostMainMessageLoopRun() {
    142   active_desktop_monitor_.reset();
    143 
    144   // aura::Env instance is deleted in BrowserProcessImpl::StartTearDown
    145   // after the metrics service is deleted.
    146 }
    147 
    148 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
    149 void ChromeBrowserMainExtraPartsAura::DetectRunningAsRoot() {
    150   if (getuid() == 0) {
    151     const CommandLine& command_line = *CommandLine::ForCurrentProcess();
    152     if (command_line.HasSwitch(switches::kUserDataDir))
    153       return;
    154 
    155     base::string16 title = l10n_util::GetStringFUTF16(
    156         IDS_REFUSE_TO_RUN_AS_ROOT,
    157         l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
    158     base::string16 message = l10n_util::GetStringFUTF16(
    159         IDS_REFUSE_TO_RUN_AS_ROOT_2,
    160         l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
    161 
    162     chrome::ShowMessageBox(NULL,
    163                            title,
    164                            message,
    165                            chrome::MESSAGE_BOX_TYPE_WARNING);
    166 
    167     // Avoids gpu_process_transport_factory.cc(153)] Check failed:
    168     // per_compositor_data_.empty() when quit is chosen.
    169     base::RunLoop().RunUntilIdle();
    170 
    171     exit(EXIT_FAILURE);
    172   }
    173 }
    174 #endif
    175