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