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