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/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/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 namespace options { | |
29 | |
30 namespace { | |
31 | |
32 const char kCreateProfileIconGridName[] = "create-profile-icon-grid"; | |
33 const char kManageProfileIconGridName[] = "manage-profile-icon-grid"; | |
34 | |
35 } // namespace | |
36 | |
37 ManageProfileHandler::ManageProfileHandler() { | |
38 } | |
39 | |
40 ManageProfileHandler::~ManageProfileHandler() { | |
41 } | |
42 | |
43 void ManageProfileHandler::GetLocalizedValues( | |
44 DictionaryValue* localized_strings) { | |
45 DCHECK(localized_strings); | |
46 | |
47 static OptionsStringResource resources[] = { | |
48 { "manageProfilesNameLabel", IDS_PROFILES_MANAGE_NAME_LABEL }, | |
49 { "manageProfilesDuplicateNameError", | |
50 IDS_PROFILES_MANAGE_DUPLICATE_NAME_ERROR }, | |
51 { "manageProfilesIconLabel", IDS_PROFILES_MANAGE_ICON_LABEL }, | |
52 { "deleteProfileTitle", IDS_PROFILES_DELETE_TITLE }, | |
53 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL }, | |
54 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE }, | |
55 { "createProfileTitle", IDS_PROFILES_CREATE_TITLE }, | |
56 { "createProfileInstructions", IDS_PROFILES_CREATE_INSTRUCTIONS }, | |
57 { "createProfileConfirm", IDS_PROFILES_CREATE_CONFIRM }, | |
58 { "createProfileShortcut", IDS_PROFILES_CREATE_SHORTCUT_CHKBOX }, | |
59 }; | |
60 | |
61 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
62 RegisterTitle(localized_strings, "manageProfile", | |
63 IDS_PROFILES_MANAGE_TITLE); | |
64 } | |
65 | |
66 void ManageProfileHandler::InitializeHandler() { | |
67 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
68 content::NotificationService::AllSources()); | |
69 } | |
70 | |
71 void ManageProfileHandler::InitializePage() { | |
72 SendProfileNames(); | |
73 } | |
74 | |
75 void ManageProfileHandler::RegisterMessages() { | |
76 web_ui()->RegisterMessageCallback("setProfileNameAndIcon", | |
77 base::Bind(&ManageProfileHandler::SetProfileNameAndIcon, | |
78 base::Unretained(this))); | |
79 web_ui()->RegisterMessageCallback("deleteProfile", | |
80 base::Bind(&ManageProfileHandler::DeleteProfile, | |
81 base::Unretained(this))); | |
82 web_ui()->RegisterMessageCallback("requestDefaultProfileIcons", | |
83 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons, | |
84 base::Unretained(this))); | |
85 web_ui()->RegisterMessageCallback("profileIconSelectionChanged", | |
86 base::Bind(&ManageProfileHandler::ProfileIconSelectionChanged, | |
87 base::Unretained(this))); | |
88 } | |
89 | |
90 void ManageProfileHandler::Observe( | |
91 int type, | |
92 const content::NotificationSource& source, | |
93 const content::NotificationDetails& details) { | |
94 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) { | |
95 SendProfileNames(); | |
96 base::StringValue value(kManageProfileIconGridName); | |
97 SendProfileIcons(value); | |
98 } else { | |
99 OptionsPageUIHandler::Observe(type, source, details); | |
100 } | |
101 } | |
102 | |
103 void ManageProfileHandler::RequestDefaultProfileIcons(const ListValue* args) { | |
104 base::StringValue create_value(kCreateProfileIconGridName); | |
105 base::StringValue manage_value(kManageProfileIconGridName); | |
106 SendProfileIcons(manage_value); | |
107 SendProfileIcons(create_value); | |
108 } | |
109 | |
110 void ManageProfileHandler::SendProfileIcons( | |
111 const base::StringValue& icon_grid) { | |
112 ListValue image_url_list; | |
113 | |
114 // First add the GAIA picture if it's available. | |
115 ProfileInfoCache& cache = | |
116 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
117 Profile* profile = Profile::FromWebUI(web_ui()); | |
118 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); | |
119 if (profile_index != std::string::npos) { | |
120 const gfx::Image* icon = | |
121 cache.GetGAIAPictureOfProfileAtIndex(profile_index); | |
122 if (icon) { | |
123 gfx::Image icon2 = profiles::GetAvatarIconForWebUI(*icon, true); | |
124 gaia_picture_url_ = web_ui_util::GetImageDataUrl(*icon2.ToImageSkia()); | |
125 image_url_list.Append(Value::CreateStringValue(gaia_picture_url_)); | |
126 } | |
127 } | |
128 | |
129 // Next add the default avatar icons. | |
130 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { | |
131 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); | |
132 image_url_list.Append(Value::CreateStringValue(url)); | |
133 } | |
134 | |
135 web_ui()->CallJavascriptFunction( | |
136 "ManageProfileOverlay.receiveDefaultProfileIcons", icon_grid, | |
137 image_url_list); | |
138 } | |
139 | |
140 void ManageProfileHandler::SendProfileNames() { | |
141 ProfileInfoCache& cache = | |
142 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
143 DictionaryValue profile_name_dict; | |
144 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i) | |
145 profile_name_dict.SetBoolean(UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)), | |
146 true); | |
147 | |
148 web_ui()->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames", | |
149 profile_name_dict); | |
150 } | |
151 | |
152 void ManageProfileHandler::SetProfileNameAndIcon(const ListValue* args) { | |
153 DCHECK(args); | |
154 | |
155 const Value* file_path_value; | |
156 FilePath profile_file_path; | |
157 if (!args->Get(0, &file_path_value) || | |
158 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | |
159 return; | |
160 | |
161 ProfileInfoCache& cache = | |
162 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
163 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
164 if (profile_index == std::string::npos) | |
165 return; | |
166 | |
167 Profile* profile = | |
168 g_browser_process->profile_manager()->GetProfile(profile_file_path); | |
169 if (!profile) | |
170 return; | |
171 | |
172 string16 new_profile_name; | |
173 if (!args->GetString(1, &new_profile_name)) | |
174 return; | |
175 | |
176 if (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index)) { | |
177 // Set the profile to use the GAIA name as the profile name. Note, this | |
178 // is a little weird if the user typed their GAIA name manually but | |
179 // it's not a big deal. | |
180 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true); | |
181 // Using the GAIA name as the profile name can invalidate the profile index. | |
182 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
183 if (profile_index == std::string::npos) | |
184 return; | |
185 } else { | |
186 PrefService* pref_service = profile->GetPrefs(); | |
187 // Updating the profile preference will cause the cache to be updated for | |
188 // this preference. | |
189 pref_service->SetString(prefs::kProfileName, UTF16ToUTF8(new_profile_name)); | |
190 | |
191 // Changing the profile name can invalidate the profile index. | |
192 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
193 if (profile_index == std::string::npos) | |
194 return; | |
195 | |
196 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, false); | |
197 // Unsetting the GAIA name as the profile name can invalidate the profile | |
198 // index. | |
199 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | |
200 if (profile_index == std::string::npos) | |
201 return; | |
202 } | |
203 | |
204 std::string icon_url; | |
205 if (!args->GetString(2, &icon_url)) | |
206 return; | |
207 | |
208 // Metrics logging variable. | |
209 bool previously_using_gaia_icon = | |
210 cache.IsUsingGAIANameOfProfileAtIndex(profile_index); | |
211 | |
212 size_t new_icon_index; | |
213 if (icon_url == gaia_picture_url_) { | |
214 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true); | |
215 if (!previously_using_gaia_icon) { | |
216 // Only log if they changed to the GAIA photo. | |
217 // Selection of GAIA photo as avatar is logged as part of the function | |
218 // below. | |
219 ProfileMetrics::LogProfileSwitchGaia(ProfileMetrics::GAIA_OPT_IN); | |
220 } | |
221 } else if (cache.IsDefaultAvatarIconUrl(icon_url, &new_icon_index)) { | |
222 ProfileMetrics::LogProfileAvatarSelection(new_icon_index); | |
223 PrefService* pref_service = profile->GetPrefs(); | |
224 // Updating the profile preference will cause the cache to be updated for | |
225 // this preference. | |
226 pref_service->SetInteger(prefs::kProfileAvatarIndex, new_icon_index); | |
227 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, false); | |
228 } | |
229 | |
230 ProfileMetrics::LogProfileUpdate(profile_file_path); | |
231 } | |
232 | |
233 void ManageProfileHandler::DeleteProfile(const ListValue* args) { | |
234 DCHECK(args); | |
235 // This handler could have been called in managed mode, for example because | |
236 // the user fiddled with the web inspector. Silently return in this case. | |
237 if (!ProfileManager::IsMultipleProfilesEnabled()) | |
238 return; | |
239 | |
240 ProfileMetrics::LogProfileDeleteUser(ProfileMetrics::PROFILE_DELETED); | |
241 | |
242 const Value* file_path_value; | |
243 FilePath profile_file_path; | |
244 if (!args->Get(0, &file_path_value) || | |
245 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | |
246 return; | |
247 | |
248 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | |
249 profile_file_path); | |
250 } | |
251 | |
252 void ManageProfileHandler::ProfileIconSelectionChanged( | |
253 const base::ListValue* args) { | |
254 DCHECK(args); | |
255 | |
256 const Value* file_path_value; | |
257 FilePath file_path; | |
258 if (!args->Get(0, &file_path_value) || | |
259 !base::GetValueAsFilePath(*file_path_value, &file_path)) { | |
260 return; | |
261 } | |
262 | |
263 // Currently this only supports editing the current profile's info. | |
264 if (file_path != Profile::FromWebUI(web_ui())->GetPath()) | |
265 return; | |
266 | |
267 std::string icon_url; | |
268 if (!args->GetString(1, &icon_url)) | |
269 return; | |
270 | |
271 if (icon_url != gaia_picture_url_) | |
272 return; | |
273 | |
274 // If the selection is the GAIA picture then also show the GAIA name in the | |
275 // text field. | |
276 ProfileInfoCache& cache = | |
277 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
278 size_t i = cache.GetIndexOfProfileWithPath(file_path); | |
279 if (i == std::string::npos) | |
280 return; | |
281 string16 gaia_name = cache.GetGAIANameOfProfileAtIndex(i); | |
282 if (gaia_name.empty()) | |
283 return; | |
284 | |
285 StringValue gaia_name_value(gaia_name); | |
286 web_ui()->CallJavascriptFunction("ManageProfileOverlay.setProfileName", | |
287 gaia_name_value); | |
288 } | |
289 | |
290 } // namespace options | |
OLD | NEW |