| OLD | NEW | 
|---|
| 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  Loading... | 
| 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  Loading... | 
| 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 | 
| 297 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) { | 275 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) { | 
| 298   java_object_ = new JavaObject; | 276   java_object_ = new JavaObject; | 
| 299   java_object_->rect_clazz.Reset(GetClass(env, "android/graphics/Rect")); | 277   java_object_->rect_clazz.Reset(GetClass(env, "android/graphics/Rect")); | 
| 300   java_object_->rect_constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( | 278   java_object_->rect_constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( | 
| 301       env, java_object_->rect_clazz.obj(), "<init>", "(IIII)V"); | 279       env, java_object_->rect_clazz.obj(), "<init>", "(IIII)V"); | 
| 302 } | 280 } | 
| 303 | 281 | 
| (...skipping 896 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1200 } | 1178 } | 
| 1201 | 1179 | 
| 1202 void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView( | 1180 void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView( | 
| 1203     JNIEnv* env, | 1181     JNIEnv* env, | 
| 1204     jobject obj) { | 1182     jobject obj) { | 
| 1205   RenderViewHost* host = web_contents_->GetRenderViewHost(); | 1183   RenderViewHost* host = web_contents_->GetRenderViewHost(); | 
| 1206   host->Send( | 1184   host->Send( | 
| 1207       new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID())); | 1185       new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID())); | 
| 1208 } | 1186 } | 
| 1209 | 1187 | 
| 1210 jint ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, | 1188 namespace { | 
|  | 1189 void JavaScriptResultCallback(ScopedJavaGlobalRef<jobject>* message, | 
|  | 1190                               const base::Value* result) { | 
|  | 1191   // |message| is passed as base::Owned, so it will automatically be deleted | 
|  | 1192   // when the callback goes out of scope. | 
|  | 1193   JNIEnv* env = base::android::AttachCurrentThread(); | 
|  | 1194   std::string json; | 
|  | 1195   base::JSONWriter::Write(result, &json); | 
|  | 1196   ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); | 
|  | 1197   Java_ContentViewCore_onEvaluateJavaScriptResult(env, | 
|  | 1198                                                   j_json.obj(), | 
|  | 1199                                                   message->obj()); | 
|  | 1200 } | 
|  | 1201 }  // namespace | 
|  | 1202 | 
|  | 1203 void ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, | 
| 1211                                              jobject obj, | 1204                                              jobject obj, | 
| 1212                                              jstring script) { | 1205                                              jstring script, | 
|  | 1206                                              jobject message) { | 
| 1213   RenderViewHost* host = web_contents_->GetRenderViewHost(); | 1207   RenderViewHost* host = web_contents_->GetRenderViewHost(); | 
| 1214   DCHECK(host); | 1208   DCHECK(host); | 
| 1215 | 1209 | 
| 1216   string16 script_utf16 = ConvertJavaStringToUTF16(env, script); | 1210   if (!message) { | 
| 1217   return host->ExecuteJavascriptInWebFrameNotifyResult(string16(), | 1211     // No callback requested. | 
| 1218                                                        script_utf16); | 1212     host->ExecuteJavascriptInWebFrame(string16(),  // frame_xpath | 
|  | 1213                                       ConvertJavaStringToUTF16(env, script)); | 
|  | 1214     return; | 
|  | 1215   } | 
|  | 1216 | 
|  | 1217   // Secure the message in a scoped object and give ownership of it to the | 
|  | 1218   // callback. | 
|  | 1219   ScopedJavaGlobalRef<jobject>* j_message = new ScopedJavaGlobalRef<jobject>(); | 
|  | 1220   j_message->Reset(env, message); | 
|  | 1221   content::RenderViewHost::JavascriptResultCallback callback = | 
|  | 1222       base::Bind(&JavaScriptResultCallback, base::Owned(j_message)); | 
|  | 1223 | 
|  | 1224   host->ExecuteJavascriptInWebFrameCallbackResult( | 
|  | 1225       string16(),  // frame_xpath | 
|  | 1226       ConvertJavaStringToUTF16(env, script), | 
|  | 1227       callback); | 
| 1219 } | 1228 } | 
| 1220 | 1229 | 
| 1221 bool ContentViewCoreImpl::GetUseDesktopUserAgent( | 1230 bool ContentViewCoreImpl::GetUseDesktopUserAgent( | 
| 1222     JNIEnv* env, jobject obj) { | 1231     JNIEnv* env, jobject obj) { | 
| 1223   NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); | 1232   NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); | 
| 1224   return entry && entry->GetIsOverridingUserAgent(); | 1233   return entry && entry->GetIsOverridingUserAgent(); | 
| 1225 } | 1234 } | 
| 1226 | 1235 | 
| 1227 void ContentViewCoreImpl::ImeUpdateAdapter(int native_ime_adapter, | 1236 void ContentViewCoreImpl::ImeUpdateAdapter(int native_ime_adapter, | 
| 1228                                            int text_input_type, | 1237                                            int text_input_type, | 
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1284           jboolean hardware_accelerated, | 1293           jboolean hardware_accelerated, | 
| 1285           jint native_web_contents, | 1294           jint native_web_contents, | 
| 1286           jint native_window) { | 1295           jint native_window) { | 
| 1287   ContentViewCoreImpl* view = new ContentViewCoreImpl( | 1296   ContentViewCoreImpl* view = new ContentViewCoreImpl( | 
| 1288       env, obj, input_events_delivered_at_vsync, hardware_accelerated, | 1297       env, obj, input_events_delivered_at_vsync, hardware_accelerated, | 
| 1289       reinterpret_cast<WebContents*>(native_web_contents), | 1298       reinterpret_cast<WebContents*>(native_web_contents), | 
| 1290       reinterpret_cast<ui::WindowAndroid*>(native_window)); | 1299       reinterpret_cast<ui::WindowAndroid*>(native_window)); | 
| 1291   return reinterpret_cast<jint>(view); | 1300   return reinterpret_cast<jint>(view); | 
| 1292 } | 1301 } | 
| 1293 | 1302 | 
| 1294 jint EvaluateJavaScript(JNIEnv* env, jobject obj, jstring script) { |  | 
| 1295   ContentViewCoreImpl* view = static_cast<ContentViewCoreImpl*>( |  | 
| 1296       ContentViewCore::GetNativeContentViewCore(env, obj)); |  | 
| 1297   DCHECK(view); |  | 
| 1298 |  | 
| 1299   return view->EvaluateJavaScript(env, obj, script); |  | 
| 1300 } |  | 
| 1301 |  | 
| 1302 bool RegisterContentViewCore(JNIEnv* env) { | 1303 bool RegisterContentViewCore(JNIEnv* env) { | 
| 1303   if (!base::android::HasClass(env, kContentViewCoreClassPath)) { | 1304   if (!base::android::HasClass(env, kContentViewCoreClassPath)) { | 
| 1304     DLOG(ERROR) << "Unable to find class ContentViewCore!"; | 1305     DLOG(ERROR) << "Unable to find class ContentViewCore!"; | 
| 1305     return false; | 1306     return false; | 
| 1306   } | 1307   } | 
| 1307   ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath); | 1308   ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath); | 
| 1308   if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { | 1309   if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { | 
| 1309     DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; | 1310     DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; | 
| 1310     return false; | 1311     return false; | 
| 1311   } | 1312   } | 
| 1312   g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); | 1313   g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); | 
| 1313 | 1314 | 
| 1314   return RegisterNativesImpl(env) >= 0; | 1315   return RegisterNativesImpl(env) >= 0; | 
| 1315 } | 1316 } | 
| 1316 | 1317 | 
| 1317 }  // namespace content | 1318 }  // namespace content | 
| OLD | NEW | 
|---|