Skip to content

Commit 270a3c8

Browse files
author
Steve Block
committed
Clean up SslErrorHandlerImpl
- Use assert rather than junit.framework.Assert - Add some comments - There's no need for checkSslPrefTable() to call handleSslErrorResponse() as we'll never update the table. Instead call LoadListener.handleSslErrorResponse() directly. No functional change. Bug: 5409251 Change-Id: I0c6cdae43fa966f86f4a6c43b74c2f2a01f60319
1 parent fa03f9a commit 270a3c8

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

core/java/android/webkit/SslErrorHandlerImpl.java

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package android.webkit;
1818

19-
import junit.framework.Assert;
20-
2119
import android.net.http.SslError;
2220
import android.os.Bundle;
2321
import android.os.Handler;
@@ -54,7 +52,7 @@ class SslErrorHandlerImpl extends SslErrorHandler {
5452
private final SslErrorHandler mOriginHandler;
5553
private final LoadListener mLoadListener;
5654

57-
// Message id for handling the response
55+
// Message id for handling the response from the client.
5856
private static final int HANDLE_RESPONSE = 100;
5957

6058
@Override
@@ -130,7 +128,9 @@ private SslErrorHandlerImpl(SslErrorHandler origin, LoadListener listener) {
130128
}
131129

132130
/**
133-
* Handles SSL error(s) on the way up to the user.
131+
* Handles requests from the network stack about whether to proceed with a
132+
* load given an SSL error(s). We may ask the client what to do, or use a
133+
* cached response.
134134
*/
135135
/* package */ synchronized void handleSslErrorRequest(LoadListener loader) {
136136
if (DebugFlags.SSL_ERROR_HANDLER) {
@@ -147,30 +147,33 @@ private SslErrorHandlerImpl(SslErrorHandler origin, LoadListener listener) {
147147
}
148148

149149
/**
150-
* Check the preference table for a ssl error that has already been shown
151-
* to the user.
150+
* Check the preference table to see if we already have a 'proceed' decision
151+
* from the client for this host and for an error of equal or greater
152+
* severity than the supplied error. If so, instruct the loader to proceed
153+
* and return true. Otherwise return false.
152154
*/
153155
/* package */ synchronized boolean checkSslPrefTable(LoadListener loader,
154156
SslError error) {
155157
final String host = loader.host();
156158
final int primary = error.getPrimaryError();
157159

158160
if (DebugFlags.SSL_ERROR_HANDLER) {
159-
Assert.assertTrue(host != null && primary != 0);
161+
assert host != null;
162+
assert primary != 0;
160163
}
161164

162-
if (mSslPrefTable.containsKey(host)) {
163-
if (primary <= mSslPrefTable.getInt(host)) {
164-
handleSslErrorResponse(loader, error, true);
165-
return true;
165+
if (mSslPrefTable.containsKey(host) && primary <= mSslPrefTable.getInt(host)) {
166+
if (!loader.cancelled()) {
167+
loader.handleSslErrorResponse(true);
166168
}
169+
return true;
167170
}
168171
return false;
169172
}
170173

171174
/**
172175
* Processes queued SSL-error confirmation requests in
173-
* a tight loop while there is no need to ask the user.
176+
* a tight loop while there is no need to ask the client.
174177
*/
175178
/* package */void fastProcessQueuedSslErrors() {
176179
while (processNextLoader());
@@ -195,19 +198,18 @@ private synchronized boolean processNextLoader() {
195198
SslError error = loader.sslError();
196199

197200
if (DebugFlags.SSL_ERROR_HANDLER) {
198-
Assert.assertNotNull(error);
201+
assert error != null;
199202
}
200203

201-
// checkSslPrefTable will handle the ssl error response if the
202-
// answer is available. It does not remove the loader from the
203-
// queue.
204+
// checkSslPrefTable() will instruct the loader to proceed if we
205+
// have a cached 'proceed' decision. It does not remove the loader
206+
// from the queue.
204207
if (checkSslPrefTable(loader, error)) {
205208
mLoaderQueue.remove(loader);
206209
return true;
207210
}
208211

209-
// if we do not have information on record, ask
210-
// the user (display a dialog)
212+
// If we can not proceed based on a cached decision, ask the client.
211213
CallbackProxy proxy = loader.getFrame().getCallbackProxy();
212214
proxy.onReceivedSslError(new SslErrorHandlerImpl(this, loader), error);
213215
}
@@ -217,32 +219,31 @@ private synchronized boolean processNextLoader() {
217219
}
218220

219221
/**
220-
* Proceed with the SSL certificate.
222+
* Proceed with this load.
221223
*/
222224
public void proceed() {
223-
mOriginHandler.sendMessage(
224-
mOriginHandler.obtainMessage(
225-
HANDLE_RESPONSE, 1, 0, mLoadListener));
225+
mOriginHandler.sendMessage(mOriginHandler.obtainMessage(
226+
HANDLE_RESPONSE, 1, 0, mLoadListener));
226227
}
227228

228229
/**
229-
* Cancel this request and all pending requests for the WebView that had
230-
* the error.
230+
* Cancel this load and all pending loads for the WebView that had the
231+
* error.
231232
*/
232233
public void cancel() {
233-
mOriginHandler.sendMessage(
234-
mOriginHandler.obtainMessage(
235-
HANDLE_RESPONSE, 0, 0, mLoadListener));
234+
mOriginHandler.sendMessage(mOriginHandler.obtainMessage(
235+
HANDLE_RESPONSE, 0, 0, mLoadListener));
236236
}
237237

238238
/**
239-
* Handles SSL error(s) on the way down from the user.
239+
* Handles the response from the client about whether to proceed with this
240+
* load. We save the response to be re-used in the future.
240241
*/
241242
/* package */ synchronized void handleSslErrorResponse(LoadListener loader,
242243
SslError error, boolean proceed) {
243244
if (DebugFlags.SSL_ERROR_HANDLER) {
244-
Assert.assertNotNull(loader);
245-
Assert.assertNotNull(error);
245+
assert loader != null;
246+
assert error != null;
246247
}
247248

248249
if (DebugFlags.SSL_ERROR_HANDLER) {
@@ -253,16 +254,16 @@ public void cancel() {
253254

254255
if (!loader.cancelled()) {
255256
if (proceed) {
256-
// update the user's SSL error preference table
257+
// Update the SSL error preference table
257258
int primary = error.getPrimaryError();
258259
String host = loader.host();
259260

260261
if (DebugFlags.SSL_ERROR_HANDLER) {
261-
Assert.assertTrue(host != null && primary != 0);
262+
assert host != null;
263+
assert primary != 0;
262264
}
263265
boolean hasKey = mSslPrefTable.containsKey(host);
264-
if (!hasKey ||
265-
primary > mSslPrefTable.getInt(host)) {
266+
if (!hasKey || primary > mSslPrefTable.getInt(host)) {
266267
mSslPrefTable.putInt(host, primary);
267268
}
268269
}

0 commit comments

Comments
 (0)