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 5 package org.chromium.android_webview; 6 7 import android.net.http.SslCertificate; 8 import android.net.http.SslError; 9 import android.webkit.ValueCallback; 10 11 import org.chromium.base.CalledByNative; 12 import org.chromium.base.JNINamespace; 13 14 /** 15 * This class handles the JNI communication logic for the the AwContentsClient class. 16 * Both the Java and the native peers of AwContentsClientBridge are owned by the 17 * corresponding AwContents instances. This class and its native peer are connected 18 * via weak references. The native AwContentsClientBridge sets up and clear these weak 19 * references. 20 */ 21 @JNINamespace("android_webview") 22 public class AwContentsClientBridge { 23 24 private AwContentsClient mClient; 25 // The native peer of this object. 26 private long mNativeContentsClientBridge; 27 AwContentsClientBridge(AwContentsClient client)28 public AwContentsClientBridge(AwContentsClient client) { 29 assert client != null; 30 mClient = client; 31 } 32 33 // Used by the native peer to set/reset a weak ref to the native peer. 34 @CalledByNative setNativeContentsClientBridge(long nativeContentsClientBridge)35 private void setNativeContentsClientBridge(long nativeContentsClientBridge) { 36 mNativeContentsClientBridge = nativeContentsClientBridge; 37 } 38 39 // If returns false, the request is immediately canceled, and any call to proceedSslError 40 // has no effect. If returns true, the request should be canceled or proceeded using 41 // proceedSslError(). 42 // Unlike the webview classic, we do not keep keep a database of certificates that 43 // are allowed by the user, because this functionality is already handled via 44 // ssl_policy in native layers. 45 @CalledByNative allowCertificateError(int certError, byte[] derBytes, final String url, final int id)46 private boolean allowCertificateError(int certError, byte[] derBytes, final String url, 47 final int id) { 48 final SslCertificate cert = SslUtil.getCertificateFromDerBytes(derBytes); 49 if (cert == null) { 50 // if the certificate or the client is null, cancel the request 51 return false; 52 } 53 final SslError sslError = SslUtil.sslErrorFromNetErrorCode(certError, cert, url); 54 ValueCallback<Boolean> callback = new ValueCallback<Boolean>() { 55 @Override 56 public void onReceiveValue(Boolean value) { 57 proceedSslError(value.booleanValue(), id); 58 } 59 }; 60 mClient.onReceivedSslError(callback, sslError); 61 return true; 62 } 63 proceedSslError(boolean proceed, int id)64 private void proceedSslError(boolean proceed, int id) { 65 if (mNativeContentsClientBridge == 0) return; 66 nativeProceedSslError(mNativeContentsClientBridge, proceed, id); 67 } 68 69 @CalledByNative handleJsAlert(String url, String message, int id)70 private void handleJsAlert(String url, String message, int id) { 71 JsResultHandler handler = new JsResultHandler(this, id); 72 mClient.handleJsAlert(url, message, handler); 73 } 74 75 @CalledByNative handleJsConfirm(String url, String message, int id)76 private void handleJsConfirm(String url, String message, int id) { 77 JsResultHandler handler = new JsResultHandler(this, id); 78 mClient.handleJsConfirm(url, message, handler); 79 } 80 81 @CalledByNative handleJsPrompt(String url, String message, String defaultValue, int id)82 private void handleJsPrompt(String url, String message, String defaultValue, int id) { 83 JsResultHandler handler = new JsResultHandler(this, id); 84 mClient.handleJsPrompt(url, message, defaultValue, handler); 85 } 86 87 @CalledByNative handleJsBeforeUnload(String url, String message, int id)88 private void handleJsBeforeUnload(String url, String message, int id) { 89 JsResultHandler handler = new JsResultHandler(this, id); 90 mClient.handleJsBeforeUnload(url, message, handler); 91 } 92 93 @CalledByNative shouldOverrideUrlLoading(String url)94 private boolean shouldOverrideUrlLoading(String url) { 95 return mClient.shouldOverrideUrlLoading(url); 96 } 97 confirmJsResult(int id, String prompt)98 void confirmJsResult(int id, String prompt) { 99 if (mNativeContentsClientBridge == 0) return; 100 nativeConfirmJsResult(mNativeContentsClientBridge, id, prompt); 101 } 102 cancelJsResult(int id)103 void cancelJsResult(int id) { 104 if (mNativeContentsClientBridge == 0) return; 105 nativeCancelJsResult(mNativeContentsClientBridge, id); 106 } 107 108 //-------------------------------------------------------------------------------------------- 109 // Native methods 110 //-------------------------------------------------------------------------------------------- nativeProceedSslError(long nativeAwContentsClientBridge, boolean proceed, int id)111 private native void nativeProceedSslError(long nativeAwContentsClientBridge, boolean proceed, 112 int id); 113 nativeConfirmJsResult(long nativeAwContentsClientBridge, int id, String prompt)114 private native void nativeConfirmJsResult(long nativeAwContentsClientBridge, int id, 115 String prompt); nativeCancelJsResult(long nativeAwContentsClientBridge, int id)116 private native void nativeCancelJsResult(long nativeAwContentsClientBridge, int id); 117 } 118