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