| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/managed_user_import_handler.h" | 5 #include "chrome/browser/ui/webui/options/managed_user_import_handler.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/managed_mode/managed_user_constants.h" | |
| 14 #include "chrome/browser/managed_mode/managed_user_shared_settings_service.h" | |
| 15 #include "chrome/browser/managed_mode/managed_user_shared_settings_service_facto
ry.h" | |
| 16 #include "chrome/browser/managed_mode/managed_user_sync_service.h" | |
| 17 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h" | |
| 18 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 14 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| 20 #include "chrome/browser/profiles/profile_info_cache.h" | 15 #include "chrome/browser/profiles/profile_info_cache.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 23 #include "chrome/browser/signin/signin_manager_factory.h" | 18 #include "chrome/browser/signin/signin_manager_factory.h" |
| 19 #include "chrome/browser/supervised_user/supervised_user_constants.h" |
| 20 #include "chrome/browser/supervised_user/supervised_user_shared_settings_service
.h" |
| 21 #include "chrome/browser/supervised_user/supervised_user_shared_settings_service
_factory.h" |
| 22 #include "chrome/browser/supervised_user/supervised_user_sync_service.h" |
| 23 #include "chrome/browser/supervised_user/supervised_user_sync_service_factory.h" |
| 24 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 25 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
| 26 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 26 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 27 #include "components/signin/core/browser/signin_error_controller.h" | 27 #include "components/signin/core/browser/signin_error_controller.h" |
| 28 #include "components/signin/core/browser/signin_manager.h" | 28 #include "components/signin/core/browser/signin_manager.h" |
| 29 #include "content/public/browser/web_ui.h" | 29 #include "content/public/browser/web_ui.h" |
| 30 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
| 31 #include "grit/theme_resources.h" | 31 #include "grit/theme_resources.h" |
| 32 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
| 33 | 33 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 47 | 47 |
| 48 namespace options { | 48 namespace options { |
| 49 | 49 |
| 50 ManagedUserImportHandler::ManagedUserImportHandler() | 50 ManagedUserImportHandler::ManagedUserImportHandler() |
| 51 : observer_(this), | 51 : observer_(this), |
| 52 weak_ptr_factory_(this) {} | 52 weak_ptr_factory_(this) {} |
| 53 | 53 |
| 54 ManagedUserImportHandler::~ManagedUserImportHandler() { | 54 ManagedUserImportHandler::~ManagedUserImportHandler() { |
| 55 Profile* profile = Profile::FromWebUI(web_ui()); | 55 Profile* profile = Profile::FromWebUI(web_ui()); |
| 56 if (!profile->IsSupervised()) { | 56 if (!profile->IsSupervised()) { |
| 57 ManagedUserSyncService* service = | 57 SupervisedUserSyncService* service = |
| 58 ManagedUserSyncServiceFactory::GetForProfile(profile); | 58 SupervisedUserSyncServiceFactory::GetForProfile(profile); |
| 59 if (service) | 59 if (service) |
| 60 service->RemoveObserver(this); | 60 service->RemoveObserver(this); |
| 61 subscription_.reset(); | 61 subscription_.reset(); |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 void ManagedUserImportHandler::GetLocalizedValues( | 65 void ManagedUserImportHandler::GetLocalizedValues( |
| 66 base::DictionaryValue* localized_strings) { | 66 base::DictionaryValue* localized_strings) { |
| 67 DCHECK(localized_strings); | 67 DCHECK(localized_strings); |
| 68 | 68 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 80 { "managedUserSelectAvatarOk", IDS_MANAGED_USER_SELECT_AVATAR_OK }, | 80 { "managedUserSelectAvatarOk", IDS_MANAGED_USER_SELECT_AVATAR_OK }, |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 RegisterStrings(localized_strings, resources, arraysize(resources)); | 83 RegisterStrings(localized_strings, resources, arraysize(resources)); |
| 84 localized_strings->Set("avatarIcons", GetAvatarIcons().release()); | 84 localized_strings->Set("avatarIcons", GetAvatarIcons().release()); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void ManagedUserImportHandler::InitializeHandler() { | 87 void ManagedUserImportHandler::InitializeHandler() { |
| 88 Profile* profile = Profile::FromWebUI(web_ui()); | 88 Profile* profile = Profile::FromWebUI(web_ui()); |
| 89 if (!profile->IsSupervised()) { | 89 if (!profile->IsSupervised()) { |
| 90 ManagedUserSyncService* sync_service = | 90 SupervisedUserSyncService* sync_service = |
| 91 ManagedUserSyncServiceFactory::GetForProfile(profile); | 91 SupervisedUserSyncServiceFactory::GetForProfile(profile); |
| 92 if (sync_service) { | 92 if (sync_service) { |
| 93 sync_service->AddObserver(this); | 93 sync_service->AddObserver(this); |
| 94 observer_.Add(ProfileOAuth2TokenServiceFactory::GetForProfile(profile)-> | 94 observer_.Add(ProfileOAuth2TokenServiceFactory::GetForProfile(profile)-> |
| 95 signin_error_controller()); | 95 signin_error_controller()); |
| 96 ManagedUserSharedSettingsService* settings_service = | 96 SupervisedUserSharedSettingsService* settings_service = |
| 97 ManagedUserSharedSettingsServiceFactory::GetForBrowserContext( | 97 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext( |
| 98 profile); | 98 profile); |
| 99 subscription_ = settings_service->Subscribe( | 99 subscription_ = settings_service->Subscribe( |
| 100 base::Bind(&ManagedUserImportHandler::OnSharedSettingChanged, | 100 base::Bind(&ManagedUserImportHandler::OnSharedSettingChanged, |
| 101 weak_ptr_factory_.GetWeakPtr())); | 101 weak_ptr_factory_.GetWeakPtr())); |
| 102 } else { | 102 } else { |
| 103 DCHECK(!ManagedUserSharedSettingsServiceFactory::GetForBrowserContext( | 103 DCHECK(!SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext( |
| 104 profile)); | 104 profile)); |
| 105 DCHECK(!ProfileOAuth2TokenServiceFactory::GetForProfile(profile)); | 105 DCHECK(!ProfileOAuth2TokenServiceFactory::GetForProfile(profile)); |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 | 109 |
| 110 void ManagedUserImportHandler::RegisterMessages() { | 110 void ManagedUserImportHandler::RegisterMessages() { |
| 111 web_ui()->RegisterMessageCallback("requestManagedUserImportUpdate", | 111 web_ui()->RegisterMessageCallback("requestManagedUserImportUpdate", |
| 112 base::Bind(&ManagedUserImportHandler::RequestManagedUserImportUpdate, | 112 base::Bind(&ManagedUserImportHandler::RequestManagedUserImportUpdate, |
| 113 base::Unretained(this))); | 113 base::Unretained(this))); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void ManagedUserImportHandler::OnManagedUsersChanged() { | 116 void ManagedUserImportHandler::OnSupervisedUsersChanged() { |
| 117 FetchManagedUsers(); | 117 FetchManagedUsers(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void ManagedUserImportHandler::FetchManagedUsers() { | 120 void ManagedUserImportHandler::FetchManagedUsers() { |
| 121 web_ui()->CallJavascriptFunction("options.ManagedUserListData.resetPromise"); | 121 web_ui()->CallJavascriptFunction("options.ManagedUserListData.resetPromise"); |
| 122 RequestManagedUserImportUpdate(NULL); | 122 RequestManagedUserImportUpdate(NULL); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void ManagedUserImportHandler::RequestManagedUserImportUpdate( | 125 void ManagedUserImportHandler::RequestManagedUserImportUpdate( |
| 126 const base::ListValue* /* args */) { | 126 const base::ListValue* /* args */) { |
| 127 if (Profile::FromWebUI(web_ui())->IsSupervised()) | 127 if (Profile::FromWebUI(web_ui())->IsSupervised()) |
| 128 return; | 128 return; |
| 129 | 129 |
| 130 if (!IsAccountConnected() || HasAuthError()) { | 130 if (!IsAccountConnected() || HasAuthError()) { |
| 131 ClearManagedUsersAndShowError(); | 131 ClearManagedUsersAndShowError(); |
| 132 } else { | 132 } else { |
| 133 ManagedUserSyncService* managed_user_sync_service = | 133 SupervisedUserSyncService* supervised_user_sync_service = |
| 134 ManagedUserSyncServiceFactory::GetForProfile( | 134 SupervisedUserSyncServiceFactory::GetForProfile( |
| 135 Profile::FromWebUI(web_ui())); | 135 Profile::FromWebUI(web_ui())); |
| 136 if (managed_user_sync_service) { | 136 if (supervised_user_sync_service) { |
| 137 managed_user_sync_service->GetManagedUsersAsync( | 137 supervised_user_sync_service->GetSupervisedUsersAsync( |
| 138 base::Bind(&ManagedUserImportHandler::SendExistingManagedUsers, | 138 base::Bind(&ManagedUserImportHandler::SendExistingManagedUsers, |
| 139 weak_ptr_factory_.GetWeakPtr())); | 139 weak_ptr_factory_.GetWeakPtr())); |
| 140 } | 140 } |
| 141 } | 141 } |
| 142 } | 142 } |
| 143 | 143 |
| 144 void ManagedUserImportHandler::SendExistingManagedUsers( | 144 void ManagedUserImportHandler::SendExistingManagedUsers( |
| 145 const base::DictionaryValue* dict) { | 145 const base::DictionaryValue* dict) { |
| 146 DCHECK(dict); | 146 DCHECK(dict); |
| 147 const ProfileInfoCache& cache = | 147 const ProfileInfoCache& cache = |
| 148 g_browser_process->profile_manager()->GetProfileInfoCache(); | 148 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 149 | 149 |
| 150 // Collect the ids of local supervised user profiles. | 150 // Collect the ids of local supervised user profiles. |
| 151 std::set<std::string> managed_user_ids; | 151 std::set<std::string> supervised_user_ids; |
| 152 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) { | 152 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) { |
| 153 if (cache.ProfileIsSupervisedAtIndex(i)) | 153 if (cache.ProfileIsSupervisedAtIndex(i)) |
| 154 managed_user_ids.insert(cache.GetSupervisedUserIdOfProfileAtIndex(i)); | 154 supervised_user_ids.insert(cache.GetSupervisedUserIdOfProfileAtIndex(i)); |
| 155 } | 155 } |
| 156 | 156 |
| 157 base::ListValue managed_users; | 157 base::ListValue supervised_users; |
| 158 Profile* profile = Profile::FromWebUI(web_ui()); | 158 Profile* profile = Profile::FromWebUI(web_ui()); |
| 159 ManagedUserSharedSettingsService* service = | 159 SupervisedUserSharedSettingsService* service = |
| 160 ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(profile); | 160 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(profile); |
| 161 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | 161 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { |
| 162 const base::DictionaryValue* value = NULL; | 162 const base::DictionaryValue* value = NULL; |
| 163 bool success = it.value().GetAsDictionary(&value); | 163 bool success = it.value().GetAsDictionary(&value); |
| 164 DCHECK(success); | 164 DCHECK(success); |
| 165 std::string name; | 165 std::string name; |
| 166 value->GetString(ManagedUserSyncService::kName, &name); | 166 value->GetString(SupervisedUserSyncService::kName, &name); |
| 167 | 167 |
| 168 base::DictionaryValue* managed_user = new base::DictionaryValue; | 168 base::DictionaryValue* supervised_user = new base::DictionaryValue; |
| 169 managed_user->SetString("id", it.key()); | 169 supervised_user->SetString("id", it.key()); |
| 170 managed_user->SetString("name", name); | 170 supervised_user->SetString("name", name); |
| 171 | 171 |
| 172 int avatar_index = ManagedUserSyncService::kNoAvatar; | 172 int avatar_index = SupervisedUserSyncService::kNoAvatar; |
| 173 const base::Value* avatar_index_value = | 173 const base::Value* avatar_index_value = |
| 174 service->GetValue(it.key(), managed_users::kChromeAvatarIndex); | 174 service->GetValue(it.key(), supervised_users::kChromeAvatarIndex); |
| 175 if (avatar_index_value) { | 175 if (avatar_index_value) { |
| 176 success = avatar_index_value->GetAsInteger(&avatar_index); | 176 success = avatar_index_value->GetAsInteger(&avatar_index); |
| 177 } else { | 177 } else { |
| 178 // Check if there is a legacy avatar index stored. | 178 // Check if there is a legacy avatar index stored. |
| 179 std::string avatar_str; | 179 std::string avatar_str; |
| 180 value->GetString(ManagedUserSyncService::kChromeAvatar, &avatar_str); | 180 value->GetString(SupervisedUserSyncService::kChromeAvatar, &avatar_str); |
| 181 success = | 181 success = |
| 182 ManagedUserSyncService::GetAvatarIndex(avatar_str, &avatar_index); | 182 SupervisedUserSyncService::GetAvatarIndex(avatar_str, &avatar_index); |
| 183 } | 183 } |
| 184 DCHECK(success); | 184 DCHECK(success); |
| 185 managed_user->SetBoolean("needAvatar", | 185 supervised_user->SetBoolean( |
| 186 avatar_index == ManagedUserSyncService::kNoAvatar); | 186 "needAvatar", |
| 187 avatar_index == SupervisedUserSyncService::kNoAvatar); |
| 187 | 188 |
| 188 std::string supervised_user_icon = | 189 std::string supervised_user_icon = |
| 189 std::string(chrome::kChromeUIThemeURL) + | 190 std::string(chrome::kChromeUIThemeURL) + |
| 190 "IDR_SUPERVISED_USER_PLACEHOLDER"; | 191 "IDR_SUPERVISED_USER_PLACEHOLDER"; |
| 191 std::string avatar_url = | 192 std::string avatar_url = |
| 192 avatar_index == ManagedUserSyncService::kNoAvatar ? | 193 avatar_index == SupervisedUserSyncService::kNoAvatar ? |
| 193 supervised_user_icon : | 194 supervised_user_icon : |
| 194 profiles::GetDefaultAvatarIconUrl(avatar_index); | 195 profiles::GetDefaultAvatarIconUrl(avatar_index); |
| 195 managed_user->SetString("iconURL", avatar_url); | 196 supervised_user->SetString("iconURL", avatar_url); |
| 196 bool on_current_device = | 197 bool on_current_device = |
| 197 managed_user_ids.find(it.key()) != managed_user_ids.end(); | 198 supervised_user_ids.find(it.key()) != supervised_user_ids.end(); |
| 198 managed_user->SetBoolean("onCurrentDevice", on_current_device); | 199 supervised_user->SetBoolean("onCurrentDevice", on_current_device); |
| 199 | 200 |
| 200 managed_users.Append(managed_user); | 201 supervised_users.Append(supervised_user); |
| 201 } | 202 } |
| 202 | 203 |
| 203 web_ui()->CallJavascriptFunction( | 204 web_ui()->CallJavascriptFunction( |
| 204 "options.ManagedUserListData.receiveExistingManagedUsers", | 205 "options.ManagedUserListData.receiveExistingManagedUsers", |
| 205 managed_users); | 206 supervised_users); |
| 206 } | 207 } |
| 207 | 208 |
| 208 void ManagedUserImportHandler::ClearManagedUsersAndShowError() { | 209 void ManagedUserImportHandler::ClearManagedUsersAndShowError() { |
| 209 web_ui()->CallJavascriptFunction("options.ManagedUserListData.onSigninError"); | 210 web_ui()->CallJavascriptFunction("options.ManagedUserListData.onSigninError"); |
| 210 } | 211 } |
| 211 | 212 |
| 212 bool ManagedUserImportHandler::IsAccountConnected() const { | 213 bool ManagedUserImportHandler::IsAccountConnected() const { |
| 213 Profile* profile = Profile::FromWebUI(web_ui()); | 214 Profile* profile = Profile::FromWebUI(web_ui()); |
| 214 SigninManagerBase* signin_manager = | 215 SigninManagerBase* signin_manager = |
| 215 SigninManagerFactory::GetForProfile(profile); | 216 SigninManagerFactory::GetForProfile(profile); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 228 | 229 |
| 229 GoogleServiceAuthError::State state = error_controller->auth_error().state(); | 230 GoogleServiceAuthError::State state = error_controller->auth_error().state(); |
| 230 | 231 |
| 231 return state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS || | 232 return state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS || |
| 232 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP || | 233 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP || |
| 233 state == GoogleServiceAuthError::ACCOUNT_DELETED || | 234 state == GoogleServiceAuthError::ACCOUNT_DELETED || |
| 234 state == GoogleServiceAuthError::ACCOUNT_DISABLED; | 235 state == GoogleServiceAuthError::ACCOUNT_DISABLED; |
| 235 } | 236 } |
| 236 | 237 |
| 237 void ManagedUserImportHandler::OnSharedSettingChanged( | 238 void ManagedUserImportHandler::OnSharedSettingChanged( |
| 238 const std::string& managed_user_id, | 239 const std::string& supervised_user_id, |
| 239 const std::string& key) { | 240 const std::string& key) { |
| 240 if (key == managed_users::kChromeAvatarIndex) | 241 if (key == supervised_users::kChromeAvatarIndex) |
| 241 FetchManagedUsers(); | 242 FetchManagedUsers(); |
| 242 } | 243 } |
| 243 | 244 |
| 244 void ManagedUserImportHandler::OnErrorChanged() { | 245 void ManagedUserImportHandler::OnErrorChanged() { |
| 245 FetchManagedUsers(); | 246 FetchManagedUsers(); |
| 246 } | 247 } |
| 247 | 248 |
| 248 } // namespace options | 249 } // namespace options |
| OLD | NEW |