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

Side by Side 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: Nit Fixes Created 8 years, 4 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/android/content_view_core_impl.h ('k') | content/content_shell.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/android/content_view_core_impl.h" 5 #include "content/browser/android/content_view_core_impl.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h" 8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h" 9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h" 10 #include "base/android/scoped_java_ref.h"
11 #include "base/json/json_writer.h"
11 #include "content/browser/android/content_view_client.h" 12 #include "content/browser/android/content_view_client.h"
12 #include "content/browser/android/touch_point.h" 13 #include "content/browser/android/touch_point.h"
13 #include "content/browser/renderer_host/java/java_bound_object.h" 14 #include "content/browser/renderer_host/java/java_bound_object.h"
14 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager .h" 15 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager .h"
15 #include "content/browser/renderer_host/render_view_host_impl.h" 16 #include "content/browser/renderer_host/render_view_host_impl.h"
16 #include "content/browser/renderer_host/render_widget_host_impl.h" 17 #include "content/browser/renderer_host/render_widget_host_impl.h"
17 #include "content/browser/renderer_host/render_widget_host_view_android.h" 18 #include "content/browser/renderer_host/render_widget_host_view_android.h"
18 #include "content/browser/web_contents/navigation_controller_impl.h" 19 #include "content/browser/web_contents/navigation_controller_impl.h"
19 #include "content/public/browser/browser_context.h" 20 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/interstitial_page.h" 21 #include "content/public/browser/interstitial_page.h"
22 #include "content/public/browser/notification_details.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/notification_source.h"
25 #include "content/public/browser/notification_types.h"
21 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
22 #include "jni/ContentViewCore_jni.h" 27 #include "jni/ContentViewCore_jni.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/android/WebInputEvent Factory.h" 30 #include "third_party/WebKit/Source/WebKit/chromium/public/android/WebInputEvent Factory.h"
26 #include "webkit/glue/webmenuitem.h" 31 #include "webkit/glue/webmenuitem.h"
27 32
28 using base::android::AttachCurrentThread; 33 using base::android::AttachCurrentThread;
29 using base::android::ConvertJavaStringToUTF16; 34 using base::android::ConvertJavaStringToUTF16;
30 using base::android::ConvertUTF16ToJavaString; 35 using base::android::ConvertUTF16ToJavaString;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 // ---------------------------------------------------------------------------- 80 // ----------------------------------------------------------------------------
76 81
77 ContentViewCoreImpl::ContentViewCoreImpl(JNIEnv* env, jobject obj, 82 ContentViewCoreImpl::ContentViewCoreImpl(JNIEnv* env, jobject obj,
78 WebContents* web_contents) 83 WebContents* web_contents)
79 : web_contents_(static_cast<WebContentsImpl*>(web_contents)), 84 : web_contents_(static_cast<WebContentsImpl*>(web_contents)),
80 tab_crashed_(false) { 85 tab_crashed_(false) {
81 DCHECK(web_contents) << 86 DCHECK(web_contents) <<
82 "A ContentViewCoreImpl should be created with a valid WebContents."; 87 "A ContentViewCoreImpl should be created with a valid WebContents.";
83 88
84 InitJNI(env, obj); 89 InitJNI(env, obj);
90
91 notification_registrar_.Add(this,
92 NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT,
93 NotificationService::AllSources());
85 } 94 }
86 95
87 ContentViewCoreImpl::~ContentViewCoreImpl() { 96 ContentViewCoreImpl::~ContentViewCoreImpl() {
97 // Make sure nobody calls back into this object while we are tearing things
98 // down.
99 notification_registrar_.RemoveAll();
100
88 if (java_object_) { 101 if (java_object_) {
89 JNIEnv* env = AttachCurrentThread(); 102 JNIEnv* env = AttachCurrentThread();
90 env->DeleteWeakGlobalRef(java_object_->obj); 103 env->DeleteWeakGlobalRef(java_object_->obj);
91 delete java_object_; 104 delete java_object_;
92 java_object_ = 0; 105 java_object_ = 0;
93 } 106 }
94 } 107 }
95 108
96 void ContentViewCoreImpl::Destroy(JNIEnv* env, jobject obj) { 109 void ContentViewCoreImpl::Destroy(JNIEnv* env, jobject obj) {
97 delete this; 110 delete this;
98 } 111 }
99 112
100 void ContentViewCoreImpl::Observe(int type, 113 void ContentViewCoreImpl::Observe(int type,
101 const NotificationSource& source, 114 const NotificationSource& source,
102 const NotificationDetails& details) { 115 const NotificationDetails& details) {
116 switch (type) {
117 case NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT: {
118 if (!web_contents_ || Source<RenderViewHost>(source).ptr() !=
119 web_contents_->GetRenderViewHost()) {
120 return;
121 }
122
123 JNIEnv* env = base::android::AttachCurrentThread();
124 std::pair<int, Value*>* result_pair =
125 Details<std::pair<int, Value*> >(details).ptr();
126 std::string json;
127 base::JSONWriter::Write(result_pair->second, &json);
128 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env,
129 json);
130 Java_ContentViewCore_onEvaluateJavaScriptResult(env,
131 java_object_->View(env).obj(),
132 static_cast<jint>(result_pair->first), j_json.obj());
133 break;
134 }
135 }
136
103 // TODO(jrg) 137 // TODO(jrg)
104 } 138 }
105 139
106 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) { 140 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) {
107 java_object_ = new JavaObject; 141 java_object_ = new JavaObject;
108 java_object_->obj = env->NewWeakGlobalRef(obj); 142 java_object_->obj = env->NewWeakGlobalRef(obj);
109 } 143 }
110 144
111 RenderWidgetHostViewAndroid* 145 RenderWidgetHostViewAndroid*
112 ContentViewCoreImpl::GetRenderWidgetHostViewAndroid() { 146 ContentViewCoreImpl::GetRenderWidgetHostViewAndroid() {
(...skipping 19 matching lines...) Expand all
132 166
133 int selected_count = env->GetArrayLength(indices); 167 int selected_count = env->GetArrayLength(indices);
134 std::vector<int> selected_indices; 168 std::vector<int> selected_indices;
135 jint* indices_ptr = env->GetIntArrayElements(indices, NULL); 169 jint* indices_ptr = env->GetIntArrayElements(indices, NULL);
136 for (int i = 0; i < selected_count; ++i) 170 for (int i = 0; i < selected_count; ++i)
137 selected_indices.push_back(indices_ptr[i]); 171 selected_indices.push_back(indices_ptr[i]);
138 env->ReleaseIntArrayElements(indices, indices_ptr, JNI_ABORT); 172 env->ReleaseIntArrayElements(indices, indices_ptr, JNI_ABORT);
139 rvhi->DidSelectPopupMenuItems(selected_indices); 173 rvhi->DidSelectPopupMenuItems(selected_indices);
140 } 174 }
141 175
176 jint ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, jobject obj,
177 jstring script) {
178 RenderViewHost* host = web_contents_->GetRenderViewHost();
179
180 string16 script_utf16 = ConvertJavaStringToUTF16(env, script);
181 return host->ExecuteJavascriptInWebFrameNotifyResult(string16(),
182 script_utf16);
183 }
184
142 void ContentViewCoreImpl::LoadUrlWithoutUrlSanitization(JNIEnv* env, 185 void ContentViewCoreImpl::LoadUrlWithoutUrlSanitization(JNIEnv* env,
143 jobject, 186 jobject,
144 jstring jurl, 187 jstring jurl,
145 int page_transition) { 188 int page_transition) {
146 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl)); 189 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl));
147 190
148 LoadUrl(url, page_transition); 191 LoadUrl(url, page_transition);
149 } 192 }
150 193
151 void ContentViewCoreImpl::LoadUrlWithoutUrlSanitizationWithUserAgentOverride( 194 void ContentViewCoreImpl::LoadUrlWithoutUrlSanitizationWithUserAgentOverride(
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 // Native JNI methods 437 // Native JNI methods
395 // ---------------------------------------------------------------------------- 438 // ----------------------------------------------------------------------------
396 439
397 // This is called for each ContentViewCore. 440 // This is called for each ContentViewCore.
398 jint Init(JNIEnv* env, jobject obj, jint native_web_contents) { 441 jint Init(JNIEnv* env, jobject obj, jint native_web_contents) {
399 ContentViewCore* view = ContentViewCore::Create( 442 ContentViewCore* view = ContentViewCore::Create(
400 env, obj, reinterpret_cast<WebContents*>(native_web_contents)); 443 env, obj, reinterpret_cast<WebContents*>(native_web_contents));
401 return reinterpret_cast<jint>(view); 444 return reinterpret_cast<jint>(view);
402 } 445 }
403 446
447 jint EvaluateJavaScript(JNIEnv* env, jobject obj, jstring script) {
448 ContentViewCoreImpl* view = static_cast<ContentViewCoreImpl*>
449 (ContentViewCore::GetNativeContentViewCore(env, obj));
450
451 return view->EvaluateJavaScript(env, obj, script);
452 }
453
404 // -------------------------------------------------------------------------- 454 // --------------------------------------------------------------------------
405 // Public methods that call to Java via JNI 455 // Public methods that call to Java via JNI
406 // -------------------------------------------------------------------------- 456 // --------------------------------------------------------------------------
407 457
408 void ContentViewCoreImpl::OnTabCrashed(const base::ProcessHandle handle) { 458 void ContentViewCoreImpl::OnTabCrashed(const base::ProcessHandle handle) {
409 NOTIMPLEMENTED() << "not upstreamed yet"; 459 NOTIMPLEMENTED() << "not upstreamed yet";
410 } 460 }
411 461
412 void ContentViewCoreImpl::SetTitle(const string16& title) { 462 void ContentViewCoreImpl::SetTitle(const string16& title) {
413 NOTIMPLEMENTED() << "not upstreamed yet"; 463 NOTIMPLEMENTED() << "not upstreamed yet";
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { 581 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) {
532 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; 582 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!";
533 return false; 583 return false;
534 } 584 }
535 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); 585 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I");
536 586
537 return RegisterNativesImpl(env) >= 0; 587 return RegisterNativesImpl(env) >= 0;
538 } 588 }
539 589
540 } // namespace content 590 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/android/content_view_core_impl.h ('k') | content/content_shell.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698