Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / manage_profile_handler.cc
blobbc8c2d6094f30dc784b5da14ee273fa420c6e3ac
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_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 "components/signin/core/browser/signin_manager.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 "google_apis/gaia/gaia_auth_util.h"
39 #include "grit/generated_resources.h"
40 #include "grit/google_chrome_strings.h"
41 #include "ui/base/l10n/l10n_util.h"
42 #include "ui/base/webui/web_ui_util.h"
44 #if defined(ENABLE_SETTINGS_APP)
45 #include "chrome/browser/ui/app_list/app_list_service.h"
46 #include "content/public/browser/web_contents.h"
47 #endif
49 namespace options {
51 namespace {
53 const char kCreateProfileIdentifier[] = "create";
54 const char kManageProfileIdentifier[] = "manage";
56 // Given |args| from the WebUI, parses value 0 as a FilePath |profile_file_path|
57 // and returns true on success.
58 bool GetProfilePathFromArgs(const base::ListValue* args,
59 base::FilePath* profile_file_path) {
60 const base::Value* file_path_value;
61 if (!args->Get(0, &file_path_value))
62 return false;
63 return base::GetValueAsFilePath(*file_path_value, profile_file_path);
66 } // namespace
68 ManageProfileHandler::ManageProfileHandler()
69 : weak_factory_(this) {
72 ManageProfileHandler::~ManageProfileHandler() {
73 ProfileSyncService* service =
74 ProfileSyncServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()));
75 // Sync may be disabled in tests.
76 if (service)
77 service->RemoveObserver(this);
80 void ManageProfileHandler::GetLocalizedValues(
81 base::DictionaryValue* localized_strings) {
82 DCHECK(localized_strings);
84 static OptionsStringResource resources[] = {
85 { "manageProfilesNameLabel", IDS_PROFILES_MANAGE_NAME_LABEL },
86 { "manageProfilesDuplicateNameError",
87 IDS_PROFILES_MANAGE_DUPLICATE_NAME_ERROR },
88 { "manageProfilesIconLabel", IDS_PROFILES_MANAGE_ICON_LABEL },
89 { "manageProfilesExistingSupervisedUser",
90 IDS_PROFILES_CREATE_EXISTING_MANAGED_USER_ERROR },
91 { "manageProfilesManagedSignedInLabel",
92 IDS_PROFILES_CREATE_MANAGED_SIGNED_IN_LABEL },
93 { "manageProfilesManagedNotSignedInLabel",
94 IDS_PROFILES_CREATE_MANAGED_NOT_SIGNED_IN_LABEL },
95 { "manageProfilesManagedAccountDetailsOutOfDate",
96 IDS_PROFILES_CREATE_MANAGED_ACCOUNT_DETAILS_OUT_OF_DATE_LABEL },
97 { "manageProfilesManagedSignInAgainLink",
98 IDS_PROFILES_CREATE_MANAGED_ACCOUNT_SIGN_IN_AGAIN_LINK },
99 { "manageProfilesManagedNotSignedInLink",
100 IDS_PROFILES_CREATE_MANAGED_NOT_SIGNED_IN_LINK },
101 { "deleteProfileTitle", IDS_PROFILES_DELETE_TITLE },
102 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL },
103 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE },
104 { "deleteManagedProfileAddendum", IDS_PROFILES_DELETE_MANAGED_ADDENDUM },
105 { "disconnectManagedProfileTitle",
106 IDS_PROFILES_DISCONNECT_MANAGED_PROFILE_TITLE },
107 { "disconnectManagedProfileOK",
108 IDS_PROFILES_DISCONNECT_MANAGED_PROFILE_OK_BUTTON_LABEL },
109 { "createProfileTitle", IDS_PROFILES_CREATE_TITLE },
110 { "createProfileInstructions", IDS_PROFILES_CREATE_INSTRUCTIONS },
111 { "createProfileConfirm", IDS_PROFILES_CREATE_CONFIRM },
112 { "createProfileShortcutCheckbox", IDS_PROFILES_CREATE_SHORTCUT_CHECKBOX },
113 { "createProfileShortcutButton", IDS_PROFILES_CREATE_SHORTCUT_BUTTON },
114 { "removeProfileShortcutButton", IDS_PROFILES_REMOVE_SHORTCUT_BUTTON },
115 { "importExistingManagedUserLink",
116 IDS_PROFILES_IMPORT_EXISTING_MANAGED_USER_LINK },
117 { "signInToImportManagedUsers",
118 IDS_PROFILES_IMPORT_MANAGED_USER_NOT_SIGNED_IN },
121 RegisterStrings(localized_strings, resources, arraysize(resources));
122 RegisterTitle(localized_strings, "manageProfile",
123 IDS_PROFILES_MANAGE_TITLE);
124 RegisterTitle(localized_strings, "createProfile",
125 IDS_PROFILES_CREATE_TITLE);
127 localized_strings->SetBoolean("profileShortcutsEnabled",
128 ProfileShortcutManager::IsFeatureEnabled());
130 GenerateSignedinUserSpecificStrings(localized_strings);
133 void ManageProfileHandler::InitializeHandler() {
134 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
135 content::NotificationService::AllSources());
137 Profile* profile = Profile::FromWebUI(web_ui());
138 pref_change_registrar_.Init(profile->GetPrefs());
139 pref_change_registrar_.Add(
140 prefs::kManagedUserCreationAllowed,
141 base::Bind(&ManageProfileHandler::OnCreateManagedUserPrefChange,
142 base::Unretained(this)));
143 ProfileSyncService* service =
144 ProfileSyncServiceFactory::GetForProfile(profile);
145 // Sync may be disabled for tests.
146 if (service)
147 service->AddObserver(this);
150 void ManageProfileHandler::InitializePage() {
151 SendExistingProfileNames();
152 OnCreateManagedUserPrefChange();
155 void ManageProfileHandler::RegisterMessages() {
156 web_ui()->RegisterMessageCallback("setProfileIconAndName",
157 base::Bind(&ManageProfileHandler::SetProfileIconAndName,
158 base::Unretained(this)));
159 web_ui()->RegisterMessageCallback("requestDefaultProfileIcons",
160 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons,
161 base::Unretained(this)));
162 web_ui()->RegisterMessageCallback("requestNewProfileDefaults",
163 base::Bind(&ManageProfileHandler::RequestNewProfileDefaults,
164 base::Unretained(this)));
165 web_ui()->RegisterMessageCallback("requestHasProfileShortcuts",
166 base::Bind(&ManageProfileHandler::RequestHasProfileShortcuts,
167 base::Unretained(this)));
168 web_ui()->RegisterMessageCallback("requestCreateProfileUpdate",
169 base::Bind(&ManageProfileHandler::RequestCreateProfileUpdate,
170 base::Unretained(this)));
171 web_ui()->RegisterMessageCallback("profileIconSelectionChanged",
172 base::Bind(&ManageProfileHandler::ProfileIconSelectionChanged,
173 base::Unretained(this)));
174 #if defined(ENABLE_SETTINGS_APP)
175 web_ui()->RegisterMessageCallback("switchAppListProfile",
176 base::Bind(&ManageProfileHandler::SwitchAppListProfile,
177 base::Unretained(this)));
178 #endif
179 web_ui()->RegisterMessageCallback("addProfileShortcut",
180 base::Bind(&ManageProfileHandler::AddProfileShortcut,
181 base::Unretained(this)));
182 web_ui()->RegisterMessageCallback("removeProfileShortcut",
183 base::Bind(&ManageProfileHandler::RemoveProfileShortcut,
184 base::Unretained(this)));
187 void ManageProfileHandler::Uninitialize() {
188 registrar_.RemoveAll();
191 void ManageProfileHandler::Observe(
192 int type,
193 const content::NotificationSource& source,
194 const content::NotificationDetails& details) {
195 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) {
196 SendExistingProfileNames();
197 base::StringValue value(kManageProfileIdentifier);
198 SendProfileIconsAndNames(value);
202 void ManageProfileHandler::OnStateChanged() {
203 RequestCreateProfileUpdate(NULL);
206 void ManageProfileHandler::GenerateSignedinUserSpecificStrings(
207 base::DictionaryValue* dictionary) {
208 #if !defined(OS_CHROMEOS)
209 Profile* profile = Profile::FromWebUI(web_ui());
210 DCHECK(profile);
211 SigninManagerBase* manager = SigninManagerFactory::GetForProfile(profile);
212 DCHECK(manager);
213 std::string username = manager->GetAuthenticatedUsername();
214 std::string domain_name;
215 // If there is no one logged in or if the profile name is empty then the
216 // domain name is empty. This happens in browser tests.
217 if (!username.empty())
218 domain_name = "<span id=disconnect-managed-profile-domain-name>" +
219 gaia::ExtractDomainName(username) + "</span>";
221 dictionary->SetString(
222 "disconnectManagedProfileDomainInformation",
223 l10n_util::GetStringFUTF16(
224 IDS_PROFILES_DISCONNECT_MANAGED_PROFILE_DOMAIN_INFORMATION,
225 base::ASCIIToUTF16(domain_name)));
227 dictionary->SetString(
228 "disconnectManagedProfileText",
229 l10n_util::GetStringFUTF16(
230 IDS_PROFILES_DISCONNECT_MANAGED_PROFILE_TEXT,
231 base::UTF8ToUTF16(username),
232 base::UTF8ToUTF16(chrome::kSyncGoogleDashboardURL)));
233 #else
234 dictionary->SetString("disconnectManagedProfileDomainInformation",
235 base::string16());
236 dictionary->SetString("disconnectManagedProfileText",
237 base::string16());
238 #endif
241 void ManageProfileHandler::RequestDefaultProfileIcons(
242 const base::ListValue* args) {
243 std::string mode;
244 bool ok = args->GetString(0, &mode);
245 DCHECK(ok);
246 DCHECK(mode == kCreateProfileIdentifier || mode == kManageProfileIdentifier);
247 if (ok) {
248 base::StringValue value(mode);
249 SendProfileIconsAndNames(value);
253 void ManageProfileHandler::RequestNewProfileDefaults(
254 const base::ListValue* args) {
255 const ProfileInfoCache& cache =
256 g_browser_process->profile_manager()->GetProfileInfoCache();
257 const size_t icon_index = cache.ChooseAvatarIconIndexForNewProfile();
259 base::DictionaryValue profile_info;
260 profile_info.SetString("name", cache.ChooseNameForNewProfile(icon_index));
261 profile_info.SetString("iconURL",
262 profiles::GetDefaultAvatarIconUrl(icon_index));
264 web_ui()->CallJavascriptFunction(
265 "ManageProfileOverlay.receiveNewProfileDefaults", profile_info);
268 void ManageProfileHandler::SendProfileIconsAndNames(
269 const base::StringValue& mode) {
270 base::ListValue image_url_list;
271 base::ListValue default_name_list;
273 // First add the GAIA picture if it's available.
274 const ProfileInfoCache& cache =
275 g_browser_process->profile_manager()->GetProfileInfoCache();
276 Profile* profile = Profile::FromWebUI(web_ui());
277 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
278 if (profile_index != std::string::npos) {
279 const gfx::Image* icon =
280 cache.GetGAIAPictureOfProfileAtIndex(profile_index);
281 if (icon) {
282 gfx::Image icon2 = profiles::GetAvatarIconForWebUI(*icon, true);
283 gaia_picture_url_ = webui::GetBitmapDataUrl(icon2.AsBitmap());
284 image_url_list.AppendString(gaia_picture_url_);
285 default_name_list.AppendString(std::string());
289 // Next add the default avatar icons and names.
290 for (size_t i = 0; i < profiles::GetDefaultAvatarIconCount(); i++) {
291 std::string url = profiles::GetDefaultAvatarIconUrl(i);
292 image_url_list.AppendString(url);
293 default_name_list.AppendString(cache.ChooseNameForNewProfile(i));
296 web_ui()->CallJavascriptFunction(
297 "ManageProfileOverlay.receiveDefaultProfileIconsAndNames", mode,
298 image_url_list, default_name_list);
301 void ManageProfileHandler::SendExistingProfileNames() {
302 const ProfileInfoCache& cache =
303 g_browser_process->profile_manager()->GetProfileInfoCache();
304 base::DictionaryValue profile_name_dict;
305 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i) {
306 profile_name_dict.SetBoolean(
307 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)), true);
310 web_ui()->CallJavascriptFunction(
311 "ManageProfileOverlay.receiveExistingProfileNames", profile_name_dict);
314 void ManageProfileHandler::SetProfileIconAndName(const base::ListValue* args) {
315 DCHECK(args);
317 base::FilePath profile_file_path;
318 if (!GetProfilePathFromArgs(args, &profile_file_path))
319 return;
321 ProfileInfoCache& cache =
322 g_browser_process->profile_manager()->GetProfileInfoCache();
323 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
324 if (profile_index == std::string::npos)
325 return;
327 Profile* profile =
328 g_browser_process->profile_manager()->GetProfile(profile_file_path);
329 if (!profile)
330 return;
332 std::string icon_url;
333 if (!args->GetString(1, &icon_url))
334 return;
336 // Metrics logging variable.
337 bool previously_using_gaia_icon =
338 cache.IsUsingGAIAPictureOfProfileAtIndex(profile_index);
340 size_t new_icon_index;
341 if (icon_url == gaia_picture_url_) {
342 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true);
343 if (!previously_using_gaia_icon) {
344 // Only log if they changed to the GAIA photo.
345 // Selection of GAIA photo as avatar is logged as part of the function
346 // below.
347 ProfileMetrics::LogProfileSwitchGaia(ProfileMetrics::GAIA_OPT_IN);
349 } else if (profiles::IsDefaultAvatarIconUrl(icon_url, &new_icon_index)) {
350 ProfileMetrics::LogProfileAvatarSelection(new_icon_index);
351 PrefService* pref_service = profile->GetPrefs();
352 // Updating the profile preference will cause the cache to be updated for
353 // this preference.
354 pref_service->SetInteger(prefs::kProfileAvatarIndex, new_icon_index);
355 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, false);
357 ProfileMetrics::LogProfileUpdate(profile_file_path);
359 if (profile->IsManaged())
360 return;
362 base::string16 new_profile_name;
363 if (!args->GetString(2, &new_profile_name))
364 return;
366 profiles::UpdateProfileName(profile, new_profile_name);
369 #if defined(ENABLE_SETTINGS_APP)
370 void ManageProfileHandler::SwitchAppListProfile(const base::ListValue* args) {
371 DCHECK(args);
372 DCHECK(profiles::IsMultipleProfilesEnabled());
374 const base::Value* file_path_value;
375 base::FilePath profile_file_path;
376 if (!args->Get(0, &file_path_value) ||
377 !base::GetValueAsFilePath(*file_path_value, &profile_file_path))
378 return;
380 AppListService* app_list_service = AppListService::Get(
381 options::helper::GetDesktopType(web_ui()));
382 app_list_service->SetProfilePath(profile_file_path);
383 app_list_service->Show();
385 // Close the settings app, since it will now be for the wrong profile.
386 web_ui()->GetWebContents()->Close();
388 #endif // defined(ENABLE_SETTINGS_APP)
390 void ManageProfileHandler::ProfileIconSelectionChanged(
391 const base::ListValue* args) {
392 DCHECK(args);
394 base::FilePath profile_file_path;
395 if (!GetProfilePathFromArgs(args, &profile_file_path))
396 return;
398 // Currently this only supports editing the current profile's info.
399 if (profile_file_path != Profile::FromWebUI(web_ui())->GetPath())
400 return;
402 std::string icon_url;
403 if (!args->GetString(1, &icon_url))
404 return;
406 if (icon_url != gaia_picture_url_)
407 return;
409 // If the selection is the GAIA picture then also show the profile name in the
410 // text field. This will display either the GAIA given name, if available,
411 // or the first name.
412 ProfileInfoCache& cache =
413 g_browser_process->profile_manager()->GetProfileInfoCache();
414 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
415 if (profile_index == std::string::npos)
416 return;
417 base::string16 gaia_name = cache.GetNameOfProfileAtIndex(profile_index);
418 if (gaia_name.empty())
419 return;
421 base::StringValue gaia_name_value(gaia_name);
422 base::StringValue mode_value(kManageProfileIdentifier);
423 web_ui()->CallJavascriptFunction("ManageProfileOverlay.setProfileName",
424 gaia_name_value, mode_value);
427 void ManageProfileHandler::RequestHasProfileShortcuts(
428 const base::ListValue* args) {
429 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
430 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
432 base::FilePath profile_file_path;
433 if (!GetProfilePathFromArgs(args, &profile_file_path))
434 return;
436 const ProfileInfoCache& cache =
437 g_browser_process->profile_manager()->GetProfileInfoCache();
438 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
439 if (profile_index == std::string::npos)
440 return;
442 const base::FilePath profile_path =
443 cache.GetPathOfProfileAtIndex(profile_index);
444 ProfileShortcutManager* shortcut_manager =
445 g_browser_process->profile_manager()->profile_shortcut_manager();
446 shortcut_manager->HasProfileShortcuts(
447 profile_path, base::Bind(&ManageProfileHandler::OnHasProfileShortcuts,
448 weak_factory_.GetWeakPtr()));
451 void ManageProfileHandler::RequestCreateProfileUpdate(
452 const base::ListValue* args) {
453 Profile* profile = Profile::FromWebUI(web_ui());
454 SigninManagerBase* manager =
455 SigninManagerFactory::GetForProfile(profile);
456 base::string16 username =
457 base::UTF8ToUTF16(manager->GetAuthenticatedUsername());
458 ProfileSyncService* service =
459 ProfileSyncServiceFactory::GetForProfile(profile);
460 GoogleServiceAuthError::State state = service->GetAuthError().state();
461 bool has_error = (state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
462 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP ||
463 state == GoogleServiceAuthError::ACCOUNT_DELETED ||
464 state == GoogleServiceAuthError::ACCOUNT_DISABLED);
465 web_ui()->CallJavascriptFunction("CreateProfileOverlay.updateSignedInStatus",
466 base::StringValue(username),
467 base::FundamentalValue(has_error));
469 base::DictionaryValue replacements;
470 GenerateSignedinUserSpecificStrings(&replacements);
471 web_ui()->CallJavascriptFunction("loadTimeData.overrideValues", replacements);
473 OnCreateManagedUserPrefChange();
476 void ManageProfileHandler::OnCreateManagedUserPrefChange() {
477 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
478 base::FundamentalValue allowed(
479 prefs->GetBoolean(prefs::kManagedUserCreationAllowed));
480 web_ui()->CallJavascriptFunction(
481 "CreateProfileOverlay.updateManagedUsersAllowed", allowed);
484 void ManageProfileHandler::OnHasProfileShortcuts(bool has_shortcuts) {
485 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
487 const base::FundamentalValue has_shortcuts_value(has_shortcuts);
488 web_ui()->CallJavascriptFunction(
489 "ManageProfileOverlay.receiveHasProfileShortcuts", has_shortcuts_value);
492 void ManageProfileHandler::AddProfileShortcut(const base::ListValue* args) {
493 base::FilePath profile_file_path;
494 if (!GetProfilePathFromArgs(args, &profile_file_path))
495 return;
497 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
498 ProfileShortcutManager* shortcut_manager =
499 g_browser_process->profile_manager()->profile_shortcut_manager();
500 DCHECK(shortcut_manager);
502 shortcut_manager->CreateProfileShortcut(profile_file_path);
504 // Update the UI buttons.
505 OnHasProfileShortcuts(true);
508 void ManageProfileHandler::RemoveProfileShortcut(const base::ListValue* args) {
509 base::FilePath profile_file_path;
510 if (!GetProfilePathFromArgs(args, &profile_file_path))
511 return;
513 DCHECK(ProfileShortcutManager::IsFeatureEnabled());
514 ProfileShortcutManager* shortcut_manager =
515 g_browser_process->profile_manager()->profile_shortcut_manager();
516 DCHECK(shortcut_manager);
518 shortcut_manager->RemoveProfileShortcuts(profile_file_path);
520 // Update the UI buttons.
521 OnHasProfileShortcuts(false);
524 } // namespace options