| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "chrome/browser/ui/webui/signin/signin_supervised_user_import_handler.h
" |
| 6 |
| 7 #include <stddef.h> |
| 8 |
| 9 #include <set> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/value_conversions.h" |
| 15 #include "base/values.h" |
| 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| 19 #include "chrome/browser/profiles/profile_info_cache.h" |
| 20 #include "chrome/browser/profiles/profile_manager.h" |
| 21 #include "chrome/browser/signin/signin_error_controller_factory.h" |
| 22 #include "chrome/browser/signin/signin_manager_factory.h" |
| 23 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_
service.h" |
| 24 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_
service_factory.h" |
| 25 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service.h" |
| 26 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service_fac
tory.h" |
| 27 #include "chrome/browser/supervised_user/supervised_user_constants.h" |
| 28 #include "chrome/common/url_constants.h" |
| 29 #include "chrome/grit/generated_resources.h" |
| 30 #include "components/prefs/pref_service.h" |
| 31 #include "components/signin/core/browser/signin_error_controller.h" |
| 32 #include "components/signin/core/browser/signin_manager.h" |
| 33 #include "content/public/browser/web_ui.h" |
| 34 #include "grit/theme_resources.h" |
| 35 #include "ui/base/l10n/l10n_util.h" |
| 36 |
| 37 |
| 38 SigninSupervisedUserImportHandler::SigninSupervisedUserImportHandler() |
| 39 : weak_ptr_factory_(this) { |
| 40 } |
| 41 |
| 42 SigninSupervisedUserImportHandler::~SigninSupervisedUserImportHandler() { |
| 43 } |
| 44 |
| 45 void SigninSupervisedUserImportHandler::RegisterMessages() { |
| 46 web_ui()->RegisterMessageCallback("getExistingSupervisedUsers", |
| 47 base::Bind(&SigninSupervisedUserImportHandler:: |
| 48 GetExistingSupervisedUsers, |
| 49 base::Unretained(this))); |
| 50 } |
| 51 |
| 52 void SigninSupervisedUserImportHandler::AssignWebUICallbackId( |
| 53 const base::ListValue* args) { |
| 54 CHECK_LE(1U, args->GetSize()); |
| 55 CHECK(webui_callback_id_.empty()); |
| 56 CHECK(args->GetString(0, &webui_callback_id_)); |
| 57 } |
| 58 |
| 59 void SigninSupervisedUserImportHandler::GetExistingSupervisedUsers( |
| 60 const base::ListValue* args) { |
| 61 CHECK_EQ(2U, args->GetSize()); |
| 62 AssignWebUICallbackId(args); |
| 63 |
| 64 base::FilePath custodian_profile_path; |
| 65 const base::Value* profile_path_value; |
| 66 bool success = args->Get(1, &profile_path_value); |
| 67 DCHECK(success); |
| 68 success = base::GetValueAsFilePath(*profile_path_value, |
| 69 &custodian_profile_path); |
| 70 DCHECK(success); |
| 71 |
| 72 // Load custodian profile. |
| 73 g_browser_process->profile_manager()->CreateProfileAsync( |
| 74 custodian_profile_path, |
| 75 base::Bind( |
| 76 &SigninSupervisedUserImportHandler::LoadCustodianProfileCallback, |
| 77 weak_ptr_factory_.GetWeakPtr()), |
| 78 base::string16(), std::string(), std::string()); |
| 79 } |
| 80 |
| 81 void SigninSupervisedUserImportHandler::LoadCustodianProfileCallback( |
| 82 Profile* profile, Profile::CreateStatus status) { |
| 83 |
| 84 // This method gets called once before with Profile::CREATE_STATUS_CREATED. |
| 85 switch (status) { |
| 86 case Profile::CREATE_STATUS_LOCAL_FAIL: { |
| 87 // TODO(mahmadi): see if a better error message is required here. |
| 88 RejectCallback(GetLocalErorrMessage()); |
| 89 break; |
| 90 } |
| 91 case Profile::CREATE_STATUS_CREATED: { |
| 92 // Ignore the intermediate status. |
| 93 break; |
| 94 } |
| 95 case Profile::CREATE_STATUS_INITIALIZED: { |
| 96 // We are only interested in Profile::CREATE_STATUS_INITIALIZED when |
| 97 // everything is ready. |
| 98 if (profile->IsSupervised()) |
| 99 return; |
| 100 |
| 101 if (!IsAccountConnected(profile) || HasAuthError(profile)) { |
| 102 RejectCallback(GetAuthErorrMessage()); |
| 103 return; |
| 104 } |
| 105 |
| 106 SupervisedUserSyncService* supervised_user_sync_service = |
| 107 SupervisedUserSyncServiceFactory::GetForProfile(profile); |
| 108 if (supervised_user_sync_service) { |
| 109 supervised_user_sync_service->GetSupervisedUsersAsync( |
| 110 base::Bind( |
| 111 &SigninSupervisedUserImportHandler::SendExistingSupervisedUsers, |
| 112 weak_ptr_factory_.GetWeakPtr(), profile)); |
| 113 } |
| 114 break; |
| 115 } |
| 116 case Profile::CREATE_STATUS_CANCELED: |
| 117 case Profile::CREATE_STATUS_REMOTE_FAIL: |
| 118 case Profile::MAX_CREATE_STATUS: { |
| 119 NOTREACHED(); |
| 120 break; |
| 121 } |
| 122 } |
| 123 } |
| 124 |
| 125 void SigninSupervisedUserImportHandler::RejectCallback( |
| 126 const base::string16& error) { |
| 127 web_ui()->CallJavascriptFunction("cr.webUIResponse", |
| 128 base::StringValue(webui_callback_id_), |
| 129 base::FundamentalValue(false), |
| 130 base::StringValue(error)); |
| 131 webui_callback_id_.clear(); |
| 132 } |
| 133 |
| 134 base::string16 SigninSupervisedUserImportHandler::GetLocalErorrMessage() const { |
| 135 return l10n_util::GetStringUTF16( |
| 136 IDS_LEGACY_SUPERVISED_USER_IMPORT_LOCAL_ERROR); |
| 137 } |
| 138 |
| 139 base::string16 SigninSupervisedUserImportHandler::GetAuthErorrMessage() const { |
| 140 return l10n_util::GetStringUTF16( |
| 141 IDS_PROFILES_CREATE_CUSTODIAN_ACCOUNT_DETAILS_OUT_OF_DATE_ERROR); |
| 142 } |
| 143 |
| 144 void SigninSupervisedUserImportHandler::SendExistingSupervisedUsers( |
| 145 Profile* profile, |
| 146 const base::DictionaryValue* dict) { |
| 147 DCHECK(dict); |
| 148 ProfileInfoCache& cache = |
| 149 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 150 std::vector<ProfileAttributesEntry*> entries = |
| 151 cache.GetAllProfilesAttributes(); |
| 152 |
| 153 // Collect the ids of local supervised user profiles. |
| 154 std::set<std::string> supervised_user_ids; |
| 155 for (auto& entry : entries) { |
| 156 // Filter out omitted profiles. These are currently being imported, and |
| 157 // shouldn't show up as "already on this device" just yet. |
| 158 if (entry->IsLegacySupervised() && !entry->IsOmitted()) { |
| 159 supervised_user_ids.insert(entry->GetSupervisedUserId()); |
| 160 } |
| 161 } |
| 162 |
| 163 base::ListValue supervised_users; |
| 164 SupervisedUserSharedSettingsService* service = |
| 165 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(profile); |
| 166 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { |
| 167 const base::DictionaryValue* value = NULL; |
| 168 bool success = it.value().GetAsDictionary(&value); |
| 169 DCHECK(success); |
| 170 std::string name; |
| 171 value->GetString(SupervisedUserSyncService::kName, &name); |
| 172 |
| 173 base::DictionaryValue* supervised_user = new base::DictionaryValue; |
| 174 supervised_user->SetString("id", it.key()); |
| 175 supervised_user->SetString("name", name); |
| 176 |
| 177 int avatar_index = SupervisedUserSyncService::kNoAvatar; |
| 178 const base::Value* avatar_index_value = |
| 179 service->GetValue(it.key(), supervised_users::kChromeAvatarIndex); |
| 180 if (avatar_index_value) { |
| 181 success = avatar_index_value->GetAsInteger(&avatar_index); |
| 182 } else { |
| 183 // Check if there is a legacy avatar index stored. |
| 184 std::string avatar_str; |
| 185 value->GetString(SupervisedUserSyncService::kChromeAvatar, &avatar_str); |
| 186 success = |
| 187 SupervisedUserSyncService::GetAvatarIndex(avatar_str, &avatar_index); |
| 188 } |
| 189 DCHECK(success); |
| 190 |
| 191 std::string avatar_url = |
| 192 avatar_index == SupervisedUserSyncService::kNoAvatar ? |
| 193 profiles::GetDefaultAvatarIconUrl( |
| 194 profiles::GetPlaceholderAvatarIndex()) : |
| 195 profiles::GetDefaultAvatarIconUrl(avatar_index); |
| 196 supervised_user->SetString("iconURL", avatar_url); |
| 197 bool on_current_device = |
| 198 supervised_user_ids.find(it.key()) != supervised_user_ids.end(); |
| 199 supervised_user->SetBoolean("onCurrentDevice", on_current_device); |
| 200 |
| 201 supervised_users.Append(supervised_user); |
| 202 } |
| 203 |
| 204 // Resolve callback with response. |
| 205 web_ui()->CallJavascriptFunction("cr.webUIResponse", |
| 206 base::StringValue(webui_callback_id_), |
| 207 base::FundamentalValue(true), |
| 208 supervised_users); |
| 209 webui_callback_id_.clear(); |
| 210 } |
| 211 |
| 212 bool SigninSupervisedUserImportHandler::IsAccountConnected( |
| 213 Profile* profile) const { |
| 214 SigninManagerBase* signin_manager = |
| 215 SigninManagerFactory::GetForProfile(profile); |
| 216 return signin_manager && signin_manager->IsAuthenticated(); |
| 217 } |
| 218 |
| 219 bool SigninSupervisedUserImportHandler::HasAuthError(Profile* profile) const { |
| 220 SigninErrorController* error_controller = |
| 221 SigninErrorControllerFactory::GetForProfile(profile); |
| 222 if (!error_controller) |
| 223 return true; |
| 224 |
| 225 GoogleServiceAuthError::State state = error_controller->auth_error().state(); |
| 226 return state != GoogleServiceAuthError::NONE; |
| 227 } |
| OLD | NEW |