Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / password_manager / PasswordAuthenticationManager.java
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.
4
5 package org.chromium.chrome.browser.password_manager;
6
7 import org.chromium.base.CalledByNative;
8 import org.chromium.chrome.browser.TabBase;
9
10 /**
11  * Allows embedders to authenticate the usage of passwords.
12  */
13 public class PasswordAuthenticationManager {
14
15     /**
16      * The delegate that allows embedders to control the authentication of passwords.
17      */
18     public interface PasswordAuthenticationDelegate {
19         /**
20          * @return Whether password authentication is enabled.
21          */
22         boolean isPasswordAuthenticationEnabled();
23
24         /**
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.
28          */
29         void requestAuthentication(TabBase tab, PasswordAuthenticationCallback callback);
30
31         /**
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.
34          */
35         String getPasswordProtectionString();
36     }
37
38     /**
39      * The callback to be triggered on success or failure of the password authentication.
40      */
41     public static class PasswordAuthenticationCallback {
42         private long mNativePtr;
43
44         @CalledByNative("PasswordAuthenticationCallback")
45         private static PasswordAuthenticationCallback create(long nativePtr) {
46             return new PasswordAuthenticationCallback(nativePtr);
47         }
48
49         private PasswordAuthenticationCallback(long nativePtr) {
50             mNativePtr = nativePtr;
51         }
52
53         /**
54          * Called upon authentication results to allow usage of the password or not.
55          * @param authenticated Whether the authentication was successful.
56          */
57         public final void onResult(boolean authenticated) {
58             if (mNativePtr == 0) {
59                 assert false : "Can not call onResult more than once per callback.";
60                 return;
61             }
62             nativeOnResult(mNativePtr, authenticated);
63             mNativePtr = 0;
64         }
65     }
66
67     private static class DefaultPasswordAuthenticationDelegate
68             implements PasswordAuthenticationDelegate {
69         @Override
70         public boolean isPasswordAuthenticationEnabled() {
71             return false;
72         }
73
74         @Override
75         public void requestAuthentication(TabBase tab, PasswordAuthenticationCallback callback) {
76             callback.onResult(true);
77         }
78
79         @Override
80         public String getPasswordProtectionString() {
81             return "";
82         }
83     }
84
85     private static PasswordAuthenticationDelegate sDelegate;
86
87     private PasswordAuthenticationManager() {}
88
89     private static PasswordAuthenticationDelegate getDelegate() {
90         if (sDelegate == null) {
91             sDelegate = new DefaultPasswordAuthenticationDelegate();
92         }
93         return sDelegate;
94     }
95
96     /**
97      * Sets the password authentication delegate to be used.
98      */
99     public static void setDelegate(PasswordAuthenticationDelegate delegate) {
100         sDelegate = delegate;
101     }
102
103     /**
104      * @return Whether password authentication is enabled.
105      */
106     public static boolean isPasswordAuthenticationEnabled() {
107         return getDelegate().isPasswordAuthenticationEnabled();
108     }
109
110     /**
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.
114      */
115     @CalledByNative
116     public static void requestAuthentication(
117             TabBase tab, PasswordAuthenticationCallback callback) {
118         getDelegate().requestAuthentication(tab, callback);
119     }
120
121     /**
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.
124      */
125     public static String getPasswordProtectionString() {
126         return getDelegate().getPasswordProtectionString();
127     }
128
129     private static native void nativeOnResult(long callbackPtr, boolean authenticated);
130 }