shell_browser_main_parts.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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/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/client/stacking_client.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/focus/accelerator_handler.h"
34#include "ui/views/test/test_views_delegate.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 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
90void ShellBrowserMainParts::PreMainMessageLoopStart() {
91#if defined(USE_X11)
92  ui::TouchFactory::SetTouchDeviceListFromCommandLine();
93#endif
94}
95
96void ShellBrowserMainParts::PostMainMessageLoopStart() {
97#if defined(OS_CHROMEOS)
98  chromeos::DBusThreadManager::Initialize();
99#endif
100}
101
102void ShellBrowserMainParts::PreMainMessageLoopRun() {
103  net_log_.reset(new content::ShellNetLog());
104  browser_context_.reset(new content::ShellBrowserContext(
105      false, net_log_.get()));
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  // The global message center state must be initialized absent
113  // g_browser_process.
114  message_center::MessageCenter::Initialize();
115
116#if defined(OS_CHROMEOS)
117  // Create CrasAudioHandler for testing since g_browser_process
118  // is absent.
119  chromeos::CrasAudioHandler::InitializeForTesting();
120#endif
121
122  ash::Shell::CreateInstance(delegate_);
123  ash::Shell::GetInstance()->set_browser_context(browser_context_.get());
124  ash::Shell::GetInstance()->CreateLauncher();
125  ash::Shell::GetInstance()->UpdateAfterLoginStatusChange(
126      user::LOGGED_IN_USER);
127
128  window_watcher_.reset(new ash::shell::WindowWatcher);
129  gfx::Screen* screen = Shell::GetInstance()->GetScreen();
130  screen->AddObserver(window_watcher_.get());
131  delegate_->SetWatcher(window_watcher_.get());
132
133  ash::shell::InitWindowTypeLauncher();
134
135  Shell::GetInstance()->desktop_background_controller()->SetDefaultWallpaper(
136      false /* is_guest */);
137
138  ash::Shell::GetPrimaryRootWindow()->ShowRootWindow();
139}
140
141void ShellBrowserMainParts::PostMainMessageLoopRun() {
142  gfx::Screen* screen = Shell::GetInstance()->GetScreen();
143  screen->RemoveObserver(window_watcher_.get());
144
145  window_watcher_.reset();
146  delegate_->SetWatcher(NULL);
147  delegate_ = NULL;
148  ash::Shell::DeleteInstance();
149  // The global message center state must be shutdown absent
150  // g_browser_process.
151  message_center::MessageCenter::Shutdown();
152
153#if defined(OS_CHROMEOS)
154  chromeos::CrasAudioHandler::Shutdown();
155#endif
156
157  aura::Env::DeleteInstance();
158
159  // The keyboard may have created a WebContents. The WebContents is destroyed
160  // with the UI, and it needs the BrowserContext to be alive during its
161  // destruction. So destroy all of the UI elements before destroying the
162  // browser context.
163  browser_context_.reset();
164}
165
166bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code) {
167  base::MessageLoopForUI::current()->Run();
168  return true;
169}
170
171}  // namespace shell
172}  // namespace ash
173