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 "ash/shell/content_client/shell_browser_main_parts.h"
7 #include "ash/ash_switches.h"
8 #include "ash/desktop_background/desktop_background_controller.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/focus/accelerator_handler.h"
33 #include "ui/views/test/test_views_delegate.h"
36 #include "ui/events/x/touch_factory_x11.h"
39 #if defined(OS_CHROMEOS)
40 #include "chromeos/audio/cras_audio_handler.h"
41 #include "chromeos/dbus/dbus_thread_manager.h"
46 void InitWindowTypeLauncher();
50 class ShellViewsDelegate : public views::TestViewsDelegate {
52 ShellViewsDelegate() {}
53 virtual ~ShellViewsDelegate() {}
55 // Overridden from views::TestViewsDelegate:
56 virtual views::NonClientFrameView* CreateDefaultNonClientFrameView(
57 views::Widget* widget) OVERRIDE {
58 return ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget);
60 virtual bool UseTransparentWindows() const OVERRIDE {
61 // Ash uses transparent window frames.
64 virtual void OnBeforeWidgetInit(
65 views::Widget::InitParams* params,
66 views::internal::NativeWidgetDelegate* delegate) OVERRIDE {
67 if (params->native_widget)
70 if (!params->parent && !params->context && params->top_level)
71 params->context = Shell::GetPrimaryRootWindow();
75 DISALLOW_COPY_AND_ASSIGN(ShellViewsDelegate);
80 ShellBrowserMainParts::ShellBrowserMainParts(
81 const content::MainFunctionParams& parameters)
86 ShellBrowserMainParts::~ShellBrowserMainParts() {
89 void ShellBrowserMainParts::PreMainMessageLoopStart() {
91 ui::TouchFactory::SetTouchDeviceListFromCommandLine();
95 void ShellBrowserMainParts::PostMainMessageLoopStart() {
96 #if defined(OS_CHROMEOS)
97 chromeos::DBusThreadManager::Initialize();
101 void ShellBrowserMainParts::PreMainMessageLoopRun() {
102 net_log_.reset(new content::ShellNetLog());
103 browser_context_.reset(new content::ShellBrowserContext(
104 false, net_log_.get()));
106 // A ViewsDelegate is required.
107 if (!views::ViewsDelegate::views_delegate)
108 views::ViewsDelegate::views_delegate = new ShellViewsDelegate;
110 delegate_ = new ash::shell::ShellDelegateImpl;
111 // The global message center state must be initialized absent
112 // g_browser_process.
113 message_center::MessageCenter::Initialize();
115 #if defined(OS_CHROMEOS)
116 // Create CrasAudioHandler for testing since g_browser_process
118 chromeos::CrasAudioHandler::InitializeForTesting();
121 ash::Shell::CreateInstance(delegate_);
122 ash::Shell::GetInstance()->set_browser_context(browser_context_.get());
123 ash::Shell::GetInstance()->CreateLauncher();
124 ash::Shell::GetInstance()->UpdateAfterLoginStatusChange(
125 user::LOGGED_IN_USER);
127 window_watcher_.reset(new ash::shell::WindowWatcher);
128 gfx::Screen* screen = Shell::GetInstance()->GetScreen();
129 screen->AddObserver(window_watcher_.get());
130 delegate_->SetWatcher(window_watcher_.get());
132 ash::shell::InitWindowTypeLauncher();
134 Shell::GetInstance()->desktop_background_controller()->SetDefaultWallpaper(
135 false /* is_guest */);
137 ash::Shell::GetPrimaryRootWindow()->GetDispatcher()->ShowRootWindow();
140 void ShellBrowserMainParts::PostMainMessageLoopRun() {
141 gfx::Screen* screen = Shell::GetInstance()->GetScreen();
142 screen->RemoveObserver(window_watcher_.get());
144 window_watcher_.reset();
145 delegate_->SetWatcher(NULL);
147 ash::Shell::DeleteInstance();
148 // The global message center state must be shutdown absent
149 // g_browser_process.
150 message_center::MessageCenter::Shutdown();
152 #if defined(OS_CHROMEOS)
153 chromeos::CrasAudioHandler::Shutdown();
156 aura::Env::DeleteInstance();
158 // The keyboard may have created a WebContents. The WebContents is destroyed
159 // with the UI, and it needs the BrowserContext to be alive during its
160 // destruction. So destroy all of the UI elements before destroying the
162 browser_context_.reset();
165 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code) {
166 base::MessageLoopForUI::current()->Run();