Commit ad1a7385 authored by dtrainor@chromium.org's avatar dtrainor@chromium.org

Upstream evaluateJavaScript

Added ContentViewCore.evaluateJavaScript and tied it into
ContentViewClient.onEvaluateJavaScriptResult.

BUG=http://crbug.com/139732


Review URL: https://chromiumcodereview.appspot.com/10848002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149335 0039d316-1c4b-4281-b951-d872f2087c98
parent 09a4a495
......@@ -8,6 +8,7 @@
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/json/json_writer.h"
#include "content/browser/android/content_view_client.h"
#include "content/browser/android/touch_point.h"
#include "content/browser/renderer_host/java/java_bound_object.h"
......@@ -18,6 +19,10 @@
#include "content/browser/web_contents/navigation_controller_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/interstitial_page.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/web_contents.h"
#include "jni/ContentViewCore_jni.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
......@@ -82,9 +87,17 @@ ContentViewCoreImpl::ContentViewCoreImpl(JNIEnv* env, jobject obj,
"A ContentViewCoreImpl should be created with a valid WebContents.";
InitJNI(env, obj);
notification_registrar_.Add(this,
NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT,
NotificationService::AllSources());
}
ContentViewCoreImpl::~ContentViewCoreImpl() {
// Make sure nobody calls back into this object while we are tearing things
// down.
notification_registrar_.RemoveAll();
if (java_object_) {
JNIEnv* env = AttachCurrentThread();
env->DeleteWeakGlobalRef(java_object_->obj);
......@@ -100,6 +113,27 @@ void ContentViewCoreImpl::Destroy(JNIEnv* env, jobject obj) {
void ContentViewCoreImpl::Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type) {
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);
Java_ContentViewCore_onEvaluateJavaScriptResult(env,
java_object_->View(env).obj(),
static_cast<jint>(result_pair->first), j_json.obj());
break;
}
}
// TODO(jrg)
}
......@@ -139,6 +173,15 @@ void ContentViewCoreImpl::SelectPopupMenuItems(JNIEnv* env, jobject obj,
rvhi->DidSelectPopupMenuItems(selected_indices);
}
jint ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, jobject obj,
jstring script) {
RenderViewHost* host = web_contents_->GetRenderViewHost();
string16 script_utf16 = ConvertJavaStringToUTF16(env, script);
return host->ExecuteJavascriptInWebFrameNotifyResult(string16(),
script_utf16);
}
void ContentViewCoreImpl::LoadUrlWithoutUrlSanitization(JNIEnv* env,
jobject,
jstring jurl,
......@@ -401,6 +444,13 @@ jint Init(JNIEnv* env, jobject obj, jint native_web_contents) {
return reinterpret_cast<jint>(view);
}
jint EvaluateJavaScript(JNIEnv* env, jobject obj, jstring script) {
ContentViewCoreImpl* view = static_cast<ContentViewCoreImpl*>
(ContentViewCore::GetNativeContentViewCore(env, obj));
return view->EvaluateJavaScript(env, obj, script);
}
// --------------------------------------------------------------------------
// Public methods that call to Java via JNI
// --------------------------------------------------------------------------
......
......@@ -17,6 +17,7 @@
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/android/content_view_core.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "googleurl/src/gurl.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
#include "ui/gfx/rect.h"
......@@ -111,6 +112,7 @@ class ContentViewCoreImpl : public ContentViewCore,
jboolean NeedsReload(JNIEnv* env, jobject obj);
void ClearHistory(JNIEnv* env, jobject obj);
void SetClient(JNIEnv* env, jobject obj, jobject jclient);
jint EvaluateJavaScript(JNIEnv* env, jobject obj, jstring script);
void AddJavascriptInterface(JNIEnv* env,
jobject obj,
jobject object,
......@@ -193,6 +195,8 @@ class ContentViewCoreImpl : public ContentViewCore,
struct JavaObject;
JavaObject* java_object_;
NotificationRegistrar notification_registrar_;
// Reference to the current WebContents used to determine how and what to
// display in the ContentViewCore.
WebContentsImpl* web_contents_;
......
......@@ -649,7 +649,10 @@
'-buildfile',
'shell/android/java/content_shell_apk.xml',
# '<(CONFIGURATION_NAME)',
]
],
'dependencies': [
'content_java',
],
}
],
},
......
......@@ -577,6 +577,21 @@ public class ContentViewCore implements MotionEventDelegate {
}
}
/**
* Injects the passed JavaScript code in the current page and evaluates it.
* Once evaluated, an asynchronous call to
* ContentViewClient.onJavaScriptEvaluationResult is made. Used in automation
* tests.
*
* @return an id that is passed along in the asynchronous onJavaScriptEvaluationResult callback
* @throws IllegalStateException If the ContentView has been destroyed.
* @hide
*/
public int evaluateJavaScript(String script) throws IllegalStateException {
checkIsAlive();
return nativeEvaluateJavaScript(script);
}
/**
* This method should be called when the containing activity is paused
*/
......@@ -810,6 +825,12 @@ public class ContentViewCore implements MotionEventDelegate {
SelectPopupDialog.show(this, items, enabled, multiple, selectedIndices);
}
@SuppressWarnings("unused")
@CalledByNative
private void onEvaluateJavaScriptResult(int id, String jsonResult) {
getContentViewClient().onEvaluateJavaScriptResult(id, jsonResult);
}
/**
* Called (from native) when page loading begins.
*/
......@@ -1069,6 +1090,8 @@ public class ContentViewCore implements MotionEventDelegate {
private native void nativeClearHistory(int nativeContentViewCoreImpl);
private native int nativeEvaluateJavaScript(String script);
private native void nativeAddJavascriptInterface(int nativeContentViewCoreImpl, Object object,
String name, boolean allowInheritedMethods);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment