Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Unified Diff: content/browser/android/content_view_core_impl.cc

Issue 10848002: Upstream evaluateJavaScript (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/android/content_view_core_impl.cc
diff --git a/content/browser/android/content_view_core_impl.cc b/content/browser/android/content_view_core_impl.cc
index d0e5bd2aee2cfa46819d05aa64ba972f8919d543..c5624af8bffbef3d365106469f7cdf0fc2b8fdae 100644
--- a/content/browser/android/content_view_core_impl.cc
+++ b/content/browser/android/content_view_core_impl.cc
@@ -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,16 @@ 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();
+ DCHECK(host);
jam 2012/07/31 18:20:44 nit: unnecessary, if null will crash right below
David Trainor- moved to gerrit 2012/07/31 20:38:47 Done.
+
+ string16 script_utf16 = ConvertJavaStringToUTF16(env, script);
+ return host->ExecuteJavascriptInWebFrameNotifyResult(string16(),
+ script_utf16);
+}
+
void ContentViewCoreImpl::LoadUrlWithoutUrlSanitization(JNIEnv* env,
jobject,
jstring jurl,
@@ -401,6 +445,15 @@ 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 =
+ (ContentViewCoreImpl*) ContentViewCore::GetNativeContentViewCore(env,
jam 2012/07/31 18:20:44 google style is to not use c style casts. so use s
David Trainor- moved to gerrit 2012/07/31 20:38:47 Done.
+ obj);
+ DCHECK(view);
jam 2012/07/31 18:20:44 ditto
David Trainor- moved to gerrit 2012/07/31 20:38:47 Done.
+
+ return view->EvaluateJavaScript(env, obj, script);
+}
+
// --------------------------------------------------------------------------
// Public methods that call to Java via JNI
// --------------------------------------------------------------------------

Powered by Google App Engine
This is Rietveld 408576698