OLD | NEW |
---|---|
(Empty) | |
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 #include "android_webview/native/aw_contents_client_bridge.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_array.h" | |
9 #include "base/android/jni_string.h" | |
10 #include "base/callback.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "googleurl/src/gurl.h" | |
13 #include "jni/AwContentsClientBridge_jni.h" | |
14 #include "net/base/x509_certificate.h" | |
15 | |
16 using base::android::AttachCurrentThread; | |
17 using base::android::ConvertJavaStringToUTF16; | |
18 using base::android::ConvertUTF8ToJavaString; | |
19 using base::android::JavaRef; | |
20 using base::android::ScopedJavaLocalRef; | |
21 using content::BrowserThread; | |
22 | |
23 namespace android_webview { | |
24 | |
25 AwContentsClientBridge::AwContentsClientBridge(JNIEnv* env, jobject obj) | |
26 : java_ref_(env, obj) { | |
27 DCHECK(obj); | |
28 Java_AwContentsClientBridge_setNativeContentsClientBridge(env, obj, | |
29 reinterpret_cast<jint>(this)); | |
30 } | |
31 | |
32 AwContentsClientBridge::~AwContentsClientBridge() { | |
33 JNIEnv* env = AttachCurrentThread(); | |
34 | |
35 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
36 if (obj.is_null()) | |
37 return; | |
38 // Clear the weak reference from the java peer to the native object since | |
39 // it is possible that java object lifetime can exceed the AwContens. | |
40 Java_AwContentsClientBridge_setNativeContentsClientBridge(env, obj.obj(), 0); | |
41 } | |
42 | |
43 void AwContentsClientBridge::AllowCertificateError( | |
44 int cert_error, | |
45 net::X509Certificate* cert, | |
46 const GURL& request_url, | |
47 const base::Callback<void(bool)>& callback, | |
48 bool* cancel_request) { | |
49 | |
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
51 JNIEnv* env = AttachCurrentThread(); | |
52 | |
53 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
54 if (obj.is_null()) | |
55 return; | |
56 | |
57 std::string der_string; | |
58 net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), &der_string); | |
59 ScopedJavaLocalRef<jbyteArray> jcert = base::android::ToJavaByteArray(env, | |
60 reinterpret_cast<const uint8*>(der_string.data()), der_string.length()); | |
61 ScopedJavaLocalRef<jstring> jurl(ConvertUTF8ToJavaString( | |
62 env, request_url.spec())); | |
63 // We need to add the callback before making the call to java side, | |
64 // as it may do a synchronous callback prior to returning. | |
65 int request_id = pending_cert_error_callbacks_.Add( | |
66 new CertErrorCallback(callback)); | |
67 *cancel_request = !Java_AwContentsClientBridge_allowCertificateError(env, | |
68 obj.obj(), reinterpret_cast<jint>(cert_error), jcert.obj(), jurl.obj(), | |
69 request_id); | |
70 // if the request is cancelled, then cancel the stored callback | |
71 if (*cancel_request) { | |
72 pending_cert_error_callbacks_.Remove(request_id); | |
73 } | |
74 } | |
75 | |
76 void AwContentsClientBridge::ProceedSslError(JNIEnv* env, jobject obj, | |
77 jboolean proceed, jint id) { | |
benm (inactive)
2013/02/25 12:16:38
nit: indent looks wrong here
sgurun-gerrit only
2013/02/25 19:45:22
Done.
| |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
79 CertErrorCallback* callback = pending_cert_error_callbacks_.Lookup(id); | |
80 if (!callback || callback->is_null()) { | |
81 LOG(WARNING) << "Ignoring unexpected ssl error proceed callback"; | |
82 return; | |
83 } | |
84 callback->Run(proceed); | |
85 pending_cert_error_callbacks_.Remove(id); | |
86 } | |
87 | |
88 bool RegisterAwContentsClientBridge(JNIEnv* env) { | |
89 return RegisterNativesImpl(env) >= 0; | |
90 } | |
91 | |
92 } // namespace android_webview | |
OLD | NEW |