NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / manage_profile_handler.cc
blobaeb1ee3f3c177db28cd6669d085be01e08f876fb
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"
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"
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
47 namespace options {
49 namespace {
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))
60 return false;
61 return base::GetValueAsFilePath(*file_path_value, profile_file_path);
64 } // namespace
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.
74 if (service)
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 "disableCreateExistingManagedUsers",
126 CommandLine::ForCurrentProcess()->HasSwitch(
127 switches::kDisableCreateExistingManagedUsers));
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.
143 if (service)
144 service->AddObserver(this);
147 void ManageProfileHandler::InitializePage() {
148 SendProfileNames();
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)));
175 #endif
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(
189 int type,
190 const content::NotificationSource& source,
191 const content::NotificationDetails& details) {
192 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) {
193 SendProfileNames();
194 base::StringValue value(kManageProfileIconGridName);
195 SendProfileIcons(value);
199 void ManageProfileHandler::OnStateChanged() {
200 RequestCreateProfileUpdate(NULL);
203 void ManageProfileHandler::RequestDefaultProfileIcons(
204 const base::ListValue* args) {
205 base::StringValue create_value(kCreateProfileIconGridName);
206 base::StringValue manage_value(kManageProfileIconGridName);
207 SendProfileIcons(manage_value);
208 SendProfileIcons(create_value);
211 void ManageProfileHandler::RequestNewProfileDefaults(
212 const base::ListValue* args) {
213 const ProfileInfoCache& cache =
214 g_browser_process->profile_manager()->GetProfileInfoCache();
215 const size_t icon_index = cache.ChooseAvatarIconIndexForNewProfile();
217 base::DictionaryValue profile_info;
218 profile_info.SetString("name", cache.ChooseNameForNewProfile(icon_index));
219 profile_info.SetString("iconURL", cache.GetDefaultAvatarIconUrl(icon_index));
221 web_ui()->CallJavascriptFunction(
222 "ManageProfileOverlay.receiveNewProfileDefaults", profile_info);
225 void ManageProfileHandler::SendProfileIcons(
226 const base::StringValue& icon_grid) {
227 base::ListValue image_url_list;
229 // First add the GAIA picture if it's available.
230 const ProfileInfoCache& cache =
231 g_browser_process->profile_manager()->GetProfileInfoCache();
232 Profile* profile = Profile::FromWebUI(web_ui());
233 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
234 if (profile_index != std::string::npos) {
235 const gfx::Image* icon =
236 cache.GetGAIAPictureOfProfileAtIndex(profile_index);
237 if (icon) {
238 gfx::Image icon2 = profiles::GetAvatarIconForWebUI(*icon, true);
239 gaia_picture_url_ = webui::GetBitmapDataUrl(icon2.AsBitmap());
240 image_url_list.Append(new base::StringValue(gaia_picture_url_));
244 // Next add the default avatar icons.
245 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) {
246 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i);
247 image_url_list.Append(new base::StringValue(url));
250 web_ui()->CallJavascriptFunction(
251 "ManageProfileOverlay.receiveDefaultProfileIcons", icon_grid,
252 image_url_list);
255 void ManageProfileHandler::SendProfileNames() {
256 const ProfileInfoCache& cache =
257 g_browser_process->profile_manager()->GetProfileInfoCache();
258 base::DictionaryValue profile_name_dict;
259 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i) {
260 profile_name_dict.SetBoolean(
261 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)), true);
264 web_ui()->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames",
265 profile_name_dict);
268 void ManageProfileHandler::SetProfileIconAndName(const base::ListValue* args) {
269 DCHECK(args);
271 base::FilePath profile_file_path;
272 if (!GetProfilePathFromArgs(args, &profile_file_path))
273 return;
275 ProfileInfoCache& cache =
276 g_browser_process->profile_manager()->GetProfileInfoCache();
277 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
278 if (profile_index == std::string::npos)
279 return;
281 Profile* profile =
282 g_browser_process->profile_manager()->GetProfile(profile_file_path);
283 if (!profile)
284 return;
286 std::string icon_url;
287 if (!args->GetString(1, &icon_url))
288 return;
290 // Metrics logging variable.
291 bool previously_using_gaia_icon =
292 cache.IsUsingGAIAPictureOfProfileAtIndex(profile_index);
294 size_t new_icon_index;
295 if (icon_url == gaia_picture_url_) {
296 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true);
297 if (!previously_using_gaia_icon) {
298 // Only log if they changed to the GAIA photo.
299 // Selection of GAIA photo as avatar is logged as part of the function
300 // below.
301 ProfileMetrics::LogProfileSwitchGaia(ProfileMetrics::GAIA_OPT_IN);
303 } else if (cache.IsDefaultAvatarIconUrl(icon_url, &new_icon_index)) {
304 ProfileMetrics::LogProfileAvatarSelection(new_icon_index);
305 PrefService* pref_service = profile->GetPrefs();
306 // Updating the profile preference will cause the cache to be updated for
307 // this preference.
308 pref_service->SetInteger(prefs::kProfileAvatarIndex, new_icon_index);
309 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, false);
311 ProfileMetrics::LogProfileUpdate(profile_file_path);
313 if (profile->IsManaged())
314 return;
316 base::string16 new_profile_name;
317 if (!args->GetString(2, &new_profile_name))
318 return;
320 profiles::UpdateProfileName(profile, new_profile_name);
323 #if defined(ENABLE_SETTINGS_APP)
324 void ManageProfileHandler::SwitchAppListProfile(const base::ListValue* args) {
325 DCHECK(args);
326 DCHECK(profiles::IsMultipleProfilesEnabled());
328 const base::Value* file_path_value;
329 base::FilePath profile_file_path;
330 if (!args->Get(0, &file_path_value) ||
331 !base::GetValueAsFilePath(*file_path_value, &profile_file_path))
332 return;
334 AppListService* app_list_service = AppListService::Get(
335 options::helper::GetDesktopType(web_ui()));
336 app_list_service->SetProfilePath(profile_file_path);
337 app_list_service->Show();
339 // Close the settings app, since it will now be for the wrong profile.
340 web_ui()->GetWebContents()->Close();
342 #endif // defined(ENABLE_SETTINGS_APP)
344 void ManageProfileHandler::ProfileIconSelectionChanged(
345 const base::ListValue* args) {
346 DCHECK(args);
348 base::FilePath profile_file_path;
349 if (!GetProfilePathFromArgs(args, &profile_file_path))
350 return;
352 // Currently this only supports editing the current profile's info.
353 if (profile_file_path != Profile::FromWebUI(web_ui())->GetPath())
354 return;
356 std::string icon_url;
357 if (!args->GetString(1, &icon_url))
358 return;
360 if (icon_url != gaia_picture_url_)
361 return;
363 // If the selection is the GAIA picture then also show the profile name in the
364 // text field. This will display either the GAIA given name, if available,
365 // or the first name.
366 ProfileInfoCache& cache =
367 g_browser_process->profile_manager()->GetProfileInfoCache();
368 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
369 if (profile_index == std::string::npos)
370 return;
371 base::string16 gaia_name = cache.GetNameOfProfileAtIndex(profile_index);
372 if (gaia_name.empty())
373 return;
375 base::StringValue gaia_name_value(gaia_name);
376 web_ui()->CallJavascriptFunction("ManageProfileOverlay.setProfileName",
377 gaia_name_value);
380 void ManageProfileHandler::RequestHasProfileShortcuts(
381 const base::ListValue* args) {
382 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
383 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
385 base::FilePath profile_file_path;
386 if (!GetProfilePathFromArgs(args, &profile_file_path))
387 return;
389 const ProfileInfoCache& cache =
390 g_browser_process->profile_manager()->GetProfileInfoCache();
391 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
392 if (profile_index == std::string::npos)
393 return;
395 const base::FilePath profile_path =
396 cache.GetPathOfProfileAtIndex(profile_index);
397 ProfileShortcutManager* shortcut_manager =
398 g_browser_process->profile_manager()->profile_shortcut_manager();
399 shortcut_manager->HasProfileShortcuts(
400 profile_path, base::Bind(&ManageProfileHandler::OnHasProfileShortcuts,
401 weak_factory_.GetWeakPtr()));
404 void ManageProfileHandler::RequestCreateProfileUpdate(
405 const base::ListValue* args) {
406 Profile* profile = Profile::FromWebUI(web_ui());
407 SigninManagerBase* manager =
408 SigninManagerFactory::GetForProfile(profile);
409 base::string16 username =
410 base::UTF8ToUTF16(manager->GetAuthenticatedUsername());
411 ProfileSyncService* service =
412 ProfileSyncServiceFactory::GetForProfile(profile);
413 GoogleServiceAuthError::State state = service->GetAuthError().state();
414 bool has_error = (state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
415 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP ||
416 state == GoogleServiceAuthError::ACCOUNT_DELETED ||
417 state == GoogleServiceAuthError::ACCOUNT_DISABLED);
418 web_ui()->CallJavascriptFunction("CreateProfileOverlay.updateSignedInStatus",
419 base::StringValue(username),
420 base::FundamentalValue(has_error));
421 OnCreateManagedUserPrefChange();
424 void ManageProfileHandler::OnCreateManagedUserPrefChange() {
425 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
426 base::FundamentalValue allowed(
427 prefs->GetBoolean(prefs::kManagedUserCreationAllowed));
428 web_ui()->CallJavascriptFunction(
429 "CreateProfileOverlay.updateManagedUsersAllowed", allowed);
432 void ManageProfileHandler::OnHasProfileShortcuts(bool has_shortcuts) {
433 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
435 const base::FundamentalValue has_shortcuts_value(has_shortcuts);
436 web_ui()->CallJavascriptFunction(
437 "ManageProfileOverlay.receiveHasProfileShortcuts", has_shortcuts_value);
440 void ManageProfileHandler::AddProfileShortcut(const base::ListValue* args) {
441 base::FilePath profile_file_path;
442 if (!GetProfilePathFromArgs(args, &profile_file_path))
443 return;
445 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
446 ProfileShortcutManager* shortcut_manager =
447 g_browser_process->profile_manager()->profile_shortcut_manager();
448 DCHECK(shortcut_manager);
450 shortcut_manager->CreateProfileShortcut(profile_file_path);
452 // Update the UI buttons.
453 OnHasProfileShortcuts(true);
456 void ManageProfileHandler::RemoveProfileShortcut(const base::ListValue* args) {
457 base::FilePath profile_file_path;
458 if (!GetProfilePathFromArgs(args, &profile_file_path))
459 return;
461 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
462 ProfileShortcutManager* shortcut_manager =
463 g_browser_process->profile_manager()->profile_shortcut_manager();
464 DCHECK(shortcut_manager);
466 shortcut_manager->RemoveProfileShortcuts(profile_file_path);
468 // Update the UI buttons.
469 OnHasProfileShortcuts(false);
472 } // namespace options