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.
5 package org.chromium.chrome.browser.password_manager;
7 import org.chromium.base.CalledByNative;
8 import org.chromium.chrome.browser.TabBase;
11 * Allows embedders to authenticate the usage of passwords.
13 public class PasswordAuthenticationManager {
16 * The delegate that allows embedders to control the authentication of passwords.
18 public interface PasswordAuthenticationDelegate {
20 * @return Whether password authentication is enabled.
22 boolean isPasswordAuthenticationEnabled();
25 * Requests password authentication be presented for the given tab.
26 * @param tab The tab containing the protected password.
27 * @param callback The callback to be triggered on authentication result.
29 void requestAuthentication(TabBase tab, PasswordAuthenticationCallback callback);
32 * @return The message to be displayed in the save password infobar that will allow
33 * the user to opt-in to additional password authentication.
35 String getPasswordProtectionString();
39 * The callback to be triggered on success or failure of the password authentication.
41 public static class PasswordAuthenticationCallback {
42 private long mNativePtr;
44 @CalledByNative("PasswordAuthenticationCallback")
45 private static PasswordAuthenticationCallback create(long nativePtr) {
46 return new PasswordAuthenticationCallback(nativePtr);
49 private PasswordAuthenticationCallback(long nativePtr) {
50 mNativePtr = nativePtr;
54 * Called upon authentication results to allow usage of the password or not.
55 * @param authenticated Whether the authentication was successful.
57 public final void onResult(boolean authenticated) {
58 if (mNativePtr == 0) {
59 assert false : "Can not call onResult more than once per callback.";
62 nativeOnResult(mNativePtr, authenticated);
67 private static class DefaultPasswordAuthenticationDelegate
68 implements PasswordAuthenticationDelegate {
70 public boolean isPasswordAuthenticationEnabled() {
75 public void requestAuthentication(TabBase tab, PasswordAuthenticationCallback callback) {
76 callback.onResult(true);
80 public String getPasswordProtectionString() {
85 private static PasswordAuthenticationDelegate sDelegate;
87 private PasswordAuthenticationManager() {}
89 private static PasswordAuthenticationDelegate getDelegate() {
90 if (sDelegate == null) {
91 sDelegate = new DefaultPasswordAuthenticationDelegate();
97 * Sets the password authentication delegate to be used.
99 public static void setDelegate(PasswordAuthenticationDelegate delegate) {
100 sDelegate = delegate;
104 * @return Whether password authentication is enabled.
106 public static boolean isPasswordAuthenticationEnabled() {
107 return getDelegate().isPasswordAuthenticationEnabled();
111 * Requests password authentication be presented for the given tab.
112 * @param tab The tab containing the protected password.
113 * @param callback The callback to be triggered on authentication result.
116 public static void requestAuthentication(
117 TabBase tab, PasswordAuthenticationCallback callback) {
118 getDelegate().requestAuthentication(tab, callback);
122 * @return The message to be displayed in the save password infobar that will allow the user
123 * to opt-in to additional password authentication.
125 public static String getPasswordProtectionString() {
126 return getDelegate().getPasswordProtectionString();
129 private static native void nativeOnResult(long callbackPtr, boolean authenticated);