diff options
author | kristianm@chromium.org <kristianm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-05 02:05:39 +0000 |
---|---|---|
committer | kristianm@chromium.org <kristianm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-05 02:05:39 +0000 |
commit | 44100446a3f13e5fa2ef69a0f5d342e9bf5d23f4 (patch) | |
tree | 87f4650da39d5f67907a76f331f6f775ee00e32c /android_webview/native/aw_contents_client_bridge.cc | |
parent | e2d81b055fbfcdaff73c52da161ce330ea25b0ab (diff) | |
download | chromium_src-44100446a3f13e5fa2ef69a0f5d342e9bf5d23f4.zip chromium_src-44100446a3f13e5fa2ef69a0f5d342e9bf5d23f4.tar.gz chromium_src-44100446a3f13e5fa2ef69a0f5d342e9bf5d23f4.tar.bz2 |
Callbacks cannot be owned by Java
Would like to merge js_result_handler into aw_javascript_dialog_manager
as it is not that much code, and it would mean one less global.
BUG=
Review URL: https://chromiumcodereview.appspot.com/12797007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@192445 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'android_webview/native/aw_contents_client_bridge.cc')
-rw-r--r-- | android_webview/native/aw_contents_client_bridge.cc | 107 |
1 files changed, 101 insertions, 6 deletions
diff --git a/android_webview/native/aw_contents_client_bridge.cc b/android_webview/native/aw_contents_client_bridge.cc index 0c13ebe..213646f 100644 --- a/android_webview/native/aw_contents_client_bridge.cc +++ b/android_webview/native/aw_contents_client_bridge.cc @@ -16,6 +16,7 @@ using base::android::AttachCurrentThread; using base::android::ConvertJavaStringToUTF16; using base::android::ConvertUTF8ToJavaString; +using base::android::ConvertUTF16ToJavaString; using base::android::JavaRef; using base::android::ScopedJavaLocalRef; using content::BrowserThread; @@ -25,8 +26,8 @@ namespace android_webview { AwContentsClientBridge::AwContentsClientBridge(JNIEnv* env, jobject obj) : java_ref_(env, obj) { DCHECK(obj); - Java_AwContentsClientBridge_setNativeContentsClientBridge(env, obj, - reinterpret_cast<jint>(this)); + Java_AwContentsClientBridge_setNativeContentsClientBridge( + env, obj, reinterpret_cast<jint>(this)); } AwContentsClientBridge::~AwContentsClientBridge() { @@ -56,16 +57,18 @@ void AwContentsClientBridge::AllowCertificateError( std::string der_string; net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), &der_string); - ScopedJavaLocalRef<jbyteArray> jcert = base::android::ToJavaByteArray(env, - reinterpret_cast<const uint8*>(der_string.data()), der_string.length()); + ScopedJavaLocalRef<jbyteArray> jcert = base::android::ToJavaByteArray( + env, + reinterpret_cast<const uint8*>(der_string.data()), + der_string.length()); ScopedJavaLocalRef<jstring> jurl(ConvertUTF8ToJavaString( env, request_url.spec())); // We need to add the callback before making the call to java side, // as it may do a synchronous callback prior to returning. int request_id = pending_cert_error_callbacks_.Add( new CertErrorCallback(callback)); - *cancel_request = !Java_AwContentsClientBridge_allowCertificateError(env, - obj.obj(), cert_error, jcert.obj(), jurl.obj(), request_id); + *cancel_request = !Java_AwContentsClientBridge_allowCertificateError( + env, obj.obj(), cert_error, jcert.obj(), jurl.obj(), request_id); // if the request is cancelled, then cancel the stored callback if (*cancel_request) { pending_cert_error_callbacks_.Remove(request_id); @@ -84,6 +87,98 @@ void AwContentsClientBridge::ProceedSslError(JNIEnv* env, jobject obj, pending_cert_error_callbacks_.Remove(id); } +void AwContentsClientBridge::RunJavaScriptDialog( + content::JavaScriptMessageType message_type, + const GURL& origin_url, + const string16& message_text, + const string16& default_prompt_text, + const content::JavaScriptDialogManager::DialogClosedCallback& callback) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + JNIEnv* env = AttachCurrentThread(); + + ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); + if (obj.is_null()) + return; + + int callback_id = pending_js_dialog_callbacks_.Add( + new content::JavaScriptDialogManager::DialogClosedCallback(callback)); + ScopedJavaLocalRef<jstring> jurl( + ConvertUTF8ToJavaString(env, origin_url.spec())); + ScopedJavaLocalRef<jstring> jmessage( + ConvertUTF16ToJavaString(env, message_text)); + + switch (message_type) { + case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: + Java_AwContentsClientBridge_handleJsAlert( + env, obj.obj(), jurl.obj(), jmessage.obj(), callback_id); + break; + case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: + Java_AwContentsClientBridge_handleJsConfirm( + env, obj.obj(), jurl.obj(), jmessage.obj(), callback_id); + break; + case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT: { + ScopedJavaLocalRef<jstring> jdefault_value( + ConvertUTF16ToJavaString(env, default_prompt_text)); + Java_AwContentsClientBridge_handleJsPrompt(env, + obj.obj(), + jurl.obj(), + jmessage.obj(), + jdefault_value.obj(), + callback_id); + break; + } + default: + NOTREACHED(); + } +} + +void AwContentsClientBridge::RunBeforeUnloadDialog( + const GURL& origin_url, + const string16& message_text, + const content::JavaScriptDialogManager::DialogClosedCallback& callback) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + JNIEnv* env = AttachCurrentThread(); + + ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); + if (obj.is_null()) + return; + + int callback_id = pending_js_dialog_callbacks_.Add( + new content::JavaScriptDialogManager::DialogClosedCallback(callback)); + ScopedJavaLocalRef<jstring> jurl( + ConvertUTF8ToJavaString(env, origin_url.spec())); + ScopedJavaLocalRef<jstring> jmessage( + ConvertUTF16ToJavaString(env, message_text)); + + Java_AwContentsClientBridge_handleJsBeforeUnload( + env, obj.obj(), jurl.obj(), jmessage.obj(), callback_id); +} + +void AwContentsClientBridge::ConfirmJsResult(JNIEnv* env, + jobject, + int id, + jstring prompt) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + content::JavaScriptDialogManager::DialogClosedCallback* callback = + pending_js_dialog_callbacks_.Lookup(id); + string16 prompt_text; + if (prompt) { + prompt_text = ConvertJavaStringToUTF16(env, prompt); + } + if (callback) + callback->Run(true, prompt_text); + pending_js_dialog_callbacks_.Remove(id); +} + +void AwContentsClientBridge::CancelJsResult(JNIEnv*, jobject, int id) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + content::JavaScriptDialogManager::DialogClosedCallback* callback = + pending_js_dialog_callbacks_.Lookup(id); + if (callback) + callback->Run(false, string16()); + pending_js_dialog_callbacks_.Remove(id); +} + bool RegisterAwContentsClientBridge(JNIEnv* env) { return RegisterNativesImpl(env) >= 0; } |