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

Side by Side Diff: content/browser/android/content_view_core_impl.cc

Issue 11788005: Switch Android code to use ExecuteJavascriptInWebFrameCallbackResult. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: new approach with Messages Created 7 years, 11 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
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 "base/json/json_writer.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "base/values.h"
14 #include "cc/layer.h" 15 #include "cc/layer.h"
15 #include "content/browser/android/interstitial_page_delegate_android.h" 16 #include "content/browser/android/interstitial_page_delegate_android.h"
16 #include "content/browser/android/load_url_params.h" 17 #include "content/browser/android/load_url_params.h"
17 #include "content/browser/android/touch_point.h" 18 #include "content/browser/android/touch_point.h"
18 #include "content/browser/renderer_host/java/java_bound_object.h" 19 #include "content/browser/renderer_host/java/java_bound_object.h"
19 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager .h" 20 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager .h"
20 #include "content/browser/renderer_host/compositor_impl_android.h" 21 #include "content/browser/renderer_host/compositor_impl_android.h"
21 #include "content/browser/renderer_host/render_view_host_impl.h" 22 #include "content/browser/renderer_host/render_view_host_impl.h"
22 #include "content/browser/renderer_host/render_widget_host_impl.h" 23 #include "content/browser/renderer_host/render_widget_host_impl.h"
23 #include "content/browser/renderer_host/render_widget_host_view_android.h" 24 #include "content/browser/renderer_host/render_widget_host_view_android.h"
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 168
168 InitJNI(env, obj); 169 InitJNI(env, obj);
169 170
170 if (!gfx::Screen::GetNativeScreen()->IsDIPEnabled()) { 171 if (!gfx::Screen::GetNativeScreen()->IsDIPEnabled()) {
171 dpi_scale_ = 1; 172 dpi_scale_ = 1;
172 } else { 173 } else {
173 scoped_ptr<content::DeviceInfo> device_info(new content::DeviceInfo()); 174 scoped_ptr<content::DeviceInfo> device_info(new content::DeviceInfo());
174 dpi_scale_ = device_info->GetDPIScale(); 175 dpi_scale_ = device_info->GetDPIScale();
175 } 176 }
176 177
177 notification_registrar_.Add(this,
178 NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT,
179 NotificationService::AllSources());
180
181 // Currently, the only use case we have for overriding a user agent involves 178 // Currently, the only use case we have for overriding a user agent involves
182 // spoofing a desktop Linux user agent for "Request desktop site". 179 // spoofing a desktop Linux user agent for "Request desktop site".
183 // Automatically set it for all WebContents so that it is available when a 180 // Automatically set it for all WebContents so that it is available when a
184 // NavigationEntry requires the user agent to be overridden. 181 // NavigationEntry requires the user agent to be overridden.
185 const char kLinuxInfoStr[] = "X11; Linux x86_64"; 182 const char kLinuxInfoStr[] = "X11; Linux x86_64";
186 std::string product = content::GetContentClient()->GetProduct(); 183 std::string product = content::GetContentClient()->GetProduct();
187 std::string spoofed_ua = 184 std::string spoofed_ua =
188 webkit_glue::BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product); 185 webkit_glue::BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product);
189 web_contents->SetUserAgentOverride(spoofed_ua); 186 web_contents->SetUserAgentOverride(spoofed_ua);
190 187
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 int pid = GetRenderProcessIdFromRenderViewHost( 262 int pid = GetRenderProcessIdFromRenderViewHost(
266 web_contents_->GetRenderViewHost()); 263 web_contents_->GetRenderViewHost());
267 JNIEnv* env = AttachCurrentThread(); 264 JNIEnv* env = AttachCurrentThread();
268 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); 265 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
269 if (!obj.is_null()) { 266 if (!obj.is_null()) {
270 Java_ContentViewCore_onRenderProcessSwap(env, obj.obj(), 0, pid); 267 Java_ContentViewCore_onRenderProcessSwap(env, obj.obj(), 0, pid);
271 } 268 }
272 } 269 }
273 break; 270 break;
274 } 271 }
275 case NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT: {
276 if (!web_contents_ || Source<RenderViewHost>(source).ptr() !=
277 web_contents_->GetRenderViewHost()) {
278 return;
279 }
280
281 JNIEnv* env = base::android::AttachCurrentThread();
282 std::pair<int, Value*>* result_pair =
283 Details<std::pair<int, Value*> >(details).ptr();
284 std::string json;
285 base::JSONWriter::Write(result_pair->second, &json);
286 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json);
287 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
288 if (!j_obj.is_null()) {
289 Java_ContentViewCore_onEvaluateJavaScriptResult(env, j_obj.obj(),
290 static_cast<jint>(result_pair->first), j_json.obj());
291 }
292 break;
293 }
294 } 272 }
295 } 273 }
296 274
275 void ContentViewCoreImpl::JavaScriptResultCallback(
276 ScopedJavaGlobalRef<jobject>* message,
277 const base::Value* result) {
278 // |message| is passed as base::Owned, so it will automatically be deleted
279 // when the callback goes out of scope.
280 JNIEnv* env = base::android::AttachCurrentThread();
281 std::string json;
282 base::JSONWriter::Write(result, &json);
283 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json);
284 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
285 if (!j_obj.is_null()) {
286 Java_ContentViewCore_onEvaluateJavaScriptResult(env,
287 j_obj.obj(),
288 j_json.obj(),
289 message->obj());
290 }
291 }
292
297 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) { 293 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) {
298 java_object_ = new JavaObject; 294 java_object_ = new JavaObject;
299 java_object_->rect_clazz.Reset(GetClass(env, "android/graphics/Rect")); 295 java_object_->rect_clazz.Reset(GetClass(env, "android/graphics/Rect"));
300 java_object_->rect_constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( 296 java_object_->rect_constructor = MethodID::Get<MethodID::TYPE_INSTANCE>(
301 env, java_object_->rect_clazz.obj(), "<init>", "(IIII)V"); 297 env, java_object_->rect_clazz.obj(), "<init>", "(IIII)V");
302 } 298 }
303 299
304 RenderWidgetHostViewAndroid* 300 RenderWidgetHostViewAndroid*
305 ContentViewCoreImpl::GetRenderWidgetHostViewAndroid() { 301 ContentViewCoreImpl::GetRenderWidgetHostViewAndroid() {
306 RenderWidgetHostView* rwhv = NULL; 302 RenderWidgetHostView* rwhv = NULL;
(...skipping 893 matching lines...) Expand 10 before | Expand all | Expand 10 after
1200 } 1196 }
1201 1197
1202 void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView( 1198 void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView(
1203 JNIEnv* env, 1199 JNIEnv* env,
1204 jobject obj) { 1200 jobject obj) {
1205 RenderViewHost* host = web_contents_->GetRenderViewHost(); 1201 RenderViewHost* host = web_contents_->GetRenderViewHost();
1206 host->Send( 1202 host->Send(
1207 new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID())); 1203 new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID()));
1208 } 1204 }
1209 1205
1210 jint ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, 1206 void ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env,
1211 jobject obj, 1207 jobject obj,
1212 jstring script) { 1208 jstring script,
1209 jobject message) {
1213 RenderViewHost* host = web_contents_->GetRenderViewHost(); 1210 RenderViewHost* host = web_contents_->GetRenderViewHost();
1214 DCHECK(host); 1211 DCHECK(host);
1215 1212
1216 string16 script_utf16 = ConvertJavaStringToUTF16(env, script); 1213 if (!message) {
1217 return host->ExecuteJavascriptInWebFrameNotifyResult(string16(), 1214 // No callback requested.
1218 script_utf16); 1215 host->ExecuteJavascriptInWebFrame(string16(), // frame_xpath
1216 ConvertJavaStringToUTF16(env, script));
1217 return;
1218 }
1219
1220 // Secure the message in a scoped object and give ownership of it to the
1221 // callback.
1222 ScopedJavaGlobalRef<jobject>* j_message = new ScopedJavaGlobalRef<jobject>();
1223 j_message->Reset(env, message);
1224 content::RenderViewHost::JavascriptResultCallback callback =
1225 base::Bind(&ContentViewCoreImpl::JavaScriptResultCallback,
1226 base::Unretained(this),
joth 2013/01/05 00:54:42 make JavaScriptResultCallback into a static (e.g.
Avi (use Gerrit) 2013/01/05 01:03:34 JavaScriptResultCallback accesses java_ref_, so I
1227 base::Owned(j_message));
1228
1229 host->ExecuteJavascriptInWebFrameCallbackResult(
1230 string16(), // frame_xpath
1231 ConvertJavaStringToUTF16(env, script),
1232 callback);
1219 } 1233 }
1220 1234
1221 bool ContentViewCoreImpl::GetUseDesktopUserAgent( 1235 bool ContentViewCoreImpl::GetUseDesktopUserAgent(
1222 JNIEnv* env, jobject obj) { 1236 JNIEnv* env, jobject obj) {
1223 NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); 1237 NavigationEntry* entry = web_contents_->GetController().GetActiveEntry();
1224 return entry && entry->GetIsOverridingUserAgent(); 1238 return entry && entry->GetIsOverridingUserAgent();
1225 } 1239 }
1226 1240
1227 void ContentViewCoreImpl::ImeUpdateAdapter(int native_ime_adapter, 1241 void ContentViewCoreImpl::ImeUpdateAdapter(int native_ime_adapter,
1228 int text_input_type, 1242 int text_input_type,
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1284 jboolean hardware_accelerated, 1298 jboolean hardware_accelerated,
1285 jint native_web_contents, 1299 jint native_web_contents,
1286 jint native_window) { 1300 jint native_window) {
1287 ContentViewCoreImpl* view = new ContentViewCoreImpl( 1301 ContentViewCoreImpl* view = new ContentViewCoreImpl(
1288 env, obj, input_events_delivered_at_vsync, hardware_accelerated, 1302 env, obj, input_events_delivered_at_vsync, hardware_accelerated,
1289 reinterpret_cast<WebContents*>(native_web_contents), 1303 reinterpret_cast<WebContents*>(native_web_contents),
1290 reinterpret_cast<ui::WindowAndroid*>(native_window)); 1304 reinterpret_cast<ui::WindowAndroid*>(native_window));
1291 return reinterpret_cast<jint>(view); 1305 return reinterpret_cast<jint>(view);
1292 } 1306 }
1293 1307
1294 jint EvaluateJavaScript(JNIEnv* env, jobject obj, jstring script) { 1308 void EvaluateJavaScript(JNIEnv* env,
1309 jobject obj,
1310 jstring script,
1311 jobject message) {
1295 ContentViewCoreImpl* view = static_cast<ContentViewCoreImpl*>( 1312 ContentViewCoreImpl* view = static_cast<ContentViewCoreImpl*>(
1296 ContentViewCore::GetNativeContentViewCore(env, obj)); 1313 ContentViewCore::GetNativeContentViewCore(env, obj));
1297 DCHECK(view); 1314 DCHECK(view);
1298 1315
1299 return view->EvaluateJavaScript(env, obj, script); 1316 view->EvaluateJavaScript(env, obj, script, message);
1300 } 1317 }
1301 1318
1302 bool RegisterContentViewCore(JNIEnv* env) { 1319 bool RegisterContentViewCore(JNIEnv* env) {
1303 if (!base::android::HasClass(env, kContentViewCoreClassPath)) { 1320 if (!base::android::HasClass(env, kContentViewCoreClassPath)) {
1304 DLOG(ERROR) << "Unable to find class ContentViewCore!"; 1321 DLOG(ERROR) << "Unable to find class ContentViewCore!";
1305 return false; 1322 return false;
1306 } 1323 }
1307 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath); 1324 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath);
1308 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { 1325 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) {
1309 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; 1326 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!";
1310 return false; 1327 return false;
1311 } 1328 }
1312 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); 1329 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I");
1313 1330
1314 return RegisterNativesImpl(env) >= 0; 1331 return RegisterNativesImpl(env) >= 0;
1315 } 1332 }
1316 1333
1317 } // namespace content 1334 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698