Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / managed_user_import_handler.cc
blobf86f6b9b76e4487c38bccf15df53d8ab36b5d291
1 // Copyright 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/managed_user_import_handler.h"
7 #include <set>
9 #include "base/bind.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/values.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/managed_mode/managed_user_sync_service.h"
15 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_info_cache.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/signin/signin_global_error.h"
20 #include "chrome/browser/signin/signin_manager.h"
21 #include "chrome/browser/signin/signin_manager_factory.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/common/url_constants.h"
24 #include "content/public/browser/notification_details.h"
25 #include "content/public/browser/notification_source.h"
26 #include "content/public/browser/web_ui.h"
27 #include "grit/generated_resources.h"
28 #include "grit/theme_resources.h"
29 #include "ui/base/l10n/l10n_util.h"
31 namespace {
33 scoped_ptr<base::ListValue> GetAvatarIcons() {
34 scoped_ptr<base::ListValue> avatar_icons(new base::ListValue);
35 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); ++i) {
36 std::string avatar_url = ProfileInfoCache::GetDefaultAvatarIconUrl(i);
37 avatar_icons->Append(new base::StringValue(avatar_url));
40 return avatar_icons.Pass();
43 } // namespace
45 namespace options {
47 ManagedUserImportHandler::ManagedUserImportHandler()
48 : weak_ptr_factory_(this) {
51 ManagedUserImportHandler::~ManagedUserImportHandler() {}
53 void ManagedUserImportHandler::GetLocalizedValues(
54 base::DictionaryValue* localized_strings) {
55 DCHECK(localized_strings);
57 static OptionsStringResource resources[] = {
58 { "managedUserImportTitle", IDS_IMPORT_EXISTING_MANAGED_USER_TITLE },
59 { "managedUserImportText", IDS_IMPORT_EXISTING_MANAGED_USER_TEXT },
60 { "createNewUserLink", IDS_CREATE_NEW_USER_LINK },
61 { "managedUserImportOk", IDS_IMPORT_EXISTING_MANAGED_USER_OK },
62 { "managedUserImportSigninError", IDS_MANAGED_USER_IMPORT_SIGN_IN_ERROR },
63 { "managedUserAlreadyOnThisDevice",
64 IDS_MANAGED_USER_ALREADY_ON_THIS_DEVICE },
65 { "noExistingManagedUsers", IDS_MANAGED_USER_NO_EXISTING_ERROR },
66 { "managedUserSelectAvatarTitle", IDS_MANAGED_USER_SELECT_AVATAR_TITLE },
67 { "managedUserSelectAvatarText", IDS_MANAGED_USER_SELECT_AVATAR_TEXT },
68 { "managedUserSelectAvatarOk", IDS_MANAGED_USER_SELECT_AVATAR_OK },
71 RegisterStrings(localized_strings, resources, arraysize(resources));
72 localized_strings->Set("avatarIcons", GetAvatarIcons().release());
75 void ManagedUserImportHandler::InitializeHandler() {
76 registrar_.Add(this, chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED,
77 content::Source<Profile>(Profile::FromWebUI(web_ui())));
80 void ManagedUserImportHandler::RegisterMessages() {
81 web_ui()->RegisterMessageCallback("requestManagedUserImportUpdate",
82 base::Bind(&ManagedUserImportHandler::RequestManagedUserImportUpdate,
83 base::Unretained(this)));
86 void ManagedUserImportHandler::Observe(
87 int type,
88 const content::NotificationSource& source,
89 const content::NotificationDetails& details) {
90 if (type == chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED) {
91 SigninGlobalError* error =
92 SigninGlobalError::GetForProfile(Profile::FromWebUI(web_ui()));
93 if (content::Details<SigninGlobalError>(details).ptr() != error)
94 return;
96 RequestManagedUserImportUpdate(NULL);
97 return;
100 OptionsPageUIHandler::Observe(type, source, details);
103 void ManagedUserImportHandler::RequestManagedUserImportUpdate(
104 const base::ListValue* /* args */) {
105 if (Profile::FromWebUI(web_ui())->IsManaged())
106 return;
108 if (!IsAccountConnected() || HasAuthError()) {
109 ClearManagedUsersAndShowError();
110 } else {
111 ManagedUserSyncService* managed_user_sync_service =
112 ManagedUserSyncServiceFactory::GetForProfile(
113 Profile::FromWebUI(web_ui()));
114 managed_user_sync_service->GetManagedUsersAsync(
115 base::Bind(&ManagedUserImportHandler::SendExistingManagedUsers,
116 weak_ptr_factory_.GetWeakPtr()));
120 void ManagedUserImportHandler::SendExistingManagedUsers(
121 const base::DictionaryValue* dict) {
122 DCHECK(dict);
123 const ProfileInfoCache& cache =
124 g_browser_process->profile_manager()->GetProfileInfoCache();
126 // Collect the ids of local supervised user profiles.
127 std::set<std::string> managed_user_ids;
128 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) {
129 if (cache.ProfileIsManagedAtIndex(i))
130 managed_user_ids.insert(cache.GetManagedUserIdOfProfileAtIndex(i));
133 base::ListValue managed_users;
134 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
135 const base::DictionaryValue* value = NULL;
136 bool success = it.value().GetAsDictionary(&value);
137 DCHECK(success);
138 std::string name;
139 value->GetString(ManagedUserSyncService::kName, &name);
140 std::string avatar_str;
141 value->GetString(ManagedUserSyncService::kChromeAvatar, &avatar_str);
143 base::DictionaryValue* managed_user = new base::DictionaryValue;
144 managed_user->SetString("id", it.key());
145 managed_user->SetString("name", name);
147 int avatar_index = ManagedUserSyncService::kNoAvatar;
148 success = ManagedUserSyncService::GetAvatarIndex(avatar_str, &avatar_index);
149 DCHECK(success);
150 managed_user->SetBoolean("needAvatar",
151 avatar_index == ManagedUserSyncService::kNoAvatar);
153 std::string supervised_user_icon =
154 std::string(chrome::kChromeUIThemeURL) +
155 "IDR_SUPERVISED_USER_PLACEHOLDER";
156 std::string avatar_url =
157 avatar_index == ManagedUserSyncService::kNoAvatar ?
158 supervised_user_icon :
159 ProfileInfoCache::GetDefaultAvatarIconUrl(avatar_index);
160 managed_user->SetString("iconURL", avatar_url);
161 bool on_current_device =
162 managed_user_ids.find(it.key()) != managed_user_ids.end();
163 managed_user->SetBoolean("onCurrentDevice", on_current_device);
165 managed_users.Append(managed_user);
168 web_ui()->CallJavascriptFunction(
169 "options.ManagedUserListData.receiveExistingManagedUsers",
170 managed_users);
173 void ManagedUserImportHandler::ClearManagedUsersAndShowError() {
174 web_ui()->CallJavascriptFunction("options.ManagedUserListData.onSigninError");
177 bool ManagedUserImportHandler::IsAccountConnected() const {
178 Profile* profile = Profile::FromWebUI(web_ui());
179 SigninManagerBase* signin_manager =
180 SigninManagerFactory::GetForProfile(profile);
181 return !signin_manager->GetAuthenticatedUsername().empty();
184 bool ManagedUserImportHandler::HasAuthError() const {
185 Profile* profile = Profile::FromWebUI(web_ui());
186 SigninGlobalError* signin_global_error =
187 SigninGlobalError::GetForProfile(profile);
188 GoogleServiceAuthError::State state =
189 signin_global_error->GetLastAuthError().state();
191 return state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
192 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP ||
193 state == GoogleServiceAuthError::ACCOUNT_DELETED ||
194 state == GoogleServiceAuthError::ACCOUNT_DISABLED;
197 } // namespace options