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

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

Issue 2595263002: Introduce ViewRoot forwarding input/view events to native (Closed)
Patch Set: rebased & ViewAndroud::Bounds Created 3 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
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/android/jni_android.h" 9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h" 10 #include "base/android/jni_array.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 #include "content/public/browser/web_contents.h" 48 #include "content/public/browser/web_contents.h"
49 #include "content/public/common/content_client.h" 49 #include "content/public/common/content_client.h"
50 #include "content/public/common/content_switches.h" 50 #include "content/public/common/content_switches.h"
51 #include "content/public/common/menu_item.h" 51 #include "content/public/common/menu_item.h"
52 #include "content/public/common/user_agent.h" 52 #include "content/public/common/user_agent.h"
53 #include "device/geolocation/geolocation_service_context.h" 53 #include "device/geolocation/geolocation_service_context.h"
54 #include "jni/ContentViewCore_jni.h" 54 #include "jni/ContentViewCore_jni.h"
55 #include "jni/DragEvent_jni.h" 55 #include "jni/DragEvent_jni.h"
56 #include "third_party/WebKit/public/platform/WebInputEvent.h" 56 #include "third_party/WebKit/public/platform/WebInputEvent.h"
57 #include "ui/android/view_android.h" 57 #include "ui/android/view_android.h"
58 #include "ui/android/view_root.h"
58 #include "ui/android/window_android.h" 59 #include "ui/android/window_android.h"
59 #include "ui/base/clipboard/clipboard.h" 60 #include "ui/base/clipboard/clipboard.h"
60 #include "ui/base/ui_base_switches_util.h" 61 #include "ui/base/ui_base_switches_util.h"
61 #include "ui/events/android/motion_event_android.h" 62 #include "ui/events/android/motion_event_android.h"
62 #include "ui/events/blink/blink_event_util.h" 63 #include "ui/events/blink/blink_event_util.h"
63 #include "ui/events/blink/web_input_event_traits.h" 64 #include "ui/events/blink/web_input_event_traits.h"
64 #include "ui/events/event_utils.h" 65 #include "ui/events/event_utils.h"
65 #include "ui/events/gesture_detection/motion_event.h" 66 #include "ui/events/gesture_detection/motion_event.h"
66 #include "ui/gfx/android/java_bitmap.h" 67 #include "ui/gfx/android/java_bitmap.h"
67 #include "ui/gfx/geometry/point_conversions.h" 68 #include "ui/gfx/geometry/point_conversions.h"
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 WebContents* web_contents, 224 WebContents* web_contents,
224 float dpi_scale, 225 float dpi_scale,
225 const JavaRef<jobject>& java_bridge_retained_object_set) 226 const JavaRef<jobject>& java_bridge_retained_object_set)
226 : WebContentsObserver(web_contents), 227 : WebContentsObserver(web_contents),
227 java_ref_(env, obj), 228 java_ref_(env, obj),
228 web_contents_(static_cast<WebContentsImpl*>(web_contents)), 229 web_contents_(static_cast<WebContentsImpl*>(web_contents)),
229 page_scale_(1), 230 page_scale_(1),
230 dpi_scale_(dpi_scale), 231 dpi_scale_(dpi_scale),
231 device_orientation_(0), 232 device_orientation_(0),
232 accessibility_enabled_(false) { 233 accessibility_enabled_(false) {
233 GetViewAndroid()->SetLayer(cc::Layer::Create());
234 gfx::Size physical_size(
235 Java_ContentViewCore_getPhysicalBackingWidthPix(env, obj),
236 Java_ContentViewCore_getPhysicalBackingHeightPix(env, obj));
237 GetViewAndroid()->GetLayer()->SetBounds(physical_size);
238
239 // Currently, the only use case we have for overriding a user agent involves 234 // Currently, the only use case we have for overriding a user agent involves
240 // spoofing a desktop Linux user agent for "Request desktop site". 235 // spoofing a desktop Linux user agent for "Request desktop site".
241 // Automatically set it for all WebContents so that it is available when a 236 // Automatically set it for all WebContents so that it is available when a
242 // NavigationEntry requires the user agent to be overridden. 237 // NavigationEntry requires the user agent to be overridden.
243 const char kLinuxInfoStr[] = "X11; Linux x86_64"; 238 const char kLinuxInfoStr[] = "X11; Linux x86_64";
244 std::string product = content::GetContentClient()->GetProduct(); 239 std::string product = content::GetContentClient()->GetProduct();
245 std::string spoofed_ua = 240 std::string spoofed_ua =
246 BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product); 241 BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product);
247 web_contents->SetUserAgentOverride(spoofed_ua); 242 web_contents->SetUserAgentOverride(spoofed_ua);
248 243
(...skipping 21 matching lines...) Expand all
270 265
271 JNIEnv* env = base::android::AttachCurrentThread(); 266 JNIEnv* env = base::android::AttachCurrentThread();
272 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); 267 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
273 java_ref_.reset(); 268 java_ref_.reset();
274 if (!j_obj.is_null()) { 269 if (!j_obj.is_null()) {
275 Java_ContentViewCore_onNativeContentViewCoreDestroyed( 270 Java_ContentViewCore_onNativeContentViewCoreDestroyed(
276 env, j_obj, reinterpret_cast<intptr_t>(this)); 271 env, j_obj, reinterpret_cast<intptr_t>(this));
277 } 272 }
278 } 273 }
279 274
280 void ContentViewCoreImpl::UpdateWindowAndroid( 275 void ContentViewCoreImpl::UpdateViewRoot(
281 JNIEnv* env, 276 JNIEnv* env,
282 const base::android::JavaParamRef<jobject>& obj, 277 const base::android::JavaParamRef<jobject>& obj,
283 jlong window_android) { 278 jlong view_root_ptr) {
279 ui::ViewRoot* view_root = reinterpret_cast<ui::ViewRoot*>(view_root_ptr);
284 ui::ViewAndroid* view = GetViewAndroid(); 280 ui::ViewAndroid* view = GetViewAndroid();
285 ui::WindowAndroid* window = 281 if (view_root == view->GetViewRoot())
286 reinterpret_cast<ui::WindowAndroid*>(window_android);
287 if (window == GetWindowAndroid())
288 return; 282 return;
289 if (GetWindowAndroid()) { 283
284 if (view->GetViewRoot()) {
290 for (auto& observer : observer_list_) 285 for (auto& observer : observer_list_)
291 observer.OnDetachedFromWindow(); 286 observer.OnDetachedFromWindow();
292 view->RemoveFromParent(); 287 view->RemoveFromParent();
293 } 288 }
294 if (window) { 289
295 window->AddChild(view); 290 if (view_root) {
291 view_root->AddChild(view);
296 for (auto& observer : observer_list_) 292 for (auto& observer : observer_list_)
297 observer.OnAttachedToWindow(); 293 observer.OnAttachedToWindow();
298 } 294 }
299 } 295 }
300 296
301 base::android::ScopedJavaLocalRef<jobject> 297 base::android::ScopedJavaLocalRef<jobject>
302 ContentViewCoreImpl::GetWebContentsAndroid(JNIEnv* env, 298 ContentViewCoreImpl::GetWebContentsAndroid(JNIEnv* env,
303 const JavaParamRef<jobject>& obj) { 299 const JavaParamRef<jobject>& obj) {
304 return web_contents_->GetJavaWebContents(); 300 return web_contents_->GetJavaWebContents();
305 } 301 }
(...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after
885 JNIEnv* env, 881 JNIEnv* env,
886 const JavaParamRef<jobject>& obj, 882 const JavaParamRef<jobject>& obj,
887 jint orientation) { 883 jint orientation) {
888 if (device_orientation_ != orientation) { 884 if (device_orientation_ != orientation) {
889 base::RecordAction(base::UserMetricsAction("ScreenOrientationChange")); 885 base::RecordAction(base::UserMetricsAction("ScreenOrientationChange"));
890 device_orientation_ = orientation; 886 device_orientation_ = orientation;
891 SendOrientationChangeEventInternal(); 887 SendOrientationChangeEventInternal();
892 } 888 }
893 } 889 }
894 890
895 jboolean ContentViewCoreImpl::OnTouchEvent(
896 JNIEnv* env,
897 const JavaParamRef<jobject>& obj,
898 const JavaParamRef<jobject>& motion_event,
899 jlong time_ms,
900 jint android_action,
901 jint pointer_count,
902 jint history_size,
903 jint action_index,
904 jfloat pos_x_0,
905 jfloat pos_y_0,
906 jfloat pos_x_1,
907 jfloat pos_y_1,
908 jint pointer_id_0,
909 jint pointer_id_1,
910 jfloat touch_major_0,
911 jfloat touch_major_1,
912 jfloat touch_minor_0,
913 jfloat touch_minor_1,
914 jfloat orientation_0,
915 jfloat orientation_1,
916 jfloat tilt_0,
917 jfloat tilt_1,
918 jfloat raw_pos_x,
919 jfloat raw_pos_y,
920 jint android_tool_type_0,
921 jint android_tool_type_1,
922 jint android_button_state,
923 jint android_meta_state,
924 jboolean is_touch_handle_event) {
925 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
926 // Avoid synthesizing a touch event if it cannot be forwarded.
927 if (!rwhv)
928 return false;
929
930 MotionEventAndroid::Pointer pointer0(pointer_id_0,
931 pos_x_0,
932 pos_y_0,
933 touch_major_0,
934 touch_minor_0,
935 orientation_0,
936 tilt_0,
937 android_tool_type_0);
938 MotionEventAndroid::Pointer pointer1(pointer_id_1,
939 pos_x_1,
940 pos_y_1,
941 touch_major_1,
942 touch_minor_1,
943 orientation_1,
944 tilt_1,
945 android_tool_type_1);
946 MotionEventAndroid event(1.f / dpi_scale(),
947 env,
948 motion_event,
949 time_ms,
950 android_action,
951 pointer_count,
952 history_size,
953 action_index,
954 android_button_state,
955 android_meta_state,
956 raw_pos_x - pos_x_0,
957 raw_pos_y - pos_y_0,
958 &pointer0,
959 &pointer1);
960
961 RecordToolTypeForActionDown(event);
962
963 return is_touch_handle_event ? rwhv->OnTouchHandleEvent(event)
964 : rwhv->OnTouchEvent(event);
965 }
966
967 jboolean ContentViewCoreImpl::SendMouseEvent( 891 jboolean ContentViewCoreImpl::SendMouseEvent(
968 JNIEnv* env, 892 JNIEnv* env,
969 const JavaParamRef<jobject>& obj, 893 const JavaParamRef<jobject>& obj,
970 jlong time_ms, 894 jlong time_ms,
971 jint android_action, 895 jint android_action,
972 jfloat x, 896 jfloat x,
973 jfloat y, 897 jfloat y,
974 jint pointer_id, 898 jint pointer_id,
975 jfloat pressure, 899 jfloat pressure,
976 jfloat orientation, 900 jfloat orientation,
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
1263 ConvertJavaStringToUTF8(env, name)); 1187 ConvertJavaStringToUTF8(env, name));
1264 } 1188 }
1265 1189
1266 void ContentViewCoreImpl::WasResized(JNIEnv* env, 1190 void ContentViewCoreImpl::WasResized(JNIEnv* env,
1267 const JavaParamRef<jobject>& obj) { 1191 const JavaParamRef<jobject>& obj) {
1268 gfx::Size physical_size( 1192 gfx::Size physical_size(
1269 Java_ContentViewCore_getPhysicalBackingWidthPix(env, obj), 1193 Java_ContentViewCore_getPhysicalBackingWidthPix(env, obj),
1270 Java_ContentViewCore_getPhysicalBackingHeightPix(env, obj)); 1194 Java_ContentViewCore_getPhysicalBackingHeightPix(env, obj));
1271 GetViewAndroid()->GetLayer()->SetBounds(physical_size); 1195 GetViewAndroid()->GetLayer()->SetBounds(physical_size);
1272 1196
1197 gfx::Size view_size(GetViewSize());
1198 // TODO(jinsukkim): Take top offset into account for adjusting y
1199 // when refactoring content view.
1200 GetViewAndroid()->SetBounds(gfx::Point(),
1201 view_size.width(),
1202 view_size.height());
1273 SendScreenRectsAndResizeWidget(); 1203 SendScreenRectsAndResizeWidget();
1274 } 1204 }
1275 1205
1276 long ContentViewCoreImpl::GetNativeImeAdapter( 1206 long ContentViewCoreImpl::GetNativeImeAdapter(
1277 JNIEnv* env, 1207 JNIEnv* env,
1278 const JavaParamRef<jobject>& obj) { 1208 const JavaParamRef<jobject>& obj) {
1279 RenderWidgetHostViewAndroid* rwhva = GetRenderWidgetHostViewAndroid(); 1209 RenderWidgetHostViewAndroid* rwhva = GetRenderWidgetHostViewAndroid();
1280 if (!rwhva) 1210 if (!rwhva)
1281 return 0; 1211 return 0;
1282 return rwhva->GetNativeImeAdapter(); 1212 return rwhva->GetNativeImeAdapter();
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
1435 GetRenderWidgetHostViewAndroid()->SetBackgroundColor(SK_ColorTRANSPARENT); 1365 GetRenderWidgetHostViewAndroid()->SetBackgroundColor(SK_ColorTRANSPARENT);
1436 } 1366 }
1437 } 1367 }
1438 1368
1439 bool ContentViewCoreImpl::IsTouchDragDropEnabled( 1369 bool ContentViewCoreImpl::IsTouchDragDropEnabled(
1440 JNIEnv* env, 1370 JNIEnv* env,
1441 const base::android::JavaParamRef<jobject>& jobj) { 1371 const base::android::JavaParamRef<jobject>& jobj) {
1442 return switches::IsTouchDragDropEnabled(); 1372 return switches::IsTouchDragDropEnabled();
1443 } 1373 }
1444 1374
1375 void ContentViewCoreImpl::MoveToFrontInViewHierarchy(
1376 JNIEnv* env,
1377 const base::android::JavaParamRef<jobject>& jobj) {
1378 ui::ViewAndroid* view_android = GetViewAndroid();
1379 ui::ViewRoot* view_root =
1380 static_cast<ui::ViewRoot*>(view_android->GetViewRoot());
1381 view_root->MoveToFront(view_android);
1382 }
1383
1384 void ContentViewCoreImpl::UpdateViewBounds(
1385 JNIEnv* env,
1386 const base::android::JavaParamRef<jobject>& jobj,
1387 int x,
1388 int y,
1389 int width,
1390 int height) {
1391 ui::ViewAndroid* view_android = GetViewAndroid();
1392 view_android->SetBounds(gfx::Point(x, y), width, height);
1393 }
1394
1445 void ContentViewCoreImpl::OnDragEvent( 1395 void ContentViewCoreImpl::OnDragEvent(
1446 JNIEnv* env, 1396 JNIEnv* env,
1447 const base::android::JavaParamRef<jobject>& jobj, 1397 const base::android::JavaParamRef<jobject>& jobj,
1448 jint action, 1398 jint action,
1449 jint x, 1399 jint x,
1450 jint y, 1400 jint y,
1451 jint screen_x, 1401 jint screen_x,
1452 jint screen_y, 1402 jint screen_y,
1453 const base::android::JavaParamRef<jobjectArray>& j_mimeTypes, 1403 const base::android::JavaParamRef<jobjectArray>& j_mimeTypes,
1454 const base::android::JavaParamRef<jstring>& j_content) { 1404 const base::android::JavaParamRef<jstring>& j_content) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1543 static_cast<WebContentsImpl*>(web_contents())->GetView()); 1493 static_cast<WebContentsImpl*>(web_contents())->GetView());
1544 DCHECK(wcva); 1494 DCHECK(wcva);
1545 wcva->SetContentViewCore(NULL); 1495 wcva->SetContentViewCore(NULL);
1546 } 1496 }
1547 1497
1548 // This is called for each ContentView. 1498 // This is called for each ContentView.
1549 jlong Init(JNIEnv* env, 1499 jlong Init(JNIEnv* env,
1550 const JavaParamRef<jobject>& obj, 1500 const JavaParamRef<jobject>& obj,
1551 const JavaParamRef<jobject>& jweb_contents, 1501 const JavaParamRef<jobject>& jweb_contents,
1552 const JavaParamRef<jobject>& jview_android_delegate, 1502 const JavaParamRef<jobject>& jview_android_delegate,
1553 jlong jwindow_android, 1503 jlong view_root_ptr,
1554 jfloat dipScale, 1504 jfloat dipScale,
1555 const JavaParamRef<jobject>& retained_objects_set) { 1505 const JavaParamRef<jobject>& retained_objects_set) {
1556 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( 1506 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
1557 WebContents::FromJavaWebContents(jweb_contents)); 1507 WebContents::FromJavaWebContents(jweb_contents));
1558 CHECK(web_contents) << 1508 CHECK(web_contents) <<
1559 "A ContentViewCoreImpl should be created with a valid WebContents."; 1509 "A ContentViewCoreImpl should be created with a valid WebContents.";
1560 ui::ViewAndroid* view_android = web_contents->GetView()->GetNativeView(); 1510 ui::ViewAndroid* view_android = web_contents->GetView()->GetNativeView();
1561 view_android->SetDelegate(jview_android_delegate); 1511 view_android->SetDelegate(jview_android_delegate);
1512 view_android->SetLayer(cc::Layer::Create());
1513 gfx::Size physical_size(
1514 Java_ContentViewCore_getPhysicalBackingWidthPix(env, obj),
1515 Java_ContentViewCore_getPhysicalBackingHeightPix(env, obj));
1516 view_android->GetLayer()->SetBounds(physical_size);
1562 1517
1563 ui::WindowAndroid* window_android = 1518 ui::ViewRoot* view_root = reinterpret_cast<ui::ViewRoot*>(view_root_ptr);
1564 reinterpret_cast<ui::WindowAndroid*>(jwindow_android); 1519 view_root->AddChild(view_android);
1565 DCHECK(window_android);
1566 window_android->AddChild(view_android);
1567 1520
1568 // TODO: pass dipScale. 1521 // TODO: pass dipScale.
1569 ContentViewCoreImpl* view = new ContentViewCoreImpl( 1522 ContentViewCoreImpl* view = new ContentViewCoreImpl(
1570 env, obj, web_contents, dipScale, retained_objects_set); 1523 env, obj, web_contents, dipScale, retained_objects_set);
1571 return reinterpret_cast<intptr_t>(view); 1524 return reinterpret_cast<intptr_t>(view);
1572 } 1525 }
1573 1526
1574 static ScopedJavaLocalRef<jobject> FromWebContentsAndroid( 1527 static ScopedJavaLocalRef<jobject> FromWebContentsAndroid(
1575 JNIEnv* env, 1528 JNIEnv* env,
1576 const JavaParamRef<jclass>& clazz, 1529 const JavaParamRef<jclass>& clazz,
1577 const JavaParamRef<jobject>& jweb_contents) { 1530 const JavaParamRef<jobject>& jweb_contents) {
1578 WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents); 1531 WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents);
1579 if (!web_contents) 1532 if (!web_contents)
1580 return ScopedJavaLocalRef<jobject>(); 1533 return ScopedJavaLocalRef<jobject>();
1581 1534
1582 ContentViewCore* view = ContentViewCore::FromWebContents(web_contents); 1535 ContentViewCore* view = ContentViewCore::FromWebContents(web_contents);
1583 if (!view) 1536 if (!view)
1584 return ScopedJavaLocalRef<jobject>(); 1537 return ScopedJavaLocalRef<jobject>();
1585 1538
1586 return view->GetJavaObject(); 1539 return view->GetJavaObject();
1587 } 1540 }
1588 1541
1589 bool RegisterContentViewCore(JNIEnv* env) { 1542 bool RegisterContentViewCore(JNIEnv* env) {
1590 return RegisterNativesImpl(env); 1543 return RegisterNativesImpl(env);
1591 } 1544 }
1592 1545
1593 } // namespace content 1546 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698