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/content_support/inject.h"
9 #include "ash/desktop_background/desktop_background_controller.h"
10 #include "ash/shell.h"
11 #include "ash/shell/shell_delegate_impl.h"
12 #include "ash/shell/window_watcher.h"
13 #include "ash/system/user/login_status.h"
14 #include "base/bind.h"
15 #include "base/command_line.h"
16 #include "base/i18n/icu_util.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/threading/thread.h"
20 #include "base/threading/thread_restrictions.h"
21 #include "content/public/common/content_switches.h"
22 #include "content/shell/browser/shell_browser_context.h"
23 #include "content/shell/browser/shell_net_log.h"
24 #include "net/base/net_module.h"
25 #include "ui/aura/env.h"
26 #include "ui/aura/root_window.h"
27 #include "ui/aura/window.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/base/ui_base_paths.h"
30 #include "ui/compositor/compositor.h"
31 #include "ui/gfx/screen.h"
32 #include "ui/message_center/message_center.h"
33 #include "ui/views/corewm/wm_state.h"
34 #include "ui/views/focus/accelerator_handler.h"
35 #include "ui/views/test/test_views_delegate.h"
38 #include "ui/events/x/touch_factory_x11.h"
41 #if defined(OS_CHROMEOS)
42 #include "chromeos/audio/cras_audio_handler.h"
43 #include "chromeos/dbus/dbus_thread_manager.h"
48 void InitWindowTypeLauncher();
52 class ShellViewsDelegate
: public views::TestViewsDelegate
{
54 ShellViewsDelegate() {}
55 virtual ~ShellViewsDelegate() {}
57 // Overridden from views::TestViewsDelegate:
58 virtual views::NonClientFrameView
* CreateDefaultNonClientFrameView(
59 views::Widget
* widget
) OVERRIDE
{
60 return ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget
);
62 virtual void OnBeforeWidgetInit(
63 views::Widget::InitParams
* params
,
64 views::internal::NativeWidgetDelegate
* delegate
) OVERRIDE
{
65 if (params
->opacity
== views::Widget::InitParams::INFER_OPACITY
)
66 params
->opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
68 if (params
->native_widget
)
71 if (!params
->parent
&& !params
->context
&& params
->top_level
)
72 params
->context
= Shell::GetPrimaryRootWindow();
76 DISALLOW_COPY_AND_ASSIGN(ShellViewsDelegate
);
81 ShellBrowserMainParts::ShellBrowserMainParts(
82 const content::MainFunctionParams
& parameters
)
87 ShellBrowserMainParts::~ShellBrowserMainParts() {
90 void ShellBrowserMainParts::PreMainMessageLoopStart() {
92 ui::TouchFactory::SetTouchDeviceListFromCommandLine();
96 void ShellBrowserMainParts::PostMainMessageLoopStart() {
97 #if defined(OS_CHROMEOS)
98 chromeos::DBusThreadManager::Initialize();
102 void ShellBrowserMainParts::ToolkitInitialized() {
103 wm_state_
.reset(new views::corewm::WMState
);
106 void ShellBrowserMainParts::PreMainMessageLoopRun() {
107 net_log_
.reset(new content::ShellNetLog());
108 browser_context_
.reset(new content::ShellBrowserContext(
109 false, net_log_
.get()));
111 // A ViewsDelegate is required.
112 if (!views::ViewsDelegate::views_delegate
)
113 views::ViewsDelegate::views_delegate
= new ShellViewsDelegate
;
115 delegate_
= new ash::shell::ShellDelegateImpl
;
116 // The global message center state must be initialized absent
117 // g_browser_process.
118 message_center::MessageCenter::Initialize();
120 #if defined(OS_CHROMEOS)
121 // Create CrasAudioHandler for testing since g_browser_process
123 chromeos::CrasAudioHandler::InitializeForTesting();
126 ash::Shell::CreateInstance(delegate_
);
127 ash::InitContentSupport();
128 delegate_
->set_browser_context(browser_context_
.get());
129 ash::Shell::GetInstance()->CreateShelf();
130 ash::Shell::GetInstance()->UpdateAfterLoginStatusChange(
131 user::LOGGED_IN_USER
);
133 window_watcher_
.reset(new ash::shell::WindowWatcher
);
134 gfx::Screen
* screen
= Shell::GetInstance()->GetScreen();
135 screen
->AddObserver(window_watcher_
.get());
136 delegate_
->SetWatcher(window_watcher_
.get());
138 ash::shell::InitWindowTypeLauncher();
140 Shell::GetInstance()->desktop_background_controller()->SetDefaultWallpaper(
141 false /* is_guest */);
143 ash::Shell::GetPrimaryRootWindow()->GetDispatcher()->host()->Show();
146 void ShellBrowserMainParts::PostMainMessageLoopRun() {
147 gfx::Screen
* screen
= Shell::GetInstance()->GetScreen();
148 screen
->RemoveObserver(window_watcher_
.get());
150 window_watcher_
.reset();
151 delegate_
->SetWatcher(NULL
);
153 ash::Shell::DeleteInstance();
154 // The global message center state must be shutdown absent
155 // g_browser_process.
156 message_center::MessageCenter::Shutdown();
158 #if defined(OS_CHROMEOS)
159 chromeos::CrasAudioHandler::Shutdown();
162 aura::Env::DeleteInstance();
164 // The keyboard may have created a WebContents. The WebContents is destroyed
165 // with the UI, and it needs the BrowserContext to be alive during its
166 // destruction. So destroy all of the UI elements before destroying the
168 browser_context_
.reset();
171 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code
) {
172 base::MessageLoopForUI::current()->Run();