| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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/settings/settings_manage_profile_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/prefs/pref_service.h" | |
| 10 #include "base/prefs/scoped_user_pref_update.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 14 #include "base/value_conversions.h" | |
| 15 #include "base/values.h" | |
| 16 #include "chrome/browser/browser_process.h" | |
| 17 #include "chrome/browser/chrome_notification_types.h" | |
| 18 #include "chrome/browser/profiles/gaia_info_update_service.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 20 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | |
| 21 #include "chrome/browser/profiles/profile_info_cache.h" | |
| 22 #include "chrome/browser/profiles/profile_manager.h" | |
| 23 #include "chrome/browser/profiles/profile_metrics.h" | |
| 24 #include "chrome/browser/profiles/profile_shortcut_manager.h" | |
| 25 #include "chrome/browser/profiles/profile_window.h" | |
| 26 #include "chrome/browser/profiles/profiles_state.h" | |
| 27 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 28 #include "chrome/browser/ui/browser_finder.h" | |
| 29 #include "chrome/browser/ui/webui/options/options_handlers_helper.h" | |
| 30 #include "chrome/common/pref_names.h" | |
| 31 #include "chrome/common/url_constants.h" | |
| 32 #include "chrome/grit/chromium_strings.h" | |
| 33 #include "chrome/grit/generated_resources.h" | |
| 34 #include "components/signin/core/browser/signin_manager.h" | |
| 35 #include "components/signin/core/common/profile_management_switches.h" | |
| 36 #include "content/public/browser/browser_thread.h" | |
| 37 #include "content/public/browser/notification_service.h" | |
| 38 #include "content/public/browser/web_ui.h" | |
| 39 #include "google_apis/gaia/gaia_auth_util.h" | |
| 40 #include "ui/base/l10n/l10n_util.h" | |
| 41 #include "ui/base/webui/web_ui_util.h" | |
| 42 | |
| 43 namespace settings { | |
| 44 | |
| 45 ManageProfileHandler::ManageProfileHandler(Profile* profile) | |
| 46 : profile_(profile), weak_factory_(this) { | |
| 47 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); | |
| 48 } | |
| 49 | |
| 50 ManageProfileHandler::~ManageProfileHandler() { | |
| 51 g_browser_process->profile_manager()-> | |
| 52 GetProfileInfoCache().RemoveObserver(this); | |
| 53 } | |
| 54 | |
| 55 void ManageProfileHandler::RegisterMessages() { | |
| 56 web_ui()->RegisterMessageCallback("setProfileIconAndName", | |
| 57 base::Bind(&ManageProfileHandler::SetProfileIconAndName, | |
| 58 base::Unretained(this))); | |
| 59 web_ui()->RegisterMessageCallback("requestDefaultProfileIcons", | |
| 60 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons, | |
| 61 base::Unretained(this))); | |
| 62 web_ui()->RegisterMessageCallback("requestHasProfileShortcuts", | |
| 63 base::Bind(&ManageProfileHandler::RequestHasProfileShortcuts, | |
| 64 base::Unretained(this))); | |
| 65 web_ui()->RegisterMessageCallback("profileIconSelectionChanged", | |
| 66 base::Bind(&ManageProfileHandler::ProfileIconSelectionChanged, | |
| 67 base::Unretained(this))); | |
| 68 web_ui()->RegisterMessageCallback("addProfileShortcut", | |
| 69 base::Bind(&ManageProfileHandler::AddProfileShortcut, | |
| 70 base::Unretained(this))); | |
| 71 web_ui()->RegisterMessageCallback("removeProfileShortcut", | |
| 72 base::Bind(&ManageProfileHandler::RemoveProfileShortcut, | |
| 73 base::Unretained(this))); | |
| 74 web_ui()->RegisterMessageCallback("refreshGaiaPicture", | |
| 75 base::Bind(&ManageProfileHandler::RefreshGaiaPicture, | |
| 76 base::Unretained(this))); | |
| 77 } | |
| 78 | |
| 79 void ManageProfileHandler::OnProfileNameChanged( | |
| 80 const base::FilePath& profile_path, | |
| 81 const base::string16& old_profile_name) { | |
| 82 SendAvailableIcons(); | |
| 83 } | |
| 84 | |
| 85 void ManageProfileHandler::OnProfileAvatarChanged( | |
| 86 const base::FilePath& profile_path) { | |
| 87 SendAvailableIcons(); | |
| 88 } | |
| 89 | |
| 90 void ManageProfileHandler::RequestDefaultProfileIcons( | |
| 91 const base::ListValue* args) { | |
| 92 SendAvailableIcons(); | |
| 93 } | |
| 94 | |
| 95 void ManageProfileHandler::SendAvailableIcons() { | |
| 96 base::ListValue image_url_list; | |
| 97 const ProfileInfoCache& cache = | |
| 98 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 99 | |
| 100 // First add the GAIA picture if it is available. | |
| 101 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | |
| 102 if (profile_index != std::string::npos) { | |
| 103 const gfx::Image* icon = | |
| 104 cache.GetGAIAPictureOfProfileAtIndex(profile_index); | |
| 105 if (icon) { | |
| 106 gfx::Image icon2 = profiles::GetAvatarIconForWebUI(*icon, true); | |
| 107 gaia_picture_url_ = webui::GetBitmapDataUrl(icon2.AsBitmap()); | |
| 108 image_url_list.AppendString(gaia_picture_url_); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 // Next add the default avatar icons and names. | |
| 113 for (size_t i = 0; i < profiles::GetDefaultAvatarIconCount(); i++) { | |
| 114 std::string url = profiles::GetDefaultAvatarIconUrl(i); | |
| 115 image_url_list.AppendString(url); | |
| 116 } | |
| 117 | |
| 118 web_ui()->CallJavascriptFunction( | |
| 119 "settings.SyncPrivateApi.receiveAvailableIcons", | |
| 120 image_url_list); | |
| 121 } | |
| 122 | |
| 123 void ManageProfileHandler::SetProfileIconAndName(const base::ListValue* args) { | |
| 124 DCHECK(args); | |
| 125 | |
| 126 std::string icon_url; | |
| 127 if (!args->GetString(0, &icon_url)) | |
| 128 return; | |
| 129 | |
| 130 PrefService* pref_service = profile_->GetPrefs(); | |
| 131 // Updating the profile preferences will cause the cache to be updated. | |
| 132 | |
| 133 // Metrics logging variable. | |
| 134 bool previously_using_gaia_icon = | |
| 135 pref_service->GetBoolean(prefs::kProfileUsingGAIAAvatar); | |
| 136 | |
| 137 size_t new_icon_index; | |
| 138 if (icon_url == gaia_picture_url_) { | |
| 139 pref_service->SetBoolean(prefs::kProfileUsingDefaultAvatar, false); | |
| 140 pref_service->SetBoolean(prefs::kProfileUsingGAIAAvatar, true); | |
| 141 if (!previously_using_gaia_icon) { | |
| 142 // Only log if they changed to the GAIA photo. | |
| 143 // Selection of GAIA photo as avatar is logged as part of the function | |
| 144 // below. | |
| 145 ProfileMetrics::LogProfileSwitchGaia(ProfileMetrics::GAIA_OPT_IN); | |
| 146 } | |
| 147 } else if (profiles::IsDefaultAvatarIconUrl(icon_url, &new_icon_index)) { | |
| 148 ProfileMetrics::LogProfileAvatarSelection(new_icon_index); | |
| 149 pref_service->SetInteger(prefs::kProfileAvatarIndex, new_icon_index); | |
| 150 pref_service->SetBoolean(prefs::kProfileUsingDefaultAvatar, false); | |
| 151 pref_service->SetBoolean(prefs::kProfileUsingGAIAAvatar, false); | |
| 152 } else { | |
| 153 // Only default avatars and Gaia account photos are supported. | |
| 154 CHECK(false); | |
| 155 } | |
| 156 ProfileMetrics::LogProfileUpdate(profile_->GetPath()); | |
| 157 | |
| 158 if (profile_->IsLegacySupervised()) | |
| 159 return; | |
| 160 | |
| 161 base::string16 new_profile_name; | |
| 162 if (!args->GetString(1, &new_profile_name)) | |
| 163 return; | |
| 164 | |
| 165 base::TrimWhitespace(new_profile_name, base::TRIM_ALL, &new_profile_name); | |
| 166 CHECK(!new_profile_name.empty()); | |
| 167 profiles::UpdateProfileName(profile_, new_profile_name); | |
| 168 } | |
| 169 | |
| 170 void ManageProfileHandler::ProfileIconSelectionChanged( | |
| 171 const base::ListValue* args) { | |
| 172 DCHECK(args); | |
| 173 | |
| 174 std::string icon_url; | |
| 175 if (!args->GetString(0, &icon_url)) | |
| 176 return; | |
| 177 | |
| 178 if (icon_url != gaia_picture_url_) | |
| 179 return; | |
| 180 | |
| 181 // If the selection is the GAIA picture then also show the profile name in the | |
| 182 // text field. This will display either the GAIA given name, if available, | |
| 183 // or the first name. | |
| 184 ProfileInfoCache& cache = | |
| 185 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 186 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | |
| 187 if (profile_index == std::string::npos) | |
| 188 return; | |
| 189 base::string16 gaia_name = cache.GetNameOfProfileAtIndex(profile_index); | |
| 190 if (gaia_name.empty()) | |
| 191 return; | |
| 192 | |
| 193 base::StringValue gaia_name_value(gaia_name); | |
| 194 web_ui()->CallJavascriptFunction( | |
| 195 "settings.SyncPrivateApi.setProfileName", | |
| 196 gaia_name_value); | |
| 197 } | |
| 198 | |
| 199 void ManageProfileHandler::RequestHasProfileShortcuts( | |
| 200 const base::ListValue* args) { | |
| 201 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 202 DCHECK(ProfileShortcutManager::IsFeatureEnabled()); | |
| 203 | |
| 204 const ProfileInfoCache& cache = | |
| 205 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 206 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | |
| 207 if (profile_index == std::string::npos) | |
| 208 return; | |
| 209 | |
| 210 // Don't show the add/remove desktop shortcut button in the single user case. | |
| 211 if (cache.GetNumberOfProfiles() <= 1) | |
| 212 return; | |
| 213 | |
| 214 const base::FilePath profile_path = | |
| 215 cache.GetPathOfProfileAtIndex(profile_index); | |
| 216 ProfileShortcutManager* shortcut_manager = | |
| 217 g_browser_process->profile_manager()->profile_shortcut_manager(); | |
| 218 shortcut_manager->HasProfileShortcuts( | |
| 219 profile_path, base::Bind(&ManageProfileHandler::OnHasProfileShortcuts, | |
| 220 weak_factory_.GetWeakPtr())); | |
| 221 } | |
| 222 | |
| 223 void ManageProfileHandler::OnHasProfileShortcuts(bool has_shortcuts) { | |
| 224 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 225 | |
| 226 const base::FundamentalValue has_shortcuts_value(has_shortcuts); | |
| 227 web_ui()->CallJavascriptFunction( | |
| 228 "settings.SyncPrivateApi.receiveHasProfileShortcuts", | |
| 229 has_shortcuts_value); | |
| 230 } | |
| 231 | |
| 232 void ManageProfileHandler::AddProfileShortcut(const base::ListValue* args) { | |
| 233 DCHECK(ProfileShortcutManager::IsFeatureEnabled()); | |
| 234 ProfileShortcutManager* shortcut_manager = | |
| 235 g_browser_process->profile_manager()->profile_shortcut_manager(); | |
| 236 DCHECK(shortcut_manager); | |
| 237 | |
| 238 shortcut_manager->CreateProfileShortcut(profile_->GetPath()); | |
| 239 | |
| 240 // Update the UI buttons. | |
| 241 OnHasProfileShortcuts(true); | |
| 242 } | |
| 243 | |
| 244 void ManageProfileHandler::RemoveProfileShortcut(const base::ListValue* args) { | |
| 245 DCHECK(ProfileShortcutManager::IsFeatureEnabled()); | |
| 246 ProfileShortcutManager* shortcut_manager = | |
| 247 g_browser_process->profile_manager()->profile_shortcut_manager(); | |
| 248 DCHECK(shortcut_manager); | |
| 249 | |
| 250 shortcut_manager->RemoveProfileShortcuts(profile_->GetPath()); | |
| 251 | |
| 252 // Update the UI buttons. | |
| 253 OnHasProfileShortcuts(false); | |
| 254 } | |
| 255 | |
| 256 void ManageProfileHandler::RefreshGaiaPicture(const base::ListValue* args) { | |
| 257 profiles::UpdateGaiaProfileInfoIfNeeded(Profile::FromWebUI(web_ui())); | |
| 258 } | |
| 259 | |
| 260 } // namespace settings | |
| OLD | NEW |