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/shell_init_params.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/browser/context_factory.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/shell/browser/shell_browser_context.h"
24 #include "content/shell/browser/shell_net_log.h"
25 #include "net/base/net_module.h"
26 #include "ui/aura/env.h"
27 #include "ui/aura/window.h"
28 #include "ui/aura/window_tree_host.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/test/test_views_delegate.h"
34 #include "ui/wm/core/wm_state.h"
37 #include "ui/events/devices/x11/touch_factory_x11.h"
40 #if defined(OS_CHROMEOS)
41 #include "chromeos/audio/cras_audio_handler.h"
42 #include "chromeos/dbus/dbus_thread_manager.h"
47 void InitWindowTypeLauncher();
51 class ShellViewsDelegate
: public views::TestViewsDelegate
{
53 ShellViewsDelegate() {}
54 ~ShellViewsDelegate() override
{}
56 // Overridden from views::TestViewsDelegate:
57 views::NonClientFrameView
* CreateDefaultNonClientFrameView(
58 views::Widget
* widget
) override
{
59 return ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget
);
61 void OnBeforeWidgetInit(
62 views::Widget::InitParams
* params
,
63 views::internal::NativeWidgetDelegate
* delegate
) override
{
64 if (params
->opacity
== views::Widget::InitParams::INFER_OPACITY
)
65 params
->opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
67 if (params
->native_widget
)
70 if (!params
->parent
&& !params
->context
&& !params
->child
)
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::ToolkitInitialized() {
102 wm_state_
.reset(new wm::WMState
);
105 void ShellBrowserMainParts::PreMainMessageLoopRun() {
106 net_log_
.reset(new content::ShellNetLog("ash_shell"));
107 browser_context_
.reset(new content::ShellBrowserContext(
108 false, net_log_
.get()));
110 // A ViewsDelegate is required.
111 if (!views::ViewsDelegate::views_delegate
)
112 views::ViewsDelegate::views_delegate
= new ShellViewsDelegate
;
114 delegate_
= new ash::shell::ShellDelegateImpl
;
115 // The global message center state must be initialized absent
116 // g_browser_process.
117 message_center::MessageCenter::Initialize();
119 #if defined(OS_CHROMEOS)
120 // Create CrasAudioHandler for testing since g_browser_process
122 chromeos::CrasAudioHandler::InitializeForTesting();
125 ash::ShellInitParams init_params
;
126 init_params
.delegate
= delegate_
;
127 init_params
.context_factory
= content::GetContextFactory();
128 ash::Shell::CreateInstance(init_params
);
129 delegate_
->set_browser_context(browser_context_
.get());
130 ash::Shell::GetInstance()->CreateShelf();
131 ash::Shell::GetInstance()->UpdateAfterLoginStatusChange(
132 user::LOGGED_IN_USER
);
134 window_watcher_
.reset(new ash::shell::WindowWatcher
);
135 gfx::Screen
* screen
= Shell::GetInstance()->GetScreen();
136 screen
->AddObserver(window_watcher_
.get());
137 delegate_
->SetWatcher(window_watcher_
.get());
139 ash::shell::InitWindowTypeLauncher();
141 ash::Shell::GetPrimaryRootWindow()->GetHost()->Show();
144 void ShellBrowserMainParts::PostMainMessageLoopRun() {
145 gfx::Screen
* screen
= Shell::GetInstance()->GetScreen();
146 screen
->RemoveObserver(window_watcher_
.get());
148 window_watcher_
.reset();
149 delegate_
->SetWatcher(NULL
);
151 ash::Shell::DeleteInstance();
152 // The global message center state must be shutdown absent
153 // g_browser_process.
154 message_center::MessageCenter::Shutdown();
156 #if defined(OS_CHROMEOS)
157 chromeos::CrasAudioHandler::Shutdown();
160 aura::Env::DeleteInstance();
162 // The keyboard may have created a WebContents. The WebContents is destroyed
163 // with the UI, and it needs the BrowserContext to be alive during its
164 // destruction. So destroy all of the UI elements before destroying the
166 browser_context_
.reset();
169 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code
) {
170 base::MessageLoopForUI::current()->Run();