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/profile_info_cache.h" | 14 #include "chrome/browser/profiles/profile_info_cache.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "content/browser/tab_contents/tab_contents.h" | |
| 17 #include "content/common/notification_service.h" | 18 #include "content/common/notification_service.h" |
| 18 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
| 19 | 20 |
| 20 ManageProfileHandler::ManageProfileHandler() { | 21 ManageProfileHandler::ManageProfileHandler() { |
| 21 } | 22 } |
| 22 | 23 |
| 23 ManageProfileHandler::~ManageProfileHandler() { | 24 ManageProfileHandler::~ManageProfileHandler() { |
| 24 } | 25 } |
| 25 | 26 |
| 26 void ManageProfileHandler::GetLocalizedValues( | 27 void ManageProfileHandler::GetLocalizedValues( |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 37 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL }, | 38 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL }, |
| 38 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE }, | 39 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE }, |
| 39 }; | 40 }; |
| 40 | 41 |
| 41 RegisterStrings(localized_strings, resources, arraysize(resources)); | 42 RegisterStrings(localized_strings, resources, arraysize(resources)); |
| 42 } | 43 } |
| 43 | 44 |
| 44 void ManageProfileHandler::Initialize() { | 45 void ManageProfileHandler::Initialize() { |
| 45 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 46 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 46 NotificationService::AllSources()); | 47 NotificationService::AllSources()); |
| 47 InitializeDefaultProfileIcons(); | |
| 48 SendProfileNames(); | 48 SendProfileNames(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void ManageProfileHandler::RegisterMessages() { | 51 void ManageProfileHandler::RegisterMessages() { |
| 52 web_ui_->RegisterMessageCallback("setProfileNameAndIcon", | 52 web_ui_->RegisterMessageCallback("setProfileNameAndIcon", |
| 53 base::Bind(&ManageProfileHandler::SetProfileNameAndIcon, | 53 base::Bind(&ManageProfileHandler::SetProfileNameAndIcon, |
| 54 base::Unretained(this))); | 54 base::Unretained(this))); |
| 55 web_ui_->RegisterMessageCallback("deleteProfile", | 55 web_ui_->RegisterMessageCallback("deleteProfile", |
| 56 base::Bind(&ManageProfileHandler::DeleteProfile, | 56 base::Bind(&ManageProfileHandler::DeleteProfile, |
| 57 base::Unretained(this))); | 57 base::Unretained(this))); |
| 58 web_ui_->RegisterMessageCallback("requestDefaultProfileIcons", | |
| 59 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons, | |
| 60 base::Unretained(this))); | |
| 61 web_ui_->RegisterMessageCallback("requestProfileInfo", | |
| 62 base::Bind(&ManageProfileHandler::RequestProfileInfo, | |
| 63 base::Unretained(this))); | |
| 58 } | 64 } |
| 59 | 65 |
| 60 void ManageProfileHandler::Observe(int type, | 66 void ManageProfileHandler::Observe(int type, |
| 61 const NotificationSource& source, | 67 const NotificationSource& source, |
| 62 const NotificationDetails& details) { | 68 const NotificationDetails& details) { |
| 63 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) | 69 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) |
| 64 SendProfileNames(); | 70 SendProfileNames(); |
| 65 else | 71 else |
| 66 OptionsPageUIHandler::Observe(type, source, details); | 72 OptionsPageUIHandler::Observe(type, source, details); |
| 67 } | 73 } |
| 68 | 74 |
| 69 void ManageProfileHandler::InitializeDefaultProfileIcons() { | 75 void ManageProfileHandler::RequestDefaultProfileIcons(const ListValue* args) { |
| 76 DCHECK(args); | |
|
James Hawkins
2011/10/04 22:05:39
I don't see |args| being used in this method. Pro
| |
| 77 | |
| 70 ListValue image_url_list; | 78 ListValue image_url_list; |
| 71 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { | 79 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { |
| 72 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); | 80 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); |
| 73 image_url_list.Append(Value::CreateStringValue(url)); | 81 image_url_list.Append(Value::CreateStringValue(url)); |
| 74 } | 82 } |
| 75 | 83 |
| 76 web_ui_->CallJavascriptFunction( | 84 web_ui_->CallJavascriptFunction( |
| 77 "ManageProfileOverlay.receiveDefaultProfileIcons", | 85 "ManageProfileOverlay.receiveDefaultProfileIcons", |
| 78 image_url_list); | 86 image_url_list); |
| 79 } | 87 } |
| 80 | 88 |
| 81 void ManageProfileHandler::SendProfileNames() { | 89 void ManageProfileHandler::SendProfileNames() { |
| 82 ProfileInfoCache& cache = | 90 ProfileInfoCache& cache = |
| 83 g_browser_process->profile_manager()->GetProfileInfoCache(); | 91 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 84 DictionaryValue profile_name_dict; | 92 DictionaryValue profile_name_dict; |
| 85 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i) | 93 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i) |
| 86 profile_name_dict.SetBoolean(UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)), | 94 profile_name_dict.SetBoolean(UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)), |
| 87 true); | 95 true); |
| 88 | 96 |
| 89 web_ui_->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames", | 97 web_ui_->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames", |
| 90 profile_name_dict); | 98 profile_name_dict); |
| 91 } | 99 } |
| 92 | 100 |
| 93 void ManageProfileHandler::SetProfileNameAndIcon(const ListValue* args) { | 101 void ManageProfileHandler::SetProfileNameAndIcon(const ListValue* args) { |
| 102 DCHECK(args); | |
| 103 | |
| 94 Value* file_path_value; | 104 Value* file_path_value; |
| 95 FilePath profile_file_path; | 105 FilePath profile_file_path; |
| 96 if (!args->Get(0, &file_path_value) || | 106 if (!args->Get(0, &file_path_value) || |
| 97 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | 107 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) |
| 98 return; | 108 return; |
| 99 | 109 |
| 100 ProfileInfoCache& cache = | 110 ProfileInfoCache& cache = |
| 101 g_browser_process->profile_manager()->GetProfileInfoCache(); | 111 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 102 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | 112 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); |
| 103 if (profile_index == std::string::npos) | 113 if (profile_index == std::string::npos) |
| 104 return; | 114 return; |
| 105 | 115 |
| 106 string16 new_profile_name; | 116 string16 new_profile_name; |
| 107 if (!args->GetString(1, &new_profile_name)) | 117 if (!args->GetString(1, &new_profile_name)) |
| 108 return; | 118 return; |
| 109 | 119 |
| 110 cache.SetNameOfProfileAtIndex(profile_index, new_profile_name); | 120 cache.SetNameOfProfileAtIndex(profile_index, new_profile_name); |
| 111 | 121 |
| 112 string16 icon_url; | 122 string16 icon_url; |
| 113 size_t new_icon_index; | 123 size_t new_icon_index; |
| 114 if (!args->GetString(2, &icon_url) || | 124 if (!args->GetString(2, &icon_url) || |
| 115 !cache.IsDefaultAvatarIconUrl(UTF16ToUTF8(icon_url), &new_icon_index)) | 125 !cache.IsDefaultAvatarIconUrl(UTF16ToUTF8(icon_url), &new_icon_index)) |
| 116 return; | 126 return; |
| 117 | 127 |
| 118 cache.SetAvatarIconOfProfileAtIndex(profile_index, new_icon_index); | 128 cache.SetAvatarIconOfProfileAtIndex(profile_index, new_icon_index); |
| 119 } | 129 } |
| 120 | 130 |
| 121 void ManageProfileHandler::DeleteProfile(const ListValue* args) { | 131 void ManageProfileHandler::DeleteProfile(const ListValue* args) { |
| 132 DCHECK(args); | |
| 133 | |
| 122 Value* file_path_value; | 134 Value* file_path_value; |
| 123 FilePath profile_file_path; | 135 FilePath profile_file_path; |
| 124 if (!args->Get(0, &file_path_value) || | 136 if (!args->Get(0, &file_path_value) || |
| 125 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | 137 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) |
| 126 return; | 138 return; |
| 127 | 139 |
| 128 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | 140 g_browser_process->profile_manager()->ScheduleProfileForDeletion( |
| 129 profile_file_path); | 141 profile_file_path); |
| 130 } | 142 } |
| 131 | 143 |
| 144 void ManageProfileHandler::RequestProfileInfo(const ListValue* args) { | |
| 145 DCHECK(args); | |
| 146 | |
| 147 DictionaryValue profile_value; | |
| 148 | |
| 149 Value* index_value; | |
| 150 double index_double; | |
| 151 if (!args->Get(0, &index_value) || !index_value->GetAsDouble(&index_double)) | |
| 152 return; | |
| 153 | |
| 154 int index = static_cast<int>(index_double); | |
| 155 | |
| 156 ProfileInfoCache& cache = | |
| 157 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 158 int profile_count = cache.GetNumberOfProfiles(); | |
| 159 if (index < 0 && index >= profile_count) | |
| 160 return; | |
| 161 | |
| 162 FilePath current_profile_path = | |
| 163 web_ui_->tab_contents()->browser_context()->GetPath(); | |
| 164 size_t icon_index = cache.GetAvatarIconIndexOfProfileAtIndex(index); | |
| 165 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); | |
| 166 profile_value.SetString("name", cache.GetNameOfProfileAtIndex(index)); | |
| 167 profile_value.SetString("iconURL", | |
| 168 cache.GetDefaultAvatarIconUrl(icon_index)); | |
| 169 profile_value.Set("filePath", base::CreateFilePathValue(profile_path)); | |
| 170 profile_value.SetBoolean("isCurrentProfile", | |
| 171 profile_path == current_profile_path); | |
| 172 | |
| 173 web_ui_->CallJavascriptFunction("ManageProfileOverlay.setProfileInfo", | |
| 174 profile_value); | |
| 175 } | |
| OLD | NEW |