OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.android_webview; | 5 package org.chromium.android_webview; |
6 | 6 |
7 import android.content.ContentValues; | 7 import android.content.ContentValues; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.database.Cursor; | 9 import android.database.Cursor; |
10 import android.database.sqlite.SQLiteDatabase; | 10 import android.database.sqlite.SQLiteDatabase; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 private static final String HTTPAUTH_HOST_COL = "host"; | 45 private static final String HTTPAUTH_HOST_COL = "host"; |
46 private static final String HTTPAUTH_REALM_COL = "realm"; | 46 private static final String HTTPAUTH_REALM_COL = "realm"; |
47 private static final String HTTPAUTH_USERNAME_COL = "username"; | 47 private static final String HTTPAUTH_USERNAME_COL = "username"; |
48 private static final String HTTPAUTH_PASSWORD_COL = "password"; | 48 private static final String HTTPAUTH_PASSWORD_COL = "password"; |
49 | 49 |
50 /** | 50 /** |
51 * Initially false until the background thread completes. | 51 * Initially false until the background thread completes. |
52 */ | 52 */ |
53 private boolean mInitialized = false; | 53 private boolean mInitialized = false; |
54 | 54 |
55 private final Object mInitializedLock = new Object(); | |
56 | |
55 /** | 57 /** |
56 * Create an instance of HttpAuthDatabase for the named file, and kick-off b ackground | 58 * Create an instance of HttpAuthDatabase for the named file, and kick-off b ackground |
57 * initialization of that database. | 59 * initialization of that database. |
58 * | 60 * |
59 * @param context the Context to use for opening the database | 61 * @param context the Context to use for opening the database |
60 * @param databaseFile Name of the file to be initialized. | 62 * @param databaseFile Name of the file to be initialized. |
61 */ | 63 */ |
62 public HttpAuthDatabase(final Context context, final String databaseFile) { | 64 public HttpAuthDatabase(final Context context, final String databaseFile) { |
63 new Thread() { | 65 new Thread() { |
64 @Override | 66 @Override |
65 public void run() { | 67 public void run() { |
66 initOnBackgroundThread(context, databaseFile); | 68 initOnBackgroundThread(context, databaseFile); |
67 } | 69 } |
68 }.start(); | 70 }.start(); |
69 } | 71 } |
70 | 72 |
71 /** | 73 /** |
72 * Initializes the databases and notifies any callers waiting on waitForInit . | 74 * Initializes the databases and notifies any callers waiting on waitForInit . |
73 * | 75 * |
74 * @param context the Context to use for opening the database | 76 * @param context the Context to use for opening the database |
75 * @param databaseFile Name of the file to be initialized. | 77 * @param databaseFile Name of the file to be initialized. |
76 */ | 78 */ |
77 private synchronized void initOnBackgroundThread(Context context, String dat abaseFile) { | 79 private void initOnBackgroundThread(Context context, String databaseFile) { |
78 if (mInitialized) { | 80 synchronized (mInitializedLock) { |
79 return; | 81 if (mInitialized) { |
82 return; | |
83 } | |
84 | |
85 initDatabase(context, databaseFile); | |
86 | |
87 // Thread done, notify. | |
88 mInitialized = true; | |
89 notifyAll(); | |
80 } | 90 } |
81 | |
82 initDatabase(context, databaseFile); | |
83 | |
84 // Thread done, notify. | |
85 mInitialized = true; | |
86 notifyAll(); | |
87 } | 91 } |
88 | 92 |
89 /** | 93 /** |
90 * Opens the database, and upgrades it if necessary. | 94 * Opens the database, and upgrades it if necessary. |
91 * | 95 * |
92 * @param context the Context to use for opening the database | 96 * @param context the Context to use for opening the database |
93 * @param databaseFile Name of the file to be initialized. | 97 * @param databaseFile Name of the file to be initialized. |
94 */ | 98 */ |
95 private void initDatabase(Context context, String databaseFile) { | 99 private void initDatabase(Context context, String databaseFile) { |
96 try { | 100 try { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 mDatabase.setVersion(DATABASE_VERSION); | 135 mDatabase.setVersion(DATABASE_VERSION); |
132 } | 136 } |
133 | 137 |
134 /** | 138 /** |
135 * Waits for the background initialization thread to complete and check the database creation | 139 * Waits for the background initialization thread to complete and check the database creation |
136 * status. | 140 * status. |
137 * | 141 * |
138 * @return true if the database was initialized, false otherwise | 142 * @return true if the database was initialized, false otherwise |
139 */ | 143 */ |
140 private boolean waitForInit() { | 144 private boolean waitForInit() { |
141 synchronized (this) { | 145 synchronized (mInitializedLock) { |
142 while (!mInitialized) { | 146 while (!mInitialized) { |
143 try { | 147 try { |
144 wait(); | 148 wait(); |
boliu
2014/02/04 23:26:25
This needs to be mInitializedLock.wait() I think.
Lei Zhang
2014/02/05 00:50:42
Thanks. Done.
| |
145 } catch (InterruptedException e) { | 149 } catch (InterruptedException e) { |
146 Log.e(LOGTAG, "Caught exception while checking initializatio n", e); | 150 Log.e(LOGTAG, "Caught exception while checking initializatio n", e); |
147 } | 151 } |
148 } | 152 } |
149 } | 153 } |
150 return mDatabase != null; | 154 return mDatabase != null; |
151 } | 155 } |
152 | 156 |
153 /** | 157 /** |
154 * Sets the HTTP authentication password. Tuple (HTTPAUTH_HOST_COL, HTTPAUTH _REALM_COL, | 158 * Sets the HTTP authentication password. Tuple (HTTPAUTH_HOST_COL, HTTPAUTH _REALM_COL, |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 /** | 244 /** |
241 * Clears the HTTP authentication password database. | 245 * Clears the HTTP authentication password database. |
242 */ | 246 */ |
243 public void clearHttpAuthUsernamePassword() { | 247 public void clearHttpAuthUsernamePassword() { |
244 if (!waitForInit()) { | 248 if (!waitForInit()) { |
245 return; | 249 return; |
246 } | 250 } |
247 mDatabase.delete(HTTPAUTH_TABLE_NAME, null, null); | 251 mDatabase.delete(HTTPAUTH_TABLE_NAME, null, null); |
248 } | 252 } |
249 } | 253 } |
OLD | NEW |