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