user_accounts_delegate_chromeos.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 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/ash/user_accounts_delegate_chromeos.h"
6
7#include <algorithm>
8#include <iterator>
9
10#include "base/logging.h"
11#include "chrome/browser/chromeos/login/users/user_manager.h"
12#include "chrome/browser/chromeos/profiles/profile_helper.h"
13#include "chrome/browser/chromeos/ui/inline_login_dialog.h"
14#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15#include "chrome/browser/signin/signin_manager_factory.h"
16#include "components/signin/core/browser/mutable_profile_oauth2_token_service.h"
17#include "components/signin/core/browser/profile_oauth2_token_service.h"
18#include "components/signin/core/browser/signin_manager.h"
19#include "google_apis/gaia/gaia_auth_util.h"
20
21namespace chromeos {
22
23UserAccountsDelegateChromeOS::UserAccountsDelegateChromeOS(
24    Profile* user_profile)
25    : user_profile_(user_profile) {
26  ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_)
27      ->AddObserver(this);
28}
29
30UserAccountsDelegateChromeOS::~UserAccountsDelegateChromeOS() {
31  ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_)
32      ->RemoveObserver(this);
33}
34
35std::string UserAccountsDelegateChromeOS::GetPrimaryAccountId() {
36  return SigninManagerFactory::GetForProfile(user_profile_)
37      ->GetAuthenticatedAccountId();
38}
39
40std::vector<std::string>
41UserAccountsDelegateChromeOS::GetSecondaryAccountIds() {
42  ProfileOAuth2TokenService* token_service =
43      ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_);
44  std::vector<std::string> accounts = token_service->GetAccounts();
45  // Filter primary account.
46  std::vector<std::string>::iterator it =
47      std::remove(accounts.begin(), accounts.end(), GetPrimaryAccountId());
48  LOG_IF(WARNING, std::distance(it, accounts.end()) != 1)
49      << "Found " << std::distance(it, accounts.end())
50      << " primary accounts in the account list.";
51  accounts.erase(it, accounts.end());
52  return accounts;
53}
54
55std::string UserAccountsDelegateChromeOS::GetAccountDisplayName(
56    const std::string& account_id) {
57  User* user = ProfileHelper::Get()->GetUserByProfile(user_profile_);
58  if (gaia::AreEmailsSame(user->email(), account_id) &&
59      !user->display_email().empty())
60    return user->display_email();
61  return account_id;
62}
63
64void UserAccountsDelegateChromeOS::DeleteAccount(
65    const std::string& account_id) {
66  MutableProfileOAuth2TokenService* oauth2_token_service =
67      ProfileOAuth2TokenServiceFactory::GetPlatformSpecificForProfile(
68          user_profile_);
69  oauth2_token_service->RevokeCredentials(account_id);
70}
71
72void UserAccountsDelegateChromeOS::LaunchAddAccountDialog() {
73  ui::InlineLoginDialog::Show(user_profile_);
74}
75
76void UserAccountsDelegateChromeOS::OnRefreshTokenAvailable(
77    const std::string& account_id) {
78  NotifyAccountListChanged();
79}
80
81void UserAccountsDelegateChromeOS::OnRefreshTokenRevoked(
82    const std::string& account_id) {
83  NotifyAccountListChanged();
84}
85
86}  // namespace chromeos
87