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/shell_window.h" 8 #include "apps/shell_window_registry.h" 9 #include "ash/host/root_window_host_factory.h" 10 #include "ash/magnifier/magnifier_constants.h" 11 #include "ash/wm/window_state.h" 12 #include "ash/wm/window_util.h" 13 #include "base/command_line.h" 14 #include "chrome/browser/app_mode/app_mode_utils.h" 15 #include "chrome/browser/lifetime/application_lifetime.h" 16 #include "chrome/browser/profiles/profile_manager.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/ash/user_action_handler.h" 24 #include "chrome/browser/ui/browser_commands.h" 25 #include "chrome/common/chrome_switches.h" 26 #include "grit/chromium_strings.h" 27 #include "grit/generated_resources.h" 28 #include "ui/base/l10n/l10n_util.h" 29 30 #if defined(OS_CHROMEOS) 31 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" 32 #include "chrome/browser/chromeos/login/user_manager.h" 33 #endif 34 35 // static 36 ChromeShellDelegate* ChromeShellDelegate::instance_ = NULL; 37 ChromeShellDelegate()38ChromeShellDelegate::ChromeShellDelegate() 39 : shelf_delegate_(NULL) { 40 instance_ = this; 41 PlatformInit(); 42 } 43 ~ChromeShellDelegate()44ChromeShellDelegate::~ChromeShellDelegate() { 45 if (instance_ == this) 46 instance_ = NULL; 47 } 48 IsMultiProfilesEnabled() const49bool ChromeShellDelegate::IsMultiProfilesEnabled() const { 50 // TODO(skuhne): There is a function named profiles::IsMultiProfilesEnabled 51 // which does similar things - but it is not the same. We should investigate 52 // if these two could be folded together. 53 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kMultiProfiles)) 54 return false; 55 #if defined(OS_CHROMEOS) 56 // If there is a user manager, we need to see that we can at least have 2 57 // simultaneous users to allow this feature. 58 if (!chromeos::UserManager::IsInitialized()) 59 return false; 60 size_t admitted_users_to_be_added = 61 chromeos::UserManager::Get()->GetUsersAdmittedForMultiProfile().size(); 62 size_t logged_in_users = 63 chromeos::UserManager::Get()->GetLoggedInUsers().size(); 64 if (!logged_in_users) { 65 // The shelf gets created on the login screen and as such we have to create 66 // all multi profile items of the the system tray menu before the user logs 67 // in. For special cases like Kiosk mode and / or guest mode this isn't a 68 // problem since either the browser gets restarted and / or the flag is not 69 // allowed, but for an "ephermal" user (see crbug.com/312324) it is not 70 // decided yet if he could add other users to his session or not. 71 // TODO(skuhne): As soon as the issue above needs to be resolved, this logic 72 // should change. 73 logged_in_users = 1; 74 } 75 if (admitted_users_to_be_added + logged_in_users <= 1) 76 return false; 77 #endif 78 return true; 79 } 80 IsIncognitoAllowed() const81bool ChromeShellDelegate::IsIncognitoAllowed() const { 82 #if defined(OS_CHROMEOS) 83 return chromeos::AccessibilityManager::Get()->IsIncognitoAllowed(); 84 #endif 85 return true; 86 } 87 IsRunningInForcedAppMode() const88bool ChromeShellDelegate::IsRunningInForcedAppMode() const { 89 return chrome::IsRunningInForcedAppMode(); 90 } 91 Exit()92void ChromeShellDelegate::Exit() { 93 chrome::AttemptUserExit(); 94 } 95 GetActiveBrowserContext()96content::BrowserContext* ChromeShellDelegate::GetActiveBrowserContext() { 97 #if defined(OS_CHROMEOS) 98 DCHECK(chromeos::UserManager::Get()->GetLoggedInUsers().size()); 99 #endif 100 return ProfileManager::GetActiveUserProfileOrOffTheRecord(); 101 } 102 103 app_list::AppListViewDelegate* CreateAppListViewDelegate()104ChromeShellDelegate::CreateAppListViewDelegate() { 105 DCHECK(ash::Shell::HasInstance()); 106 // Shell will own the created delegate, and the delegate will own 107 // the controller. 108 return new AppListViewDelegate( 109 Profile::FromBrowserContext(GetActiveBrowserContext()), 110 AppListService::Get(chrome::HOST_DESKTOP_TYPE_ASH)-> 111 GetControllerDelegate()); 112 } 113 CreateShelfDelegate(ash::ShelfModel * model)114ash::ShelfDelegate* ChromeShellDelegate::CreateShelfDelegate( 115 ash::ShelfModel* model) { 116 DCHECK(ProfileManager::IsGetDefaultProfileAllowed()); 117 // TODO(oshima): This is currently broken with multiple launchers. 118 // Refactor so that there is just one launcher delegate in the 119 // shell. 120 if (!shelf_delegate_) { 121 shelf_delegate_ = ChromeLauncherController::CreateInstance(NULL, model); 122 shelf_delegate_->Init(); 123 } 124 return shelf_delegate_; 125 } 126 CreateUserActionClient()127aura::client::UserActionClient* ChromeShellDelegate::CreateUserActionClient() { 128 return new UserActionHandler; 129 } 130 CreateContextMenu(aura::Window * root)131ui::MenuModel* ChromeShellDelegate::CreateContextMenu(aura::Window* root) { 132 DCHECK(shelf_delegate_); 133 // Don't show context menu for exclusive app runtime mode. 134 if (chrome::IsRunningInAppMode()) 135 return NULL; 136 137 return new LauncherContextMenu(shelf_delegate_, root); 138 } 139 CreateRootWindowHostFactory()140ash::RootWindowHostFactory* ChromeShellDelegate::CreateRootWindowHostFactory() { 141 return ash::RootWindowHostFactory::Create(); 142 } 143 GetProductName() const144base::string16 ChromeShellDelegate::GetProductName() const { 145 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); 146 } 147 148 keyboard::KeyboardControllerProxy* CreateKeyboardControllerProxy()149 ChromeShellDelegate::CreateKeyboardControllerProxy() { 150 return new AshKeyboardControllerProxy(); 151 } 152