| OLD | NEW |
| (Empty) |
| 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/webui/options2/manage_profile_handler2.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "base/value_conversions.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/browser/prefs/pref_service.h" | |
| 15 #include "chrome/browser/profiles/gaia_info_update_service.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/browser/profiles/profile_info_cache.h" | |
| 18 #include "chrome/browser/profiles/profile_info_util.h" | |
| 19 #include "chrome/browser/profiles/profile_manager.h" | |
| 20 #include "chrome/browser/profiles/profile_metrics.h" | |
| 21 #include "chrome/browser/ui/webui/web_ui_util.h" | |
| 22 #include "chrome/common/chrome_notification_types.h" | |
| 23 #include "chrome/common/pref_names.h" | |
| 24 #include "content/public/browser/notification_service.h" | |
| 25 #include "content/public/browser/web_ui.h" | |
| 26 #include "grit/generated_resources.h" | |
| 27 | |
| 28 namespace options2 { | |
| 29 | |
| 30 ManageProfileHandler::ManageProfileHandler() { | |
| 31 } | |
| 32 | |
| 33 ManageProfileHandler::~ManageProfileHandler() { | |
| 34 } | |
| 35 | |
| 36 void ManageProfileHandler::GetLocalizedValues( | |
| 37 DictionaryValue* localized_strings) { | |
| 38 DCHECK(localized_strings); | |
| 39 | |
| 40 static OptionsStringResource resources[] = { | |
| 41 { "manageProfilesNameLabel", IDS_PROFILES_MANAGE_NAME_LABEL }, | |
| 42 { "manageProfilesDuplicateNameError", | |
| 43 IDS_PROFILES_MANAGE_DUPLICATE_NAME_ERROR }, | |
| 44 { "manageProfilesIconLabel", IDS_PROFILES_MANAGE_ICON_LABEL }, | |
| 45 { "deleteProfileTitle", IDS_PROFILES_DELETE_TITLE }, | |
| 46 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL }, | |
| 47 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE }, | |
| 48 { "createProfileTitle", IDS_PROFILES_CREATE_TITLE }, | |
| 49 { "createProfileInstructions", IDS_PROFILES_CREATE_INSTRUCTIONS }, | |
| 50 { "createProfileConfirm", IDS_PROFILES_CREATE_CONFIRM }, | |
| 51 { "createProfileShortcut", IDS_PROFILES_CREATE_SHORTCUT_CHKBOX }, | |
| 52 }; | |
| 53 | |
| 54 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 55 RegisterTitle(localized_strings, "manageProfile", | |
| 56 IDS_PROFILES_MANAGE_TITLE); | |
| 57 } | |
| 58 | |
| 59 void ManageProfileHandler::InitializeHandler() { | |
| 60 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
| 61 content::NotificationService::AllSources()); | |
| 62 } | |
| 63 | |
| 64 void ManageProfileHandler::InitializePage() { | |
| 65 SendProfileNames(); | |
| 66 } | |
| 67 | |
| 68 void ManageProfileHandler::RegisterMessages() { | |
| 69 web_ui()->RegisterMessageCallback("setProfileNameAndIcon", | |
| 70 base::Bind(&ManageProfileHandler::SetProfileNameAndIcon, | |
| 71 base::Unretained(this))); | |
| 72 web_ui()->RegisterMessageCallback("deleteProfile", | |
| 73 base::Bind(&ManageProfileHandler::DeleteProfile, | |
| 74 base::Unretained(this))); | |
| 75 web_ui()->RegisterMessageCallback("requestDefaultProfileIcons", | |
| 76 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons, | |
| 77 base::Unretained(this))); | |
| 78 web_ui()->RegisterMessageCallback("profileIconSelectionChanged", | |
| 79 base::Bind(&ManageProfileHandler::ProfileIconSelectionChanged, | |
| 80 base::Unretained(this))); | |
| 81 } | |
| 82 | |
| 83 void ManageProfileHandler::Observe( | |
| 84 int type, | |
| 85 const content::NotificationSource& source, | |
| 86 const content::NotificationDetails& details) { | |
| 87 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) { | |
| 88 SendProfileNames(); | |
| 89 SendProfileIcons(Value::CreateStringValue("manage-profile-icon-grid")); | |
| 90 } else { | |
| 91 OptionsPageUIHandler::Observe(type, source, details); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void ManageProfileHandler::RequestDefaultProfileIcons(const ListValue* args) { | |
| 96 SendProfileIcons(Value::CreateStringValue("manage-profile-icon-grid")); | |
| 97 SendProfileIcons(Value::CreateStringValue("create-profile-icon-grid")); | |
| 98 } | |
| 99 | |
| 100 void ManageProfileHandler::SendProfileIcons(base::StringValue* icon_grid) { | |
| 101 ListValue image_url_list; | |
| 102 | |
| 103 // First add the GAIA picture if it's available. | |
| 104 ProfileInfoCache& cache = | |
| 105 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 106 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 107 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); | |
| 108 if (profile_index != std::string::npos) { | |
| 109 const gfx::Image* icon = | |
| 110 cache.GetGAIAPictureOfProfileAtIndex(profile_index); | |
| 111 if (icon) { | |
| 112 gfx::Image icon2 = profiles::GetAvatarIconForWebUI(*icon, true); | |
| 113 gaia_picture_url_ = web_ui_util::GetImageDataUrl(*icon2.ToImageSkia()); | |
| 114 image_url_list.Append(Value::CreateStringValue(gaia_picture_url_)); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 // Next add the default avatar icons. | |
| 119 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { | |
| 120 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); | |
| 121 image_url_list.Append(Value::CreateStringValue(url)); | |
| 122 } | |
| 123 | |
| 124 web_ui()->CallJavascriptFunction( | |
| 125 "ManageProfileOverlay.receiveDefaultProfileIcons", *icon_grid, | |
| 126 image_url_list); | |
| 127 } | |
| 128 | |
| 129 void ManageProfileHandler::SendProfileNames() { | |
| 130 ProfileInfoCache& cache = | |
| 131 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 132 DictionaryValue profile_name_dict; | |
| 133 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i) | |
| 134 profile_name_dict.SetBoolean(UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)), | |
| 135 true); | |
| 136 | |
| 137 web_ui()->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames", | |
| 138 profile_name_dict); | |
| 139 } | |
| 140 | |
| 141 void ManageProfileHandler::SetProfileNameAndIcon(const ListValue* args) { | |
| 142 DCHECK(args); | |
| 143 | |
| 144 Value* file_path_value; | |
| 145 FilePath profile_file_path; | |
| 146 if (!args->Get(0, &file_path_value) || | |
| 147 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | |
| 148 return; | |
| 149 | |
| 150 ProfileInfoCache& cache = | |
| 151 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 152 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
| 153 if (profile_index == std::string::npos) | |
| 154 return; | |
| 155 | |
| 156 Profile* profile = | |
| 157 g_browser_process->profile_manager()->GetProfile(profile_file_path); | |
| 158 if (!profile) | |
| 159 return; | |
| 160 | |
| 161 string16 new_profile_name; | |
| 162 if (!args->GetString(1, &new_profile_name)) | |
| 163 return; | |
| 164 | |
| 165 if (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index)) { | |
| 166 // Set the profile to use the GAIA name as the profile name. Note, this | |
| 167 // is a little weird if the user typed their GAIA name manually but | |
| 168 // it's not a big deal. | |
| 169 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true); | |
| 170 // Using the GAIA name as the profile name can invalidate the profile index. | |
| 171 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
| 172 if (profile_index == std::string::npos) | |
| 173 return; | |
| 174 } else { | |
| 175 PrefService* pref_service = profile->GetPrefs(); | |
| 176 // Updating the profile preference will cause the cache to be updated for | |
| 177 // this preference. | |
| 178 pref_service->SetString(prefs::kProfileName, UTF16ToUTF8(new_profile_name)); | |
| 179 | |
| 180 // Changing the profile name can invalidate the profile index. | |
| 181 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
| 182 if (profile_index == std::string::npos) | |
| 183 return; | |
| 184 | |
| 185 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, false); | |
| 186 // Unsetting the GAIA name as the profile name can invalidate the profile | |
| 187 // index. | |
| 188 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
| 189 if (profile_index == std::string::npos) | |
| 190 return; | |
| 191 } | |
| 192 | |
| 193 std::string icon_url; | |
| 194 if (!args->GetString(2, &icon_url)) | |
| 195 return; | |
| 196 | |
| 197 // Metrics logging variable. | |
| 198 bool previously_using_gaia_icon = | |
| 199 cache.IsUsingGAIANameOfProfileAtIndex(profile_index); | |
| 200 | |
| 201 size_t new_icon_index; | |
| 202 if (icon_url == gaia_picture_url_) { | |
| 203 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true); | |
| 204 if (!previously_using_gaia_icon) { | |
| 205 // Only log if they changed to the GAIA photo. | |
| 206 // Selection of GAIA photo as avatar is logged as part of the function | |
| 207 // below. | |
| 208 ProfileMetrics::LogProfileSwitchGaia(ProfileMetrics::GAIA_OPT_IN); | |
| 209 } | |
| 210 } else if (cache.IsDefaultAvatarIconUrl(icon_url, &new_icon_index)) { | |
| 211 ProfileMetrics::LogProfileAvatarSelection(new_icon_index); | |
| 212 PrefService* pref_service = profile->GetPrefs(); | |
| 213 // Updating the profile preference will cause the cache to be updated for | |
| 214 // this preference. | |
| 215 pref_service->SetInteger(prefs::kProfileAvatarIndex, new_icon_index); | |
| 216 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, false); | |
| 217 } | |
| 218 | |
| 219 ProfileMetrics::LogProfileUpdate(profile_file_path); | |
| 220 } | |
| 221 | |
| 222 void ManageProfileHandler::DeleteProfile(const ListValue* args) { | |
| 223 DCHECK(args); | |
| 224 // This handler could have been called in managed mode, for example because | |
| 225 // the user fiddled with the web inspector. Silently return in this case. | |
| 226 if (!ProfileManager::IsMultipleProfilesEnabled()) | |
| 227 return; | |
| 228 | |
| 229 ProfileMetrics::LogProfileDeleteUser(ProfileMetrics::PROFILE_DELETED); | |
| 230 | |
| 231 Value* file_path_value; | |
| 232 FilePath profile_file_path; | |
| 233 if (!args->Get(0, &file_path_value) || | |
| 234 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | |
| 235 return; | |
| 236 | |
| 237 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | |
| 238 profile_file_path); | |
| 239 } | |
| 240 | |
| 241 void ManageProfileHandler::ProfileIconSelectionChanged( | |
| 242 const base::ListValue* args) { | |
| 243 DCHECK(args); | |
| 244 | |
| 245 Value* file_path_value; | |
| 246 FilePath file_path; | |
| 247 if (!args->Get(0, &file_path_value) || | |
| 248 !base::GetValueAsFilePath(*file_path_value, &file_path)) { | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 // Currently this only supports editing the current profile's info. | |
| 253 if (file_path != Profile::FromWebUI(web_ui())->GetPath()) | |
| 254 return; | |
| 255 | |
| 256 std::string icon_url; | |
| 257 if (!args->GetString(1, &icon_url)) | |
| 258 return; | |
| 259 | |
| 260 if (icon_url != gaia_picture_url_) | |
| 261 return; | |
| 262 | |
| 263 // If the selection is the GAIA picture then also show the GAIA name in the | |
| 264 // text field. | |
| 265 ProfileInfoCache& cache = | |
| 266 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 267 size_t i = cache.GetIndexOfProfileWithPath(file_path); | |
| 268 if (i == std::string::npos) | |
| 269 return; | |
| 270 string16 gaia_name = cache.GetGAIANameOfProfileAtIndex(i); | |
| 271 if (gaia_name.empty()) | |
| 272 return; | |
| 273 | |
| 274 StringValue gaia_name_value(gaia_name); | |
| 275 web_ui()->CallJavascriptFunction("ManageProfileOverlay.setProfileName", | |
| 276 gaia_name_value); | |
| 277 } | |
| 278 | |
| 279 } // namespace options2 | |
| OLD | NEW |