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

Unified Diff: ui/android/view_android.cc

Issue 2502763003: Introduce ViewRoot to forward input/view events to native (Closed)
Patch Set: rebased/fixed tests 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..b877212bd14ad40b1bd783c0dfb4ca018788ef25 100644
--- a/ui/android/view_android.cc
+++ b/ui/android/view_android.cc
@@ -9,12 +9,15 @@
#include "base/android/jni_android.h"
#include "cc/layers/layer.h"
#include "jni/ViewAndroidDelegate_jni.h"
+#include "jni/ViewRoot_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_ViewRoot_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(!HasViewRootInTreeHierarchy() ||
+ !child->HasViewRootInSubtree());
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::CreateViewRoot() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ return Java_ViewRoot_create(env, reinterpret_cast<intptr_t>(this));
+}
+
+bool ViewAndroid::HasViewRoot() {
+ return !event_handler_.is_uninitialized();
+}
+
const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate()
const {
JNIEnv* env = base::android::AttachCurrentThread();
@@ -167,6 +190,36 @@ cc::Layer* ViewAndroid::GetLayer() const {
void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) {
layer_ = layer;
+ UpdateLayerBounds();
+}
+
+ScopedJavaLocalRef<jobject> ViewAndroid::GetViewRoot() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ if (!HasViewRoot()) {
Jinsuk Kim 2016/12/19 04:40:58 HasViewRoot() makes this method simpler.
boliu 2016/12/19 17:33:09 simpler appears to be wrong though.. this is not
Jinsuk Kim 2016/12/19 22:59:34 Hm reverted.
+ DCHECK(!HasViewRootInTreeHierarchy());
+ event_handler_ = JavaObjectWeakGlobalRef(env, CreateViewRoot());
boliu 2016/12/19 17:33:09 rename the var name view_root_?
Jinsuk Kim 2016/12/19 22:59:34 Done.
+ }
+ return event_handler_.get(env);
+}
+
+bool ViewAndroid::HasViewRootInTreeHierarchy() {
+ ViewAndroid* view = parent_;
+ while (view) {
+ if (view->HasViewRoot())
+ return true;
+ view = view->parent_;
+ }
+ return HasViewRootInSubtree();
+}
+
+bool ViewAndroid::HasViewRootInSubtree() {
+ if (HasViewRoot())
+ return true;
+ for (auto& child : children_) {
+ if (child->HasViewRootInSubtree())
+ return true;
+ }
+ return false;
}
bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext,
@@ -179,4 +232,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) {
+ 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 RegisterViewRoot(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698