Chromium Code Reviews| 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 supervisor_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 &supervisor_profile_path); | |
|
Roger Tawa OOO till Jul 10th
2016/03/24 14:39:05
Indent 3 more.
Moe
2016/03/24 22:07:57
Done.
| |
| 70 DCHECK(success); | |
| 71 | |
| 72 // Load supervisor profile. | |
| 73 g_browser_process->profile_manager()->CreateProfileAsync( | |
| 74 supervisor_profile_path, | |
| 75 base::Bind( | |
| 76 &SigninSupervisedUserImportHandler::LoadSupervisorProfileCallback, | |
| 77 weak_ptr_factory_.GetWeakPtr()), | |
| 78 base::string16(), std::string(), std::string()); | |
| 79 } | |
| 80 | |
| 81 void SigninSupervisedUserImportHandler::LoadSupervisorProfileCallback( | |
| 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 break; | |
| 88 } | |
| 89 case Profile::CREATE_STATUS_CREATED: { | |
| 90 // Ignore the intermediate status. | |
| 91 break; | |
| 92 } | |
| 93 case Profile::CREATE_STATUS_INITIALIZED: { | |
| 94 // We are only interested in Profile::CREATE_STATUS_INITIALIZED when | |
| 95 // everything is ready. | |
| 96 if (profile->IsSupervised()) | |
| 97 return; | |
| 98 | |
| 99 if (!IsAccountConnected(profile) || HasAuthError(profile)) { | |
| 100 // Reject callback with error message. | |
| 101 web_ui()->CallJavascriptFunction("cr.webUIResponse", | |
| 102 base::StringValue(webui_callback_id_), | |
| 103 base::FundamentalValue(false), | |
| 104 base::StringValue(GetAuthErorrMessage())); | |
| 105 webui_callback_id_.clear(); | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 SupervisedUserSyncService* supervised_user_sync_service = | |
| 110 SupervisedUserSyncServiceFactory::GetForProfile(profile); | |
| 111 if (supervised_user_sync_service) { | |
| 112 supervised_user_sync_service->GetSupervisedUsersAsync( | |
| 113 base::Bind( | |
| 114 &SigninSupervisedUserImportHandler::SendExistingSupervisedUsers, | |
| 115 weak_ptr_factory_.GetWeakPtr(), profile)); | |
| 116 } | |
| 117 break; | |
| 118 } | |
| 119 case Profile::CREATE_STATUS_CANCELED: | |
| 120 case Profile::CREATE_STATUS_REMOTE_FAIL: | |
| 121 case Profile::MAX_CREATE_STATUS: { | |
| 122 NOTREACHED(); | |
| 123 break; | |
|
Roger Tawa OOO till Jul 10th
2016/03/24 14:39:05
It seems like some code paths will not invoke "cr.
Moe
2016/03/24 22:07:57
Added error handling for the CREATE_STATUS_LOCAL_F
| |
| 124 } | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 base::string16 SigninSupervisedUserImportHandler::GetAuthErorrMessage() const { | |
| 129 return l10n_util::GetStringUTF16( | |
| 130 IDS_PROFILES_CREATE_SUPERVISOR_ACCOUNT_DETAILS_OUT_OF_DATE_ERROR); | |
| 131 } | |
| 132 | |
| 133 void SigninSupervisedUserImportHandler::SendExistingSupervisedUsers( | |
| 134 Profile* profile, const base::DictionaryValue* dict) { | |
|
Roger Tawa OOO till Jul 10th
2016/03/24 14:39:05
Nit: put |dict| on next line.
Moe
2016/03/24 22:07:57
Done.
| |
| 135 DCHECK(dict); | |
| 136 ProfileInfoCache& cache = | |
| 137 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 138 std::vector<ProfileAttributesEntry*> entries = | |
| 139 cache.GetAllProfilesAttributes(); | |
| 140 | |
| 141 // Collect the ids of local supervised user profiles. | |
| 142 std::set<std::string> supervised_user_ids; | |
| 143 for (auto& entry : entries) { | |
| 144 // Filter out omitted profiles. These are currently being imported, and | |
| 145 // shouldn't show up as "already on this device" just yet. | |
| 146 if (entry->IsLegacySupervised() && !entry->IsOmitted()) { | |
| 147 supervised_user_ids.insert(entry->GetSupervisedUserId()); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 base::ListValue supervised_users; | |
| 152 SupervisedUserSharedSettingsService* service = | |
| 153 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(profile); | |
| 154 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | |
| 155 const base::DictionaryValue* value = NULL; | |
| 156 bool success = it.value().GetAsDictionary(&value); | |
| 157 DCHECK(success); | |
| 158 std::string name; | |
| 159 value->GetString(SupervisedUserSyncService::kName, &name); | |
| 160 | |
| 161 base::DictionaryValue* supervised_user = new base::DictionaryValue; | |
| 162 supervised_user->SetString("id", it.key()); | |
| 163 supervised_user->SetString("name", name); | |
| 164 | |
| 165 int avatar_index = SupervisedUserSyncService::kNoAvatar; | |
| 166 const base::Value* avatar_index_value = | |
| 167 service->GetValue(it.key(), supervised_users::kChromeAvatarIndex); | |
| 168 if (avatar_index_value) { | |
| 169 success = avatar_index_value->GetAsInteger(&avatar_index); | |
| 170 } else { | |
| 171 // Check if there is a legacy avatar index stored. | |
| 172 std::string avatar_str; | |
| 173 value->GetString(SupervisedUserSyncService::kChromeAvatar, &avatar_str); | |
| 174 success = | |
| 175 SupervisedUserSyncService::GetAvatarIndex(avatar_str, &avatar_index); | |
| 176 } | |
| 177 DCHECK(success); | |
| 178 | |
| 179 std::string avatar_url = | |
| 180 avatar_index == SupervisedUserSyncService::kNoAvatar ? | |
| 181 profiles::GetDefaultAvatarIconUrl( | |
| 182 profiles::GetPlaceholderAvatarIndex()) : | |
| 183 profiles::GetDefaultAvatarIconUrl(avatar_index); | |
| 184 supervised_user->SetString("iconURL", avatar_url); | |
| 185 bool on_current_device = | |
| 186 supervised_user_ids.find(it.key()) != supervised_user_ids.end(); | |
| 187 supervised_user->SetBoolean("onCurrentDevice", on_current_device); | |
| 188 | |
| 189 supervised_users.Append(supervised_user); | |
| 190 } | |
| 191 | |
| 192 // Resolve callback with response. | |
| 193 web_ui()->CallJavascriptFunction("cr.webUIResponse", | |
| 194 base::StringValue(webui_callback_id_), | |
| 195 base::FundamentalValue(true), | |
| 196 supervised_users); | |
| 197 webui_callback_id_.clear(); | |
| 198 } | |
| 199 | |
| 200 bool SigninSupervisedUserImportHandler::IsAccountConnected( | |
| 201 Profile* profile) const { | |
| 202 SigninManagerBase* signin_manager = | |
| 203 SigninManagerFactory::GetForProfile(profile); | |
| 204 return signin_manager && signin_manager->IsAuthenticated(); | |
| 205 } | |
| 206 | |
| 207 bool SigninSupervisedUserImportHandler::HasAuthError(Profile* profile) const { | |
| 208 SigninErrorController* error_controller = | |
| 209 SigninErrorControllerFactory::GetForProfile(profile); | |
| 210 if (!error_controller) | |
| 211 return true; | |
| 212 | |
| 213 GoogleServiceAuthError::State state = error_controller->auth_error().state(); | |
| 214 | |
| 215 return state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS || | |
| 216 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP || | |
| 217 state == GoogleServiceAuthError::ACCOUNT_DELETED || | |
| 218 state == GoogleServiceAuthError::ACCOUNT_DISABLED; | |
|
Roger Tawa OOO till Jul 10th
2016/03/24 14:39:05
Same comment as earlier.
Moe
2016/03/24 22:07:57
Done.
| |
| 219 } | |
| OLD | NEW |