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;
6
7import org.chromium.base.CalledByNative;
8import org.chromium.base.ObserverList;
9
10/**
11 * Class for retrieving passwords and password exceptions (websites for which Chrome should not save
12 * password) from native code.
13 */
14public final class PasswordUIView {
15
16    /**
17     * Class representing information about a saved password entry.
18     */
19    public static final class SavedPasswordEntry {
20        private final String mUrl;
21        private final String mName;
22
23        private SavedPasswordEntry(String url, String name) {
24            mUrl = url;
25            mName = name;
26        }
27
28        public String getUrl() {
29            return mUrl;
30        }
31
32        public String getUserName() {
33            return mName;
34        }
35    }
36
37    @CalledByNative
38    private static SavedPasswordEntry createSavedPasswordEntry(String url, String name) {
39        return new SavedPasswordEntry(url, name);
40    }
41
42    /**
43     * Interface which client can use to listen to changes to password and password exception lists.
44     * Clients can register and unregister themselves with addObserver and removeObserver.
45     */
46    public interface PasswordListObserver {
47        /**
48         * Called when passwords list is updated.
49         * @param count Number of entries in the password list.
50         */
51        void passwordListAvailable(int count);
52
53        /**
54         * Called when password exceptions list is updated.
55         * @param count Number of entries in the password exception list.
56         */
57        void passwordExceptionListAvailable(int count);
58    }
59
60    private ObserverList<PasswordListObserver> mObservers =
61            new ObserverList<PasswordListObserver>();
62
63    // Pointer to native implementation, set to 0 in destroy().
64    private long mNativePasswordUIViewAndroid;
65
66    /**
67     * Constructor creates the native object as well. Callers should call destroy() after usage.
68     */
69    public PasswordUIView() {
70        mNativePasswordUIViewAndroid = nativeInit();
71    }
72
73    @CalledByNative
74    private void passwordListAvailable(int count) {
75        for (PasswordListObserver observer : mObservers) {
76            observer.passwordListAvailable(count);
77        }
78    }
79
80    @CalledByNative
81    private void passwordExceptionListAvailable(int count) {
82        for (PasswordListObserver observer : mObservers) {
83            observer.passwordExceptionListAvailable(count);
84        }
85    }
86
87    public void addObserver(PasswordListObserver observer) {
88        mObservers.addObserver(observer);
89    }
90
91    public void removeObserver(PasswordListObserver observer) {
92        mObservers.removeObserver(observer);
93    }
94
95    /**
96     * Calls native to refresh password and exception lists. Observers are notified when fetch to
97     * passwords is complete.
98     */
99    public void updatePasswordLists() {
100        nativeUpdatePasswordLists(mNativePasswordUIViewAndroid);
101    }
102
103    /**
104     * Get the saved password entry at index.
105     *
106     * @param index Index of Password.
107     * @return SavedPasswordEntry at index.
108     */
109    public SavedPasswordEntry getSavedPasswordEntry(int index) {
110        return nativeGetSavedPasswordEntry(mNativePasswordUIViewAndroid, index);
111    }
112
113    /**
114     * Get saved password exception at index.
115     *
116     * @param index of exception
117     * @return Origin of password exception.
118     */
119    public String getSavedPasswordException(int index) {
120        return nativeGetSavedPasswordException(mNativePasswordUIViewAndroid, index);
121    }
122
123    /**
124     * Remove saved password entry at index.
125     *
126     * @param index of password entry to remove.
127     */
128    public void removeSavedPasswordEntry(int index) {
129        nativeHandleRemoveSavedPasswordEntry(mNativePasswordUIViewAndroid, index);
130    }
131
132    /**
133     * Remove saved exception entry at index.
134     *
135     * @param index of exception entry.
136     */
137    public void removeSavedPasswordException(int index) {
138        nativeHandleRemoveSavedPasswordException(mNativePasswordUIViewAndroid, index);
139    }
140
141    public static String getAccountDashboardURL() {
142        return nativeGetAccountDashboardURL();
143    }
144
145    public static boolean shouldDisplayManageAccountLink() {
146        return nativeShouldDisplayManageAccountLink();
147    }
148
149    /**
150     * Destroy the native object.
151     */
152    public void destroy() {
153        if (mNativePasswordUIViewAndroid != 0) {
154            nativeDestroy(mNativePasswordUIViewAndroid);
155            mNativePasswordUIViewAndroid = 0;
156        }
157        mObservers.clear();
158    }
159
160    private native long nativeInit();
161
162    private native void nativeUpdatePasswordLists(long nativePasswordUIViewAndroid);
163
164    private native SavedPasswordEntry nativeGetSavedPasswordEntry(
165            long nativePasswordUIViewAndroid,
166            int index);
167
168    private native String nativeGetSavedPasswordException(long nativePasswordUIViewAndroid,
169            int index);
170
171    private native void nativeHandleRemoveSavedPasswordEntry(
172            long nativePasswordUIViewAndroid,
173            int index);
174
175    private native void nativeHandleRemoveSavedPasswordException(
176            long nativePasswordUIViewAndroid,
177            int index);
178
179    private static native String nativeGetAccountDashboardURL();
180
181    private static native boolean nativeShouldDisplayManageAccountLink();
182
183    private native void nativeDestroy(long nativePasswordUIViewAndroid);
184
185}
186