Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / supervised_user_import_handler.cc
blob548d44190ea4aa6969fdf113d0edd2850373d768
1 // Copyright 2014 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/supervised_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/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
15 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/signin/signin_error_controller_factory.h"
18 #include "chrome/browser/signin/signin_manager_factory.h"
19 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service.h"
20 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service_factory.h"
21 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service.h"
22 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service_factory.h"
23 #include "chrome/browser/supervised_user/supervised_user_constants.h"
24 #include "chrome/common/url_constants.h"
25 #include "chrome/grit/generated_resources.h"
26 #include "components/signin/core/browser/signin_error_controller.h"
27 #include "components/signin/core/browser/signin_manager.h"
28 #include "content/public/browser/web_ui.h"
29 #include "grit/theme_resources.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 < profiles::GetDefaultAvatarIconCount(); ++i) {
36 std::string avatar_url = profiles::GetDefaultAvatarIconUrl(i);
37 avatar_icons->Append(new base::StringValue(avatar_url));
40 return avatar_icons.Pass();
43 bool ProfileIsLegacySupervised(const base::FilePath& profile_path) {
44 const ProfileInfoCache& cache =
45 g_browser_process->profile_manager()->GetProfileInfoCache();
46 size_t index = cache.GetIndexOfProfileWithPath(profile_path);
47 if (index == std::string::npos)
48 return false;
49 return cache.ProfileIsLegacySupervisedAtIndex(index);
52 } // namespace
54 namespace options {
56 SupervisedUserImportHandler::SupervisedUserImportHandler()
57 : profile_observer_(this),
58 signin_error_observer_(this),
59 supervised_user_sync_service_observer_(this),
60 removed_profile_is_supervised_(false),
61 weak_ptr_factory_(this) {
64 SupervisedUserImportHandler::~SupervisedUserImportHandler() {
67 void SupervisedUserImportHandler::GetLocalizedValues(
68 base::DictionaryValue* localized_strings) {
69 DCHECK(localized_strings);
71 static OptionsStringResource resources[] = {
72 { "supervisedUserImportTitle",
73 IDS_IMPORT_EXISTING_LEGACY_SUPERVISED_USER_TITLE },
74 { "supervisedUserImportText",
75 IDS_IMPORT_EXISTING_LEGACY_SUPERVISED_USER_TEXT },
76 { "createNewUserLink", IDS_CREATE_NEW_LEGACY_SUPERVISED_USER_LINK },
77 { "supervisedUserImportOk",
78 IDS_IMPORT_EXISTING_LEGACY_SUPERVISED_USER_OK },
79 { "supervisedUserImportSigninError",
80 IDS_LEGACY_SUPERVISED_USER_IMPORT_SIGN_IN_ERROR },
81 { "supervisedUserAlreadyOnThisDevice",
82 IDS_LEGACY_SUPERVISED_USER_ALREADY_ON_THIS_DEVICE },
83 { "noExistingSupervisedUsers",
84 IDS_LEGACY_SUPERVISED_USER_NO_EXISTING_ERROR },
85 { "supervisedUserSelectAvatarTitle",
86 IDS_LEGACY_SUPERVISED_USER_SELECT_AVATAR_TITLE },
87 { "supervisedUserSelectAvatarText",
88 IDS_LEGACY_SUPERVISED_USER_SELECT_AVATAR_TEXT },
89 { "supervisedUserSelectAvatarOk",
90 IDS_LEGACY_SUPERVISED_USER_SELECT_AVATAR_OK },
93 RegisterStrings(localized_strings, resources, arraysize(resources));
94 localized_strings->Set("avatarIcons", GetAvatarIcons().release());
97 void SupervisedUserImportHandler::InitializeHandler() {
98 Profile* profile = Profile::FromWebUI(web_ui());
99 if (!profile->IsSupervised()) {
100 profile_observer_.Add(
101 &g_browser_process->profile_manager()->GetProfileInfoCache());
102 SupervisedUserSyncService* sync_service =
103 SupervisedUserSyncServiceFactory::GetForProfile(profile);
104 if (sync_service) {
105 supervised_user_sync_service_observer_.Add(sync_service);
106 signin_error_observer_.Add(
107 SigninErrorControllerFactory::GetForProfile(profile));
108 SupervisedUserSharedSettingsService* settings_service =
109 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
110 profile);
111 subscription_ = settings_service->Subscribe(
112 base::Bind(&SupervisedUserImportHandler::OnSharedSettingChanged,
113 weak_ptr_factory_.GetWeakPtr()));
114 } else {
115 DCHECK(!SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
116 profile));
117 DCHECK(!SigninErrorControllerFactory::GetForProfile(profile));
122 void SupervisedUserImportHandler::RegisterMessages() {
123 web_ui()->RegisterMessageCallback("requestSupervisedUserImportUpdate",
124 base::Bind(&SupervisedUserImportHandler::
125 RequestSupervisedUserImportUpdate,
126 base::Unretained(this)));
129 void SupervisedUserImportHandler::OnProfileAdded(
130 const base::FilePath& profile_path) {
131 // When a supervised profile is added, re-send the list to update the
132 // the "already on this device" status.
133 if (ProfileIsLegacySupervised(profile_path))
134 FetchSupervisedUsers();
137 void SupervisedUserImportHandler::OnProfileWillBeRemoved(
138 const base::FilePath& profile_path) {
139 DCHECK(!removed_profile_is_supervised_);
140 // When a supervised profile is removed, re-send the list to update the
141 // "already on this device" status. We can't do that right now because the
142 // profile still exists, so defer to OnProfileWasRemoved.
143 if (ProfileIsLegacySupervised(profile_path))
144 removed_profile_is_supervised_ = true;
147 void SupervisedUserImportHandler::OnProfileWasRemoved(
148 const base::FilePath& profile_path,
149 const base::string16& profile_name) {
150 if (removed_profile_is_supervised_) {
151 removed_profile_is_supervised_ = false;
152 FetchSupervisedUsers();
156 void SupervisedUserImportHandler::OnProfileIsOmittedChanged(
157 const base::FilePath& profile_path) {
158 if (ProfileIsLegacySupervised(profile_path))
159 FetchSupervisedUsers();
162 void SupervisedUserImportHandler::OnSupervisedUsersChanged() {
163 FetchSupervisedUsers();
166 void SupervisedUserImportHandler::FetchSupervisedUsers() {
167 web_ui()->CallJavascriptFunction(
168 "options.SupervisedUserListData.resetPromise");
169 RequestSupervisedUserImportUpdate(NULL);
172 void SupervisedUserImportHandler::RequestSupervisedUserImportUpdate(
173 const base::ListValue* /* args */) {
174 if (Profile::FromWebUI(web_ui())->IsSupervised())
175 return;
177 if (!IsAccountConnected() || HasAuthError()) {
178 ClearSupervisedUsersAndShowError();
179 } else {
180 SupervisedUserSyncService* supervised_user_sync_service =
181 SupervisedUserSyncServiceFactory::GetForProfile(
182 Profile::FromWebUI(web_ui()));
183 if (supervised_user_sync_service) {
184 supervised_user_sync_service->GetSupervisedUsersAsync(
185 base::Bind(&SupervisedUserImportHandler::SendExistingSupervisedUsers,
186 weak_ptr_factory_.GetWeakPtr()));
191 void SupervisedUserImportHandler::SendExistingSupervisedUsers(
192 const base::DictionaryValue* dict) {
193 DCHECK(dict);
194 const ProfileInfoCache& cache =
195 g_browser_process->profile_manager()->GetProfileInfoCache();
197 // Collect the ids of local supervised user profiles.
198 std::set<std::string> supervised_user_ids;
199 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) {
200 // Filter out omitted profiles. These are currently being imported, and
201 // shouldn't show up as "already on this device" just yet.
202 if (cache.ProfileIsLegacySupervisedAtIndex(i) &&
203 !cache.IsOmittedProfileAtIndex(i)) {
204 supervised_user_ids.insert(cache.GetSupervisedUserIdOfProfileAtIndex(i));
208 base::ListValue supervised_users;
209 Profile* profile = Profile::FromWebUI(web_ui());
210 SupervisedUserSharedSettingsService* service =
211 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(profile);
212 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
213 const base::DictionaryValue* value = NULL;
214 bool success = it.value().GetAsDictionary(&value);
215 DCHECK(success);
216 std::string name;
217 value->GetString(SupervisedUserSyncService::kName, &name);
219 base::DictionaryValue* supervised_user = new base::DictionaryValue;
220 supervised_user->SetString("id", it.key());
221 supervised_user->SetString("name", name);
223 int avatar_index = SupervisedUserSyncService::kNoAvatar;
224 const base::Value* avatar_index_value =
225 service->GetValue(it.key(), supervised_users::kChromeAvatarIndex);
226 if (avatar_index_value) {
227 success = avatar_index_value->GetAsInteger(&avatar_index);
228 } else {
229 // Check if there is a legacy avatar index stored.
230 std::string avatar_str;
231 value->GetString(SupervisedUserSyncService::kChromeAvatar, &avatar_str);
232 success =
233 SupervisedUserSyncService::GetAvatarIndex(avatar_str, &avatar_index);
235 DCHECK(success);
236 supervised_user->SetBoolean(
237 "needAvatar",
238 avatar_index == SupervisedUserSyncService::kNoAvatar);
240 std::string supervised_user_icon =
241 std::string(chrome::kChromeUIThemeURL) +
242 "IDR_SUPERVISED_USER_PLACEHOLDER";
243 std::string avatar_url =
244 avatar_index == SupervisedUserSyncService::kNoAvatar ?
245 supervised_user_icon :
246 profiles::GetDefaultAvatarIconUrl(avatar_index);
247 supervised_user->SetString("iconURL", avatar_url);
248 bool on_current_device =
249 supervised_user_ids.find(it.key()) != supervised_user_ids.end();
250 supervised_user->SetBoolean("onCurrentDevice", on_current_device);
252 supervised_users.Append(supervised_user);
255 web_ui()->CallJavascriptFunction(
256 "options.SupervisedUserListData.receiveExistingSupervisedUsers",
257 supervised_users);
260 void SupervisedUserImportHandler::ClearSupervisedUsersAndShowError() {
261 web_ui()->CallJavascriptFunction(
262 "options.SupervisedUserListData.onSigninError");
265 bool SupervisedUserImportHandler::IsAccountConnected() const {
266 Profile* profile = Profile::FromWebUI(web_ui());
267 SigninManagerBase* signin_manager =
268 SigninManagerFactory::GetForProfile(profile);
269 return signin_manager && signin_manager->IsAuthenticated();
272 bool SupervisedUserImportHandler::HasAuthError() const {
273 Profile* profile = Profile::FromWebUI(web_ui());
274 SigninErrorController* error_controller =
275 SigninErrorControllerFactory::GetForProfile(profile);
276 if (!error_controller)
277 return true;
279 GoogleServiceAuthError::State state = error_controller->auth_error().state();
281 return state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
282 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP ||
283 state == GoogleServiceAuthError::ACCOUNT_DELETED ||
284 state == GoogleServiceAuthError::ACCOUNT_DISABLED;
287 void SupervisedUserImportHandler::OnSharedSettingChanged(
288 const std::string& supervised_user_id,
289 const std::string& key) {
290 if (key == supervised_users::kChromeAvatarIndex)
291 FetchSupervisedUsers();
294 void SupervisedUserImportHandler::OnErrorChanged() {
295 FetchSupervisedUsers();
298 } // namespace options