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/chrome_shell_delegate.h"
6
7#include "ash/content_support/gpu_support_impl.h"
8#include "ash/magnifier/magnifier_constants.h"
9#include "ash/wm/window_state.h"
10#include "ash/wm/window_util.h"
11#include "chrome/browser/app_mode/app_mode_utils.h"
12#include "chrome/browser/lifetime/application_lifetime.h"
13#include "chrome/browser/profiles/profile_manager.h"
14#include "chrome/browser/profiles/profiles_state.h"
15#include "chrome/browser/ui/app_list/app_list_view_delegate.h"
16#include "chrome/browser/ui/ash/app_list/app_list_service_ash.h"
17#include "chrome/browser/ui/ash/ash_keyboard_controller_proxy.h"
18#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
19#include "chrome/browser/ui/ash/launcher/launcher_context_menu.h"
20#include "chrome/browser/ui/browser_commands.h"
21#include "chrome/grit/chromium_strings.h"
22#include "components/signin/core/common/profile_management_switches.h"
23#include "ui/base/l10n/l10n_util.h"
24
25#if defined(OS_CHROMEOS)
26#include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
27#include "chrome/browser/chromeos/display/display_configuration_observer.h"
28#include "components/user_manager/user_manager.h"
29#endif
30
31// static
32ChromeShellDelegate* ChromeShellDelegate::instance_ = NULL;
33
34ChromeShellDelegate::ChromeShellDelegate()
35    : shelf_delegate_(NULL) {
36  instance_ = this;
37  PlatformInit();
38}
39
40ChromeShellDelegate::~ChromeShellDelegate() {
41  if (instance_ == this)
42    instance_ = NULL;
43}
44
45bool ChromeShellDelegate::IsMultiProfilesEnabled() const {
46  if (!profiles::IsMultipleProfilesEnabled())
47    return false;
48#if defined(OS_CHROMEOS)
49  // If there is a user manager, we need to see that we can at least have 2
50  // simultaneous users to allow this feature.
51  if (!user_manager::UserManager::IsInitialized())
52    return false;
53  size_t admitted_users_to_be_added = user_manager::UserManager::Get()
54                                          ->GetUsersAdmittedForMultiProfile()
55                                          .size();
56  size_t logged_in_users =
57      user_manager::UserManager::Get()->GetLoggedInUsers().size();
58  if (!logged_in_users) {
59    // The shelf gets created on the login screen and as such we have to create
60    // all multi profile items of the the system tray menu before the user logs
61    // in. For special cases like Kiosk mode and / or guest mode this isn't a
62    // problem since either the browser gets restarted and / or the flag is not
63    // allowed, but for an "ephermal" user (see crbug.com/312324) it is not
64    // decided yet if he could add other users to his session or not.
65    // TODO(skuhne): As soon as the issue above needs to be resolved, this logic
66    // should change.
67    logged_in_users = 1;
68  }
69  if (admitted_users_to_be_added + logged_in_users <= 1)
70    return false;
71#endif
72  return true;
73}
74
75bool ChromeShellDelegate::IsIncognitoAllowed() const {
76#if defined(OS_CHROMEOS)
77  return chromeos::AccessibilityManager::Get()->IsIncognitoAllowed();
78#endif
79  return true;
80}
81
82bool ChromeShellDelegate::IsRunningInForcedAppMode() const {
83  return chrome::IsRunningInForcedAppMode();
84}
85
86bool ChromeShellDelegate::IsMultiAccountEnabled() const {
87#if defined(OS_CHROMEOS)
88  return switches::IsEnableAccountConsistency();
89#endif
90  return false;
91}
92
93void ChromeShellDelegate::Exit() {
94  chrome::AttemptUserExit();
95}
96
97content::BrowserContext* ChromeShellDelegate::GetActiveBrowserContext() {
98#if defined(OS_CHROMEOS)
99  DCHECK(user_manager::UserManager::Get()->GetLoggedInUsers().size());
100#endif
101  return ProfileManager::GetActiveUserProfile();
102}
103
104app_list::AppListViewDelegate* ChromeShellDelegate::GetAppListViewDelegate() {
105  DCHECK(ash::Shell::HasInstance());
106  return AppListServiceAsh::GetInstance()->GetViewDelegate(
107      Profile::FromBrowserContext(GetActiveBrowserContext()));
108}
109
110ash::ShelfDelegate* ChromeShellDelegate::CreateShelfDelegate(
111    ash::ShelfModel* model) {
112  if (!shelf_delegate_) {
113    shelf_delegate_ = ChromeLauncherController::CreateInstance(NULL, model);
114    shelf_delegate_->Init();
115  }
116  return shelf_delegate_;
117}
118
119ui::MenuModel* ChromeShellDelegate::CreateContextMenu(
120    aura::Window* root,
121    ash::ShelfItemDelegate* item_delegate,
122    ash::ShelfItem* item) {
123  DCHECK(shelf_delegate_);
124  // Don't show context menu for exclusive app runtime mode.
125  if (chrome::IsRunningInAppMode())
126    return NULL;
127
128  if (item_delegate && item)
129    return new LauncherContextMenu(shelf_delegate_, item_delegate, item, root);
130
131  return new LauncherContextMenu(shelf_delegate_, root);
132}
133
134ash::GPUSupport* ChromeShellDelegate::CreateGPUSupport() {
135  // Chrome uses real GPU support.
136  return new ash::GPUSupportImpl;
137}
138
139base::string16 ChromeShellDelegate::GetProductName() const {
140  return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
141}
142
143keyboard::KeyboardControllerProxy*
144    ChromeShellDelegate::CreateKeyboardControllerProxy() {
145  return new AshKeyboardControllerProxy();
146}
147
148void ChromeShellDelegate::VirtualKeyboardActivated(bool activated) {
149  FOR_EACH_OBSERVER(ash::VirtualKeyboardStateObserver,
150                    keyboard_state_observer_list_,
151                    OnVirtualKeyboardStateChanged(activated));
152}
153
154void ChromeShellDelegate::AddVirtualKeyboardStateObserver(
155    ash::VirtualKeyboardStateObserver* observer) {
156  keyboard_state_observer_list_.AddObserver(observer);
157}
158
159void ChromeShellDelegate::RemoveVirtualKeyboardStateObserver(
160    ash::VirtualKeyboardStateObserver* observer) {
161  keyboard_state_observer_list_.RemoveObserver(observer);
162}
163