1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/ui/ash/user_accounts_delegate_chromeos.h"
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <algorithm>
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <iterator>
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/logging.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/chromeos/profiles/profile_helper.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/chromeos/ui/inline_login_dialog.h"
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/signin/core/browser/mutable_profile_oauth2_token_service.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/signin/core/browser/profile_oauth2_token_service.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/signin/core/browser/signin_manager.h"
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/user_manager/user.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "google_apis/gaia/gaia_auth_util.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace chromeos {
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)UserAccountsDelegateChromeOS::UserAccountsDelegateChromeOS(
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    Profile* user_profile)
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : user_profile_(user_profile) {
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_)
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ->AddObserver(this);
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)UserAccountsDelegateChromeOS::~UserAccountsDelegateChromeOS() {
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_)
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ->RemoveObserver(this);
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)std::string UserAccountsDelegateChromeOS::GetPrimaryAccountId() {
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return SigninManagerFactory::GetForProfile(user_profile_)
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ->GetAuthenticatedAccountId();
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)std::vector<std::string>
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)UserAccountsDelegateChromeOS::GetSecondaryAccountIds() {
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ProfileOAuth2TokenService* token_service =
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_);
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::vector<std::string> accounts = token_service->GetAccounts();
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Filter primary account.
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::vector<std::string>::iterator it =
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      std::remove(accounts.begin(), accounts.end(), GetPrimaryAccountId());
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  LOG_IF(WARNING, std::distance(it, accounts.end()) != 1)
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      << "Found " << std::distance(it, accounts.end())
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      << " primary accounts in the account list.";
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  accounts.erase(it, accounts.end());
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return accounts;
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)std::string UserAccountsDelegateChromeOS::GetAccountDisplayName(
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const std::string& account_id) {
575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  user_manager::User* user =
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      ProfileHelper::Get()->GetUserByProfile(user_profile_);
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (gaia::AreEmailsSame(user->email(), account_id) &&
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      !user->display_email().empty())
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return user->display_email();
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return account_id;
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void UserAccountsDelegateChromeOS::DeleteAccount(
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const std::string& account_id) {
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  MutableProfileOAuth2TokenService* oauth2_token_service =
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ProfileOAuth2TokenServiceFactory::GetPlatformSpecificForProfile(
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          user_profile_);
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  oauth2_token_service->RevokeCredentials(account_id);
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void UserAccountsDelegateChromeOS::LaunchAddAccountDialog() {
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ui::InlineLoginDialog::Show(user_profile_);
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void UserAccountsDelegateChromeOS::OnRefreshTokenAvailable(
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const std::string& account_id) {
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NotifyAccountListChanged();
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void UserAccountsDelegateChromeOS::OnRefreshTokenRevoked(
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const std::string& account_id) {
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NotifyAccountListChanged();
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace chromeos
88