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

Unified Diff: ui/android/view_android.cc

Issue 2502763003: Introduce ViewRoot to forward input/view events to native (Closed)
Patch Set: no overloaded GetEventHandler Created 4 years 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 side-by-side diff with in-line comments
Download patch
Index: ui/android/view_android.cc
diff --git a/ui/android/view_android.cc b/ui/android/view_android.cc
index 664758cd5304088709f1cd99bdb655bae764aadf..9bb8c2cce741088fde672d0df302a7defafca606 100644
--- a/ui/android/view_android.cc
+++ b/ui/android/view_android.cc
@@ -8,13 +8,16 @@
#include "base/android/jni_android.h"
#include "cc/layers/layer.h"
+#include "jni/EventHandler_jni.h"
#include "jni/ViewAndroidDelegate_jni.h"
+#include "ui/android/view_client.h"
#include "ui/android/window_android.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
namespace ui {
+using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
@@ -68,24 +71,29 @@ ViewAndroid::ScopedAnchorView::view() const {
return view_.get(env);
}
-ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate)
- : parent_(nullptr)
- , delegate_(base::android::AttachCurrentThread(),
- delegate.obj()) {}
-
-ViewAndroid::ViewAndroid() : parent_(nullptr) {}
+ViewAndroid::ViewAndroid(ViewClient* client) : parent_(nullptr),
+ client_(client),
+ physical_width_pix_(0),
+ physical_height_pix_(0) {}
+ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {}
ViewAndroid::~ViewAndroid() {
RemoveFromParent();
- for (std::list<ViewAndroid*>::iterator it = children_.begin();
- it != children_.end(); it++) {
- DCHECK_EQ((*it)->parent_, this);
- (*it)->parent_ = nullptr;
+ for (auto& child : children_) {
+ DCHECK_EQ(child->parent_, this);
+ child->parent_ = nullptr;
}
+
+ JNIEnv* env = base::android::AttachCurrentThread();
+ const ScopedJavaLocalRef<jobject> handler = event_handler_.get(env);
+ if (!handler.is_null())
+ Java_EventHandler_onDestroyNativeView(env, handler);
}
void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) {
+ // A ViewAndroid may have its own delegate or otherwise will
+ // use the next available parent's delegate.
JNIEnv* env = base::android::AttachCurrentThread();
delegate_ = JavaObjectWeakGlobalRef(env, delegate);
}
@@ -94,11 +102,17 @@ void ViewAndroid::AddChild(ViewAndroid* child) {
DCHECK(child);
DCHECK(std::find(children_.begin(), children_.end(), child) ==
children_.end());
+ DCHECK(!HasEventHandlerInTreeHierarchy() ||
+ !child->HasEventHandlerInSubtree());
children_.push_back(child);
if (child->parent_)
child->RemoveFromParent();
child->parent_ = this;
+ if (physical_width_pix_ || physical_height_pix_) {
+ child->OnPhysicalBackingSizeChanged(physical_width_pix_,
+ physical_height_pix_);
+ }
}
void ViewAndroid::RemoveFromParent() {
@@ -151,6 +165,15 @@ WindowAndroid* ViewAndroid::GetWindowAndroid() const {
return parent_ ? parent_->GetWindowAndroid() : nullptr;
}
+ScopedJavaLocalRef<jobject> ViewAndroid::CreateEventHandler() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ return Java_EventHandler_create(env, reinterpret_cast<intptr_t>(this));
+}
+
+bool ViewAndroid::HasEventHandler() {
+ return !event_handler_.is_uninitialized();
+}
+
const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate()
const {
JNIEnv* env = base::android::AttachCurrentThread();
@@ -167,6 +190,38 @@ cc::Layer* ViewAndroid::GetLayer() const {
void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) {
layer_ = layer;
+ UpdateLayerBounds();
+}
+
+ScopedJavaLocalRef<jobject> ViewAndroid::GetEventHandler() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ const ScopedJavaLocalRef<jobject> handler = event_handler_.get(env);
+ if (!handler.is_null())
+ return handler;
+
+ DCHECK(!HasEventHandlerInTreeHierarchy());
+ event_handler_ = JavaObjectWeakGlobalRef(env, CreateEventHandler());
+ return event_handler_.get(env);
+}
+
+bool ViewAndroid::HasEventHandlerInTreeHierarchy() {
+ ViewAndroid* view = parent_;
+ while (view) {
+ if (view->HasEventHandler())
+ return true;
+ view = view->parent_;
+ }
+ return HasEventHandlerInSubtree();
+}
+
+bool ViewAndroid::HasEventHandlerInSubtree() {
+ if (HasEventHandler())
+ return true;
+ for (auto& child : children_) {
+ if (child->HasEventHandlerInSubtree())
+ return true;
+ }
+ return false;
}
bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext,
@@ -179,4 +234,42 @@ bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext,
jimage);
}
+gfx::Size ViewAndroid::GetPhysicalBackingSize() {
+ return gfx::Size(physical_width_pix_, physical_height_pix_);
+}
+
+void ViewAndroid::UpdateLayerBounds() {
+ if (layer_)
+ layer_->SetBounds(GetPhysicalBackingSize());
+}
+
+void ViewAndroid::OnPhysicalBackingSizeChanged(int width, int height) {
Ted C 2016/12/15 23:36:44 does physical backing size make sense on anything
Jinsuk Kim 2016/12/16 02:04:17 In early rounds of patches the size was stored at
+ if (width == physical_width_pix_ && height == physical_height_pix_)
+ return;
+
+ physical_width_pix_ = width;
+ physical_height_pix_ = height;
+ UpdateLayerBounds();
+
+ if (client_)
+ client_->OnPhysicalBackingSizeChanged(width, height);
+
+ for (auto& child : children_)
+ child->OnPhysicalBackingSizeChanged(width, height);
+}
+
+// static
+void OnPhysicalBackingSizeChanged(JNIEnv* env,
+ const JavaParamRef<jclass>& jcaller,
+ jlong native_view,
+ int width,
+ int height) {
+ ViewAndroid* view_android = reinterpret_cast<ViewAndroid*>(native_view);
+ view_android->OnPhysicalBackingSizeChanged(width, height);
+}
+
+bool RegisterEventHandler(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698