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/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"
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"
51 const char kCreateProfileIconGridName
[] = "create-profile-icon-grid";
52 const char kManageProfileIconGridName
[] = "manage-profile-icon-grid";
54 // Given |args| from the WebUI, parses value 0 as a FilePath |profile_file_path|
55 // and returns true on success.
56 bool GetProfilePathFromArgs(const base::ListValue
* args
,
57 base::FilePath
* profile_file_path
) {
58 const base::Value
* file_path_value
;
59 if (!args
->Get(0, &file_path_value
))
61 return base::GetValueAsFilePath(*file_path_value
, profile_file_path
);
66 ManageProfileHandler::ManageProfileHandler()
67 : weak_factory_(this) {
70 ManageProfileHandler::~ManageProfileHandler() {
71 ProfileSyncService
* service
=
72 ProfileSyncServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()));
73 // Sync may be disabled in tests.
75 service
->RemoveObserver(this);
78 void ManageProfileHandler::GetLocalizedValues(
79 base::DictionaryValue
* localized_strings
) {
80 DCHECK(localized_strings
);
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 { "manageProfilesExistingSupervisedUser",
88 IDS_PROFILES_CREATE_EXISTING_MANAGED_USER_ERROR
},
89 { "manageProfilesManagedSignedInLabel",
90 IDS_PROFILES_CREATE_MANAGED_SIGNED_IN_LABEL
},
91 { "manageProfilesManagedNotSignedInLabel",
92 IDS_PROFILES_CREATE_MANAGED_NOT_SIGNED_IN_LABEL
},
93 { "manageProfilesManagedAccountDetailsOutOfDate",
94 IDS_PROFILES_CREATE_MANAGED_ACCOUNT_DETAILS_OUT_OF_DATE_LABEL
},
95 { "manageProfilesManagedSignInAgainLink",
96 IDS_PROFILES_CREATE_MANAGED_ACCOUNT_SIGN_IN_AGAIN_LINK
},
97 { "manageProfilesManagedNotSignedInLink",
98 IDS_PROFILES_CREATE_MANAGED_NOT_SIGNED_IN_LINK
},
99 { "deleteProfileTitle", IDS_PROFILES_DELETE_TITLE
},
100 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL
},
101 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE
},
102 { "deleteManagedProfileAddendum", IDS_PROFILES_DELETE_MANAGED_ADDENDUM
},
103 { "createProfileTitle", IDS_PROFILES_CREATE_TITLE
},
104 { "createProfileInstructions", IDS_PROFILES_CREATE_INSTRUCTIONS
},
105 { "createProfileConfirm", IDS_PROFILES_CREATE_CONFIRM
},
106 { "createProfileShortcutCheckbox", IDS_PROFILES_CREATE_SHORTCUT_CHECKBOX
},
107 { "createProfileShortcutButton", IDS_PROFILES_CREATE_SHORTCUT_BUTTON
},
108 { "removeProfileShortcutButton", IDS_PROFILES_REMOVE_SHORTCUT_BUTTON
},
109 { "importExistingManagedUserLink",
110 IDS_PROFILES_IMPORT_EXISTING_MANAGED_USER_LINK
},
111 { "signInToImportManagedUsers",
112 IDS_PROFILES_IMPORT_MANAGED_USER_NOT_SIGNED_IN
},
115 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
116 RegisterTitle(localized_strings
, "manageProfile",
117 IDS_PROFILES_MANAGE_TITLE
);
118 RegisterTitle(localized_strings
, "createProfile",
119 IDS_PROFILES_CREATE_TITLE
);
121 localized_strings
->SetBoolean("profileShortcutsEnabled",
122 ProfileShortcutManager::IsFeatureEnabled());
124 localized_strings
->SetBoolean(
125 "allowCreateExistingManagedUsers",
126 CommandLine::ForCurrentProcess()->HasSwitch(
127 switches::kAllowCreateExistingManagedUsers
));
130 void ManageProfileHandler::InitializeHandler() {
131 registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED
,
132 content::NotificationService::AllSources());
134 Profile
* profile
= Profile::FromWebUI(web_ui());
135 pref_change_registrar_
.Init(profile
->GetPrefs());
136 pref_change_registrar_
.Add(
137 prefs::kManagedUserCreationAllowed
,
138 base::Bind(&ManageProfileHandler::OnCreateManagedUserPrefChange
,
139 base::Unretained(this)));
140 ProfileSyncService
* service
=
141 ProfileSyncServiceFactory::GetForProfile(profile
);
142 // Sync may be disabled for tests.
144 service
->AddObserver(this);
147 void ManageProfileHandler::InitializePage() {
149 OnCreateManagedUserPrefChange();
152 void ManageProfileHandler::RegisterMessages() {
153 web_ui()->RegisterMessageCallback("setProfileIconAndName",
154 base::Bind(&ManageProfileHandler::SetProfileIconAndName
,
155 base::Unretained(this)));
156 web_ui()->RegisterMessageCallback("requestDefaultProfileIcons",
157 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons
,
158 base::Unretained(this)));
159 web_ui()->RegisterMessageCallback("requestNewProfileDefaults",
160 base::Bind(&ManageProfileHandler::RequestNewProfileDefaults
,
161 base::Unretained(this)));
162 web_ui()->RegisterMessageCallback("requestHasProfileShortcuts",
163 base::Bind(&ManageProfileHandler::RequestHasProfileShortcuts
,
164 base::Unretained(this)));
165 web_ui()->RegisterMessageCallback("requestCreateProfileUpdate",
166 base::Bind(&ManageProfileHandler::RequestCreateProfileUpdate
,
167 base::Unretained(this)));
168 web_ui()->RegisterMessageCallback("profileIconSelectionChanged",
169 base::Bind(&ManageProfileHandler::ProfileIconSelectionChanged
,
170 base::Unretained(this)));
171 #if defined(ENABLE_SETTINGS_APP)
172 web_ui()->RegisterMessageCallback("switchAppListProfile",
173 base::Bind(&ManageProfileHandler::SwitchAppListProfile
,
174 base::Unretained(this)));
176 web_ui()->RegisterMessageCallback("addProfileShortcut",
177 base::Bind(&ManageProfileHandler::AddProfileShortcut
,
178 base::Unretained(this)));
179 web_ui()->RegisterMessageCallback("removeProfileShortcut",
180 base::Bind(&ManageProfileHandler::RemoveProfileShortcut
,
181 base::Unretained(this)));
184 void ManageProfileHandler::Uninitialize() {
185 registrar_
.RemoveAll();
188 void ManageProfileHandler::Observe(
190 const content::NotificationSource
& source
,
191 const content::NotificationDetails
& details
) {
192 if (type
== chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED
) {
194 base::StringValue
value(kManageProfileIconGridName
);
195 SendProfileIcons(value
);
197 OptionsPageUIHandler::Observe(type
, source
, details
);
201 void ManageProfileHandler::OnStateChanged() {
202 RequestCreateProfileUpdate(NULL
);
205 void ManageProfileHandler::RequestDefaultProfileIcons(
206 const base::ListValue
* args
) {
207 base::StringValue
create_value(kCreateProfileIconGridName
);
208 base::StringValue
manage_value(kManageProfileIconGridName
);
209 SendProfileIcons(manage_value
);
210 SendProfileIcons(create_value
);
213 void ManageProfileHandler::RequestNewProfileDefaults(
214 const base::ListValue
* args
) {
215 const ProfileInfoCache
& cache
=
216 g_browser_process
->profile_manager()->GetProfileInfoCache();
217 const size_t icon_index
= cache
.ChooseAvatarIconIndexForNewProfile();
219 base::DictionaryValue profile_info
;
220 profile_info
.SetString("name", cache
.ChooseNameForNewProfile(icon_index
));
221 profile_info
.SetString("iconURL", cache
.GetDefaultAvatarIconUrl(icon_index
));
223 web_ui()->CallJavascriptFunction(
224 "ManageProfileOverlay.receiveNewProfileDefaults", profile_info
);
227 void ManageProfileHandler::SendProfileIcons(
228 const base::StringValue
& icon_grid
) {
229 base::ListValue image_url_list
;
231 // First add the GAIA picture if it's available.
232 const ProfileInfoCache
& cache
=
233 g_browser_process
->profile_manager()->GetProfileInfoCache();
234 Profile
* profile
= Profile::FromWebUI(web_ui());
235 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
236 if (profile_index
!= std::string::npos
) {
237 const gfx::Image
* icon
=
238 cache
.GetGAIAPictureOfProfileAtIndex(profile_index
);
240 gfx::Image icon2
= profiles::GetAvatarIconForWebUI(*icon
, true);
241 gaia_picture_url_
= webui::GetBitmapDataUrl(icon2
.AsBitmap());
242 image_url_list
.Append(new base::StringValue(gaia_picture_url_
));
246 // Next add the default avatar icons.
247 for (size_t i
= 0; i
< ProfileInfoCache::GetDefaultAvatarIconCount(); i
++) {
248 std::string url
= ProfileInfoCache::GetDefaultAvatarIconUrl(i
);
249 image_url_list
.Append(new base::StringValue(url
));
252 web_ui()->CallJavascriptFunction(
253 "ManageProfileOverlay.receiveDefaultProfileIcons", icon_grid
,
257 void ManageProfileHandler::SendProfileNames() {
258 const ProfileInfoCache
& cache
=
259 g_browser_process
->profile_manager()->GetProfileInfoCache();
260 base::DictionaryValue profile_name_dict
;
261 for (size_t i
= 0, e
= cache
.GetNumberOfProfiles(); i
< e
; ++i
) {
262 profile_name_dict
.SetBoolean(
263 base::UTF16ToUTF8(cache
.GetNameOfProfileAtIndex(i
)), true);
266 web_ui()->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames",
270 void ManageProfileHandler::SetProfileIconAndName(const base::ListValue
* args
) {
273 base::FilePath profile_file_path
;
274 if (!GetProfilePathFromArgs(args
, &profile_file_path
))
277 ProfileInfoCache
& cache
=
278 g_browser_process
->profile_manager()->GetProfileInfoCache();
279 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile_file_path
);
280 if (profile_index
== std::string::npos
)
284 g_browser_process
->profile_manager()->GetProfile(profile_file_path
);
288 std::string icon_url
;
289 if (!args
->GetString(1, &icon_url
))
292 // Metrics logging variable.
293 bool previously_using_gaia_icon
=
294 cache
.IsUsingGAIAPictureOfProfileAtIndex(profile_index
);
296 size_t new_icon_index
;
297 if (icon_url
== gaia_picture_url_
) {
298 cache
.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index
, true);
299 if (!previously_using_gaia_icon
) {
300 // Only log if they changed to the GAIA photo.
301 // Selection of GAIA photo as avatar is logged as part of the function
303 ProfileMetrics::LogProfileSwitchGaia(ProfileMetrics::GAIA_OPT_IN
);
305 } else if (cache
.IsDefaultAvatarIconUrl(icon_url
, &new_icon_index
)) {
306 ProfileMetrics::LogProfileAvatarSelection(new_icon_index
);
307 PrefService
* pref_service
= profile
->GetPrefs();
308 // Updating the profile preference will cause the cache to be updated for
310 pref_service
->SetInteger(prefs::kProfileAvatarIndex
, new_icon_index
);
311 cache
.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index
, false);
313 ProfileMetrics::LogProfileUpdate(profile_file_path
);
315 if (profile
->IsManaged())
318 base::string16 new_profile_name
;
319 if (!args
->GetString(2, &new_profile_name
))
322 profiles::UpdateProfileName(profile
, new_profile_name
);
325 #if defined(ENABLE_SETTINGS_APP)
326 void ManageProfileHandler::SwitchAppListProfile(const base::ListValue
* args
) {
328 DCHECK(profiles::IsMultipleProfilesEnabled());
330 const base::Value
* file_path_value
;
331 base::FilePath profile_file_path
;
332 if (!args
->Get(0, &file_path_value
) ||
333 !base::GetValueAsFilePath(*file_path_value
, &profile_file_path
))
336 AppListService
* app_list_service
= AppListService::Get(
337 options::helper::GetDesktopType(web_ui()));
338 app_list_service
->SetProfilePath(profile_file_path
);
339 app_list_service
->Show();
341 // Close the settings app, since it will now be for the wrong profile.
342 web_ui()->GetWebContents()->Close();
344 #endif // defined(ENABLE_SETTINGS_APP)
346 void ManageProfileHandler::ProfileIconSelectionChanged(
347 const base::ListValue
* args
) {
350 base::FilePath profile_file_path
;
351 if (!GetProfilePathFromArgs(args
, &profile_file_path
))
354 // Currently this only supports editing the current profile's info.
355 if (profile_file_path
!= Profile::FromWebUI(web_ui())->GetPath())
358 std::string icon_url
;
359 if (!args
->GetString(1, &icon_url
))
362 if (icon_url
!= gaia_picture_url_
)
365 // If the selection is the GAIA picture then also show the profile name in the
366 // text field. This will display either the GAIA given name, if available,
367 // or the first name.
368 ProfileInfoCache
& cache
=
369 g_browser_process
->profile_manager()->GetProfileInfoCache();
370 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile_file_path
);
371 if (profile_index
== std::string::npos
)
373 base::string16 gaia_name
= cache
.GetNameOfProfileAtIndex(profile_index
);
374 if (gaia_name
.empty())
377 base::StringValue
gaia_name_value(gaia_name
);
378 web_ui()->CallJavascriptFunction("ManageProfileOverlay.setProfileName",
382 void ManageProfileHandler::RequestHasProfileShortcuts(
383 const base::ListValue
* args
) {
384 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
385 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
387 base::FilePath profile_file_path
;
388 if (!GetProfilePathFromArgs(args
, &profile_file_path
))
391 const ProfileInfoCache
& cache
=
392 g_browser_process
->profile_manager()->GetProfileInfoCache();
393 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile_file_path
);
394 if (profile_index
== std::string::npos
)
397 const base::FilePath profile_path
=
398 cache
.GetPathOfProfileAtIndex(profile_index
);
399 ProfileShortcutManager
* shortcut_manager
=
400 g_browser_process
->profile_manager()->profile_shortcut_manager();
401 shortcut_manager
->HasProfileShortcuts(
402 profile_path
, base::Bind(&ManageProfileHandler::OnHasProfileShortcuts
,
403 weak_factory_
.GetWeakPtr()));
406 void ManageProfileHandler::RequestCreateProfileUpdate(
407 const base::ListValue
* args
) {
408 Profile
* profile
= Profile::FromWebUI(web_ui());
409 SigninManagerBase
* manager
=
410 SigninManagerFactory::GetForProfile(profile
);
411 base::string16 username
=
412 base::UTF8ToUTF16(manager
->GetAuthenticatedUsername());
413 ProfileSyncService
* service
=
414 ProfileSyncServiceFactory::GetForProfile(profile
);
415 GoogleServiceAuthError::State state
= service
->GetAuthError().state();
416 bool has_error
= (state
== GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
||
417 state
== GoogleServiceAuthError::USER_NOT_SIGNED_UP
||
418 state
== GoogleServiceAuthError::ACCOUNT_DELETED
||
419 state
== GoogleServiceAuthError::ACCOUNT_DISABLED
);
420 web_ui()->CallJavascriptFunction("CreateProfileOverlay.updateSignedInStatus",
421 base::StringValue(username
),
422 base::FundamentalValue(has_error
));
423 OnCreateManagedUserPrefChange();
426 void ManageProfileHandler::OnCreateManagedUserPrefChange() {
427 PrefService
* prefs
= Profile::FromWebUI(web_ui())->GetPrefs();
428 base::FundamentalValue
allowed(
429 prefs
->GetBoolean(prefs::kManagedUserCreationAllowed
));
430 web_ui()->CallJavascriptFunction(
431 "CreateProfileOverlay.updateManagedUsersAllowed", allowed
);
434 void ManageProfileHandler::OnHasProfileShortcuts(bool has_shortcuts
) {
435 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
437 const base::FundamentalValue
has_shortcuts_value(has_shortcuts
);
438 web_ui()->CallJavascriptFunction(
439 "ManageProfileOverlay.receiveHasProfileShortcuts", has_shortcuts_value
);
442 void ManageProfileHandler::AddProfileShortcut(const base::ListValue
* args
) {
443 base::FilePath profile_file_path
;
444 if (!GetProfilePathFromArgs(args
, &profile_file_path
))
447 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
448 ProfileShortcutManager
* shortcut_manager
=
449 g_browser_process
->profile_manager()->profile_shortcut_manager();
450 DCHECK(shortcut_manager
);
452 shortcut_manager
->CreateProfileShortcut(profile_file_path
);
454 // Update the UI buttons.
455 OnHasProfileShortcuts(true);
458 void ManageProfileHandler::RemoveProfileShortcut(const base::ListValue
* args
) {
459 base::FilePath profile_file_path
;
460 if (!GetProfilePathFromArgs(args
, &profile_file_path
))
463 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
464 ProfileShortcutManager
* shortcut_manager
=
465 g_browser_process
->profile_manager()->profile_shortcut_manager();
466 DCHECK(shortcut_manager
);
468 shortcut_manager
->RemoveProfileShortcuts(profile_file_path
);
470 // Update the UI buttons.
471 OnHasProfileShortcuts(false);
474 } // namespace options