| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/manage_profile_handler.h" | 5 #include "chrome/browser/ui/webui/options/manage_profile_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/value_conversions.h" | 11 #include "base/value_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/profiles/gaia_info_update_service.h" |
| 14 #include "chrome/browser/profiles/profile_info_cache.h" | 15 #include "chrome/browser/profiles/profile_info_cache.h" |
| 16 #include "chrome/browser/profiles/profile_info_util.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/browser/profiles/profile_metrics.h" | 18 #include "chrome/browser/profiles/profile_metrics.h" |
| 19 #include "chrome/browser/ui/webui/web_ui_util.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "content/browser/tab_contents/tab_contents.h" | 21 #include "content/browser/tab_contents/tab_contents.h" |
| 19 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 20 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 21 | 24 |
| 22 ManageProfileHandler::ManageProfileHandler() { | 25 ManageProfileHandler::ManageProfileHandler() { |
| 23 } | 26 } |
| 24 | 27 |
| 25 ManageProfileHandler::~ManageProfileHandler() { | 28 ManageProfileHandler::~ManageProfileHandler() { |
| 26 } | 29 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 55 base::Unretained(this))); | 58 base::Unretained(this))); |
| 56 web_ui_->RegisterMessageCallback("deleteProfile", | 59 web_ui_->RegisterMessageCallback("deleteProfile", |
| 57 base::Bind(&ManageProfileHandler::DeleteProfile, | 60 base::Bind(&ManageProfileHandler::DeleteProfile, |
| 58 base::Unretained(this))); | 61 base::Unretained(this))); |
| 59 web_ui_->RegisterMessageCallback("requestDefaultProfileIcons", | 62 web_ui_->RegisterMessageCallback("requestDefaultProfileIcons", |
| 60 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons, | 63 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons, |
| 61 base::Unretained(this))); | 64 base::Unretained(this))); |
| 62 web_ui_->RegisterMessageCallback("requestProfileInfo", | 65 web_ui_->RegisterMessageCallback("requestProfileInfo", |
| 63 base::Bind(&ManageProfileHandler::RequestProfileInfo, | 66 base::Bind(&ManageProfileHandler::RequestProfileInfo, |
| 64 base::Unretained(this))); | 67 base::Unretained(this))); |
| 68 web_ui_->RegisterMessageCallback("profileIconSelectionChanged", |
| 69 base::Bind(&ManageProfileHandler::ProfileIconSelectionChanged, |
| 70 base::Unretained(this))); |
| 65 } | 71 } |
| 66 | 72 |
| 67 void ManageProfileHandler::Observe( | 73 void ManageProfileHandler::Observe( |
| 68 int type, | 74 int type, |
| 69 const content::NotificationSource& source, | 75 const content::NotificationSource& source, |
| 70 const content::NotificationDetails& details) { | 76 const content::NotificationDetails& details) { |
| 71 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) | 77 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) { |
| 72 SendProfileNames(); | 78 SendProfileNames(); |
| 73 else | 79 SendProfileIcons(); |
| 80 } else { |
| 74 OptionsPageUIHandler::Observe(type, source, details); | 81 OptionsPageUIHandler::Observe(type, source, details); |
| 82 } |
| 75 } | 83 } |
| 76 | 84 |
| 77 void ManageProfileHandler::RequestDefaultProfileIcons(const ListValue* args) { | 85 void ManageProfileHandler::RequestDefaultProfileIcons(const ListValue* args) { |
| 86 SendProfileIcons(); |
| 87 } |
| 88 |
| 89 void ManageProfileHandler::SendProfileIcons() { |
| 78 ListValue image_url_list; | 90 ListValue image_url_list; |
| 91 |
| 92 // First add the GAIA picture if it's available. |
| 93 ProfileInfoCache& cache = |
| 94 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 95 Profile* profile = Profile::FromWebUI(web_ui_); |
| 96 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); |
| 97 if (profile_index != std::string::npos) { |
| 98 const gfx::Image* icon = |
| 99 cache.GetGAIAPictureOfProfileAtIndex(profile_index); |
| 100 if (icon) { |
| 101 gfx::Image icon2 = profiles::GetAvatarIconForWebUI(*icon, true); |
| 102 gaia_picture_url_ = web_ui_util::GetImageDataUrl(icon2); |
| 103 image_url_list.Append(Value::CreateStringValue(gaia_picture_url_)); |
| 104 } |
| 105 } |
| 106 |
| 107 // Next add the default avatar icons. |
| 79 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { | 108 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { |
| 80 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); | 109 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); |
| 81 image_url_list.Append(Value::CreateStringValue(url)); | 110 image_url_list.Append(Value::CreateStringValue(url)); |
| 82 } | 111 } |
| 83 | 112 |
| 84 web_ui_->CallJavascriptFunction( | 113 web_ui_->CallJavascriptFunction( |
| 85 "ManageProfileOverlay.receiveDefaultProfileIcons", | 114 "ManageProfileOverlay.receiveDefaultProfileIcons", |
| 86 image_url_list); | 115 image_url_list); |
| 87 } | 116 } |
| 88 | 117 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 110 ProfileInfoCache& cache = | 139 ProfileInfoCache& cache = |
| 111 g_browser_process->profile_manager()->GetProfileInfoCache(); | 140 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 112 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | 141 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); |
| 113 if (profile_index == std::string::npos) | 142 if (profile_index == std::string::npos) |
| 114 return; | 143 return; |
| 115 | 144 |
| 116 string16 new_profile_name; | 145 string16 new_profile_name; |
| 117 if (!args->GetString(1, &new_profile_name)) | 146 if (!args->GetString(1, &new_profile_name)) |
| 118 return; | 147 return; |
| 119 | 148 |
| 120 cache.SetNameOfProfileAtIndex(profile_index, new_profile_name); | 149 if (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index)) { |
| 121 // The index in the cache may have changed if a new name triggered an | 150 // Set the profile to use the GAIA name as the profile name. Note, this |
| 122 // alphabetical resort. | 151 // is a little weird if the user typed their GAIA name manually but |
| 123 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | 152 // it's not a big deal. |
| 124 if (profile_index == std::string::npos) | 153 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true); |
| 154 // Using the GAIA name as the profile name can invalidate the profile index. |
| 155 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); |
| 156 if (profile_index == std::string::npos) |
| 157 return; |
| 158 } else { |
| 159 cache.SetNameOfProfileAtIndex(profile_index, new_profile_name); |
| 160 // Changing the profile name can invalidate the profile index. |
| 161 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); |
| 162 if (profile_index == std::string::npos) |
| 163 return; |
| 164 |
| 165 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, false); |
| 166 // Unsetting the GAIA name as the profile name can invalidate the profile |
| 167 // index. |
| 168 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); |
| 169 if (profile_index == std::string::npos) |
| 170 return; |
| 171 } |
| 172 |
| 173 std::string icon_url; |
| 174 if (!args->GetString(2, &icon_url)) |
| 125 return; | 175 return; |
| 126 | 176 |
| 127 string16 icon_url; | |
| 128 size_t new_icon_index; | 177 size_t new_icon_index; |
| 129 if (!args->GetString(2, &icon_url) || | 178 if (icon_url == gaia_picture_url_) { |
| 130 !cache.IsDefaultAvatarIconUrl(UTF16ToUTF8(icon_url), &new_icon_index)) | 179 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true); |
| 131 return; | 180 } else if (cache.IsDefaultAvatarIconUrl(icon_url, &new_icon_index)) { |
| 132 | 181 ProfileMetrics::LogProfileAvatarSelection(new_icon_index); |
| 133 ProfileMetrics::LogProfileAvatarSelection(new_icon_index); | 182 cache.SetAvatarIconOfProfileAtIndex(profile_index, new_icon_index); |
| 134 cache.SetAvatarIconOfProfileAtIndex(profile_index, new_icon_index); | 183 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, false); |
| 184 } |
| 135 | 185 |
| 136 ProfileMetrics::LogProfileUpdate(profile_file_path); | 186 ProfileMetrics::LogProfileUpdate(profile_file_path); |
| 137 } | 187 } |
| 138 | 188 |
| 139 void ManageProfileHandler::DeleteProfile(const ListValue* args) { | 189 void ManageProfileHandler::DeleteProfile(const ListValue* args) { |
| 140 DCHECK(args); | 190 DCHECK(args); |
| 141 | 191 |
| 142 ProfileMetrics::LogProfileDeleteUser(ProfileMetrics::PROFILE_DELETED); | 192 ProfileMetrics::LogProfileDeleteUser(ProfileMetrics::PROFILE_DELETED); |
| 143 | 193 |
| 144 Value* file_path_value; | 194 Value* file_path_value; |
| 145 FilePath profile_file_path; | 195 FilePath profile_file_path; |
| 146 if (!args->Get(0, &file_path_value) || | 196 if (!args->Get(0, &file_path_value) || |
| 147 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | 197 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) |
| 148 return; | 198 return; |
| 149 | 199 |
| 150 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | 200 g_browser_process->profile_manager()->ScheduleProfileForDeletion( |
| 151 profile_file_path); | 201 profile_file_path); |
| 152 } | 202 } |
| 153 | 203 |
| 154 void ManageProfileHandler::RequestProfileInfo(const ListValue* args) { | 204 void ManageProfileHandler::RequestProfileInfo(const ListValue* args) { |
| 155 DCHECK(args); | 205 DCHECK(args); |
| 156 | 206 |
| 157 DictionaryValue profile_value; | |
| 158 | |
| 159 Value* index_value; | 207 Value* index_value; |
| 160 double index_double; | 208 double index_double; |
| 161 if (!args->Get(0, &index_value) || !index_value->GetAsDouble(&index_double)) | 209 if (!args->Get(0, &index_value) || !index_value->GetAsDouble(&index_double)) |
| 162 return; | 210 return; |
| 163 | 211 |
| 164 int index = static_cast<int>(index_double); | 212 int index = static_cast<int>(index_double); |
| 165 | |
| 166 ProfileInfoCache& cache = | 213 ProfileInfoCache& cache = |
| 167 g_browser_process->profile_manager()->GetProfileInfoCache(); | 214 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 168 int profile_count = cache.GetNumberOfProfiles(); | 215 int profile_count = cache.GetNumberOfProfiles(); |
| 169 if (index < 0 && index >= profile_count) | 216 if (index < 0 && index >= profile_count) |
| 170 return; | 217 return; |
| 171 | 218 |
| 172 FilePath current_profile_path = | |
| 173 web_ui_->tab_contents()->browser_context()->GetPath(); | |
| 174 size_t icon_index = cache.GetAvatarIconIndexOfProfileAtIndex(index); | |
| 175 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); | 219 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); |
| 220 FilePath current_profile_path = Profile::FromWebUI(web_ui_)->GetPath(); |
| 221 bool is_current_profile = |
| 222 profile_path == Profile::FromWebUI(web_ui_)->GetPath(); |
| 223 |
| 224 DictionaryValue profile_value; |
| 176 profile_value.SetString("name", cache.GetNameOfProfileAtIndex(index)); | 225 profile_value.SetString("name", cache.GetNameOfProfileAtIndex(index)); |
| 177 profile_value.SetString("iconURL", | |
| 178 cache.GetDefaultAvatarIconUrl(icon_index)); | |
| 179 profile_value.Set("filePath", base::CreateFilePathValue(profile_path)); | 226 profile_value.Set("filePath", base::CreateFilePathValue(profile_path)); |
| 180 profile_value.SetBoolean("isCurrentProfile", | 227 profile_value.SetBoolean("isCurrentProfile", is_current_profile); |
| 181 profile_path == current_profile_path); | 228 |
| 229 bool is_gaia_picture = |
| 230 cache.IsUsingGAIAPictureOfProfileAtIndex(index) && |
| 231 cache.GetGAIAPictureOfProfileAtIndex(index); |
| 232 if (is_gaia_picture) { |
| 233 gfx::Image icon = profiles::GetAvatarIconForWebUI( |
| 234 cache.GetAvatarIconOfProfileAtIndex(index), true); |
| 235 profile_value.SetString("iconURL", web_ui_util::GetImageDataUrl(icon)); |
| 236 } else { |
| 237 size_t icon_index = cache.GetAvatarIconIndexOfProfileAtIndex(index); |
| 238 profile_value.SetString("iconURL", |
| 239 cache.GetDefaultAvatarIconUrl(icon_index)); |
| 240 } |
| 182 | 241 |
| 183 web_ui_->CallJavascriptFunction("ManageProfileOverlay.setProfileInfo", | 242 web_ui_->CallJavascriptFunction("ManageProfileOverlay.setProfileInfo", |
| 184 profile_value); | 243 profile_value); |
| 244 |
| 245 // Ensure that we have the most up to date GAIA picture. |
| 246 if (is_current_profile) { |
| 247 GAIAInfoUpdateService* service = |
| 248 Profile::FromWebUI(web_ui_)->GetGAIAInfoUpdateService(); |
| 249 if (service) |
| 250 service->Update(); |
| 251 } |
| 185 } | 252 } |
| 253 |
| 254 void ManageProfileHandler::ProfileIconSelectionChanged( |
| 255 const base::ListValue* args) { |
| 256 DCHECK(args); |
| 257 |
| 258 Value* file_path_value; |
| 259 FilePath file_path; |
| 260 if (!args->Get(0, &file_path_value) || |
| 261 !base::GetValueAsFilePath(*file_path_value, &file_path)) { |
| 262 return; |
| 263 } |
| 264 |
| 265 // Currently this only supports editing the current profile's info. |
| 266 if (file_path != Profile::FromWebUI(web_ui_)->GetPath()) |
| 267 return; |
| 268 |
| 269 std::string icon_url; |
| 270 if (!args->GetString(1, &icon_url)) |
| 271 return; |
| 272 |
| 273 if (icon_url != gaia_picture_url_) |
| 274 return; |
| 275 |
| 276 // If the selection is the GAIA picture then also show the GAIA name in the |
| 277 // text field. |
| 278 ProfileInfoCache& cache = |
| 279 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 280 size_t i = cache.GetIndexOfProfileWithPath(file_path); |
| 281 if (i == std::string::npos) |
| 282 return; |
| 283 string16 gaia_name = cache.GetGAIANameOfProfileAtIndex(i); |
| 284 if (gaia_name.empty()) |
| 285 return; |
| 286 |
| 287 StringValue gaia_name_value(gaia_name); |
| 288 web_ui_->CallJavascriptFunction("ManageProfileOverlay.setProfileName", |
| 289 gaia_name_value); |
| 290 } |
| OLD | NEW |