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"
10 #include "base/prefs/pref_service.h"
11 #include "base/values.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/managed_mode/managed_user_constants.h"
15 #include "chrome/browser/managed_mode/managed_user_shared_settings_service.h"
16 #include "chrome/browser/managed_mode/managed_user_shared_settings_service_factory.h"
17 #include "chrome/browser/managed_mode/managed_user_sync_service.h"
18 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/profiles/profile_info_cache.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/signin/signin_global_error.h"
23 #include "chrome/browser/signin/signin_manager.h"
24 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/url_constants.h"
27 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_source.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"
36 scoped_ptr
<base::ListValue
> GetAvatarIcons() {
37 scoped_ptr
<base::ListValue
> avatar_icons(new base::ListValue
);
38 for (size_t i
= 0; i
< ProfileInfoCache::GetDefaultAvatarIconCount(); ++i
) {
39 std::string avatar_url
= ProfileInfoCache::GetDefaultAvatarIconUrl(i
);
40 avatar_icons
->Append(new base::StringValue(avatar_url
));
43 return avatar_icons
.Pass();
50 ManagedUserImportHandler::ManagedUserImportHandler()
51 : weak_ptr_factory_(this) {}
53 ManagedUserImportHandler::~ManagedUserImportHandler() {
54 Profile
* profile
= Profile::FromWebUI(web_ui());
55 if (!profile
->IsManaged()) {
56 ManagedUserSyncService
* service
=
57 ManagedUserSyncServiceFactory::GetForProfile(profile
);
59 service
->RemoveObserver(this);
60 subscription_
.reset();
64 void ManagedUserImportHandler::GetLocalizedValues(
65 base::DictionaryValue
* localized_strings
) {
66 DCHECK(localized_strings
);
68 static OptionsStringResource resources
[] = {
69 { "managedUserImportTitle", IDS_IMPORT_EXISTING_MANAGED_USER_TITLE
},
70 { "managedUserImportText", IDS_IMPORT_EXISTING_MANAGED_USER_TEXT
},
71 { "createNewUserLink", IDS_CREATE_NEW_USER_LINK
},
72 { "managedUserImportOk", IDS_IMPORT_EXISTING_MANAGED_USER_OK
},
73 { "managedUserImportSigninError", IDS_MANAGED_USER_IMPORT_SIGN_IN_ERROR
},
74 { "managedUserAlreadyOnThisDevice",
75 IDS_MANAGED_USER_ALREADY_ON_THIS_DEVICE
},
76 { "noExistingManagedUsers", IDS_MANAGED_USER_NO_EXISTING_ERROR
},
77 { "managedUserSelectAvatarTitle", IDS_MANAGED_USER_SELECT_AVATAR_TITLE
},
78 { "managedUserSelectAvatarText", IDS_MANAGED_USER_SELECT_AVATAR_TEXT
},
79 { "managedUserSelectAvatarOk", IDS_MANAGED_USER_SELECT_AVATAR_OK
},
82 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
83 localized_strings
->Set("avatarIcons", GetAvatarIcons().release());
86 void ManagedUserImportHandler::InitializeHandler() {
87 Profile
* profile
= Profile::FromWebUI(web_ui());
88 registrar_
.Add(this, chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED
,
89 content::Source
<Profile
>(profile
));
90 if (!profile
->IsManaged()) {
91 ManagedUserSyncServiceFactory::GetForProfile(profile
)->AddObserver(this);
93 ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(profile
)
95 base::Bind(&ManagedUserImportHandler::OnSharedSettingChanged
,
96 weak_ptr_factory_
.GetWeakPtr()));
100 void ManagedUserImportHandler::RegisterMessages() {
101 web_ui()->RegisterMessageCallback("requestManagedUserImportUpdate",
102 base::Bind(&ManagedUserImportHandler::RequestManagedUserImportUpdate
,
103 base::Unretained(this)));
106 void ManagedUserImportHandler::Observe(
108 const content::NotificationSource
& source
,
109 const content::NotificationDetails
& details
) {
110 if (type
== chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED
) {
111 SigninGlobalError
* error
=
112 SigninGlobalError::GetForProfile(Profile::FromWebUI(web_ui()));
113 if (content::Details
<SigninGlobalError
>(details
).ptr() == error
)
118 void ManagedUserImportHandler::OnManagedUsersChanged() {
122 void ManagedUserImportHandler::FetchManagedUsers() {
123 web_ui()->CallJavascriptFunction("options.ManagedUserListData.resetPromise");
124 RequestManagedUserImportUpdate(NULL
);
127 void ManagedUserImportHandler::RequestManagedUserImportUpdate(
128 const base::ListValue
* /* args */) {
129 if (Profile::FromWebUI(web_ui())->IsManaged())
132 if (!IsAccountConnected() || HasAuthError()) {
133 ClearManagedUsersAndShowError();
135 ManagedUserSyncService
* managed_user_sync_service
=
136 ManagedUserSyncServiceFactory::GetForProfile(
137 Profile::FromWebUI(web_ui()));
138 managed_user_sync_service
->GetManagedUsersAsync(
139 base::Bind(&ManagedUserImportHandler::SendExistingManagedUsers
,
140 weak_ptr_factory_
.GetWeakPtr()));
144 void ManagedUserImportHandler::SendExistingManagedUsers(
145 const base::DictionaryValue
* 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
);
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
);
178 // Check if there is a legacy avatar index stored.
179 std::string avatar_str
;
180 value
->GetString(ManagedUserSyncService::kChromeAvatar
, &avatar_str
);
182 ManagedUserSyncService::GetAvatarIndex(avatar_str
, &avatar_index
);
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 ProfileInfoCache::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",
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
->GetAuthenticatedUsername().empty();
219 bool ManagedUserImportHandler::HasAuthError() const {
220 Profile
* profile
= Profile::FromWebUI(web_ui());
221 SigninGlobalError
* signin_global_error
=
222 SigninGlobalError::GetForProfile(profile
);
223 GoogleServiceAuthError::State state
=
224 signin_global_error
->GetLastAuthError().state();
226 return state
== GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
||
227 state
== GoogleServiceAuthError::USER_NOT_SIGNED_UP
||
228 state
== GoogleServiceAuthError::ACCOUNT_DELETED
||
229 state
== GoogleServiceAuthError::ACCOUNT_DISABLED
;
232 void ManagedUserImportHandler::OnSharedSettingChanged(
233 const std::string
& managed_user_id
,
234 const std::string
& key
) {
235 if (key
== managed_users::kChromeAvatarIndex
)
239 } // namespace options