Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ui/android/view_android.h" | 5 #include "ui/android/view_android.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "cc/layers/layer.h" | 11 #include "cc/layers/layer.h" |
| 12 #include "jni/ViewAndroidDelegate_jni.h" | 12 #include "jni/ViewAndroidDelegate_jni.h" |
| 13 #include "ui/android/event_handler.h" | |
| 14 #include "ui/android/view_client.h" | |
| 13 #include "ui/android/window_android.h" | 15 #include "ui/android/window_android.h" |
| 14 #include "ui/display/display.h" | 16 #include "ui/base/layout.h" |
| 15 #include "ui/display/screen.h" | |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace ui { | 19 namespace ui { |
| 19 | 20 |
| 20 using base::android::ConvertUTF8ToJavaString; | 21 using base::android::ConvertUTF8ToJavaString; |
| 21 using base::android::JavaRef; | 22 using base::android::JavaRef; |
| 22 using base::android::ScopedJavaLocalRef; | 23 using base::android::ScopedJavaLocalRef; |
| 23 | 24 |
| 24 ViewAndroid::ScopedAnchorView::ScopedAnchorView( | 25 ViewAndroid::ScopedAnchorView::ScopedAnchorView( |
| 25 JNIEnv* env, | 26 JNIEnv* env, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 view_.reset(); | 65 view_.reset(); |
| 65 delegate_.reset(); | 66 delegate_.reset(); |
| 66 } | 67 } |
| 67 | 68 |
| 68 const base::android::ScopedJavaLocalRef<jobject> | 69 const base::android::ScopedJavaLocalRef<jobject> |
| 69 ViewAndroid::ScopedAnchorView::view() const { | 70 ViewAndroid::ScopedAnchorView::view() const { |
| 70 JNIEnv* env = base::android::AttachCurrentThread(); | 71 JNIEnv* env = base::android::AttachCurrentThread(); |
| 71 return view_.get(env); | 72 return view_.get(env); |
| 72 } | 73 } |
| 73 | 74 |
| 74 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) | 75 ViewAndroid::ViewAndroid(ViewClient* view_client) |
| 75 : parent_(nullptr), | 76 : parent_(nullptr), |
| 76 delegate_(base::android::AttachCurrentThread(), delegate.obj()) {} | 77 client_(view_client), |
| 78 dip_scale_(ui::GetScaleFactorForNativeView(this)) {} | |
|
boliu
2017/02/27 19:21:57
again, don't cache this
Jinsuk Kim
2017/02/28 06:56:04
Done.
| |
| 77 | 79 |
| 78 ViewAndroid::ViewAndroid() : parent_(nullptr) {} | 80 ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {} |
| 79 | 81 |
| 80 ViewAndroid::~ViewAndroid() { | 82 ViewAndroid::~ViewAndroid() { |
| 81 RemoveFromParent(); | 83 RemoveFromParent(); |
| 82 | 84 |
| 83 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 85 for (std::list<ViewAndroid*>::iterator it = children_.begin(); |
| 84 it != children_.end(); it++) { | 86 it != children_.end(); it++) { |
| 85 DCHECK_EQ((*it)->parent_, this); | 87 DCHECK_EQ((*it)->parent_, this); |
| 86 (*it)->parent_ = nullptr; | 88 (*it)->parent_ = nullptr; |
| 87 } | 89 } |
| 88 } | 90 } |
| 89 | 91 |
| 90 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { | 92 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { |
| 93 // A ViewAndroid may have its own delegate or otherwise will | |
| 94 // use the next available parent's delegate. | |
| 91 JNIEnv* env = base::android::AttachCurrentThread(); | 95 JNIEnv* env = base::android::AttachCurrentThread(); |
| 92 delegate_ = JavaObjectWeakGlobalRef(env, delegate); | 96 delegate_ = JavaObjectWeakGlobalRef(env, delegate); |
| 93 } | 97 } |
| 94 | 98 |
| 99 float ViewAndroid::GetDipScale() { | |
| 100 return dip_scale_; | |
| 101 } | |
| 102 | |
| 103 EventHandler* ViewAndroid::CreateEventHandler() { | |
| 104 DCHECK(!parent_ || parent_ == GetWindowAndroid()) | |
|
boliu
2017/02/27 19:21:57
this is gonna break after WCVA is not immediate ch
Jinsuk Kim
2017/02/28 06:56:04
Done. Restored |GetViewRoot()| that returns the to
| |
| 105 << "Only WebContentViewAndroid's ViewAndroid can have event handler."; | |
| 106 DCHECK(!event_handler_) << "EventHandler is already created"; | |
| 107 | |
| 108 event_handler_.reset(EventHandler::Create(this)); | |
| 109 return event_handler_.get(); | |
| 110 } | |
| 111 | |
| 95 void ViewAndroid::AddChild(ViewAndroid* child) { | 112 void ViewAndroid::AddChild(ViewAndroid* child) { |
| 96 DCHECK(child); | 113 DCHECK(child); |
| 97 DCHECK(std::find(children_.begin(), children_.end(), child) == | 114 DCHECK(std::find(children_.begin(), children_.end(), child) == |
| 98 children_.end()); | 115 children_.end()); |
| 116 DCHECK(!ViewTreeHasEventHandler(child)) << "Child cannot have event handler"; | |
|
boliu
2017/02/27 19:21:57
why not..? that only holds if we don't already hav
Jinsuk Kim
2017/02/28 06:56:04
The restriction was to allow for the event handler
| |
| 99 | 117 |
| 118 // The new child goes to the top, which is the end of the list. | |
| 100 children_.push_back(child); | 119 children_.push_back(child); |
| 101 if (child->parent_) | 120 if (child->parent_) |
| 102 child->RemoveFromParent(); | 121 child->RemoveFromParent(); |
| 103 child->parent_ = this; | 122 child->parent_ = this; |
| 104 } | 123 } |
| 105 | 124 |
| 125 bool ViewAndroid::ViewTreeHasEventHandler(ViewAndroid* view) { | |
| 126 if (view->has_event_handler()) | |
| 127 return true; | |
| 128 for (auto& child : view->children_) { | |
| 129 if (child->has_event_handler()) | |
| 130 return true; | |
| 131 } | |
| 132 return false; | |
| 133 } | |
| 134 | |
| 106 void ViewAndroid::RemoveFromParent() { | 135 void ViewAndroid::RemoveFromParent() { |
| 107 if (parent_) | 136 if (parent_) |
| 108 parent_->RemoveChild(this); | 137 parent_->RemoveChild(this); |
| 109 } | 138 } |
| 110 | 139 |
| 111 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { | 140 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { |
| 112 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 141 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 113 if (delegate.is_null()) | 142 if (delegate.is_null()) |
| 114 return ViewAndroid::ScopedAnchorView(); | 143 return ViewAndroid::ScopedAnchorView(); |
| 115 | 144 |
| 116 JNIEnv* env = base::android::AttachCurrentThread(); | 145 JNIEnv* env = base::android::AttachCurrentThread(); |
| 117 return ViewAndroid::ScopedAnchorView( | 146 return ViewAndroid::ScopedAnchorView( |
| 118 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); | 147 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); |
| 119 } | 148 } |
| 120 | 149 |
| 121 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, | 150 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, |
| 122 const gfx::RectF& bounds) { | 151 const gfx::RectF& bounds) { |
| 123 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 152 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 124 if (delegate.is_null()) | 153 if (delegate.is_null()) |
| 125 return; | 154 return; |
| 126 | 155 |
| 127 float scale = display::Screen::GetScreen() | 156 int left_margin = std::round(bounds.x() * dip_scale_); |
| 128 ->GetDisplayNearestWindow(this) | 157 int top_margin = std::round((content_offset().y() + bounds.y()) * dip_scale_); |
| 129 .device_scale_factor(); | |
| 130 int left_margin = std::round(bounds.x() * scale); | |
| 131 int top_margin = std::round((content_offset().y() + bounds.y()) * scale); | |
| 132 JNIEnv* env = base::android::AttachCurrentThread(); | 158 JNIEnv* env = base::android::AttachCurrentThread(); |
| 133 Java_ViewAndroidDelegate_setViewPosition( | 159 Java_ViewAndroidDelegate_setViewPosition( |
| 134 env, delegate, anchor, bounds.x(), bounds.y(), bounds.width(), | 160 env, delegate, anchor, bounds.x(), bounds.y(), bounds.width(), |
| 135 bounds.height(), scale, left_margin, top_margin); | 161 bounds.height(), dip_scale_, left_margin, top_margin); |
| 136 } | 162 } |
| 137 | 163 |
| 138 ScopedJavaLocalRef<jobject> ViewAndroid::GetContainerView() { | 164 ScopedJavaLocalRef<jobject> ViewAndroid::GetContainerView() { |
| 139 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 165 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 140 if (delegate.is_null()) | 166 if (delegate.is_null()) |
| 141 return nullptr; | 167 return nullptr; |
| 142 | 168 |
| 143 JNIEnv* env = base::android::AttachCurrentThread(); | 169 JNIEnv* env = base::android::AttachCurrentThread(); |
| 144 return Java_ViewAndroidDelegate_getContainerView(env, delegate); | 170 return Java_ViewAndroidDelegate_getContainerView(env, delegate); |
| 145 } | 171 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 246 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 221 if (delegate.is_null()) | 247 if (delegate.is_null()) |
| 222 return; | 248 return; |
| 223 JNIEnv* env = base::android::AttachCurrentThread(); | 249 JNIEnv* env = base::android::AttachCurrentThread(); |
| 224 ScopedJavaLocalRef<jstring> jcontent_url = | 250 ScopedJavaLocalRef<jstring> jcontent_url = |
| 225 ConvertUTF8ToJavaString(env, content_url.spec()); | 251 ConvertUTF8ToJavaString(env, content_url.spec()); |
| 226 Java_ViewAndroidDelegate_onStartContentIntent(env, delegate, jcontent_url, | 252 Java_ViewAndroidDelegate_onStartContentIntent(env, delegate, jcontent_url, |
| 227 is_main_frame); | 253 is_main_frame); |
| 228 } | 254 } |
| 229 | 255 |
| 256 bool ViewAndroid::OnTouchEvent(const MotionEventAndroid& event, | |
| 257 bool is_touch_handle_event) { | |
| 258 if (client_ && client_->OnTouchEvent(event, is_touch_handle_event)) | |
|
boliu
2017/02/27 19:21:57
what happened to hit testing?
Did that part get r
boliu
2017/02/28 03:18:15
actually, never mind, hit testing can be added lat
Jinsuk Kim
2017/02/28 06:56:04
Done.
Jinsuk Kim
2017/02/28 06:56:04
Restored and saw your comment too late :(
Set |mat
boliu
2017/02/28 22:44:54
Sure :p but bring back the unit tests too :)
Jinsuk Kim
2017/03/02 04:08:34
Done.
| |
| 259 return true; | |
| 260 | |
| 261 for (auto it = children_.rbegin(); it != children_.rend(); ++it) { | |
| 262 if ((*it)->OnTouchEvent(event, is_touch_handle_event)) | |
| 263 return true; | |
| 264 } | |
| 265 | |
| 266 return false; | |
| 267 } | |
| 268 | |
| 269 bool ViewAndroid::OnMouseEvent(const MotionEventAndroid& event, | |
| 270 int action_button) { | |
| 271 if (client_ && client_->OnMouseEvent(event, action_button)) | |
| 272 return true; | |
| 273 | |
| 274 for (auto it = children_.rbegin(); it != children_.rend(); ++it) { | |
| 275 if ((*it)->OnMouseEvent(event, action_button)) | |
| 276 return true; | |
| 277 } | |
| 278 | |
| 279 return false; | |
| 280 } | |
| 281 | |
| 230 } // namespace ui | 282 } // namespace ui |
| OLD | NEW |