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.
5 #include "chrome/browser/ui/webui/options/manage_profile_handler.h"
8 #include "base/bind_helpers.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/prefs/scoped_user_pref_update.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.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_avatar_icon_util.h"
21 #include "chrome/browser/profiles/profile_info_cache.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_factory.h"
28 #include "chrome/browser/sync/profile_sync_service.h"
29 #include "chrome/browser/sync/profile_sync_service_factory.h"
30 #include "chrome/browser/ui/browser_finder.h"
31 #include "chrome/browser/ui/webui/options/options_handlers_helper.h"
32 #include "chrome/common/pref_names.h"
33 #include "chrome/common/url_constants.h"
34 #include "chrome/grit/generated_resources.h"
35 #include "chrome/grit/google_chrome_strings.h"
36 #include "components/signin/core/browser/signin_manager.h"
37 #include "components/signin/core/common/profile_management_switches.h"
38 #include "content/public/browser/browser_thread.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/web_ui.h"
41 #include "google_apis/gaia/gaia_auth_util.h"
42 #include "ui/base/l10n/l10n_util.h"
43 #include "ui/base/webui/web_ui_util.h"
45 #if defined(ENABLE_SETTINGS_APP)
46 #include "chrome/browser/ui/app_list/app_list_service.h"
47 #include "content/public/browser/web_contents.h"
54 const char kCreateProfileIdentifier
[] = "create";
55 const char kManageProfileIdentifier
[] = "manage";
57 // Given |args| from the WebUI, parses value 0 as a FilePath |profile_file_path|
58 // and returns true on success.
59 bool GetProfilePathFromArgs(const base::ListValue
* args
,
60 base::FilePath
* profile_file_path
) {
61 const base::Value
* file_path_value
;
62 if (!args
->Get(0, &file_path_value
))
64 return base::GetValueAsFilePath(*file_path_value
, profile_file_path
);
69 ManageProfileHandler::ManageProfileHandler()
70 : weak_factory_(this) {
73 ManageProfileHandler::~ManageProfileHandler() {
74 ProfileSyncService
* service
=
75 ProfileSyncServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()));
76 // Sync may be disabled in tests.
78 service
->RemoveObserver(this);
81 void ManageProfileHandler::GetLocalizedValues(
82 base::DictionaryValue
* localized_strings
) {
83 DCHECK(localized_strings
);
85 static OptionsStringResource resources
[] = {
86 { "manageProfilesNameLabel", IDS_PROFILES_MANAGE_NAME_LABEL
},
87 { "manageProfilesDuplicateNameError",
88 IDS_PROFILES_MANAGE_DUPLICATE_NAME_ERROR
},
89 { "manageProfilesIconLabel", IDS_PROFILES_MANAGE_ICON_LABEL
},
90 { "manageProfilesExistingSupervisedUser",
91 IDS_PROFILES_CREATE_EXISTING_SUPERVISED_USER_ERROR
},
92 { "manageProfilesSupervisedSignedInLabel",
93 IDS_PROFILES_CREATE_SUPERVISED_SIGNED_IN_LABEL
},
94 { "manageProfilesSupervisedNotSignedIn",
95 IDS_PROFILES_CREATE_SUPERVISED_NOT_SIGNED_IN_HTML
},
96 { "manageProfilesSupervisedAccountDetailsOutOfDate",
97 IDS_PROFILES_CREATE_SUPERVISED_ACCOUNT_DETAILS_OUT_OF_DATE_LABEL
},
98 { "manageProfilesSupervisedSignInAgainLink",
99 IDS_PROFILES_CREATE_SUPERVISED_SIGN_IN_AGAIN_LINK
},
100 { "manageProfilesConfirm", IDS_SAVE
},
101 { "deleteProfileTitle", IDS_PROFILES_DELETE_TITLE
},
102 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL
},
103 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE
},
104 { "deleteSupervisedProfileAddendum",
105 IDS_PROFILES_DELETE_SUPERVISED_ADDENDUM
},
106 { "disconnectManagedProfileTitle",
107 IDS_PROFILES_DISCONNECT_MANAGED_PROFILE_TITLE
},
108 { "disconnectManagedProfileOK",
109 IDS_PROFILES_DISCONNECT_MANAGED_PROFILE_OK_BUTTON_LABEL
},
110 { "createProfileTitle", IDS_PROFILES_CREATE_TITLE
},
111 { "createProfileInstructions", IDS_PROFILES_CREATE_INSTRUCTIONS
},
112 { "createProfileConfirm", IDS_ADD
},
113 { "createProfileShortcutCheckbox", IDS_PROFILES_CREATE_SHORTCUT_CHECKBOX
},
114 { "createProfileShortcutButton", IDS_PROFILES_CREATE_SHORTCUT_BUTTON
},
115 { "removeProfileShortcutButton", IDS_PROFILES_REMOVE_SHORTCUT_BUTTON
},
116 { "importExistingSupervisedUserLink",
117 IDS_PROFILES_IMPORT_EXISTING_SUPERVISED_USER_LINK
},
120 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
121 RegisterTitle(localized_strings
, "manageProfile", IDS_PROFILES_MANAGE_TITLE
);
122 RegisterTitle(localized_strings
, "createProfile", IDS_PROFILES_CREATE_TITLE
);
124 localized_strings
->SetBoolean("newAvatarMenuEnabled",
125 switches::IsNewAvatarMenu());
126 localized_strings
->SetBoolean("profileShortcutsEnabled",
127 ProfileShortcutManager::IsFeatureEnabled());
129 GenerateSignedinUserSpecificStrings(localized_strings
);
132 void ManageProfileHandler::InitializeHandler() {
133 registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED
,
134 content::NotificationService::AllSources());
136 Profile
* profile
= Profile::FromWebUI(web_ui());
137 pref_change_registrar_
.Init(profile
->GetPrefs());
138 pref_change_registrar_
.Add(
139 prefs::kSupervisedUserCreationAllowed
,
140 base::Bind(&ManageProfileHandler::OnCreateSupervisedUserPrefChange
,
141 base::Unretained(this)));
142 ProfileSyncService
* service
=
143 ProfileSyncServiceFactory::GetForProfile(profile
);
144 // Sync may be disabled for tests.
146 service
->AddObserver(this);
149 void ManageProfileHandler::InitializePage() {
150 SendExistingProfileNames();
151 OnCreateSupervisedUserPrefChange();
154 void ManageProfileHandler::RegisterMessages() {
155 web_ui()->RegisterMessageCallback("setProfileIconAndName",
156 base::Bind(&ManageProfileHandler::SetProfileIconAndName
,
157 base::Unretained(this)));
158 web_ui()->RegisterMessageCallback("requestDefaultProfileIcons",
159 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons
,
160 base::Unretained(this)));
161 web_ui()->RegisterMessageCallback("requestNewProfileDefaults",
162 base::Bind(&ManageProfileHandler::RequestNewProfileDefaults
,
163 base::Unretained(this)));
164 web_ui()->RegisterMessageCallback("requestHasProfileShortcuts",
165 base::Bind(&ManageProfileHandler::RequestHasProfileShortcuts
,
166 base::Unretained(this)));
167 web_ui()->RegisterMessageCallback("requestCreateProfileUpdate",
168 base::Bind(&ManageProfileHandler::RequestCreateProfileUpdate
,
169 base::Unretained(this)));
170 web_ui()->RegisterMessageCallback("profileIconSelectionChanged",
171 base::Bind(&ManageProfileHandler::ProfileIconSelectionChanged
,
172 base::Unretained(this)));
173 #if defined(ENABLE_SETTINGS_APP)
174 web_ui()->RegisterMessageCallback("switchAppListProfile",
175 base::Bind(&ManageProfileHandler::SwitchAppListProfile
,
176 base::Unretained(this)));
178 web_ui()->RegisterMessageCallback("addProfileShortcut",
179 base::Bind(&ManageProfileHandler::AddProfileShortcut
,
180 base::Unretained(this)));
181 web_ui()->RegisterMessageCallback("removeProfileShortcut",
182 base::Bind(&ManageProfileHandler::RemoveProfileShortcut
,
183 base::Unretained(this)));
184 web_ui()->RegisterMessageCallback("refreshGaiaPicture",
185 base::Bind(&ManageProfileHandler::RefreshGaiaPicture
,
186 base::Unretained(this)));
187 web_ui()->RegisterMessageCallback(
188 "showDisconnectManagedProfileDialog",
189 base::Bind(&ManageProfileHandler::ShowDisconnectManagedProfileDialog
,
190 base::Unretained(this)));
193 void ManageProfileHandler::Uninitialize() {
194 registrar_
.RemoveAll();
197 void ManageProfileHandler::Observe(
199 const content::NotificationSource
& source
,
200 const content::NotificationDetails
& details
) {
201 if (type
== chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED
) {
202 SendExistingProfileNames();
203 base::StringValue
value(kManageProfileIdentifier
);
204 SendProfileIconsAndNames(value
);
208 void ManageProfileHandler::OnStateChanged() {
209 RequestCreateProfileUpdate(NULL
);
212 void ManageProfileHandler::GenerateSignedinUserSpecificStrings(
213 base::DictionaryValue
* dictionary
) {
214 std::string username
;
215 std::string domain_name
;
217 #if !defined(OS_CHROMEOS)
218 Profile
* profile
= Profile::FromWebUI(web_ui());
220 SigninManagerBase
* manager
= SigninManagerFactory::GetForProfile(profile
);
222 username
= manager
->GetAuthenticatedUsername();
223 // If there is no one logged in or if the profile name is empty then the
224 // domain name is empty. This happens in browser tests.
225 if (!username
.empty()) {
226 domain_name
= "<span id=disconnect-managed-profile-domain-name>" +
227 gaia::ExtractDomainName(username
) + "</span>";
232 dictionary
->SetString(
233 "disconnectManagedProfileDomainInformation",
234 l10n_util::GetStringFUTF16(
235 IDS_PROFILES_DISCONNECT_MANAGED_PROFILE_DOMAIN_INFORMATION
,
236 base::ASCIIToUTF16(domain_name
)));
238 dictionary
->SetString(
239 "disconnectManagedProfileText",
240 l10n_util::GetStringFUTF16(
241 IDS_PROFILES_DISCONNECT_MANAGED_PROFILE_TEXT
,
242 base::UTF8ToUTF16(username
),
243 base::UTF8ToUTF16(chrome::kSyncGoogleDashboardURL
)));
246 void ManageProfileHandler::RequestDefaultProfileIcons(
247 const base::ListValue
* args
) {
249 bool ok
= args
->GetString(0, &mode
);
251 DCHECK(mode
== kCreateProfileIdentifier
|| mode
== kManageProfileIdentifier
);
253 base::StringValue
value(mode
);
254 SendProfileIconsAndNames(value
);
258 void ManageProfileHandler::RequestNewProfileDefaults(
259 const base::ListValue
* args
) {
260 const ProfileInfoCache
& cache
=
261 g_browser_process
->profile_manager()->GetProfileInfoCache();
262 const size_t icon_index
= cache
.ChooseAvatarIconIndexForNewProfile();
264 base::DictionaryValue profile_info
;
265 profile_info
.SetString("name", cache
.ChooseNameForNewProfile(icon_index
));
266 profile_info
.SetString("iconURL",
267 profiles::GetDefaultAvatarIconUrl(icon_index
));
269 web_ui()->CallJavascriptFunction(
270 "ManageProfileOverlay.receiveNewProfileDefaults", profile_info
);
273 void ManageProfileHandler::SendProfileIconsAndNames(
274 const base::StringValue
& mode
) {
275 base::ListValue image_url_list
;
276 base::ListValue default_name_list
;
278 // First add the GAIA picture if it's available.
279 const ProfileInfoCache
& cache
=
280 g_browser_process
->profile_manager()->GetProfileInfoCache();
281 Profile
* profile
= Profile::FromWebUI(web_ui());
282 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
283 if (profile_index
!= std::string::npos
) {
284 const gfx::Image
* icon
=
285 cache
.GetGAIAPictureOfProfileAtIndex(profile_index
);
287 gfx::Image icon2
= profiles::GetAvatarIconForWebUI(*icon
, true);
288 gaia_picture_url_
= webui::GetBitmapDataUrl(icon2
.AsBitmap());
289 image_url_list
.AppendString(gaia_picture_url_
);
290 default_name_list
.AppendString(std::string());
294 // Next add the default avatar icons and names.
295 for (size_t i
= 0; i
< profiles::GetDefaultAvatarIconCount(); i
++) {
296 std::string url
= profiles::GetDefaultAvatarIconUrl(i
);
297 image_url_list
.AppendString(url
);
298 default_name_list
.AppendString(cache
.ChooseNameForNewProfile(i
));
301 web_ui()->CallJavascriptFunction(
302 "ManageProfileOverlay.receiveDefaultProfileIconsAndNames", mode
,
303 image_url_list
, default_name_list
);
306 void ManageProfileHandler::SendExistingProfileNames() {
307 const ProfileInfoCache
& cache
=
308 g_browser_process
->profile_manager()->GetProfileInfoCache();
309 base::DictionaryValue profile_name_dict
;
310 for (size_t i
= 0, e
= cache
.GetNumberOfProfiles(); i
< e
; ++i
) {
311 profile_name_dict
.SetBoolean(
312 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(i
)), true);
315 web_ui()->CallJavascriptFunction(
316 "ManageProfileOverlay.receiveExistingProfileNames", profile_name_dict
);
319 void ManageProfileHandler::ShowDisconnectManagedProfileDialog(
320 const base::ListValue
* args
) {
321 base::DictionaryValue replacements
;
322 GenerateSignedinUserSpecificStrings(&replacements
);
323 web_ui()->CallJavascriptFunction(
324 "ManageProfileOverlay.showDisconnectManagedProfileDialog", replacements
);
327 void ManageProfileHandler::SetProfileIconAndName(const base::ListValue
* args
) {
330 base::FilePath profile_file_path
;
331 if (!GetProfilePathFromArgs(args
, &profile_file_path
))
335 g_browser_process
->profile_manager()->GetProfile(profile_file_path
);
339 std::string icon_url
;
340 if (!args
->GetString(1, &icon_url
))
343 PrefService
* pref_service
= profile
->GetPrefs();
344 // Updating the profile preferences will cause the cache to be updated.
346 // Metrics logging variable.
347 bool previously_using_gaia_icon
=
348 pref_service
->GetBoolean(prefs::kProfileUsingGAIAAvatar
);
350 size_t new_icon_index
;
351 if (icon_url
== gaia_picture_url_
) {
352 pref_service
->SetBoolean(prefs::kProfileUsingDefaultAvatar
, false);
353 pref_service
->SetBoolean(prefs::kProfileUsingGAIAAvatar
, true);
354 if (!previously_using_gaia_icon
) {
355 // Only log if they changed to the GAIA photo.
356 // Selection of GAIA photo as avatar is logged as part of the function
358 ProfileMetrics::LogProfileSwitchGaia(ProfileMetrics::GAIA_OPT_IN
);
360 } else if (profiles::IsDefaultAvatarIconUrl(icon_url
, &new_icon_index
)) {
361 ProfileMetrics::LogProfileAvatarSelection(new_icon_index
);
362 pref_service
->SetInteger(prefs::kProfileAvatarIndex
, new_icon_index
);
363 pref_service
->SetBoolean(prefs::kProfileUsingDefaultAvatar
, false);
364 pref_service
->SetBoolean(prefs::kProfileUsingGAIAAvatar
, false);
366 // Only default avatars and Gaia account photos are supported.
369 ProfileMetrics::LogProfileUpdate(profile_file_path
);
371 if (profile
->IsSupervised())
374 base::string16 new_profile_name
;
375 if (!args
->GetString(2, &new_profile_name
))
378 base::TrimWhitespace(new_profile_name
, base::TRIM_ALL
, &new_profile_name
);
379 CHECK(!new_profile_name
.empty());
380 profiles::UpdateProfileName(profile
, new_profile_name
);
383 #if defined(ENABLE_SETTINGS_APP)
384 void ManageProfileHandler::SwitchAppListProfile(const base::ListValue
* args
) {
386 DCHECK(profiles::IsMultipleProfilesEnabled());
388 const base::Value
* file_path_value
;
389 base::FilePath profile_file_path
;
390 if (!args
->Get(0, &file_path_value
) ||
391 !base::GetValueAsFilePath(*file_path_value
, &profile_file_path
))
394 AppListService
* app_list_service
= AppListService::Get(
395 options::helper::GetDesktopType(web_ui()));
396 app_list_service
->SetProfilePath(profile_file_path
);
397 app_list_service
->Show();
399 // Close the settings app, since it will now be for the wrong profile.
400 web_ui()->GetWebContents()->Close();
402 #endif // defined(ENABLE_SETTINGS_APP)
404 void ManageProfileHandler::ProfileIconSelectionChanged(
405 const base::ListValue
* args
) {
408 base::FilePath profile_file_path
;
409 if (!GetProfilePathFromArgs(args
, &profile_file_path
))
412 // Currently this only supports editing the current profile's info.
413 if (profile_file_path
!= Profile::FromWebUI(web_ui())->GetPath())
416 std::string icon_url
;
417 if (!args
->GetString(1, &icon_url
))
420 if (icon_url
!= gaia_picture_url_
)
423 // If the selection is the GAIA picture then also show the profile name in the
424 // text field. This will display either the GAIA given name, if available,
425 // or the first name.
426 ProfileInfoCache
& cache
=
427 g_browser_process
->profile_manager()->GetProfileInfoCache();
428 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile_file_path
);
429 if (profile_index
== std::string::npos
)
431 base::string16 gaia_name
= cache
.GetNameOfProfileAtIndex(profile_index
);
432 if (gaia_name
.empty())
435 base::StringValue
gaia_name_value(gaia_name
);
436 base::StringValue
mode_value(kManageProfileIdentifier
);
437 web_ui()->CallJavascriptFunction("ManageProfileOverlay.setProfileName",
438 gaia_name_value
, mode_value
);
441 void ManageProfileHandler::RequestHasProfileShortcuts(
442 const base::ListValue
* args
) {
443 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
444 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
446 base::FilePath profile_file_path
;
447 if (!GetProfilePathFromArgs(args
, &profile_file_path
))
450 const ProfileInfoCache
& cache
=
451 g_browser_process
->profile_manager()->GetProfileInfoCache();
452 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile_file_path
);
453 if (profile_index
== std::string::npos
)
456 // Don't show the add/remove desktop shortcut button in the single user case.
457 if (cache
.GetNumberOfProfiles() <= 1)
460 const base::FilePath profile_path
=
461 cache
.GetPathOfProfileAtIndex(profile_index
);
462 ProfileShortcutManager
* shortcut_manager
=
463 g_browser_process
->profile_manager()->profile_shortcut_manager();
464 shortcut_manager
->HasProfileShortcuts(
465 profile_path
, base::Bind(&ManageProfileHandler::OnHasProfileShortcuts
,
466 weak_factory_
.GetWeakPtr()));
469 void ManageProfileHandler::RequestCreateProfileUpdate(
470 const base::ListValue
* args
) {
471 Profile
* profile
= Profile::FromWebUI(web_ui());
472 SigninManagerBase
* manager
=
473 SigninManagerFactory::GetForProfile(profile
);
474 base::string16 username
=
475 base::UTF8ToUTF16(manager
->GetAuthenticatedUsername());
476 ProfileSyncService
* service
=
477 ProfileSyncServiceFactory::GetForProfile(profile
);
478 GoogleServiceAuthError::State state
= service
->GetAuthError().state();
479 bool has_error
= (state
== GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
||
480 state
== GoogleServiceAuthError::USER_NOT_SIGNED_UP
||
481 state
== GoogleServiceAuthError::ACCOUNT_DELETED
||
482 state
== GoogleServiceAuthError::ACCOUNT_DISABLED
);
483 web_ui()->CallJavascriptFunction("CreateProfileOverlay.updateSignedInStatus",
484 base::StringValue(username
),
485 base::FundamentalValue(has_error
));
487 OnCreateSupervisedUserPrefChange();
490 void ManageProfileHandler::OnCreateSupervisedUserPrefChange() {
491 PrefService
* prefs
= Profile::FromWebUI(web_ui())->GetPrefs();
492 base::FundamentalValue
allowed(
493 prefs
->GetBoolean(prefs::kSupervisedUserCreationAllowed
));
494 web_ui()->CallJavascriptFunction(
495 "CreateProfileOverlay.updateSupervisedUsersAllowed", allowed
);
498 void ManageProfileHandler::OnHasProfileShortcuts(bool has_shortcuts
) {
499 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
501 const base::FundamentalValue
has_shortcuts_value(has_shortcuts
);
502 web_ui()->CallJavascriptFunction(
503 "ManageProfileOverlay.receiveHasProfileShortcuts", has_shortcuts_value
);
506 void ManageProfileHandler::AddProfileShortcut(const base::ListValue
* args
) {
507 base::FilePath profile_file_path
;
508 if (!GetProfilePathFromArgs(args
, &profile_file_path
))
511 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
512 ProfileShortcutManager
* shortcut_manager
=
513 g_browser_process
->profile_manager()->profile_shortcut_manager();
514 DCHECK(shortcut_manager
);
516 shortcut_manager
->CreateProfileShortcut(profile_file_path
);
518 // Update the UI buttons.
519 OnHasProfileShortcuts(true);
522 void ManageProfileHandler::RemoveProfileShortcut(const base::ListValue
* args
) {
523 base::FilePath profile_file_path
;
524 if (!GetProfilePathFromArgs(args
, &profile_file_path
))
527 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
528 ProfileShortcutManager
* shortcut_manager
=
529 g_browser_process
->profile_manager()->profile_shortcut_manager();
530 DCHECK(shortcut_manager
);
532 shortcut_manager
->RemoveProfileShortcuts(profile_file_path
);
534 // Update the UI buttons.
535 OnHasProfileShortcuts(false);
538 void ManageProfileHandler::RefreshGaiaPicture(const base::ListValue
* args
) {
539 profiles::UpdateGaiaProfileInfoIfNeeded(Profile::FromWebUI(web_ui()));
542 } // namespace options