| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013 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/webui/signin/user_chooser_screen_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/profiles/profile_info_cache.h" |
| 12 #include "chrome/browser/profiles/profile_info_util.h" |
| 13 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "chrome/browser/ui/browser_commands.h" |
| 15 #include "chrome/browser/ui/browser_finder.h" |
| 16 #include "chrome/browser/ui/singleton_tabs.h" |
| 17 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/browser/web_ui.h" |
| 19 #include "grit/browser_resources.h" |
| 20 #include "grit/chromium_strings.h" |
| 21 #include "grit/generated_resources.h" |
| 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 23 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/gfx/image/image_util.h" |
| 25 #include "ui/webui/web_ui_util.h" |
| 26 |
| 27 namespace { |
| 28 // User dictionary keys. |
| 29 const char kKeyUsername[] = "username"; |
| 30 const char kKeyDisplayName[]= "displayName"; |
| 31 const char kKeyEmailAddress[] = "emailAddress"; |
| 32 const char kKeyPublicAccount[] = "publicAccount"; |
| 33 const char kKeyLocallyManagedUser[] = "locallyManagedUser"; |
| 34 const char kKeySignedIn[] = "signedIn"; |
| 35 const char kKeyCanRemove[] = "canRemove"; |
| 36 const char kKeyIsOwner[] = "isOwner"; |
| 37 const char kKeyIsDesktop[] = "isDesktopUser"; |
| 38 const char kKeyAvatarUrl[] = "userImage"; |
| 39 const char kKeyNeedsSignin[] = "needsSignin"; |
| 40 const char kGAIAPictureFileNameKey[] = "gaia_picture_file_name"; |
| 41 |
| 42 // Max number of users to show. |
| 43 const size_t kMaxUsers = 18; |
| 44 |
| 45 // Type of the login screen UI that is currently presented to user. |
| 46 const char kSourceGaiaSignin[] = "gaia-signin"; |
| 47 const char kSourceAccountPicker[] = "account-picker"; |
| 48 |
| 49 // JS API callback names. |
| 50 const char kJsApiUserChooserInitialize[] = "userChooserInitialize"; |
| 51 const char kJsApiUserChooserAddUser[] = "addUser"; |
| 52 const char kJsApiUserChooserLaunchGuest[] = "launchGuest"; |
| 53 const char kJsApiUserChooserLaunchUser[] = "launchUser"; |
| 54 const char kJsApiUserChooserRemoveUser[] = "removeUser"; |
| 55 |
| 56 void HandleAndDoNothing(const base::ListValue* args) { |
| 57 } |
| 58 |
| 59 } // namespace |
| 60 |
| 61 UserChooserScreenHandler::UserChooserScreenHandler() { |
| 62 } |
| 63 |
| 64 UserChooserScreenHandler::~UserChooserScreenHandler() { |
| 65 } |
| 66 |
| 67 void UserChooserScreenHandler::HandleInitialize(const base::ListValue* args) { |
| 68 SendUserList(); |
| 69 web_ui()->CallJavascriptFunction("cr.ui.Oobe.showUserChooserScreen"); |
| 70 } |
| 71 |
| 72 void UserChooserScreenHandler::HandleAddUser(const base::ListValue* args) { |
| 73 // TODO(noms): Should redirect to a sign in page. |
| 74 chrome::ShowSingletonTab(chrome::FindBrowserWithWebContents( |
| 75 web_ui()->GetWebContents()), |
| 76 GURL("chrome://settings/createProfile")); |
| 77 } |
| 78 |
| 79 void UserChooserScreenHandler::HandleRemoveUser(const base::ListValue* args) { |
| 80 // TODO(noms): Should delete the user. |
| 81 chrome::ShowSingletonTab(chrome::FindBrowserWithWebContents( |
| 82 web_ui()->GetWebContents()), |
| 83 GURL("chrome://settings/search#Users")); |
| 84 } |
| 85 |
| 86 void UserChooserScreenHandler::HandleLaunchGuest(const base::ListValue* args) { |
| 87 // TODO(noms): Once guest mode is ready, should launch a guest browser. |
| 88 chrome::NewIncognitoWindow(chrome::FindBrowserWithWebContents( |
| 89 web_ui()->GetWebContents())); |
| 90 } |
| 91 |
| 92 void UserChooserScreenHandler::HandleLaunchUser(const base::ListValue* args) { |
| 93 string16 emailAddress; |
| 94 string16 displayName; |
| 95 |
| 96 if (!args->GetString(0, &emailAddress) || |
| 97 !args->GetString(1, &displayName)) { |
| 98 NOTREACHED(); |
| 99 return; |
| 100 } |
| 101 |
| 102 ProfileInfoCache& info_cache = |
| 103 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 104 chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop(); |
| 105 |
| 106 for (size_t i = 0; i < info_cache.GetNumberOfProfiles(); ++i) { |
| 107 if (info_cache.GetUserNameOfProfileAtIndex(i) == emailAddress && |
| 108 info_cache.GetNameOfProfileAtIndex(i) == displayName) { |
| 109 base::FilePath path = info_cache.GetPathOfProfileAtIndex(i); |
| 110 ProfileManager::SwitchToProfile(path, desktop_type, true); |
| 111 break; |
| 112 } |
| 113 } |
| 114 } |
| 115 |
| 116 void UserChooserScreenHandler::RegisterMessages() { |
| 117 web_ui()->RegisterMessageCallback(kJsApiUserChooserInitialize, |
| 118 base::Bind(&UserChooserScreenHandler::HandleInitialize, |
| 119 base::Unretained(this))); |
| 120 web_ui()->RegisterMessageCallback(kJsApiUserChooserAddUser, |
| 121 base::Bind(&UserChooserScreenHandler::HandleAddUser, |
| 122 base::Unretained(this))); |
| 123 web_ui()->RegisterMessageCallback(kJsApiUserChooserLaunchGuest, |
| 124 base::Bind(&UserChooserScreenHandler::HandleLaunchGuest, |
| 125 base::Unretained(this))); |
| 126 web_ui()->RegisterMessageCallback(kJsApiUserChooserLaunchUser, |
| 127 base::Bind(&UserChooserScreenHandler::HandleLaunchUser, |
| 128 base::Unretained(this))); |
| 129 web_ui()->RegisterMessageCallback(kJsApiUserChooserRemoveUser, |
| 130 base::Bind(&UserChooserScreenHandler::HandleRemoveUser, |
| 131 base::Unretained(this))); |
| 132 |
| 133 const content::WebUI::MessageCallback& kDoNothingCallback = |
| 134 base::Bind(&HandleAndDoNothing); |
| 135 |
| 136 // Unused callbacks from screen_account_picker.js |
| 137 web_ui()->RegisterMessageCallback("accountPickerReady", kDoNothingCallback); |
| 138 web_ui()->RegisterMessageCallback("loginUIStateChanged", kDoNothingCallback); |
| 139 web_ui()->RegisterMessageCallback("hideCaptivePortal", kDoNothingCallback); |
| 140 // Unused callbacks from display_manager.js |
| 141 web_ui()->RegisterMessageCallback("showAddUser", kDoNothingCallback); |
| 142 web_ui()->RegisterMessageCallback("loadWallpaper", kDoNothingCallback); |
| 143 web_ui()->RegisterMessageCallback("updateCurrentScreen", kDoNothingCallback); |
| 144 web_ui()->RegisterMessageCallback("loginVisible", kDoNothingCallback); |
| 145 // Unused callbacks from user_pod_row.js |
| 146 web_ui()->RegisterMessageCallback("userImagesLoaded", kDoNothingCallback); |
| 147 } |
| 148 |
| 149 void UserChooserScreenHandler::GetLocalizedValues( |
| 150 base::DictionaryValue* localized_strings) { |
| 151 // For Control Bar. |
| 152 localized_strings->SetString("signedIn", |
| 153 l10n_util::GetStringUTF16(IDS_SCREEN_LOCK_ACTIVE_USER)); |
| 154 localized_strings->SetString("signinButton", |
| 155 l10n_util::GetStringUTF16(IDS_LOGIN_BUTTON)); |
| 156 localized_strings->SetString("addUser", |
| 157 l10n_util::GetStringUTF16(IDS_ADD_USER_BUTTON)); |
| 158 localized_strings->SetString("cancel", l10n_util::GetStringUTF16(IDS_CANCEL)); |
| 159 localized_strings->SetString("browseAsGuest", |
| 160 l10n_util::GetStringUTF16(IDS_GO_INCOGNITO_BUTTON)); |
| 161 localized_strings->SetString("signOutUser", |
| 162 l10n_util::GetStringUTF16(IDS_SCREEN_LOCK_SIGN_OUT)); |
| 163 |
| 164 // For AccountPickerScreen. |
| 165 localized_strings->SetString("screenType", "login-add-user"); |
| 166 localized_strings->SetString("highlightStrength", "normal"); |
| 167 localized_strings->SetString("title", "User Chooser"); |
| 168 localized_strings->SetString("passwordHint", |
| 169 l10n_util::GetStringUTF16(IDS_LOGIN_POD_EMPTY_PASSWORD_TEXT)); |
| 170 localized_strings->SetString("podMenuButtonAccessibleName", |
| 171 l10n_util::GetStringUTF16(IDS_LOGIN_POD_MENU_BUTTON_ACCESSIBLE_NAME)); |
| 172 localized_strings->SetString("podMenuRemoveItemAccessibleName", |
| 173 l10n_util::GetStringUTF16( |
| 174 IDS_LOGIN_POD_MENU_REMOVE_ITEM_ACCESSIBLE_NAME)); |
| 175 localized_strings->SetString("removeUser", |
| 176 l10n_util::GetStringUTF16(IDS_LOGIN_POD_REMOVE_USER)); |
| 177 localized_strings->SetString("passwordFieldAccessibleName", |
| 178 l10n_util::GetStringUTF16(IDS_LOGIN_POD_PASSWORD_FIELD_ACCESSIBLE_NAME)); |
| 179 localized_strings->SetString("bootIntoWallpaper", "off"); |
| 180 |
| 181 } |
| 182 |
| 183 void UserChooserScreenHandler::SendUserList() { |
| 184 ListValue users_list; |
| 185 base::FilePath current_profile_path = |
| 186 web_ui()->GetWebContents()->GetBrowserContext()->GetPath(); |
| 187 const ProfileInfoCache& info_cache = |
| 188 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 189 |
| 190 for (size_t i = 0; i < info_cache.GetNumberOfProfiles(); ++i) { |
| 191 DictionaryValue* profile_value = new DictionaryValue(); |
| 192 |
| 193 base::FilePath profile_path = info_cache.GetPathOfProfileAtIndex(i); |
| 194 bool is_active_user = (profile_path == current_profile_path); |
| 195 bool needs_signin = info_cache.ProfileIsSigninRequiredAtIndex(i); |
| 196 |
| 197 profile_value->SetString( |
| 198 kKeyUsername, info_cache.GetUserNameOfProfileAtIndex(i)); |
| 199 profile_value->SetString( |
| 200 kKeyEmailAddress, info_cache.GetUserNameOfProfileAtIndex(i)); |
| 201 profile_value->SetString( |
| 202 kKeyDisplayName, info_cache.GetNameOfProfileAtIndex(i)); |
| 203 profile_value->SetBoolean(kKeyPublicAccount, false); |
| 204 profile_value->SetBoolean(kKeyLocallyManagedUser, false); |
| 205 profile_value->SetBoolean(kKeySignedIn, is_active_user); |
| 206 profile_value->SetBoolean(kKeyNeedsSignin, needs_signin); |
| 207 profile_value->SetBoolean(kKeyIsOwner, false); |
| 208 profile_value->SetBoolean(kKeyCanRemove, true); |
| 209 profile_value->SetBoolean(kKeyIsDesktop, true); |
| 210 |
| 211 bool is_gaia_picture = |
| 212 info_cache.IsUsingGAIAPictureOfProfileAtIndex(i) && |
| 213 info_cache.GetGAIAPictureOfProfileAtIndex(i); |
| 214 |
| 215 gfx::Image icon = profiles::GetSizedAvatarIconWithBorder( |
| 216 info_cache.GetAvatarIconOfProfileAtIndex(i), is_gaia_picture, 160, 160); |
| 217 profile_value->SetString(kKeyAvatarUrl, |
| 218 webui::GetBitmapDataUrl(icon.AsBitmap())); |
| 219 |
| 220 if (is_active_user) |
| 221 users_list.Insert(0, profile_value); |
| 222 else |
| 223 users_list.Append(profile_value); |
| 224 } |
| 225 |
| 226 web_ui()->CallJavascriptFunction("login.AccountPickerScreen.loadUsers", |
| 227 users_list, base::FundamentalValue(false), base::FundamentalValue(true)); |
| 228 } |
| OLD | NEW |