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.accounts.Account;
8import android.accounts.AccountManager;
9import android.accounts.AccountManagerCallback;
10import android.accounts.AccountManagerFuture;
11import android.app.Activity;
12import android.os.Bundle;
13import android.util.Log;
14
15import org.chromium.sync.signin.ChromeSigninController;
16
17/**
18 * Performs the actual login to the requried service.
19 */
20public class AutoLoginAccountDelegate implements AccountManagerCallback<Bundle> {
21
22    private static final String WEB_LOGIN_PREFIX = "weblogin:";
23    private final Activity mActivity;
24    private final AutoLoginProcessor mAutoLoginProcessor;
25    private final AccountManager mAccountManager;
26    private final Account mAccount;
27    private boolean mLogInRequested;
28    private final String mAuthTokenType;
29
30    public AutoLoginAccountDelegate(Activity activity, AutoLoginProcessor autoLoginProcessor,
31            String realm, String account, String accountArgs) {
32        mActivity = activity;
33        mAutoLoginProcessor = autoLoginProcessor;
34        mAccountManager = AccountManager.get(activity);
35        mAccount = ChromeSigninController.get(activity).getSignedInUser();
36        mLogInRequested = false;
37        mAuthTokenType = WEB_LOGIN_PREFIX + accountArgs;
38    }
39
40    public boolean logIn() {
41        Log.i("AutoLoginAccountDelegate", "auto-login requested for "
42                + (mAccount != null ? mAccount.toString() : "?"));
43
44        Account currentAccount = ChromeSigninController.get(mActivity).getSignedInUser();
45        if (mAccount == null || !mAccount.equals(currentAccount)) {
46            Log.i("InfoBar", "auto-login failed because account is no longer valid");
47            return false;
48        }
49
50        // The callback for this request comes in on a non-UI thread.
51        mAccountManager.getAuthToken(mAccount, mAuthTokenType, null, mActivity, this, null);
52        mLogInRequested = true;
53        return true;
54    }
55
56    String getAuthToken() {
57        return mAuthTokenType;
58    }
59
60    String getAccountName() {
61        return mAccount != null ? mAccount.name : "";
62    }
63
64    boolean loginRequested() {
65        return mLogInRequested;
66    }
67
68    boolean hasAccount() {
69        return mAccount != null;
70    }
71
72    @Override
73    public void run(AccountManagerFuture<Bundle> value) {
74        String result = null;
75        try {
76            result = value.getResult().getString(AccountManager.KEY_AUTHTOKEN);
77        } catch (Exception e) {
78            result = null;
79        }
80
81        final boolean success = result != null;
82
83        // Can't rely on the Bundle's auth token or account name as they might be null
84        // if this was a failed attempt.
85        if (mAutoLoginProcessor != null) {
86            mAutoLoginProcessor.processAutoLoginResult(getAccountName(), getAuthToken(),
87                    success, result);
88        }
89    }
90}
91