/
HttpAuthDatabase.java
249 lines (221 loc) · 8.74 KB
/
HttpAuthDatabase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.android_webview;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
/**
* This database is used to support WebView's setHttpAuthUsernamePassword and
* getHttpAuthUsernamePassword methods, and WebViewDatabase's clearHttpAuthUsernamePassword and
* hasHttpAuthUsernamePassword methods.
*
* While this class is intended to be used as a singleton, this property is not enforced in this
* layer, primarily for ease of testing. To line up with the classic implementation and behavior,
* there is no specific handling and reporting when SQL errors occur.
*
* Note on thread-safety: As per the classic implementation, most API functions have thread safety
* provided by the underlying SQLiteDatabase instance. The exception is database opening: this
* is handled in the dedicated background thread, which also provides a performance gain
* if triggered early on (e.g. as a side effect of CookieSyncManager.createInstance() call),
* sufficiently in advance of the first blocking usage of the API.
*/
public class HttpAuthDatabase {
private static final String LOGTAG = "HttpAuthDatabase";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase mDatabase = null;
private static final String ID_COL = "_id";
private static final String[] ID_PROJECTION = new String[] {
ID_COL
};
// column id strings for "httpauth" table
private static final String HTTPAUTH_TABLE_NAME = "httpauth";
private static final String HTTPAUTH_HOST_COL = "host";
private static final String HTTPAUTH_REALM_COL = "realm";
private static final String HTTPAUTH_USERNAME_COL = "username";
private static final String HTTPAUTH_PASSWORD_COL = "password";
/**
* Initially false until the background thread completes.
*/
private boolean mInitialized = false;
/**
* Create an instance of HttpAuthDatabase for the named file, and kick-off background
* initialization of that database.
*
* @param context the Context to use for opening the database
* @param databaseFile Name of the file to be initialized.
*/
public HttpAuthDatabase(final Context context, final String databaseFile) {
new Thread() {
@Override
public void run() {
initOnBackgroundThread(context, databaseFile);
}
}.start();
}
/**
* Initializes the databases and notifies any callers waiting on waitForInit.
*
* @param context the Context to use for opening the database
* @param databaseFile Name of the file to be initialized.
*/
private synchronized void initOnBackgroundThread(Context context, String databaseFile) {
if (mInitialized) {
return;
}
initDatabase(context, databaseFile);
// Thread done, notify.
mInitialized = true;
notifyAll();
}
/**
* Opens the database, and upgrades it if necessary.
*
* @param context the Context to use for opening the database
* @param databaseFile Name of the file to be initialized.
*/
private void initDatabase(Context context, String databaseFile) {
try {
mDatabase = context.openOrCreateDatabase(databaseFile, 0, null);
} catch (SQLiteException e) {
// try again by deleting the old db and create a new one
if (context.deleteDatabase(databaseFile)) {
mDatabase = context.openOrCreateDatabase(databaseFile, 0, null);
}
}
if (mDatabase == null) {
// Not much we can do to recover at this point
Log.e(LOGTAG, "Unable to open or create " + databaseFile);
return;
}
if (mDatabase.getVersion() != DATABASE_VERSION) {
mDatabase.beginTransactionNonExclusive();
try {
createTable();
mDatabase.setTransactionSuccessful();
} finally {
mDatabase.endTransaction();
}
}
}
private void createTable() {
mDatabase.execSQL("CREATE TABLE " + HTTPAUTH_TABLE_NAME
+ " (" + ID_COL + " INTEGER PRIMARY KEY, "
+ HTTPAUTH_HOST_COL + " TEXT, " + HTTPAUTH_REALM_COL
+ " TEXT, " + HTTPAUTH_USERNAME_COL + " TEXT, "
+ HTTPAUTH_PASSWORD_COL + " TEXT," + " UNIQUE ("
+ HTTPAUTH_HOST_COL + ", " + HTTPAUTH_REALM_COL
+ ") ON CONFLICT REPLACE);");
mDatabase.setVersion(DATABASE_VERSION);
}
/**
* Waits for the background initialization thread to complete and check the database creation
* status.
*
* @return true if the database was initialized, false otherwise
*/
private boolean waitForInit() {
synchronized (this) {
while (!mInitialized) {
try {
wait();
} catch (InterruptedException e) {
Log.e(LOGTAG, "Caught exception while checking initialization", e);
}
}
}
return mDatabase != null;
}
/**
* Sets the HTTP authentication password. Tuple (HTTPAUTH_HOST_COL, HTTPAUTH_REALM_COL,
* HTTPAUTH_USERNAME_COL) is unique.
*
* @param host the host for the password
* @param realm the realm for the password
* @param username the username for the password.
* @param password the password
*/
public void setHttpAuthUsernamePassword(String host, String realm, String username,
String password) {
if (host == null || realm == null || !waitForInit()) {
return;
}
final ContentValues c = new ContentValues();
c.put(HTTPAUTH_HOST_COL, host);
c.put(HTTPAUTH_REALM_COL, realm);
c.put(HTTPAUTH_USERNAME_COL, username);
c.put(HTTPAUTH_PASSWORD_COL, password);
mDatabase.insert(HTTPAUTH_TABLE_NAME, HTTPAUTH_HOST_COL, c);
}
/**
* Retrieves the HTTP authentication username and password for a given host and realm pair. If
* there are multiple username/password combinations for a host/realm, only the first one will
* be returned.
*
* @param host the host the password applies to
* @param realm the realm the password applies to
* @return a String[] if found where String[0] is username (which can be null) and
* String[1] is password. Null is returned if it can't find anything.
*/
public String[] getHttpAuthUsernamePassword(String host, String realm) {
if (host == null || realm == null || !waitForInit()){
return null;
}
final String[] columns = new String[] {
HTTPAUTH_USERNAME_COL, HTTPAUTH_PASSWORD_COL
};
final String selection = "(" + HTTPAUTH_HOST_COL + " == ?) AND " +
"(" + HTTPAUTH_REALM_COL + " == ?)";
String[] ret = null;
Cursor cursor = null;
try {
cursor = mDatabase.query(HTTPAUTH_TABLE_NAME, columns, selection,
new String[] { host, realm }, null, null, null);
if (cursor.moveToFirst()) {
ret = new String[] {
cursor.getString(cursor.getColumnIndex(HTTPAUTH_USERNAME_COL)),
cursor.getString(cursor.getColumnIndex(HTTPAUTH_PASSWORD_COL)),
};
}
} catch (IllegalStateException e) {
Log.e(LOGTAG, "getHttpAuthUsernamePassword", e);
} finally {
if (cursor != null) cursor.close();
}
return ret;
}
/**
* Determines if there are any HTTP authentication passwords saved.
*
* @return true if there are passwords saved
*/
public boolean hasHttpAuthUsernamePassword() {
if (!waitForInit()) {
return false;
}
Cursor cursor = null;
boolean ret = false;
try {
cursor = mDatabase.query(HTTPAUTH_TABLE_NAME, ID_PROJECTION, null, null, null, null,
null);
ret = cursor.moveToFirst();
} catch (IllegalStateException e) {
Log.e(LOGTAG, "hasEntries", e);
} finally {
if (cursor != null) cursor.close();
}
return ret;
}
/**
* Clears the HTTP authentication password database.
*/
public void clearHttpAuthUsernamePassword() {
if (!waitForInit()) {
return;
}
mDatabase.delete(HTTPAUTH_TABLE_NAME, null, null);
}
}