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/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"
35
36#if defined(USE_X11)
37#include "ui/events/x/touch_factory_x11.h"
38#endif
39
40#if defined(OS_CHROMEOS)
41#include "chromeos/audio/cras_audio_handler.h"
42#include "chromeos/dbus/dbus_thread_manager.h"
43#endif
44
45namespace ash {
46namespace shell {
47void InitWindowTypeLauncher();
48
49namespace {
50
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 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;
66
67    if (params->native_widget)
68      return;
69
70    if (!params->parent && !params->context && !params->child)
71      params->context = Shell::GetPrimaryRootWindow();
72  }
73
74 private:
75  DISALLOW_COPY_AND_ASSIGN(ShellViewsDelegate);
76};
77
78}  // namespace
79
80ShellBrowserMainParts::ShellBrowserMainParts(
81    const content::MainFunctionParams& parameters)
82    : BrowserMainParts(),
83      delegate_(NULL) {
84}
85
86ShellBrowserMainParts::~ShellBrowserMainParts() {
87}
88
89void ShellBrowserMainParts::PreMainMessageLoopStart() {
90#if defined(USE_X11)
91  ui::TouchFactory::SetTouchDeviceListFromCommandLine();
92#endif
93}
94
95void ShellBrowserMainParts::PostMainMessageLoopStart() {
96#if defined(OS_CHROMEOS)
97  chromeos::DBusThreadManager::Initialize();
98#endif
99}
100
101void ShellBrowserMainParts::ToolkitInitialized() {
102  wm_state_.reset(new wm::WMState);
103}
104
105void ShellBrowserMainParts::PreMainMessageLoopRun() {
106  net_log_.reset(new content::ShellNetLog("ash_shell"));
107  browser_context_.reset(new content::ShellBrowserContext(
108      false, net_log_.get()));
109
110  // A ViewsDelegate is required.
111  if (!views::ViewsDelegate::views_delegate)
112    views::ViewsDelegate::views_delegate = new ShellViewsDelegate;
113
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();
118
119#if defined(OS_CHROMEOS)
120  // Create CrasAudioHandler for testing since g_browser_process
121  // is absent.
122  chromeos::CrasAudioHandler::InitializeForTesting();
123#endif
124
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);
133
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());
138
139  ash::shell::InitWindowTypeLauncher();
140
141  ash::Shell::GetPrimaryRootWindow()->GetHost()->Show();
142}
143
144void ShellBrowserMainParts::PostMainMessageLoopRun() {
145  gfx::Screen* screen = Shell::GetInstance()->GetScreen();
146  screen->RemoveObserver(window_watcher_.get());
147
148  window_watcher_.reset();
149  delegate_->SetWatcher(NULL);
150  delegate_ = NULL;
151  ash::Shell::DeleteInstance();
152  // The global message center state must be shutdown absent
153  // g_browser_process.
154  message_center::MessageCenter::Shutdown();
155
156#if defined(OS_CHROMEOS)
157  chromeos::CrasAudioHandler::Shutdown();
158#endif
159
160  aura::Env::DeleteInstance();
161
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
165  // browser context.
166  browser_context_.reset();
167}
168
169bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code) {
170  base::MessageLoopForUI::current()->Run();
171  return true;
172}
173
174}  // namespace shell
175}  // namespace ash
176