shell_browser_main_parts.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/desktop_background/desktop_background_controller.h" 8#include "ash/shell.h" 9#include "ash/shell/shell_delegate_impl.h" 10#include "ash/shell/window_watcher.h" 11#include "base/bind.h" 12#include "base/command_line.h" 13#include "base/i18n/icu_util.h" 14#include "base/message_loop.h" 15#include "base/string_number_conversions.h" 16#include "base/threading/thread.h" 17#include "base/threading/thread_restrictions.h" 18#include "content/public/common/content_switches.h" 19#include "content/shell/shell_browser_context.h" 20#include "googleurl/src/gurl.h" 21#include "net/base/net_module.h" 22#include "ui/aura/client/stacking_client.h" 23#include "ui/aura/env.h" 24#include "ui/aura/root_window.h" 25#include "ui/aura/window.h" 26#include "ui/base/resource/resource_bundle.h" 27#include "ui/base/ui_base_paths.h" 28#include "ui/compositor/compositor.h" 29#include "ui/compositor/test/compositor_test_support.h" 30#include "ui/gfx/screen.h" 31#include "ui/views/focus/accelerator_handler.h" 32#include "ui/views/test/test_views_delegate.h" 33 34#if defined(ENABLE_MESSAGE_CENTER) 35#include "ui/message_center/message_center.h" 36#endif 37 38#if defined(OS_LINUX) 39#include "ui/base/touch/touch_factory.h" 40#endif 41 42#if defined(OS_CHROMEOS) 43#include "chromeos/dbus/dbus_thread_manager.h" 44#endif 45 46namespace ash { 47namespace shell { 48void InitWindowTypeLauncher(); 49 50namespace { 51class ShellViewsDelegate : public views::TestViewsDelegate { 52 public: 53 ShellViewsDelegate() {} 54 virtual ~ShellViewsDelegate() {} 55 56 // Overridden from views::TestViewsDelegate: 57 virtual views::NonClientFrameView* CreateDefaultNonClientFrameView( 58 views::Widget* widget) OVERRIDE { 59 return ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget); 60 } 61 virtual bool UseTransparentWindows() const OVERRIDE { 62 // Ash uses transparent window frames. 63 return true; 64 } 65 virtual void OnBeforeWidgetInit( 66 views::Widget::InitParams* params, 67 views::internal::NativeWidgetDelegate* delegate) OVERRIDE { 68 if (params->native_widget) 69 return; 70 71 if (!params->parent && !params->context && params->top_level) 72 params->context = Shell::GetPrimaryRootWindow(); 73 } 74 75 private: 76 DISALLOW_COPY_AND_ASSIGN(ShellViewsDelegate); 77}; 78 79} // namespace 80 81ShellBrowserMainParts::ShellBrowserMainParts( 82 const content::MainFunctionParams& parameters) 83 : BrowserMainParts(), 84 delegate_(NULL) { 85} 86 87ShellBrowserMainParts::~ShellBrowserMainParts() { 88} 89 90#if !defined(OS_MACOSX) 91void ShellBrowserMainParts::PreMainMessageLoopStart() { 92#if defined(OS_LINUX) 93 ui::TouchFactory::SetTouchDeviceListFromCommandLine(); 94#endif 95} 96#endif 97 98void ShellBrowserMainParts::PostMainMessageLoopStart() { 99#if defined(OS_CHROMEOS) 100 chromeos::DBusThreadManager::Initialize(); 101#endif 102} 103 104void ShellBrowserMainParts::PreMainMessageLoopRun() { 105 browser_context_.reset(new content::ShellBrowserContext(false)); 106 107 // A ViewsDelegate is required. 108 if (!views::ViewsDelegate::views_delegate) 109 views::ViewsDelegate::views_delegate = new ShellViewsDelegate; 110 111 delegate_ = new ash::shell::ShellDelegateImpl; 112#if defined(ENABLE_MESSAGE_CENTER) 113 // The global message center state must be initialized absent 114 // g_browser_process. 115 message_center::MessageCenter::Initialize(); 116#endif 117 ash::Shell::CreateInstance(delegate_); 118 ash::Shell::GetInstance()->set_browser_context(browser_context_.get()); 119 120 window_watcher_.reset(new ash::shell::WindowWatcher); 121 gfx::Screen* screen = Shell::GetInstance()->GetScreen(); 122 screen->AddObserver(window_watcher_.get()); 123 delegate_->SetWatcher(window_watcher_.get()); 124 125 ash::shell::InitWindowTypeLauncher(); 126 127 DesktopBackgroundController* controller = 128 Shell::GetInstance()->desktop_background_controller(); 129 if (controller->GetAppropriateResolution() == WALLPAPER_RESOLUTION_LARGE) 130 controller->SetDefaultWallpaper(kDefaultLargeWallpaper); 131 else 132 controller->SetDefaultWallpaper(kDefaultSmallWallpaper); 133 134 ash::Shell::GetPrimaryRootWindow()->ShowRootWindow(); 135} 136 137void ShellBrowserMainParts::PostMainMessageLoopRun() { 138 browser_context_.reset(); 139 gfx::Screen* screen = Shell::GetInstance()->GetScreen(); 140 screen->RemoveObserver(window_watcher_.get()); 141 142 window_watcher_.reset(); 143 delegate_->SetWatcher(NULL); 144 delegate_ = NULL; 145 ash::Shell::DeleteInstance(); 146#if defined(ENABLE_MESSAGE_CENTER) 147 // The global message center state must be shutdown absent 148 // g_browser_process. 149 message_center::MessageCenter::Shutdown(); 150#endif 151 aura::Env::DeleteInstance(); 152} 153 154bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code) { 155 MessageLoopForUI::current()->Run(); 156 return true; 157} 158 159} // namespace shell 160} // namespace ash 161