Chromium Code Reviews| Index: content/browser/web_contents/web_contents_android.cc |
| diff --git a/content/browser/web_contents/web_contents_android.cc b/content/browser/web_contents/web_contents_android.cc |
| index 7de19d8924532e71e65dedae99f5ef94de2e4d36..e00bbf9e62b2623170283b3db821cde1b30abc00 100644 |
| --- a/content/browser/web_contents/web_contents_android.cc |
| +++ b/content/browser/web_contents/web_contents_android.cc |
| @@ -7,6 +7,7 @@ |
| #include "base/android/jni_android.h" |
| #include "base/android/jni_string.h" |
| #include "base/command_line.h" |
| +#include "base/json/json_writer.h" |
| #include "base/logging.h" |
| #include "content/browser/android/interstitial_page_delegate_android.h" |
| #include "content/browser/frame_host/interstitial_page_impl.h" |
| @@ -24,7 +25,23 @@ |
| using base::android::AttachCurrentThread; |
| using base::android::ConvertJavaStringToUTF8; |
| +using base::android::ConvertJavaStringToUTF16; |
| using base::android::ConvertUTF8ToJavaString; |
| +using base::android::ScopedJavaGlobalRef; |
| + |
| +namespace { |
| + |
| +void JavaScriptResultCallback(const ScopedJavaGlobalRef<jobject>& callback, |
| + const base::Value* result) { |
| + JNIEnv* env = base::android::AttachCurrentThread(); |
| + std::string json; |
| + base::JSONWriter::Write(result, &json); |
| + ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); |
| + content::Java_WebContentsImpl_onEvaluateJavaScriptResult( |
| + env, j_json.obj(), callback.obj()); |
| +} |
| + |
| +} // namespace |
| namespace content { |
| @@ -307,4 +324,38 @@ void WebContentsAndroid::DidStartNavigationTransitionForFrame(int64 frame_id) { |
| env, obj_.obj(), frame_id); |
| } |
| +void WebContentsAndroid::EvaluateJavaScript(JNIEnv* env, |
| + jobject obj, |
| + jstring script, |
| + jobject callback, |
| + jboolean start_renderer) { |
| + RenderViewHost* rvh = web_contents_->GetRenderViewHost(); |
| + DCHECK(rvh); |
| + |
| + if (start_renderer && !rvh->IsRenderViewLive()) { |
| + if (!static_cast<WebContentsImpl*>(web_contents_) |
| + ->CreateRenderViewForInitialEmptyDocument()) { |
|
Ted C
2014/08/14 17:28:07
This indenting seems off, I think the -> should go
AKVT
2014/08/14 17:39:59
Done.
|
| + LOG(ERROR) << "Failed to create RenderView in EvaluateJavaScript"; |
| + return; |
| + } |
| + } |
| + |
| + if (!callback) { |
| + // No callback requested. |
| + web_contents_->GetMainFrame()->ExecuteJavaScript( |
| + ConvertJavaStringToUTF16(env, script)); |
| + return; |
| + } |
| + |
| + // Secure the Java callback in a scoped object and give ownership of it to the |
| + // base::Callback. |
| + ScopedJavaGlobalRef<jobject> j_callback; |
| + j_callback.Reset(env, callback); |
| + content::RenderFrameHost::JavaScriptResultCallback c_callback = |
| + base::Bind(&JavaScriptResultCallback, j_callback); |
| + |
| + web_contents_->GetMainFrame()->ExecuteJavaScript( |
| + ConvertJavaStringToUTF16(env, script), c_callback); |
|
Ted C
2014/08/14 17:28:07
The c_ prefix is not something I've seen before.
AKVT
2014/08/14 17:39:59
Done. Thanks
|
| +} |
| + |
| } // namespace content |