summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/identity/identity_signin_flow.cc
blob: 74b5f3550abcffc98a8e00a35d13111d87344ca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/identity/identity_signin_flow.h"

#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/webui/signin/login_ui_service.h"
#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"

namespace extensions {

IdentitySigninFlow::IdentitySigninFlow(Delegate* delegate, Profile* profile)
    : delegate_(delegate),
      profile_(profile) {
}

IdentitySigninFlow::~IdentitySigninFlow() {
  ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
      RemoveObserver(this);
}

void IdentitySigninFlow::Start() {
  DCHECK(delegate_);

#if defined(OS_CHROMEOS)
  // In normal mode (i.e. non-forced app mode), the user has to log out to
  // re-establish credentials. Let the global error popup handle everything.
  if (!chrome::IsRunningInForcedAppMode()) {
    delegate_->SigninFailed();
    return;
  }
#endif

  ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->AddObserver(this);

  LoginUIService* login_ui_service =
      LoginUIServiceFactory::GetForProfile(profile_);
  login_ui_service->ShowLoginPopup();
}

void IdentitySigninFlow::OnRefreshTokenAvailable(
    const std::string& account_id) {
  if (SigninManagerFactory::GetForProfile(profile_)->
      GetAuthenticatedAccountId() == account_id) {
    delegate_->SigninSuccess();
  }
}

}  // namespace extensions