1 // Copyright (c) 2013 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/command_line.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/prefs/scoped_user_pref_update.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/value_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/profiles/gaia_info_update_service.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/profiles/profile_info_cache.h"
21 #include "chrome/browser/profiles/profile_info_util.h"
22 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/browser/profiles/profile_metrics.h"
24 #include "chrome/browser/profiles/profile_shortcut_manager.h"
25 #include "chrome/browser/profiles/profile_window.h"
26 #include "chrome/browser/profiles/profiles_state.h"
27 #include "chrome/browser/signin/signin_manager.h"
28 #include "chrome/browser/signin/signin_manager_factory.h"
29 #include "chrome/browser/sync/profile_sync_service.h"
30 #include "chrome/browser/sync/profile_sync_service_factory.h"
31 #include "chrome/browser/ui/browser_finder.h"
32 #include "chrome/browser/ui/webui/options/options_handlers_helper.h"
33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/pref_names.h"
35 #include "content/public/browser/browser_thread.h"
36 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/web_ui.h"
38 #include "grit/generated_resources.h"
39 #include "ui/base/l10n/l10n_util.h"
40 #include "ui/base/webui/web_ui_util.h"
41
42 #if defined(ENABLE_SETTINGS_APP)
43 #include "chrome/browser/ui/app_list/app_list_service.h"
44 #include "content/public/browser/web_contents.h"
45 #endif
46
47 namespace options {
48
49 namespace {
50
51 const char kCreateProfileIconGridName[] = "create-profile-icon-grid";
52 const char kManageProfileIconGridName[] = "manage-profile-icon-grid";
53
54 // Given |args| from the WebUI, parses value 0 as a FilePath |profile_file_path|
55 // and returns true on success.
GetProfilePathFromArgs(const ListValue * args,base::FilePath * profile_file_path)56 bool GetProfilePathFromArgs(const ListValue* args,
57 base::FilePath* profile_file_path) {
58 const Value* file_path_value;
59 if (!args->Get(0, &file_path_value))
60 return false;
61 return base::GetValueAsFilePath(*file_path_value, profile_file_path);
62 }
63
64 } // namespace
65
ManageProfileHandler()66 ManageProfileHandler::ManageProfileHandler()
67 : weak_factory_(this) {
68 }
69
~ManageProfileHandler()70 ManageProfileHandler::~ManageProfileHandler() {
71 ProfileSyncService* service =
72 ProfileSyncServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()));
73 // Sync may be disabled in tests.
74 if (service)
75 service->RemoveObserver(this);
76 }
77
GetLocalizedValues(DictionaryValue * localized_strings)78 void ManageProfileHandler::GetLocalizedValues(
79 DictionaryValue* localized_strings) {
80 DCHECK(localized_strings);
81
82 static OptionsStringResource resources[] = {
83 { "manageProfilesNameLabel", IDS_PROFILES_MANAGE_NAME_LABEL },
84 { "manageProfilesDuplicateNameError",
85 IDS_PROFILES_MANAGE_DUPLICATE_NAME_ERROR },
86 { "manageProfilesIconLabel", IDS_PROFILES_MANAGE_ICON_LABEL },
87 { "manageProfilesManagedSignedInLabel",
88 IDS_PROFILES_CREATE_MANAGED_SIGNED_IN_LABEL },
89 { "manageProfilesManagedNotSignedInLabel",
90 IDS_PROFILES_CREATE_MANAGED_NOT_SIGNED_IN_LABEL },
91 { "manageProfilesManagedAccountDetailsOutOfDate",
92 IDS_PROFILES_CREATE_MANAGED_ACCOUNT_DETAILS_OUT_OF_DATE_LABEL },
93 { "manageProfilesManagedSignInAgainLink",
94 IDS_PROFILES_CREATE_MANAGED_ACCOUNT_SIGN_IN_AGAIN_LINK },
95 { "manageProfilesManagedNotSignedInLink",
96 IDS_PROFILES_CREATE_MANAGED_NOT_SIGNED_IN_LINK },
97 { "deleteProfileTitle", IDS_PROFILES_DELETE_TITLE },
98 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL },
99 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE },
100 { "deleteManagedProfileAddendum", IDS_PROFILES_DELETE_MANAGED_ADDENDUM },
101 { "createProfileTitle", IDS_PROFILES_CREATE_TITLE },
102 { "createProfileInstructions", IDS_PROFILES_CREATE_INSTRUCTIONS },
103 { "createProfileConfirm", IDS_PROFILES_CREATE_CONFIRM },
104 { "createProfileShortcutCheckbox", IDS_PROFILES_CREATE_SHORTCUT_CHECKBOX },
105 { "createProfileShortcutButton", IDS_PROFILES_CREATE_SHORTCUT_BUTTON },
106 { "removeProfileShortcutButton", IDS_PROFILES_REMOVE_SHORTCUT_BUTTON },
107 { "importExistingManagedUserLink",
108 IDS_PROFILES_IMPORT_EXISTING_MANAGED_USER_LINK },
109 { "signInToImportManagedUsers",
110 IDS_PROFILES_IMPORT_MANAGED_USER_NOT_SIGNED_IN },
111 };
112
113 RegisterStrings(localized_strings, resources, arraysize(resources));
114 RegisterTitle(localized_strings, "manageProfile",
115 IDS_PROFILES_MANAGE_TITLE);
116 RegisterTitle(localized_strings, "createProfile",
117 IDS_PROFILES_CREATE_TITLE);
118
119 localized_strings->SetBoolean("profileShortcutsEnabled",
120 ProfileShortcutManager::IsFeatureEnabled());
121
122 localized_strings->SetBoolean(
123 "allowCreateExistingManagedUsers",
124 CommandLine::ForCurrentProcess()->HasSwitch(
125 switches::kAllowCreateExistingManagedUsers));
126 }
127
InitializeHandler()128 void ManageProfileHandler::InitializeHandler() {
129 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
130 content::NotificationService::AllSources());
131
132 Profile* profile = Profile::FromWebUI(web_ui());
133 pref_change_registrar_.Init(profile->GetPrefs());
134 pref_change_registrar_.Add(
135 prefs::kManagedUserCreationAllowed,
136 base::Bind(&ManageProfileHandler::OnCreateManagedUserPrefChange,
137 base::Unretained(this)));
138 ProfileSyncService* service =
139 ProfileSyncServiceFactory::GetForProfile(profile);
140 // Sync may be disabled for tests.
141 if (service)
142 service->AddObserver(this);
143 }
144
InitializePage()145 void ManageProfileHandler::InitializePage() {
146 SendProfileNames();
147 OnCreateManagedUserPrefChange();
148 }
149
RegisterMessages()150 void ManageProfileHandler::RegisterMessages() {
151 web_ui()->RegisterMessageCallback("setProfileIconAndName",
152 base::Bind(&ManageProfileHandler::SetProfileIconAndName,
153 base::Unretained(this)));
154 web_ui()->RegisterMessageCallback("requestDefaultProfileIcons",
155 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons,
156 base::Unretained(this)));
157 web_ui()->RegisterMessageCallback("requestNewProfileDefaults",
158 base::Bind(&ManageProfileHandler::RequestNewProfileDefaults,
159 base::Unretained(this)));
160 web_ui()->RegisterMessageCallback("requestHasProfileShortcuts",
161 base::Bind(&ManageProfileHandler::RequestHasProfileShortcuts,
162 base::Unretained(this)));
163 web_ui()->RegisterMessageCallback("requestCreateProfileUpdate",
164 base::Bind(&ManageProfileHandler::RequestCreateProfileUpdate,
165 base::Unretained(this)));
166 web_ui()->RegisterMessageCallback("profileIconSelectionChanged",
167 base::Bind(&ManageProfileHandler::ProfileIconSelectionChanged,
168 base::Unretained(this)));
169 #if defined(ENABLE_SETTINGS_APP)
170 web_ui()->RegisterMessageCallback("switchAppListProfile",
171 base::Bind(&ManageProfileHandler::SwitchAppListProfile,
172 base::Unretained(this)));
173 #endif
174 web_ui()->RegisterMessageCallback("addProfileShortcut",
175 base::Bind(&ManageProfileHandler::AddProfileShortcut,
176 base::Unretained(this)));
177 web_ui()->RegisterMessageCallback("removeProfileShortcut",
178 base::Bind(&ManageProfileHandler::RemoveProfileShortcut,
179 base::Unretained(this)));
180 }
181
Uninitialize()182 void ManageProfileHandler::Uninitialize() {
183 registrar_.RemoveAll();
184 }
185
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)186 void ManageProfileHandler::Observe(
187 int type,
188 const content::NotificationSource& source,
189 const content::NotificationDetails& details) {
190 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) {
191 SendProfileNames();
192 base::StringValue value(kManageProfileIconGridName);
193 SendProfileIcons(value);
194 } else {
195 OptionsPageUIHandler::Observe(type, source, details);
196 }
197 }
198
OnStateChanged()199 void ManageProfileHandler::OnStateChanged() {
200 RequestCreateProfileUpdate(NULL);
201 }
202
RequestDefaultProfileIcons(const ListValue * args)203 void ManageProfileHandler::RequestDefaultProfileIcons(const ListValue* args) {
204 base::StringValue create_value(kCreateProfileIconGridName);
205 base::StringValue manage_value(kManageProfileIconGridName);
206 SendProfileIcons(manage_value);
207 SendProfileIcons(create_value);
208 }
209
RequestNewProfileDefaults(const ListValue * args)210 void ManageProfileHandler::RequestNewProfileDefaults(const ListValue* args) {
211 const ProfileInfoCache& cache =
212 g_browser_process->profile_manager()->GetProfileInfoCache();
213 const size_t icon_index = cache.ChooseAvatarIconIndexForNewProfile();
214
215 DictionaryValue profile_info;
216 profile_info.SetString("name", cache.ChooseNameForNewProfile(icon_index));
217 profile_info.SetString("iconURL", cache.GetDefaultAvatarIconUrl(icon_index));
218
219 web_ui()->CallJavascriptFunction(
220 "ManageProfileOverlay.receiveNewProfileDefaults", profile_info);
221 }
222
SendProfileIcons(const base::StringValue & icon_grid)223 void ManageProfileHandler::SendProfileIcons(
224 const base::StringValue& icon_grid) {
225 ListValue image_url_list;
226
227 // First add the GAIA picture if it's available.
228 const ProfileInfoCache& cache =
229 g_browser_process->profile_manager()->GetProfileInfoCache();
230 Profile* profile = Profile::FromWebUI(web_ui());
231 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
232 if (profile_index != std::string::npos) {
233 const gfx::Image* icon =
234 cache.GetGAIAPictureOfProfileAtIndex(profile_index);
235 if (icon) {
236 gfx::Image icon2 = profiles::GetAvatarIconForWebUI(*icon, true);
237 gaia_picture_url_ = webui::GetBitmapDataUrl(icon2.AsBitmap());
238 image_url_list.Append(new base::StringValue(gaia_picture_url_));
239 }
240 }
241
242 // Next add the default avatar icons.
243 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) {
244 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i);
245 image_url_list.Append(new base::StringValue(url));
246 }
247
248 web_ui()->CallJavascriptFunction(
249 "ManageProfileOverlay.receiveDefaultProfileIcons", icon_grid,
250 image_url_list);
251 }
252
SendProfileNames()253 void ManageProfileHandler::SendProfileNames() {
254 const ProfileInfoCache& cache =
255 g_browser_process->profile_manager()->GetProfileInfoCache();
256 DictionaryValue profile_name_dict;
257 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i)
258 profile_name_dict.SetBoolean(UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)),
259 true);
260
261 web_ui()->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames",
262 profile_name_dict);
263 }
264
SetProfileIconAndName(const ListValue * args)265 void ManageProfileHandler::SetProfileIconAndName(const ListValue* args) {
266 DCHECK(args);
267
268 base::FilePath profile_file_path;
269 if (!GetProfilePathFromArgs(args, &profile_file_path))
270 return;
271
272 ProfileInfoCache& cache =
273 g_browser_process->profile_manager()->GetProfileInfoCache();
274 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
275 if (profile_index == std::string::npos)
276 return;
277
278 Profile* profile =
279 g_browser_process->profile_manager()->GetProfile(profile_file_path);
280 if (!profile)
281 return;
282
283 std::string icon_url;
284 if (!args->GetString(1, &icon_url))
285 return;
286
287 // Metrics logging variable.
288 bool previously_using_gaia_icon =
289 cache.IsUsingGAIAPictureOfProfileAtIndex(profile_index);
290
291 size_t new_icon_index;
292 if (icon_url == gaia_picture_url_) {
293 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true);
294 if (!previously_using_gaia_icon) {
295 // Only log if they changed to the GAIA photo.
296 // Selection of GAIA photo as avatar is logged as part of the function
297 // below.
298 ProfileMetrics::LogProfileSwitchGaia(ProfileMetrics::GAIA_OPT_IN);
299 }
300 } else if (cache.IsDefaultAvatarIconUrl(icon_url, &new_icon_index)) {
301 ProfileMetrics::LogProfileAvatarSelection(new_icon_index);
302 PrefService* pref_service = profile->GetPrefs();
303 // Updating the profile preference will cause the cache to be updated for
304 // this preference.
305 pref_service->SetInteger(prefs::kProfileAvatarIndex, new_icon_index);
306 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, false);
307 }
308 ProfileMetrics::LogProfileUpdate(profile_file_path);
309
310 if (profile->IsManaged())
311 return;
312
313 base::string16 new_profile_name;
314 if (!args->GetString(2, &new_profile_name))
315 return;
316
317 profiles::UpdateProfileName(profile, new_profile_name);
318 }
319
320 #if defined(ENABLE_SETTINGS_APP)
SwitchAppListProfile(const ListValue * args)321 void ManageProfileHandler::SwitchAppListProfile(const ListValue* args) {
322 DCHECK(args);
323 DCHECK(profiles::IsMultipleProfilesEnabled());
324
325 const Value* file_path_value;
326 base::FilePath profile_file_path;
327 if (!args->Get(0, &file_path_value) ||
328 !base::GetValueAsFilePath(*file_path_value, &profile_file_path))
329 return;
330
331 AppListService* app_list_service = AppListService::Get(
332 options::helper::GetDesktopType(web_ui()));
333 app_list_service->SetProfilePath(profile_file_path);
334 app_list_service->Show();
335
336 // Close the settings app, since it will now be for the wrong profile.
337 web_ui()->GetWebContents()->Close();
338 }
339 #endif // defined(ENABLE_SETTINGS_APP)
340
ProfileIconSelectionChanged(const base::ListValue * args)341 void ManageProfileHandler::ProfileIconSelectionChanged(
342 const base::ListValue* args) {
343 DCHECK(args);
344
345 base::FilePath profile_file_path;
346 if (!GetProfilePathFromArgs(args, &profile_file_path))
347 return;
348
349 // Currently this only supports editing the current profile's info.
350 if (profile_file_path != Profile::FromWebUI(web_ui())->GetPath())
351 return;
352
353 std::string icon_url;
354 if (!args->GetString(1, &icon_url))
355 return;
356
357 if (icon_url != gaia_picture_url_)
358 return;
359
360 // If the selection is the GAIA picture then also show the profile name in the
361 // text field. This will display either the GAIA given name, if available,
362 // or the first name.
363 ProfileInfoCache& cache =
364 g_browser_process->profile_manager()->GetProfileInfoCache();
365 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
366 if (profile_index == std::string::npos)
367 return;
368 base::string16 gaia_name = cache.GetNameOfProfileAtIndex(profile_index);
369 if (gaia_name.empty())
370 return;
371
372 StringValue gaia_name_value(gaia_name);
373 web_ui()->CallJavascriptFunction("ManageProfileOverlay.setProfileName",
374 gaia_name_value);
375 }
376
RequestHasProfileShortcuts(const ListValue * args)377 void ManageProfileHandler::RequestHasProfileShortcuts(const ListValue* args) {
378 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
379 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
380
381 base::FilePath profile_file_path;
382 if (!GetProfilePathFromArgs(args, &profile_file_path))
383 return;
384
385 const ProfileInfoCache& cache =
386 g_browser_process->profile_manager()->GetProfileInfoCache();
387 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
388 if (profile_index == std::string::npos)
389 return;
390
391 const base::FilePath profile_path =
392 cache.GetPathOfProfileAtIndex(profile_index);
393 ProfileShortcutManager* shortcut_manager =
394 g_browser_process->profile_manager()->profile_shortcut_manager();
395 shortcut_manager->HasProfileShortcuts(
396 profile_path, base::Bind(&ManageProfileHandler::OnHasProfileShortcuts,
397 weak_factory_.GetWeakPtr()));
398 }
399
RequestCreateProfileUpdate(const base::ListValue * args)400 void ManageProfileHandler::RequestCreateProfileUpdate(
401 const base::ListValue* args) {
402 Profile* profile = Profile::FromWebUI(web_ui());
403 SigninManagerBase* manager =
404 SigninManagerFactory::GetForProfile(profile);
405 base::string16 username = UTF8ToUTF16(manager->GetAuthenticatedUsername());
406 ProfileSyncService* service =
407 ProfileSyncServiceFactory::GetForProfile(profile);
408 GoogleServiceAuthError::State state = service->GetAuthError().state();
409 bool has_error = (state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
410 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP ||
411 state == GoogleServiceAuthError::ACCOUNT_DELETED ||
412 state == GoogleServiceAuthError::ACCOUNT_DISABLED);
413 web_ui()->CallJavascriptFunction("CreateProfileOverlay.updateSignedInStatus",
414 base::StringValue(username),
415 base::FundamentalValue(has_error));
416 OnCreateManagedUserPrefChange();
417 }
418
OnCreateManagedUserPrefChange()419 void ManageProfileHandler::OnCreateManagedUserPrefChange() {
420 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
421 base::FundamentalValue allowed(
422 prefs->GetBoolean(prefs::kManagedUserCreationAllowed));
423 web_ui()->CallJavascriptFunction(
424 "CreateProfileOverlay.updateManagedUsersAllowed", allowed);
425 }
426
OnHasProfileShortcuts(bool has_shortcuts)427 void ManageProfileHandler::OnHasProfileShortcuts(bool has_shortcuts) {
428 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
429
430 const base::FundamentalValue has_shortcuts_value(has_shortcuts);
431 web_ui()->CallJavascriptFunction(
432 "ManageProfileOverlay.receiveHasProfileShortcuts", has_shortcuts_value);
433 }
434
AddProfileShortcut(const base::ListValue * args)435 void ManageProfileHandler::AddProfileShortcut(const base::ListValue* args) {
436 base::FilePath profile_file_path;
437 if (!GetProfilePathFromArgs(args, &profile_file_path))
438 return;
439
440 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
441 ProfileShortcutManager* shortcut_manager =
442 g_browser_process->profile_manager()->profile_shortcut_manager();
443 DCHECK(shortcut_manager);
444
445 shortcut_manager->CreateProfileShortcut(profile_file_path);
446
447 // Update the UI buttons.
448 OnHasProfileShortcuts(true);
449 }
450
RemoveProfileShortcut(const base::ListValue * args)451 void ManageProfileHandler::RemoveProfileShortcut(const base::ListValue* args) {
452 base::FilePath profile_file_path;
453 if (!GetProfilePathFromArgs(args, &profile_file_path))
454 return;
455
456 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
457 ProfileShortcutManager* shortcut_manager =
458 g_browser_process->profile_manager()->profile_shortcut_manager();
459 DCHECK(shortcut_manager);
460
461 shortcut_manager->RemoveProfileShortcuts(profile_file_path);
462
463 // Update the UI buttons.
464 OnHasProfileShortcuts(false);
465 }
466
467 } // namespace options
468