This source file includes following definitions.
- initOnBackgroundThread
- initDatabase
- createTable
- waitForInit
- setHttpAuthUsernamePassword
- getHttpAuthUsernamePassword
- hasHttpAuthUsernamePassword
- clearHttpAuthUsernamePassword
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;
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
};
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";
private boolean mInitialized = false;
private final Object mInitializedLock = new Object();
public HttpAuthDatabase(final Context context, final String databaseFile) {
new Thread() {
@Override
public void run() {
initOnBackgroundThread(context, databaseFile);
}
}.start();
}
private void initOnBackgroundThread(Context context, String databaseFile) {
synchronized (mInitializedLock) {
if (mInitialized) {
return;
}
initDatabase(context, databaseFile);
mInitialized = true;
mInitializedLock.notifyAll();
}
}
private void initDatabase(Context context, String databaseFile) {
try {
mDatabase = context.openOrCreateDatabase(databaseFile, 0, null);
} catch (SQLiteException e) {
if (context.deleteDatabase(databaseFile)) {
mDatabase = context.openOrCreateDatabase(databaseFile, 0, null);
}
}
if (mDatabase == null) {
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);
}
private boolean waitForInit() {
synchronized (mInitializedLock) {
while (!mInitialized) {
try {
mInitializedLock.wait();
} catch (InterruptedException e) {
Log.e(LOGTAG, "Caught exception while checking initialization", e);
}
}
}
return mDatabase != null;
}
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);
}
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;
}
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;
}
public void clearHttpAuthUsernamePassword() {
if (!waitForInit()) {
return;
}
mDatabase.delete(HTTPAUTH_TABLE_NAME, null, null);
}
}