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 "chrome/browser/ui/ash/ash_init.h"
6
7#include "ash/accelerators/accelerator_controller.h"
8#include "ash/accelerometer/accelerometer_controller.h"
9#include "ash/ash_switches.h"
10#include "ash/high_contrast/high_contrast_controller.h"
11#include "ash/magnifier/magnification_controller.h"
12#include "ash/magnifier/partial_magnification_controller.h"
13#include "ash/shell.h"
14#include "ash/shell_init_params.h"
15#include "base/command_line.h"
16#include "chrome/browser/browser_shutdown.h"
17#include "chrome/browser/lifetime/application_lifetime.h"
18#include "chrome/browser/ui/ash/chrome_shell_delegate.h"
19#include "chrome/browser/ui/ash/screenshot_taker.h"
20#include "chrome/common/chrome_switches.h"
21#include "content/public/browser/browser_thread.h"
22#include "content/public/browser/context_factory.h"
23#include "ui/aura/env.h"
24#include "ui/aura/window_tree_host.h"
25
26#if defined(OS_CHROMEOS)
27#include "base/sys_info.h"
28#include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
29#include "chrome/browser/chromeos/accessibility/magnification_manager.h"
30#include "chrome/browser/ui/ash/ime_controller_chromeos.h"
31#include "chrome/browser/ui/ash/volume_controller_chromeos.h"
32#include "chromeos/chromeos_switches.h"
33#include "chromeos/login/login_state.h"
34#include "ui/base/x/x11_util.h"
35#endif
36
37namespace chrome {
38
39bool ShouldOpenAshOnStartup() {
40#if defined(OS_CHROMEOS)
41  return true;
42#endif
43  // TODO(scottmg): http://crbug.com/133312, will need this for Win8 too.
44  return CommandLine::ForCurrentProcess()->HasSwitch(switches::kOpenAsh);
45}
46
47void OpenAsh(gfx::AcceleratedWidget remote_window) {
48#if defined(OS_CHROMEOS)
49#if defined(USE_X11)
50  if (base::SysInfo::IsRunningOnChromeOS()) {
51    // Hides the cursor outside of the Aura root window. The cursor will be
52    // drawn within the Aura root window, and it'll remain hidden after the
53    // Aura window is closed.
54    ui::HideHostCursor();
55  }
56#endif
57
58  // Hide the mouse cursor completely at boot.
59  if (!chromeos::LoginState::Get()->IsUserLoggedIn())
60    ash::Shell::set_initially_hide_cursor(true);
61#endif
62
63  ash::ShellInitParams shell_init_params;
64  // Shell takes ownership of ChromeShellDelegate.
65  shell_init_params.delegate = new ChromeShellDelegate;
66  shell_init_params.context_factory = content::GetContextFactory();
67#if defined(OS_WIN)
68  shell_init_params.remote_hwnd = remote_window;
69#endif
70
71  ash::Shell* shell = ash::Shell::CreateInstance(shell_init_params);
72  shell->accelerator_controller()->SetScreenshotDelegate(
73      scoped_ptr<ash::ScreenshotDelegate>(new ScreenshotTaker).Pass());
74  // TODO(flackr): Investigate exposing a blocking pool task runner to chromeos.
75  shell->accelerometer_controller()->Initialize(
76      content::BrowserThread::GetBlockingPool()->
77          GetTaskRunnerWithShutdownBehavior(
78              base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
79#if defined(OS_CHROMEOS)
80  shell->accelerator_controller()->SetImeControlDelegate(
81      scoped_ptr<ash::ImeControlDelegate>(new ImeController).Pass());
82  shell->high_contrast_controller()->SetEnabled(
83      chromeos::AccessibilityManager::Get()->IsHighContrastEnabled());
84
85  DCHECK(chromeos::MagnificationManager::Get());
86  bool magnifier_enabled =
87      chromeos::MagnificationManager::Get()->IsMagnifierEnabled();
88  ash::MagnifierType magnifier_type =
89      chromeos::MagnificationManager::Get()->GetMagnifierType();
90  shell->magnification_controller()->
91      SetEnabled(magnifier_enabled && magnifier_type == ash::MAGNIFIER_FULL);
92  shell->partial_magnification_controller()->
93      SetEnabled(magnifier_enabled && magnifier_type == ash::MAGNIFIER_PARTIAL);
94
95  if (!CommandLine::ForCurrentProcess()->HasSwitch(
96      switches::kDisableZeroBrowsersOpenForTests)) {
97    chrome::IncrementKeepAliveCount();
98  }
99#endif
100  ash::Shell::GetPrimaryRootWindow()->GetHost()->Show();
101}
102
103void CloseAsh() {
104  if (ash::Shell::HasInstance())
105    ash::Shell::DeleteInstance();
106}
107
108}  // namespace chrome
109