managed_user_import_handler.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
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/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_info_cache.h"
20#include "chrome/browser/profiles/profile_manager.h"
21#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
22#include "chrome/browser/signin/signin_manager.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 "content/public/browser/web_ui.h"
29#include "grit/generated_resources.h"
30#include "grit/theme_resources.h"
31#include "ui/base/l10n/l10n_util.h"
32
33namespace {
34
35scoped_ptr<base::ListValue> GetAvatarIcons() {
36  scoped_ptr<base::ListValue> avatar_icons(new base::ListValue);
37  for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); ++i) {
38    std::string avatar_url = ProfileInfoCache::GetDefaultAvatarIconUrl(i);
39    avatar_icons->Append(new base::StringValue(avatar_url));
40  }
41
42  return avatar_icons.Pass();
43}
44
45}  // namespace
46
47namespace options {
48
49ManagedUserImportHandler::ManagedUserImportHandler()
50    : observer_(this),
51      weak_ptr_factory_(this) {}
52
53ManagedUserImportHandler::~ManagedUserImportHandler() {
54  Profile* profile = Profile::FromWebUI(web_ui());
55  if (!profile->IsManaged()) {
56    ManagedUserSyncService* service =
57        ManagedUserSyncServiceFactory::GetForProfile(profile);
58    if (service)
59      service->RemoveObserver(this);
60    subscription_.reset();
61  }
62}
63
64void ManagedUserImportHandler::GetLocalizedValues(
65    base::DictionaryValue* localized_strings) {
66  DCHECK(localized_strings);
67
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 },
80  };
81
82  RegisterStrings(localized_strings, resources, arraysize(resources));
83  localized_strings->Set("avatarIcons", GetAvatarIcons().release());
84}
85
86void ManagedUserImportHandler::InitializeHandler() {
87  Profile* profile = Profile::FromWebUI(web_ui());
88  if (!profile->IsManaged()) {
89    ManagedUserSyncService* sync_service =
90        ManagedUserSyncServiceFactory::GetForProfile(profile);
91    if (sync_service) {
92      sync_service->AddObserver(this);
93      observer_.Add(ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->
94                        signin_error_controller());
95      ManagedUserSharedSettingsService* settings_service =
96          ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(
97              profile);
98      subscription_ = settings_service->Subscribe(
99          base::Bind(&ManagedUserImportHandler::OnSharedSettingChanged,
100                     weak_ptr_factory_.GetWeakPtr()));
101    } else {
102      DCHECK(!ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(
103                 profile));
104      DCHECK(!ProfileOAuth2TokenServiceFactory::GetForProfile(profile));
105    }
106  }
107}
108
109void ManagedUserImportHandler::RegisterMessages() {
110  web_ui()->RegisterMessageCallback("requestManagedUserImportUpdate",
111      base::Bind(&ManagedUserImportHandler::RequestManagedUserImportUpdate,
112                 base::Unretained(this)));
113}
114
115void ManagedUserImportHandler::OnManagedUsersChanged() {
116  FetchManagedUsers();
117}
118
119void ManagedUserImportHandler::FetchManagedUsers() {
120  web_ui()->CallJavascriptFunction("options.ManagedUserListData.resetPromise");
121  RequestManagedUserImportUpdate(NULL);
122}
123
124void ManagedUserImportHandler::RequestManagedUserImportUpdate(
125    const base::ListValue* /* args */) {
126  if (Profile::FromWebUI(web_ui())->IsManaged())
127    return;
128
129  if (!IsAccountConnected() || HasAuthError()) {
130    ClearManagedUsersAndShowError();
131  } else {
132    ManagedUserSyncService* managed_user_sync_service =
133        ManagedUserSyncServiceFactory::GetForProfile(
134            Profile::FromWebUI(web_ui()));
135    if (managed_user_sync_service) {
136      managed_user_sync_service->GetManagedUsersAsync(
137          base::Bind(&ManagedUserImportHandler::SendExistingManagedUsers,
138                     weak_ptr_factory_.GetWeakPtr()));
139    }
140  }
141}
142
143void ManagedUserImportHandler::SendExistingManagedUsers(
144    const base::DictionaryValue* dict) {
145  DCHECK(dict);
146  const ProfileInfoCache& cache =
147      g_browser_process->profile_manager()->GetProfileInfoCache();
148
149  // Collect the ids of local supervised user profiles.
150  std::set<std::string> managed_user_ids;
151  for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) {
152    if (cache.ProfileIsManagedAtIndex(i))
153      managed_user_ids.insert(cache.GetManagedUserIdOfProfileAtIndex(i));
154  }
155
156  base::ListValue managed_users;
157  Profile* profile = Profile::FromWebUI(web_ui());
158  ManagedUserSharedSettingsService* service =
159      ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(profile);
160  for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
161    const base::DictionaryValue* value = NULL;
162    bool success = it.value().GetAsDictionary(&value);
163    DCHECK(success);
164    std::string name;
165    value->GetString(ManagedUserSyncService::kName, &name);
166
167    base::DictionaryValue* managed_user = new base::DictionaryValue;
168    managed_user->SetString("id", it.key());
169    managed_user->SetString("name", name);
170
171    int avatar_index = ManagedUserSyncService::kNoAvatar;
172    const base::Value* avatar_index_value =
173        service->GetValue(it.key(), managed_users::kChromeAvatarIndex);
174    if (avatar_index_value) {
175      success = avatar_index_value->GetAsInteger(&avatar_index);
176    } else {
177      // Check if there is a legacy avatar index stored.
178      std::string avatar_str;
179      value->GetString(ManagedUserSyncService::kChromeAvatar, &avatar_str);
180      success =
181          ManagedUserSyncService::GetAvatarIndex(avatar_str, &avatar_index);
182    }
183    DCHECK(success);
184    managed_user->SetBoolean("needAvatar",
185                             avatar_index == ManagedUserSyncService::kNoAvatar);
186
187    std::string supervised_user_icon =
188        std::string(chrome::kChromeUIThemeURL) +
189        "IDR_SUPERVISED_USER_PLACEHOLDER";
190    std::string avatar_url =
191        avatar_index == ManagedUserSyncService::kNoAvatar ?
192            supervised_user_icon :
193            ProfileInfoCache::GetDefaultAvatarIconUrl(avatar_index);
194    managed_user->SetString("iconURL", avatar_url);
195    bool on_current_device =
196        managed_user_ids.find(it.key()) != managed_user_ids.end();
197    managed_user->SetBoolean("onCurrentDevice", on_current_device);
198
199    managed_users.Append(managed_user);
200  }
201
202  web_ui()->CallJavascriptFunction(
203      "options.ManagedUserListData.receiveExistingManagedUsers",
204      managed_users);
205}
206
207void ManagedUserImportHandler::ClearManagedUsersAndShowError() {
208  web_ui()->CallJavascriptFunction("options.ManagedUserListData.onSigninError");
209}
210
211bool ManagedUserImportHandler::IsAccountConnected() const {
212  Profile* profile = Profile::FromWebUI(web_ui());
213  SigninManagerBase* signin_manager =
214      SigninManagerFactory::GetForProfile(profile);
215  return signin_manager && !signin_manager->GetAuthenticatedUsername().empty();
216}
217
218bool ManagedUserImportHandler::HasAuthError() const {
219  Profile* profile = Profile::FromWebUI(web_ui());
220  ProfileOAuth2TokenService* token_service =
221      ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
222  if (!token_service)
223    return true;
224
225  SigninErrorController* error_controller =
226      token_service->signin_error_controller();
227
228  GoogleServiceAuthError::State state = error_controller->auth_error().state();
229
230  return state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
231      state == GoogleServiceAuthError::USER_NOT_SIGNED_UP ||
232      state == GoogleServiceAuthError::ACCOUNT_DELETED ||
233      state == GoogleServiceAuthError::ACCOUNT_DISABLED;
234}
235
236void ManagedUserImportHandler::OnSharedSettingChanged(
237    const std::string& managed_user_id,
238    const std::string& key) {
239  if (key == managed_users::kChromeAvatarIndex)
240    FetchManagedUsers();
241}
242
243void ManagedUserImportHandler::OnErrorChanged() {
244  FetchManagedUsers();
245}
246
247}  // namespace options
248