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/options2/manage_profile_handler.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/profiles/gaia_info_update_service.h" | |
15 #include "chrome/browser/profiles/profile_info_cache.h" | |
16 #include "chrome/browser/profiles/profile_info_util.h" | |
17 #include "chrome/browser/profiles/profile_manager.h" | |
18 #include "chrome/browser/profiles/profile_metrics.h" | |
19 #include "chrome/browser/ui/webui/web_ui_util.h" | |
20 #include "chrome/common/chrome_notification_types.h" | |
21 #include "content/browser/tab_contents/tab_contents.h" | |
22 #include "content/public/browser/notification_service.h" | |
23 #include "grit/generated_resources.h" | |
24 | |
25 ManageProfileHandler::ManageProfileHandler() { | |
26 } | |
27 | |
28 ManageProfileHandler::~ManageProfileHandler() { | |
29 } | |
30 | |
31 void ManageProfileHandler::GetLocalizedValues( | |
32 DictionaryValue* localized_strings) { | |
33 DCHECK(localized_strings); | |
34 | |
35 static OptionsStringResource resources[] = { | |
36 { "manageProfilesTitle", IDS_PROFILES_MANAGE_TITLE }, | |
37 { "manageProfilesNameLabel", IDS_PROFILES_MANAGE_NAME_LABEL }, | |
38 { "manageProfilesDuplicateNameError", | |
39 IDS_PROFILES_MANAGE_DUPLICATE_NAME_ERROR }, | |
40 { "manageProfilesIconLabel", IDS_PROFILES_MANAGE_ICON_LABEL }, | |
41 { "deleteProfileTitle", IDS_PROFILES_DELETE_TITLE }, | |
42 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL }, | |
43 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE }, | |
44 }; | |
45 | |
46 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
47 } | |
48 | |
49 void ManageProfileHandler::Initialize() { | |
50 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
51 content::NotificationService::AllSources()); | |
52 SendProfileNames(); | |
53 } | |
54 | |
55 void ManageProfileHandler::RegisterMessages() { | |
56 web_ui_->RegisterMessageCallback("setProfileNameAndIcon", | |
57 base::Bind(&ManageProfileHandler::SetProfileNameAndIcon, | |
58 base::Unretained(this))); | |
59 web_ui_->RegisterMessageCallback("deleteProfile", | |
60 base::Bind(&ManageProfileHandler::DeleteProfile, | |
61 base::Unretained(this))); | |
62 web_ui_->RegisterMessageCallback("requestDefaultProfileIcons", | |
63 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons, | |
64 base::Unretained(this))); | |
65 web_ui_->RegisterMessageCallback("requestProfileInfo", | |
66 base::Bind(&ManageProfileHandler::RequestProfileInfo, | |
67 base::Unretained(this))); | |
68 web_ui_->RegisterMessageCallback("profileIconSelectionChanged", | |
69 base::Bind(&ManageProfileHandler::ProfileIconSelectionChanged, | |
70 base::Unretained(this))); | |
71 } | |
72 | |
73 void ManageProfileHandler::Observe( | |
74 int type, | |
75 const content::NotificationSource& source, | |
76 const content::NotificationDetails& details) { | |
77 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) { | |
78 SendProfileNames(); | |
79 SendProfileIcons(); | |
80 } else { | |
81 OptionsPage2UIHandler::Observe(type, source, details); | |
82 } | |
83 } | |
84 | |
85 void ManageProfileHandler::RequestDefaultProfileIcons(const ListValue* args) { | |
86 SendProfileIcons(); | |
87 } | |
88 | |
89 void ManageProfileHandler::SendProfileIcons() { | |
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. | |
108 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { | |
109 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); | |
110 image_url_list.Append(Value::CreateStringValue(url)); | |
111 } | |
112 | |
113 web_ui_->CallJavascriptFunction( | |
114 "ManageProfileOverlay.receiveDefaultProfileIcons", | |
115 image_url_list); | |
116 } | |
117 | |
118 void ManageProfileHandler::SendProfileNames() { | |
119 ProfileInfoCache& cache = | |
120 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
121 DictionaryValue profile_name_dict; | |
122 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i) | |
123 profile_name_dict.SetBoolean(UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)), | |
124 true); | |
125 | |
126 web_ui_->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames", | |
127 profile_name_dict); | |
128 } | |
129 | |
130 void ManageProfileHandler::SetProfileNameAndIcon(const ListValue* args) { | |
131 DCHECK(args); | |
132 | |
133 Value* file_path_value; | |
134 FilePath profile_file_path; | |
135 if (!args->Get(0, &file_path_value) || | |
136 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | |
137 return; | |
138 | |
139 ProfileInfoCache& cache = | |
140 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
141 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
142 if (profile_index == std::string::npos) | |
143 return; | |
144 | |
145 string16 new_profile_name; | |
146 if (!args->GetString(1, &new_profile_name)) | |
147 return; | |
148 | |
149 if (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index)) { | |
150 // Set the profile to use the GAIA name as the profile name. Note, this | |
151 // is a little weird if the user typed their GAIA name manually but | |
152 // it's not a big deal. | |
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)) | |
175 return; | |
176 | |
177 // Metrics logging variable. | |
178 bool previously_using_gaia_icon = | |
179 cache.IsUsingGAIANameOfProfileAtIndex(profile_index); | |
180 | |
181 size_t new_icon_index; | |
182 if (icon_url == gaia_picture_url_) { | |
183 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true); | |
184 if (!previously_using_gaia_icon) { | |
185 // Only log if they changed to the GAIA photo. | |
186 // Selection of GAIA photo as avatar is logged as part of the function | |
187 // below. | |
188 ProfileMetrics::LogProfileSwitchGaia(ProfileMetrics::GAIA_OPT_IN); | |
189 } | |
190 } else if (cache.IsDefaultAvatarIconUrl(icon_url, &new_icon_index)) { | |
191 ProfileMetrics::LogProfileAvatarSelection(new_icon_index); | |
192 cache.SetAvatarIconOfProfileAtIndex(profile_index, new_icon_index); | |
193 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, false); | |
194 } | |
195 | |
196 ProfileMetrics::LogProfileUpdate(profile_file_path); | |
197 } | |
198 | |
199 void ManageProfileHandler::DeleteProfile(const ListValue* args) { | |
200 DCHECK(args); | |
201 | |
202 ProfileMetrics::LogProfileDeleteUser(ProfileMetrics::PROFILE_DELETED); | |
203 | |
204 Value* file_path_value; | |
205 FilePath profile_file_path; | |
206 if (!args->Get(0, &file_path_value) || | |
207 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | |
208 return; | |
209 | |
210 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | |
211 profile_file_path); | |
212 } | |
213 | |
214 void ManageProfileHandler::RequestProfileInfo(const ListValue* args) { | |
215 DCHECK(args); | |
216 | |
217 Value* index_value; | |
218 double index_double; | |
219 if (!args->Get(0, &index_value) || !index_value->GetAsDouble(&index_double)) | |
220 return; | |
221 | |
222 int index = static_cast<int>(index_double); | |
223 ProfileInfoCache& cache = | |
224 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
225 int profile_count = cache.GetNumberOfProfiles(); | |
226 if (index < 0 && index >= profile_count) | |
227 return; | |
228 | |
229 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); | |
230 FilePath current_profile_path = Profile::FromWebUI(web_ui_)->GetPath(); | |
231 bool is_current_profile = | |
232 profile_path == Profile::FromWebUI(web_ui_)->GetPath(); | |
233 | |
234 DictionaryValue profile_value; | |
235 profile_value.SetString("name", cache.GetNameOfProfileAtIndex(index)); | |
236 profile_value.Set("filePath", base::CreateFilePathValue(profile_path)); | |
237 profile_value.SetBoolean("isCurrentProfile", is_current_profile); | |
238 | |
239 bool is_gaia_picture = | |
240 cache.IsUsingGAIAPictureOfProfileAtIndex(index) && | |
241 cache.GetGAIAPictureOfProfileAtIndex(index); | |
242 if (is_gaia_picture) { | |
243 gfx::Image icon = profiles::GetAvatarIconForWebUI( | |
244 cache.GetAvatarIconOfProfileAtIndex(index), true); | |
245 profile_value.SetString("iconURL", web_ui_util::GetImageDataUrl(icon)); | |
246 } else { | |
247 size_t icon_index = cache.GetAvatarIconIndexOfProfileAtIndex(index); | |
248 profile_value.SetString("iconURL", | |
249 cache.GetDefaultAvatarIconUrl(icon_index)); | |
250 } | |
251 | |
252 web_ui_->CallJavascriptFunction("ManageProfileOverlay.setProfileInfo", | |
253 profile_value); | |
254 | |
255 // Ensure that we have the most up to date GAIA picture. | |
256 if (is_current_profile) { | |
257 GAIAInfoUpdateService* service = | |
258 Profile::FromWebUI(web_ui_)->GetGAIAInfoUpdateService(); | |
259 if (service) | |
260 service->Update(); | |
261 } | |
262 } | |
263 | |
264 void ManageProfileHandler::ProfileIconSelectionChanged( | |
265 const base::ListValue* args) { | |
266 DCHECK(args); | |
267 | |
268 Value* file_path_value; | |
269 FilePath file_path; | |
270 if (!args->Get(0, &file_path_value) || | |
271 !base::GetValueAsFilePath(*file_path_value, &file_path)) { | |
272 return; | |
273 } | |
274 | |
275 // Currently this only supports editing the current profile's info. | |
276 if (file_path != Profile::FromWebUI(web_ui_)->GetPath()) | |
277 return; | |
278 | |
279 std::string icon_url; | |
280 if (!args->GetString(1, &icon_url)) | |
281 return; | |
282 | |
283 if (icon_url != gaia_picture_url_) | |
284 return; | |
285 | |
286 // If the selection is the GAIA picture then also show the GAIA name in the | |
287 // text field. | |
288 ProfileInfoCache& cache = | |
289 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
290 size_t i = cache.GetIndexOfProfileWithPath(file_path); | |
291 if (i == std::string::npos) | |
292 return; | |
293 string16 gaia_name = cache.GetGAIANameOfProfileAtIndex(i); | |
294 if (gaia_name.empty()) | |
295 return; | |
296 | |
297 StringValue gaia_name_value(gaia_name); | |
298 web_ui_->CallJavascriptFunction("ManageProfileOverlay.setProfileName", | |
299 gaia_name_value); | |
300 } | |
OLD | NEW |