Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / managed_user_import_handler.cc
blobc19021b0f282d69af3146cdc941ad94560b5ed96
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/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_factory.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"
19 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
20 #include "chrome/browser/profiles/profile_info_cache.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
23 #include "chrome/browser/signin/signin_manager_factory.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/common/url_constants.h"
26 #include "components/signin/core/browser/profile_oauth2_token_service.h"
27 #include "components/signin/core/browser/signin_error_controller.h"
28 #include "components/signin/core/browser/signin_manager.h"
29 #include "content/public/browser/web_ui.h"
30 #include "grit/generated_resources.h"
31 #include "grit/theme_resources.h"
32 #include "ui/base/l10n/l10n_util.h"
34 namespace {
36 scoped_ptr<base::ListValue> GetAvatarIcons() {
37 scoped_ptr<base::ListValue> avatar_icons(new base::ListValue);
38 for (size_t i = 0; i < profiles::GetDefaultAvatarIconCount(); ++i) {
39 std::string avatar_url = profiles::GetDefaultAvatarIconUrl(i);
40 avatar_icons->Append(new base::StringValue(avatar_url));
43 return avatar_icons.Pass();
46 } // namespace
48 namespace options {
50 ManagedUserImportHandler::ManagedUserImportHandler()
51 : observer_(this),
52 weak_ptr_factory_(this) {}
54 ManagedUserImportHandler::~ManagedUserImportHandler() {
55 Profile* profile = Profile::FromWebUI(web_ui());
56 if (!profile->IsManaged()) {
57 ManagedUserSyncService* service =
58 ManagedUserSyncServiceFactory::GetForProfile(profile);
59 if (service)
60 service->RemoveObserver(this);
61 subscription_.reset();
65 void ManagedUserImportHandler::GetLocalizedValues(
66 base::DictionaryValue* localized_strings) {
67 DCHECK(localized_strings);
69 static OptionsStringResource resources[] = {
70 { "managedUserImportTitle", IDS_IMPORT_EXISTING_MANAGED_USER_TITLE },
71 { "managedUserImportText", IDS_IMPORT_EXISTING_MANAGED_USER_TEXT },
72 { "createNewUserLink", IDS_CREATE_NEW_USER_LINK },
73 { "managedUserImportOk", IDS_IMPORT_EXISTING_MANAGED_USER_OK },
74 { "managedUserImportSigninError", IDS_MANAGED_USER_IMPORT_SIGN_IN_ERROR },
75 { "managedUserAlreadyOnThisDevice",
76 IDS_MANAGED_USER_ALREADY_ON_THIS_DEVICE },
77 { "noExistingManagedUsers", IDS_MANAGED_USER_NO_EXISTING_ERROR },
78 { "managedUserSelectAvatarTitle", IDS_MANAGED_USER_SELECT_AVATAR_TITLE },
79 { "managedUserSelectAvatarText", IDS_MANAGED_USER_SELECT_AVATAR_TEXT },
80 { "managedUserSelectAvatarOk", IDS_MANAGED_USER_SELECT_AVATAR_OK },
83 RegisterStrings(localized_strings, resources, arraysize(resources));
84 localized_strings->Set("avatarIcons", GetAvatarIcons().release());
87 void ManagedUserImportHandler::InitializeHandler() {
88 Profile* profile = Profile::FromWebUI(web_ui());
89 if (!profile->IsManaged()) {
90 ManagedUserSyncService* sync_service =
91 ManagedUserSyncServiceFactory::GetForProfile(profile);
92 if (sync_service) {
93 sync_service->AddObserver(this);
94 observer_.Add(ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->
95 signin_error_controller());
96 ManagedUserSharedSettingsService* settings_service =
97 ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(
98 profile);
99 subscription_ = settings_service->Subscribe(
100 base::Bind(&ManagedUserImportHandler::OnSharedSettingChanged,
101 weak_ptr_factory_.GetWeakPtr()));
102 } else {
103 DCHECK(!ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(
104 profile));
105 DCHECK(!ProfileOAuth2TokenServiceFactory::GetForProfile(profile));
110 void ManagedUserImportHandler::RegisterMessages() {
111 web_ui()->RegisterMessageCallback("requestManagedUserImportUpdate",
112 base::Bind(&ManagedUserImportHandler::RequestManagedUserImportUpdate,
113 base::Unretained(this)));
116 void ManagedUserImportHandler::OnManagedUsersChanged() {
117 FetchManagedUsers();
120 void ManagedUserImportHandler::FetchManagedUsers() {
121 web_ui()->CallJavascriptFunction("options.ManagedUserListData.resetPromise");
122 RequestManagedUserImportUpdate(NULL);
125 void ManagedUserImportHandler::RequestManagedUserImportUpdate(
126 const base::ListValue* /* args */) {
127 if (Profile::FromWebUI(web_ui())->IsManaged())
128 return;
130 if (!IsAccountConnected() || HasAuthError()) {
131 ClearManagedUsersAndShowError();
132 } else {
133 ManagedUserSyncService* managed_user_sync_service =
134 ManagedUserSyncServiceFactory::GetForProfile(
135 Profile::FromWebUI(web_ui()));
136 if (managed_user_sync_service) {
137 managed_user_sync_service->GetManagedUsersAsync(
138 base::Bind(&ManagedUserImportHandler::SendExistingManagedUsers,
139 weak_ptr_factory_.GetWeakPtr()));
144 void ManagedUserImportHandler::SendExistingManagedUsers(
145 const base::DictionaryValue* dict) {
146 DCHECK(dict);
147 const ProfileInfoCache& cache =
148 g_browser_process->profile_manager()->GetProfileInfoCache();
150 // Collect the ids of local supervised user profiles.
151 std::set<std::string> managed_user_ids;
152 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) {
153 if (cache.ProfileIsManagedAtIndex(i))
154 managed_user_ids.insert(cache.GetManagedUserIdOfProfileAtIndex(i));
157 base::ListValue managed_users;
158 Profile* profile = Profile::FromWebUI(web_ui());
159 ManagedUserSharedSettingsService* service =
160 ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(profile);
161 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
162 const base::DictionaryValue* value = NULL;
163 bool success = it.value().GetAsDictionary(&value);
164 DCHECK(success);
165 std::string name;
166 value->GetString(ManagedUserSyncService::kName, &name);
168 base::DictionaryValue* managed_user = new base::DictionaryValue;
169 managed_user->SetString("id", it.key());
170 managed_user->SetString("name", name);
172 int avatar_index = ManagedUserSyncService::kNoAvatar;
173 const base::Value* avatar_index_value =
174 service->GetValue(it.key(), managed_users::kChromeAvatarIndex);
175 if (avatar_index_value) {
176 success = avatar_index_value->GetAsInteger(&avatar_index);
177 } else {
178 // Check if there is a legacy avatar index stored.
179 std::string avatar_str;
180 value->GetString(ManagedUserSyncService::kChromeAvatar, &avatar_str);
181 success =
182 ManagedUserSyncService::GetAvatarIndex(avatar_str, &avatar_index);
184 DCHECK(success);
185 managed_user->SetBoolean("needAvatar",
186 avatar_index == ManagedUserSyncService::kNoAvatar);
188 std::string supervised_user_icon =
189 std::string(chrome::kChromeUIThemeURL) +
190 "IDR_SUPERVISED_USER_PLACEHOLDER";
191 std::string avatar_url =
192 avatar_index == ManagedUserSyncService::kNoAvatar ?
193 supervised_user_icon :
194 profiles::GetDefaultAvatarIconUrl(avatar_index);
195 managed_user->SetString("iconURL", avatar_url);
196 bool on_current_device =
197 managed_user_ids.find(it.key()) != managed_user_ids.end();
198 managed_user->SetBoolean("onCurrentDevice", on_current_device);
200 managed_users.Append(managed_user);
203 web_ui()->CallJavascriptFunction(
204 "options.ManagedUserListData.receiveExistingManagedUsers",
205 managed_users);
208 void ManagedUserImportHandler::ClearManagedUsersAndShowError() {
209 web_ui()->CallJavascriptFunction("options.ManagedUserListData.onSigninError");
212 bool ManagedUserImportHandler::IsAccountConnected() const {
213 Profile* profile = Profile::FromWebUI(web_ui());
214 SigninManagerBase* signin_manager =
215 SigninManagerFactory::GetForProfile(profile);
216 return signin_manager && !signin_manager->GetAuthenticatedUsername().empty();
219 bool ManagedUserImportHandler::HasAuthError() const {
220 Profile* profile = Profile::FromWebUI(web_ui());
221 ProfileOAuth2TokenService* token_service =
222 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
223 if (!token_service)
224 return true;
226 SigninErrorController* error_controller =
227 token_service->signin_error_controller();
229 GoogleServiceAuthError::State state = error_controller->auth_error().state();
231 return state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
232 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP ||
233 state == GoogleServiceAuthError::ACCOUNT_DELETED ||
234 state == GoogleServiceAuthError::ACCOUNT_DISABLED;
237 void ManagedUserImportHandler::OnSharedSettingChanged(
238 const std::string& managed_user_id,
239 const std::string& key) {
240 if (key == managed_users::kChromeAvatarIndex)
241 FetchManagedUsers();
244 void ManagedUserImportHandler::OnErrorChanged() {
245 FetchManagedUsers();
248 } // namespace options