diff options
author | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-15 01:32:16 +0000 |
---|---|---|
committer | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-15 01:32:16 +0000 |
commit | 9cb9d142c02d49fd89121d7019c0507f74b53d96 (patch) | |
tree | 8568db3341493d723a3494f6dce3230282136aa9 /content/browser/android | |
parent | c10d4fefa51679946a106e334d9ba353960aa290 (diff) | |
download | chromium_src-9cb9d142c02d49fd89121d7019c0507f74b53d96.zip chromium_src-9cb9d142c02d49fd89121d7019c0507f74b53d96.tar.gz chromium_src-9cb9d142c02d49fd89121d7019c0507f74b53d96.tar.bz2 |
Switch Android code to use ExecuteJavascriptInWebFrameCallbackResult.
BUG=168169
TEST=should all pass
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=175489
Reverted: https://src.chromium.org/viewvc/chrome?view=rev&revision=175491
Review URL: https://chromiumcodereview.appspot.com/11788005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@176773 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/android')
-rw-r--r-- | content/browser/android/content_view_core_impl.cc | 73 | ||||
-rw-r--r-- | content/browser/android/content_view_core_impl.h | 5 |
2 files changed, 41 insertions, 37 deletions
diff --git a/content/browser/android/content_view_core_impl.cc b/content/browser/android/content_view_core_impl.cc index be9a227..ffee1ca 100644 --- a/content/browser/android/content_view_core_impl.cc +++ b/content/browser/android/content_view_core_impl.cc @@ -11,6 +11,7 @@ #include "base/json/json_writer.h" #include "base/logging.h" #include "base/utf_string_conversions.h" +#include "base/values.h" #include "cc/layer.h" #include "content/browser/android/interstitial_page_delegate_android.h" #include "content/browser/android/load_url_params.h" @@ -174,10 +175,6 @@ ContentViewCoreImpl::ContentViewCoreImpl(JNIEnv* env, jobject obj, dpi_scale_ = device_info->GetDPIScale(); } - notification_registrar_.Add(this, - NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, - NotificationService::AllSources()); - // Currently, the only use case we have for overriding a user agent involves // spoofing a desktop Linux user agent for "Request desktop site". // Automatically set it for all WebContents so that it is available when a @@ -272,25 +269,6 @@ void ContentViewCoreImpl::Observe(int type, } break; } - case NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT: { - if (!web_contents_ || Source<RenderViewHost>(source).ptr() != - web_contents_->GetRenderViewHost()) { - return; - } - - JNIEnv* env = base::android::AttachCurrentThread(); - std::pair<int, Value*>* result_pair = - Details<std::pair<int, Value*> >(details).ptr(); - std::string json; - base::JSONWriter::Write(result_pair->second, &json); - ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); - ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); - if (!j_obj.is_null()) { - Java_ContentViewCore_onEvaluateJavaScriptResult(env, j_obj.obj(), - static_cast<jint>(result_pair->first), j_json.obj()); - } - break; - } } } @@ -1248,15 +1226,46 @@ void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView( new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID())); } -jint ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, +namespace { +void JavaScriptResultCallback(ScopedJavaGlobalRef<jobject>* callback, + const base::Value* result) { + // |callback| is passed as base::Owned, so it will automatically be deleted + // when this base::Callback goes out of scope. + JNIEnv* env = base::android::AttachCurrentThread(); + std::string json; + base::JSONWriter::Write(result, &json); + ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); + Java_ContentViewCore_onEvaluateJavaScriptResult(env, + j_json.obj(), + callback->obj()); +} +} // namespace + +void ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, jobject obj, - jstring script) { + jstring script, + jobject callback) { RenderViewHost* host = web_contents_->GetRenderViewHost(); DCHECK(host); - string16 script_utf16 = ConvertJavaStringToUTF16(env, script); - return host->ExecuteJavascriptInWebFrameNotifyResult(string16(), - script_utf16); + if (!callback) { + // No callback requested. + host->ExecuteJavascriptInWebFrame(string16(), // frame_xpath + 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 = new ScopedJavaGlobalRef<jobject>(); + j_callback->Reset(env, callback); + content::RenderViewHost::JavascriptResultCallback c_callback = + base::Bind(&JavaScriptResultCallback, base::Owned(j_callback)); + + host->ExecuteJavascriptInWebFrameCallbackResult( + string16(), // frame_xpath + ConvertJavaStringToUTF16(env, script), + c_callback); } bool ContentViewCoreImpl::GetUseDesktopUserAgent( @@ -1332,14 +1341,6 @@ jint Init(JNIEnv* env, jobject obj, return reinterpret_cast<jint>(view); } -jint EvaluateJavaScript(JNIEnv* env, jobject obj, jstring script) { - ContentViewCoreImpl* view = static_cast<ContentViewCoreImpl*>( - ContentViewCore::GetNativeContentViewCore(env, obj)); - DCHECK(view); - - return view->EvaluateJavaScript(env, obj, script); -} - bool RegisterContentViewCore(JNIEnv* env) { if (!base::android::HasClass(env, kContentViewCoreClassPath)) { DLOG(ERROR) << "Unable to find class ContentViewCore!"; diff --git a/content/browser/android/content_view_core_impl.h b/content/browser/android/content_view_core_impl.h index 232fa44..1eed167 100644 --- a/content/browser/android/content_view_core_impl.h +++ b/content/browser/android/content_view_core_impl.h @@ -158,7 +158,10 @@ class ContentViewCoreImpl : public ContentViewCore, void ContinuePendingReload(JNIEnv* env, jobject obj); jboolean NeedsReload(JNIEnv* env, jobject obj); void ClearHistory(JNIEnv* env, jobject obj); - jint EvaluateJavaScript(JNIEnv* env, jobject obj, jstring script); + void EvaluateJavaScript(JNIEnv* env, + jobject obj, + jstring script, + jobject callback); int GetNativeImeAdapter(JNIEnv* env, jobject obj); void SetFocus(JNIEnv* env, jobject obj, jboolean focused); void ScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj); |