Chromium Code Reviews| 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 gfx::Image icon = cache.GetGAIAPictureOfProfileAtIndex(profile_index); | |
| 99 if (!icon.IsNull()) { | |
| 100 gfx::Image icon2 = profiles::GetAvatarIconForWebUI(icon, true); | |
| 101 gaia_picture_url_ = web_ui_util::GetImageDataUrl(icon2); | |
| 102 image_url_list.Append(Value::CreateStringValue(gaia_picture_url_)); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 // Next add the default avatar icons. | |
| 79 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { | 107 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { |
| 80 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); | 108 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); |
| 81 image_url_list.Append(Value::CreateStringValue(url)); | 109 image_url_list.Append(Value::CreateStringValue(url)); |
| 82 } | 110 } |
| 83 | 111 |
| 84 web_ui_->CallJavascriptFunction( | 112 web_ui_->CallJavascriptFunction( |
| 85 "ManageProfileOverlay.receiveDefaultProfileIcons", | 113 "ManageProfileOverlay.receiveDefaultProfileIcons", |
| 86 image_url_list); | 114 image_url_list); |
| 87 } | 115 } |
| 88 | 116 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 110 ProfileInfoCache& cache = | 138 ProfileInfoCache& cache = |
| 111 g_browser_process->profile_manager()->GetProfileInfoCache(); | 139 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 112 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | 140 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); |
| 113 if (profile_index == std::string::npos) | 141 if (profile_index == std::string::npos) |
| 114 return; | 142 return; |
| 115 | 143 |
| 116 string16 new_profile_name; | 144 string16 new_profile_name; |
| 117 if (!args->GetString(1, &new_profile_name)) | 145 if (!args->GetString(1, &new_profile_name)) |
| 118 return; | 146 return; |
| 119 | 147 |
| 120 cache.SetNameOfProfileAtIndex(profile_index, new_profile_name); | 148 if (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index)) { |
|
binji
2011/11/28 19:09:20
Could you keep the comment? Maybe add something ab
binji
2011/11/28 19:09:20
Is it safe to compare against the GAIA name here?
sail
2011/11/28 19:33:36
Good point. Fixed.
(Note, I'm cleaning this up and
sail
2011/11/28 19:33:36
I agree that the behavior is a little weird if the
| |
| 121 // The index in the cache may have changed if a new name triggered an | 149 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true); |
| 122 // alphabetical resort. | 150 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); |
| 123 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | 151 if (profile_index == std::string::npos) |
| 124 if (profile_index == std::string::npos) | 152 return; |
| 153 } else { | |
| 154 cache.SetNameOfProfileAtIndex(profile_index, new_profile_name); | |
| 155 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
| 156 if (profile_index == std::string::npos) | |
| 157 return; | |
| 158 | |
| 159 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, false); | |
| 160 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
| 161 if (profile_index == std::string::npos) | |
| 162 return; | |
| 163 } | |
| 164 | |
| 165 std::string icon_url; | |
| 166 if (!args->GetString(2, &icon_url)) | |
| 125 return; | 167 return; |
| 126 | 168 |
| 127 string16 icon_url; | |
| 128 size_t new_icon_index; | 169 size_t new_icon_index; |
| 129 if (!args->GetString(2, &icon_url) || | 170 if (icon_url == gaia_picture_url_) { |
| 130 !cache.IsDefaultAvatarIconUrl(UTF16ToUTF8(icon_url), &new_icon_index)) | 171 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true); |
| 131 return; | 172 } else if (cache.IsDefaultAvatarIconUrl(icon_url, &new_icon_index)) { |
| 132 | 173 ProfileMetrics::LogProfileAvatarSelection(new_icon_index); |
| 133 ProfileMetrics::LogProfileAvatarSelection(new_icon_index); | 174 cache.SetAvatarIconOfProfileAtIndex(profile_index, new_icon_index); |
| 134 cache.SetAvatarIconOfProfileAtIndex(profile_index, new_icon_index); | 175 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, false); |
| 176 } | |
| 135 | 177 |
| 136 ProfileMetrics::LogProfileUpdate(profile_file_path); | 178 ProfileMetrics::LogProfileUpdate(profile_file_path); |
| 137 } | 179 } |
| 138 | 180 |
| 139 void ManageProfileHandler::DeleteProfile(const ListValue* args) { | 181 void ManageProfileHandler::DeleteProfile(const ListValue* args) { |
| 140 DCHECK(args); | 182 DCHECK(args); |
| 141 | 183 |
| 142 ProfileMetrics::LogProfileDeleteUser(ProfileMetrics::PROFILE_DELETED); | 184 ProfileMetrics::LogProfileDeleteUser(ProfileMetrics::PROFILE_DELETED); |
| 143 | 185 |
| 144 Value* file_path_value; | 186 Value* file_path_value; |
| 145 FilePath profile_file_path; | 187 FilePath profile_file_path; |
| 146 if (!args->Get(0, &file_path_value) || | 188 if (!args->Get(0, &file_path_value) || |
| 147 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | 189 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) |
| 148 return; | 190 return; |
| 149 | 191 |
| 150 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | 192 g_browser_process->profile_manager()->ScheduleProfileForDeletion( |
| 151 profile_file_path); | 193 profile_file_path); |
| 152 } | 194 } |
| 153 | 195 |
| 154 void ManageProfileHandler::RequestProfileInfo(const ListValue* args) { | 196 void ManageProfileHandler::RequestProfileInfo(const ListValue* args) { |
| 155 DCHECK(args); | 197 DCHECK(args); |
| 156 | 198 |
| 157 DictionaryValue profile_value; | |
| 158 | |
| 159 Value* index_value; | 199 Value* index_value; |
| 160 double index_double; | 200 double index_double; |
| 161 if (!args->Get(0, &index_value) || !index_value->GetAsDouble(&index_double)) | 201 if (!args->Get(0, &index_value) || !index_value->GetAsDouble(&index_double)) |
| 162 return; | 202 return; |
| 163 | 203 |
| 164 int index = static_cast<int>(index_double); | 204 int index = static_cast<int>(index_double); |
| 165 | |
| 166 ProfileInfoCache& cache = | 205 ProfileInfoCache& cache = |
| 167 g_browser_process->profile_manager()->GetProfileInfoCache(); | 206 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 168 int profile_count = cache.GetNumberOfProfiles(); | 207 int profile_count = cache.GetNumberOfProfiles(); |
| 169 if (index < 0 && index >= profile_count) | 208 if (index < 0 && index >= profile_count) |
| 170 return; | 209 return; |
| 171 | 210 |
| 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); | 211 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); |
| 212 FilePath current_profile_path = Profile::FromWebUI(web_ui_)->GetPath(); | |
| 213 bool is_current_profile = | |
| 214 profile_path == Profile::FromWebUI(web_ui_)->GetPath(); | |
| 215 | |
| 216 DictionaryValue profile_value; | |
| 176 profile_value.SetString("name", cache.GetNameOfProfileAtIndex(index)); | 217 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)); | 218 profile_value.Set("filePath", base::CreateFilePathValue(profile_path)); |
| 180 profile_value.SetBoolean("isCurrentProfile", | 219 profile_value.SetBoolean("isCurrentProfile", is_current_profile); |
| 181 profile_path == current_profile_path); | 220 |
| 221 bool is_gaia_picture = | |
| 222 cache.IsUsingGAIAPictureOfProfileAtIndex(index) && | |
| 223 !cache.GetGAIAPictureOfProfileAtIndex(index).IsNull(); | |
| 224 if (is_gaia_picture) { | |
| 225 gfx::Image icon = profiles::GetAvatarIconForWebUI( | |
| 226 cache.GetAvatarIconOfProfileAtIndex(index), true); | |
| 227 profile_value.SetString("iconURL", web_ui_util::GetImageDataUrl(icon)); | |
|
binji
2011/11/28 19:09:20
might be a little nicer to send the GAIA name to j
sail
2011/11/28 19:33:36
It's a littler tricker that way because the javasc
binji
2011/11/28 19:47:41
OK, I see what you mean.
| |
| 228 } else { | |
| 229 size_t icon_index = cache.GetAvatarIconIndexOfProfileAtIndex(index); | |
| 230 profile_value.SetString("iconURL", | |
| 231 cache.GetDefaultAvatarIconUrl(icon_index)); | |
| 232 } | |
| 182 | 233 |
| 183 web_ui_->CallJavascriptFunction("ManageProfileOverlay.setProfileInfo", | 234 web_ui_->CallJavascriptFunction("ManageProfileOverlay.setProfileInfo", |
| 184 profile_value); | 235 profile_value); |
| 236 | |
| 237 // Ensure that we have the most up to date GAIA picture. | |
| 238 if (is_current_profile) { | |
| 239 GAIAInfoUpdateService* service = | |
| 240 Profile::FromWebUI(web_ui_)->GetGAIAInfoUpdateService(); | |
| 241 if (service) | |
| 242 service->Update(); | |
| 243 } | |
| 185 } | 244 } |
| 245 | |
| 246 void ManageProfileHandler::ProfileIconSelectionChanged( | |
| 247 const base::ListValue* args) { | |
| 248 DCHECK(args); | |
| 249 | |
| 250 Value* file_path_value; | |
| 251 FilePath file_path; | |
| 252 if (!args->Get(0, &file_path_value) || | |
| 253 !base::GetValueAsFilePath(*file_path_value, &file_path)) { | |
| 254 return; | |
| 255 } | |
| 256 | |
| 257 // Currently this only supports editing the current profile's info. | |
| 258 if (file_path != Profile::FromWebUI(web_ui_)->GetPath()) | |
| 259 return; | |
| 260 | |
| 261 std::string icon_url; | |
| 262 if (!args->GetString(1, &icon_url)) | |
| 263 return; | |
| 264 | |
| 265 if (icon_url != gaia_picture_url_) | |
| 266 return; | |
| 267 | |
| 268 // If the selection is the GAIA picture then also show the GAIA name in the | |
| 269 // text field. | |
| 270 ProfileInfoCache& cache = | |
| 271 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 272 size_t i = cache.GetIndexOfProfileWithPath(file_path); | |
| 273 if (i == std::string::npos) | |
| 274 return; | |
| 275 string16 gaia_name = cache.GetGAIANameOfProfileAtIndex(i); | |
| 276 if (gaia_name.empty()) | |
| 277 return; | |
| 278 | |
| 279 StringValue gaia_name_value(gaia_name); | |
| 280 web_ui_->CallJavascriptFunction("ManageProfileOverlay.setProfileName", | |
| 281 gaia_name_value); | |
| 282 } | |
| OLD | NEW |