shell_browser_main_parts.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 "ash/shell/content_client/shell_browser_main_parts.h" 6 7#include "ash/ash_switches.h" 8#include "ash/desktop_background/desktop_background_controller.h" 9#include "ash/shell.h" 10#include "ash/shell/shell_delegate_impl.h" 11#include "ash/shell/window_watcher.h" 12#include "ash/system/user/login_status.h" 13#include "base/bind.h" 14#include "base/command_line.h" 15#include "base/i18n/icu_util.h" 16#include "base/message_loop/message_loop.h" 17#include "base/strings/string_number_conversions.h" 18#include "base/threading/thread.h" 19#include "base/threading/thread_restrictions.h" 20#include "content/public/common/content_switches.h" 21#include "content/shell/browser/shell_browser_context.h" 22#include "content/shell/browser/shell_net_log.h" 23#include "net/base/net_module.h" 24#include "ui/aura/env.h" 25#include "ui/aura/root_window.h" 26#include "ui/aura/window.h" 27#include "ui/base/resource/resource_bundle.h" 28#include "ui/base/ui_base_paths.h" 29#include "ui/compositor/compositor.h" 30#include "ui/gfx/screen.h" 31#include "ui/message_center/message_center.h" 32#include "ui/views/corewm/wm_state.h" 33#include "ui/views/test/test_views_delegate.h" 34 35#if defined(USE_X11) 36#include "ui/events/x/touch_factory_x11.h" 37#endif 38 39#if defined(OS_CHROMEOS) 40#include "chromeos/audio/cras_audio_handler.h" 41#include "chromeos/dbus/dbus_thread_manager.h" 42#endif 43 44namespace ash { 45namespace shell { 46void InitWindowTypeLauncher(); 47 48namespace { 49 50class ShellViewsDelegate : public views::TestViewsDelegate { 51 public: 52 ShellViewsDelegate() {} 53 virtual ~ShellViewsDelegate() {} 54 55 // Overridden from views::TestViewsDelegate: 56 virtual views::NonClientFrameView* CreateDefaultNonClientFrameView( 57 views::Widget* widget) OVERRIDE { 58 return ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget); 59 } 60 virtual void OnBeforeWidgetInit( 61 views::Widget::InitParams* params, 62 views::internal::NativeWidgetDelegate* delegate) OVERRIDE { 63 if (params->opacity == views::Widget::InitParams::INFER_OPACITY) 64 params->opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; 65 66 if (params->native_widget) 67 return; 68 69 if (!params->parent && !params->context && params->top_level) 70 params->context = Shell::GetPrimaryRootWindow(); 71 } 72 73 private: 74 DISALLOW_COPY_AND_ASSIGN(ShellViewsDelegate); 75}; 76 77} // namespace 78 79ShellBrowserMainParts::ShellBrowserMainParts( 80 const content::MainFunctionParams& parameters) 81 : BrowserMainParts(), 82 delegate_(NULL) { 83} 84 85ShellBrowserMainParts::~ShellBrowserMainParts() { 86} 87 88void ShellBrowserMainParts::PreMainMessageLoopStart() { 89#if defined(USE_X11) 90 ui::TouchFactory::SetTouchDeviceListFromCommandLine(); 91#endif 92} 93 94void ShellBrowserMainParts::PostMainMessageLoopStart() { 95#if defined(OS_CHROMEOS) 96 chromeos::DBusThreadManager::Initialize(); 97#endif 98} 99 100void ShellBrowserMainParts::ToolkitInitialized() { 101 wm_state_.reset(new views::corewm::WMState); 102} 103 104void ShellBrowserMainParts::PreMainMessageLoopRun() { 105 net_log_.reset(new content::ShellNetLog("ash_shell")); 106 browser_context_.reset(new content::ShellBrowserContext( 107 false, net_log_.get())); 108 109 // A ViewsDelegate is required. 110 if (!views::ViewsDelegate::views_delegate) 111 views::ViewsDelegate::views_delegate = new ShellViewsDelegate; 112 113 delegate_ = new ash::shell::ShellDelegateImpl; 114 // The global message center state must be initialized absent 115 // g_browser_process. 116 message_center::MessageCenter::Initialize(); 117 118#if defined(OS_CHROMEOS) 119 // Create CrasAudioHandler for testing since g_browser_process 120 // is absent. 121 chromeos::CrasAudioHandler::InitializeForTesting(); 122#endif 123 124 ash::Shell::CreateInstance(delegate_); 125 delegate_->set_browser_context(browser_context_.get()); 126 ash::Shell::GetInstance()->CreateShelf(); 127 ash::Shell::GetInstance()->UpdateAfterLoginStatusChange( 128 user::LOGGED_IN_USER); 129 130 window_watcher_.reset(new ash::shell::WindowWatcher); 131 gfx::Screen* screen = Shell::GetInstance()->GetScreen(); 132 screen->AddObserver(window_watcher_.get()); 133 delegate_->SetWatcher(window_watcher_.get()); 134 135 ash::shell::InitWindowTypeLauncher(); 136 137 Shell::GetInstance()->desktop_background_controller()->SetDefaultWallpaper( 138 false /* is_guest */); 139 140 ash::Shell::GetPrimaryRootWindow()->GetDispatcher()->host()->Show(); 141} 142 143void ShellBrowserMainParts::PostMainMessageLoopRun() { 144 gfx::Screen* screen = Shell::GetInstance()->GetScreen(); 145 screen->RemoveObserver(window_watcher_.get()); 146 147 window_watcher_.reset(); 148 delegate_->SetWatcher(NULL); 149 delegate_ = NULL; 150 ash::Shell::DeleteInstance(); 151 // The global message center state must be shutdown absent 152 // g_browser_process. 153 message_center::MessageCenter::Shutdown(); 154 155#if defined(OS_CHROMEOS) 156 chromeos::CrasAudioHandler::Shutdown(); 157#endif 158 159 aura::Env::DeleteInstance(); 160 161 // The keyboard may have created a WebContents. The WebContents is destroyed 162 // with the UI, and it needs the BrowserContext to be alive during its 163 // destruction. So destroy all of the UI elements before destroying the 164 // browser context. 165 browser_context_.reset(); 166} 167 168bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code) { 169 base::MessageLoopForUI::current()->Run(); 170 return true; 171} 172 173} // namespace shell 174} // namespace ash 175