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
5package org.chromium.chrome.browser.infobar;
6
7import android.app.Activity;
8import android.util.Pair;
9
10import org.chromium.base.CalledByNative;
11
12/**
13 * Java equivalent of the C++ AutoLoginDelegateAndroid.
14 *
15 * Offers functionality to log in using the account in the system and keeps track
16 * of all the native autologin infobars and their respective accounts.
17 */
18public class AutoLoginDelegate {
19    private final Activity mActivity;
20    private final AutoLoginProcessor mAutoLoginProcessor;
21
22    // nativeInfoBar -> AutoLoginAccountDelegate
23    private Pair<Long, AutoLoginAccountDelegate> mAccountHelper;
24
25    public AutoLoginDelegate(AutoLoginProcessor autoLoginProcessor, Activity activity) {
26        mActivity = activity;
27        mAutoLoginProcessor = autoLoginProcessor;
28        mAccountHelper = null;
29    }
30
31    /**
32     * @return the account name of the device if any.
33     */
34    @CalledByNative
35    String initializeAccount(long nativeInfoBar, String realm, String account, String args) {
36        AutoLoginAccountDelegate accountHelper =
37                new AutoLoginAccountDelegate(mActivity, mAutoLoginProcessor, realm, account, args);
38
39        if (!accountHelper.hasAccount()) {
40            return "";
41        }
42
43        mAccountHelper = new Pair<Long, AutoLoginAccountDelegate>(nativeInfoBar, accountHelper);
44        return accountHelper.getAccountName();
45    }
46
47    /**
48     * Log in a user to a given google service.
49     */
50    @CalledByNative
51    boolean logIn(long nativeInfoBar) {
52        AutoLoginAccountDelegate account =
53                mAccountHelper != null && mAccountHelper.first == nativeInfoBar ?
54                        mAccountHelper.second : null;
55
56        if (account == null || !account.logIn()) {
57            nativeLoginFailed(nativeInfoBar);
58            return false;
59        }
60        return true;
61    }
62
63    /**
64     * Clear account information for cancelled login requests.
65     */
66    @CalledByNative
67    boolean cancelLogIn(long nativeInfoBar) {
68        mAccountHelper = null;
69        return true;
70    }
71
72    /**
73     * Clear all infobars in the same tab and tells native to proceed with login if successful.
74     */
75    public void dismissAutoLogins(String accountName, String authToken, boolean success,
76            String result) {
77
78        if (mAccountHelper != null) {
79            long infoBar = mAccountHelper.first;
80            AutoLoginAccountDelegate delegate = mAccountHelper.second;
81            if (!delegate.loginRequested()) {
82                nativeLoginDismiss(infoBar);
83            } else {
84                String accountAuthToken = delegate.getAuthToken();
85                if (accountAuthToken != null && accountAuthToken.equals(authToken)
86                        && delegate.loginRequested()) {
87                    if (success) {
88                        nativeLoginSuccess(infoBar, result);
89                    } else {
90                        nativeLoginFailed(infoBar);
91                    }
92                }
93            }
94            mAccountHelper = null;
95        }
96    }
97
98    private native void nativeLoginSuccess(long nativeAutoLoginInfoBarDelegateAndroid,
99            String result);
100    private native void nativeLoginFailed(long nativeAutoLoginInfoBarDelegateAndroid);
101    private native void nativeLoginDismiss(long nativeAutoLoginInfoBarDelegateAndroid);
102}
103