OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/webui/options/managed_user_import_handler.h" | |
6 | |
7 #include <set> | |
8 | |
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/profile_oauth2_token_service_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" | |
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" | |
33 | |
34 namespace { | |
35 | |
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)); | |
41 } | |
42 | |
43 return avatar_icons.Pass(); | |
44 } | |
45 | |
46 } // namespace | |
47 | |
48 namespace options { | |
49 | |
50 ManagedUserImportHandler::ManagedUserImportHandler() | |
51 : observer_(this), | |
52 weak_ptr_factory_(this) {} | |
53 | |
54 ManagedUserImportHandler::~ManagedUserImportHandler() { | |
55 Profile* profile = Profile::FromWebUI(web_ui()); | |
56 if (!profile->IsSupervised()) { | |
57 SupervisedUserSyncService* service = | |
58 SupervisedUserSyncServiceFactory::GetForProfile(profile); | |
59 if (service) | |
60 service->RemoveObserver(this); | |
61 subscription_.reset(); | |
62 } | |
63 } | |
64 | |
65 void ManagedUserImportHandler::GetLocalizedValues( | |
66 base::DictionaryValue* localized_strings) { | |
67 DCHECK(localized_strings); | |
68 | |
69 static OptionsStringResource resources[] = { | |
70 { "managedUserImportTitle", IDS_IMPORT_EXISTING_SUPERVISED_USER_TITLE }, | |
71 { "managedUserImportText", IDS_IMPORT_EXISTING_SUPERVISED_USER_TEXT }, | |
72 { "createNewUserLink", IDS_CREATE_NEW_USER_LINK }, | |
73 { "managedUserImportOk", IDS_IMPORT_EXISTING_SUPERVISED_USER_OK }, | |
74 { "managedUserImportSigninError", | |
75 IDS_SUPERVISED_USER_IMPORT_SIGN_IN_ERROR }, | |
76 { "managedUserAlreadyOnThisDevice", | |
77 IDS_SUPERVISED_USER_ALREADY_ON_THIS_DEVICE }, | |
78 { "noExistingManagedUsers", IDS_SUPERVISED_USER_NO_EXISTING_ERROR }, | |
79 { "managedUserSelectAvatarTitle", | |
80 IDS_SUPERVISED_USER_SELECT_AVATAR_TITLE }, | |
81 { "managedUserSelectAvatarText", IDS_SUPERVISED_USER_SELECT_AVATAR_TEXT }, | |
82 { "managedUserSelectAvatarOk", IDS_SUPERVISED_USER_SELECT_AVATAR_OK }, | |
83 }; | |
84 | |
85 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
86 localized_strings->Set("avatarIcons", GetAvatarIcons().release()); | |
87 } | |
88 | |
89 void ManagedUserImportHandler::InitializeHandler() { | |
90 Profile* profile = Profile::FromWebUI(web_ui()); | |
91 if (!profile->IsSupervised()) { | |
92 SupervisedUserSyncService* sync_service = | |
93 SupervisedUserSyncServiceFactory::GetForProfile(profile); | |
94 if (sync_service) { | |
95 sync_service->AddObserver(this); | |
96 observer_.Add(ProfileOAuth2TokenServiceFactory::GetForProfile(profile)-> | |
97 signin_error_controller()); | |
98 SupervisedUserSharedSettingsService* settings_service = | |
99 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext( | |
100 profile); | |
101 subscription_ = settings_service->Subscribe( | |
102 base::Bind(&ManagedUserImportHandler::OnSharedSettingChanged, | |
103 weak_ptr_factory_.GetWeakPtr())); | |
104 } else { | |
105 DCHECK(!SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext( | |
106 profile)); | |
107 DCHECK(!ProfileOAuth2TokenServiceFactory::GetForProfile(profile)); | |
108 } | |
109 } | |
110 } | |
111 | |
112 void ManagedUserImportHandler::RegisterMessages() { | |
113 web_ui()->RegisterMessageCallback("requestManagedUserImportUpdate", | |
114 base::Bind(&ManagedUserImportHandler::RequestManagedUserImportUpdate, | |
115 base::Unretained(this))); | |
116 } | |
117 | |
118 void ManagedUserImportHandler::OnSupervisedUsersChanged() { | |
119 FetchManagedUsers(); | |
120 } | |
121 | |
122 void ManagedUserImportHandler::FetchManagedUsers() { | |
123 web_ui()->CallJavascriptFunction("options.ManagedUserListData.resetPromise"); | |
124 RequestManagedUserImportUpdate(NULL); | |
125 } | |
126 | |
127 void ManagedUserImportHandler::RequestManagedUserImportUpdate( | |
128 const base::ListValue* /* args */) { | |
129 if (Profile::FromWebUI(web_ui())->IsSupervised()) | |
130 return; | |
131 | |
132 if (!IsAccountConnected() || HasAuthError()) { | |
133 ClearManagedUsersAndShowError(); | |
134 } else { | |
135 SupervisedUserSyncService* supervised_user_sync_service = | |
136 SupervisedUserSyncServiceFactory::GetForProfile( | |
137 Profile::FromWebUI(web_ui())); | |
138 if (supervised_user_sync_service) { | |
139 supervised_user_sync_service->GetSupervisedUsersAsync( | |
140 base::Bind(&ManagedUserImportHandler::SendExistingManagedUsers, | |
141 weak_ptr_factory_.GetWeakPtr())); | |
142 } | |
143 } | |
144 } | |
145 | |
146 void ManagedUserImportHandler::SendExistingManagedUsers( | |
147 const base::DictionaryValue* dict) { | |
148 DCHECK(dict); | |
149 const ProfileInfoCache& cache = | |
150 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
151 | |
152 // Collect the ids of local supervised user profiles. | |
153 std::set<std::string> supervised_user_ids; | |
154 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) { | |
155 if (cache.ProfileIsSupervisedAtIndex(i)) | |
156 supervised_user_ids.insert(cache.GetSupervisedUserIdOfProfileAtIndex(i)); | |
157 } | |
158 | |
159 base::ListValue supervised_users; | |
160 Profile* profile = Profile::FromWebUI(web_ui()); | |
161 SupervisedUserSharedSettingsService* service = | |
162 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(profile); | |
163 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | |
164 const base::DictionaryValue* value = NULL; | |
165 bool success = it.value().GetAsDictionary(&value); | |
166 DCHECK(success); | |
167 std::string name; | |
168 value->GetString(SupervisedUserSyncService::kName, &name); | |
169 | |
170 base::DictionaryValue* supervised_user = new base::DictionaryValue; | |
171 supervised_user->SetString("id", it.key()); | |
172 supervised_user->SetString("name", name); | |
173 | |
174 int avatar_index = SupervisedUserSyncService::kNoAvatar; | |
175 const base::Value* avatar_index_value = | |
176 service->GetValue(it.key(), supervised_users::kChromeAvatarIndex); | |
177 if (avatar_index_value) { | |
178 success = avatar_index_value->GetAsInteger(&avatar_index); | |
179 } else { | |
180 // Check if there is a legacy avatar index stored. | |
181 std::string avatar_str; | |
182 value->GetString(SupervisedUserSyncService::kChromeAvatar, &avatar_str); | |
183 success = | |
184 SupervisedUserSyncService::GetAvatarIndex(avatar_str, &avatar_index); | |
185 } | |
186 DCHECK(success); | |
187 supervised_user->SetBoolean( | |
188 "needAvatar", | |
189 avatar_index == SupervisedUserSyncService::kNoAvatar); | |
190 | |
191 std::string supervised_user_icon = | |
192 std::string(chrome::kChromeUIThemeURL) + | |
193 "IDR_SUPERVISED_USER_PLACEHOLDER"; | |
194 std::string avatar_url = | |
195 avatar_index == SupervisedUserSyncService::kNoAvatar ? | |
196 supervised_user_icon : | |
197 profiles::GetDefaultAvatarIconUrl(avatar_index); | |
198 supervised_user->SetString("iconURL", avatar_url); | |
199 bool on_current_device = | |
200 supervised_user_ids.find(it.key()) != supervised_user_ids.end(); | |
201 supervised_user->SetBoolean("onCurrentDevice", on_current_device); | |
202 | |
203 supervised_users.Append(supervised_user); | |
204 } | |
205 | |
206 web_ui()->CallJavascriptFunction( | |
207 "options.ManagedUserListData.receiveExistingManagedUsers", | |
208 supervised_users); | |
209 } | |
210 | |
211 void ManagedUserImportHandler::ClearManagedUsersAndShowError() { | |
212 web_ui()->CallJavascriptFunction("options.ManagedUserListData.onSigninError"); | |
213 } | |
214 | |
215 bool ManagedUserImportHandler::IsAccountConnected() const { | |
216 Profile* profile = Profile::FromWebUI(web_ui()); | |
217 SigninManagerBase* signin_manager = | |
218 SigninManagerFactory::GetForProfile(profile); | |
219 return signin_manager && !signin_manager->GetAuthenticatedUsername().empty(); | |
220 } | |
221 | |
222 bool ManagedUserImportHandler::HasAuthError() const { | |
223 Profile* profile = Profile::FromWebUI(web_ui()); | |
224 ProfileOAuth2TokenService* token_service = | |
225 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
226 if (!token_service) | |
227 return true; | |
228 | |
229 SigninErrorController* error_controller = | |
230 token_service->signin_error_controller(); | |
231 | |
232 GoogleServiceAuthError::State state = error_controller->auth_error().state(); | |
233 | |
234 return state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS || | |
235 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP || | |
236 state == GoogleServiceAuthError::ACCOUNT_DELETED || | |
237 state == GoogleServiceAuthError::ACCOUNT_DISABLED; | |
238 } | |
239 | |
240 void ManagedUserImportHandler::OnSharedSettingChanged( | |
241 const std::string& supervised_user_id, | |
242 const std::string& key) { | |
243 if (key == supervised_users::kChromeAvatarIndex) | |
244 FetchManagedUsers(); | |
245 } | |
246 | |
247 void ManagedUserImportHandler::OnErrorChanged() { | |
248 FetchManagedUsers(); | |
249 } | |
250 | |
251 } // namespace options | |
OLD | NEW |