Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/options/manage_profile_handler.h" | |
| 6 | |
| 7 #include "base/string_number_conversions.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "base/value_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/browser_process.h" | |
| 12 #include "chrome/browser/profiles/profile_info_cache.h" | |
| 13 #include "chrome/browser/profiles/profile_manager.h" | |
| 14 #include "chrome/common/chrome_notification_types.h" | |
| 15 #include "content/common/notification_service.h" | |
| 16 #include "grit/generated_resources.h" | |
| 17 | |
| 18 ManageProfileHandler::ManageProfileHandler() { | |
| 19 } | |
| 20 | |
| 21 ManageProfileHandler::~ManageProfileHandler() { | |
| 22 } | |
| 23 | |
| 24 void ManageProfileHandler::GetLocalizedValues( | |
| 25 DictionaryValue* localized_strings) { | |
| 26 DCHECK(localized_strings); | |
| 27 | |
| 28 static OptionsStringResource resources[] = { | |
| 29 { "profilesManageTitle", IDS_PROFILES_MANAGE_TITLE }, | |
| 30 { "profilesManageNameLabel", IDS_PROFILES_MANAGE_NAME_LABEL }, | |
| 31 { "profilesManageDuplicateNameError", | |
| 32 IDS_PROFILES_MANAGE_DUPLICATE_NAME_ERROR }, | |
| 33 { "profilesManageIconLabel", IDS_PROFILES_MANAGE_ICON_LABEL }, | |
| 34 { "profilesDeleteTitle", IDS_PROFILES_DELETE_TITLE }, | |
| 35 { "profilesDeleteOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL }, | |
| 36 { "profilesDeleteMessage", IDS_PROFILES_DELETE_MESSAGE }, | |
| 37 }; | |
| 38 | |
| 39 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 40 } | |
| 41 | |
| 42 void ManageProfileHandler::Initialize() { | |
| 43 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
| 44 NotificationService::AllSources()); | |
| 45 InitializeDefaultProfileIcons(); | |
| 46 SendProfileNames(); | |
| 47 } | |
| 48 | |
| 49 void ManageProfileHandler::RegisterMessages() { | |
| 50 web_ui_->RegisterMessageCallback("setProfileNameAndIcon", | |
| 51 NewCallback(this, &ManageProfileHandler::SetProfileNameAndIcon)); | |
| 52 web_ui_->RegisterMessageCallback("deleteProfile", | |
| 53 NewCallback(this, &ManageProfileHandler::DeleteProfile)); | |
| 54 } | |
| 55 | |
| 56 void ManageProfileHandler::Observe(int type, | |
| 57 const NotificationSource& source, | |
| 58 const NotificationDetails& details) { | |
| 59 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) { | |
| 60 SendProfileNames(); | |
| 61 } else { | |
| 62 OptionsPageUIHandler::Observe(type, source, details); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void ManageProfileHandler::InitializeDefaultProfileIcons() { | |
| 67 ListValue image_url_list; | |
| 68 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { | |
| 69 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); | |
| 70 image_url_list.Append(Value::CreateStringValue(url)); | |
| 71 } | |
| 72 | |
| 73 web_ui_->CallJavascriptFunction( | |
| 74 "ManageProfileOverlay.receiveDefaultProfileIcons", | |
| 75 image_url_list); | |
| 76 } | |
| 77 | |
| 78 void ManageProfileHandler::SendProfileNames() { | |
| 79 ProfileInfoCache& cache = | |
| 80 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 81 DictionaryValue profile_name_dict; | |
| 82 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i) { | |
| 83 profile_name_dict.SetBoolean(UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)), | |
| 84 true); | |
| 85 } | |
| 86 | |
| 87 web_ui_->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames", | |
| 88 profile_name_dict); | |
| 89 } | |
| 90 | |
| 91 void ManageProfileHandler::SetProfileNameAndIcon(const ListValue* args) { | |
| 92 Value* file_path_value; | |
| 93 FilePath profile_file_path; | |
| 94 if (!args->Get(0, &file_path_value) || | |
| 95 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) { | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 ProfileInfoCache& cache = | |
| 100 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 101 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
| 102 if (profile_index == std::string::npos) { | |
| 103 return; | |
| 104 } | |
|
James Hawkins
2011/07/20 01:24:22
Optional: It's optional according to the style gui
binji
2011/07/20 01:56:17
Done.
| |
| 105 | |
| 106 string16 new_profile_name; | |
| 107 if (!args->GetString(1, &new_profile_name)) { | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 cache.SetNameOfProfileAtIndex(profile_index, new_profile_name); | |
| 112 | |
| 113 string16 icon_url; | |
| 114 size_t new_icon_index; | |
| 115 if (!args->GetString(2, &icon_url) || | |
| 116 !cache.IsDefaultAvatarIconUrl(UTF16ToUTF8(icon_url), &new_icon_index)) { | |
| 117 return; | |
| 118 } | |
| 119 | |
| 120 cache.SetAvatarIconOfProfileAtIndex(profile_index, new_icon_index); | |
| 121 } | |
| 122 | |
| 123 void ManageProfileHandler::DeleteProfile(const ListValue* args) { | |
| 124 Value* file_path_value; | |
| 125 FilePath profile_file_path; | |
| 126 if (!args->Get(0, &file_path_value) || | |
| 127 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) { | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | |
| 132 profile_file_path); | |
| 133 } | |
| 134 | |
| OLD | NEW |