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 "cc/layers/layer.h" | 10 #include "cc/layers/layer.h" |
| 11 #include "jni/ViewAndroidDelegate_jni.h" | 11 #include "jni/ViewAndroidDelegate_jni.h" |
| 12 #include "ui/android/event_handler.h" | |
| 13 #include "ui/android/view_client.h" | |
| 12 #include "ui/android/window_android.h" | 14 #include "ui/android/window_android.h" |
| 13 #include "ui/display/display.h" | 15 #include "ui/display/display.h" |
| 14 #include "ui/display/screen.h" | 16 #include "ui/display/screen.h" |
| 15 | 17 |
| 16 namespace ui { | 18 namespace ui { |
| 17 | 19 |
| 20 using base::android::JavaParamRef; | |
| 18 using base::android::JavaRef; | 21 using base::android::JavaRef; |
| 19 using base::android::ScopedJavaLocalRef; | 22 using base::android::ScopedJavaLocalRef; |
| 20 | 23 |
| 21 ViewAndroid::ScopedAnchorView::ScopedAnchorView( | 24 ViewAndroid::ScopedAnchorView::ScopedAnchorView( |
| 22 JNIEnv* env, | 25 JNIEnv* env, |
| 23 const JavaRef<jobject>& jview, | 26 const JavaRef<jobject>& jview, |
| 24 const JavaRef<jobject>& jdelegate) | 27 const JavaRef<jobject>& jdelegate) |
| 25 : view_(env, jview.obj()), delegate_(env, jdelegate.obj()) { | 28 : view_(env, jview.obj()), delegate_(env, jdelegate.obj()) { |
| 26 // If there's a view, then we need a delegate to remove it. | 29 // If there's a view, then we need a delegate to remove it. |
| 27 DCHECK(!jdelegate.is_null() || jview.is_null()); | 30 DCHECK(!jdelegate.is_null() || jview.is_null()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 view_.reset(); | 64 view_.reset(); |
| 62 delegate_.reset(); | 65 delegate_.reset(); |
| 63 } | 66 } |
| 64 | 67 |
| 65 const base::android::ScopedJavaLocalRef<jobject> | 68 const base::android::ScopedJavaLocalRef<jobject> |
| 66 ViewAndroid::ScopedAnchorView::view() const { | 69 ViewAndroid::ScopedAnchorView::view() const { |
| 67 JNIEnv* env = base::android::AttachCurrentThread(); | 70 JNIEnv* env = base::android::AttachCurrentThread(); |
| 68 return view_.get(env); | 71 return view_.get(env); |
| 69 } | 72 } |
| 70 | 73 |
| 71 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) | 74 ViewAndroid::ViewAndroid(ViewClient* client) : parent_(nullptr), |
|
boliu
2016/12/08 05:01:06
fwiw, inline init for member variable with static
Jinsuk Kim
2016/12/13 23:20:38
Acknowledged.
| |
| 72 : parent_(nullptr) | 75 client_(client), |
| 73 , delegate_(base::android::AttachCurrentThread(), | 76 physical_width_pix_(0), |
| 74 delegate.obj()) {} | 77 physical_height_pix_(0) {} |
| 75 | 78 ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {} |
| 76 ViewAndroid::ViewAndroid() : parent_(nullptr) {} | |
| 77 | 79 |
| 78 ViewAndroid::~ViewAndroid() { | 80 ViewAndroid::~ViewAndroid() { |
| 79 RemoveFromParent(); | 81 RemoveFromParent(); |
| 80 | 82 |
| 81 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 83 for (auto& child : children_) { |
| 82 it != children_.end(); it++) { | 84 DCHECK_EQ(child->parent_, this); |
| 83 DCHECK_EQ((*it)->parent_, this); | 85 child->parent_ = nullptr; |
| 84 (*it)->parent_ = nullptr; | |
| 85 } | 86 } |
| 87 | |
| 88 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 89 const ScopedJavaLocalRef<jobject> handler = event_handler_.get(env); | |
| 90 if (!handler.is_null()) | |
| 91 OnDestroyNativeView(env, handler); | |
| 86 } | 92 } |
| 87 | 93 |
| 88 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { | 94 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { |
| 95 // A ViewAndroid may have its own delegate or otherwise will | |
| 96 // use the next available parent's delegate. | |
| 89 JNIEnv* env = base::android::AttachCurrentThread(); | 97 JNIEnv* env = base::android::AttachCurrentThread(); |
| 90 delegate_ = JavaObjectWeakGlobalRef(env, delegate); | 98 delegate_ = JavaObjectWeakGlobalRef(env, delegate); |
| 91 } | 99 } |
| 92 | 100 |
| 93 void ViewAndroid::AddChild(ViewAndroid* child) { | 101 void ViewAndroid::AddChild(ViewAndroid* child) { |
| 94 DCHECK(child); | 102 DCHECK(child); |
| 95 DCHECK(std::find(children_.begin(), children_.end(), child) == | 103 DCHECK(std::find(children_.begin(), children_.end(), child) == |
| 96 children_.end()); | 104 children_.end()); |
| 105 DCHECK(!HasEventHandlerInTreeHierarchy() || | |
| 106 !child->HasEventHandlerInSubtree()); | |
| 97 | 107 |
| 98 children_.push_back(child); | 108 children_.push_back(child); |
| 99 if (child->parent_) | 109 if (child->parent_) |
| 100 child->RemoveFromParent(); | 110 child->RemoveFromParent(); |
| 101 child->parent_ = this; | 111 child->parent_ = this; |
| 112 if (physical_width_pix_ || physical_height_pix_) { | |
| 113 child->OnPhysicalBackingSizeChanged(physical_width_pix_, | |
| 114 physical_height_pix_); | |
| 115 } | |
| 102 } | 116 } |
| 103 | 117 |
| 104 void ViewAndroid::RemoveFromParent() { | 118 void ViewAndroid::RemoveFromParent() { |
| 105 if (parent_) | 119 if (parent_) |
| 106 parent_->RemoveChild(this); | 120 parent_->RemoveChild(this); |
| 107 } | 121 } |
| 108 | 122 |
| 109 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { | 123 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { |
| 110 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 124 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 111 if (delegate.is_null()) | 125 if (delegate.is_null()) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 std::find(children_.begin(), children_.end(), child); | 158 std::find(children_.begin(), children_.end(), child); |
| 145 DCHECK(it != children_.end()); | 159 DCHECK(it != children_.end()); |
| 146 children_.erase(it); | 160 children_.erase(it); |
| 147 child->parent_ = nullptr; | 161 child->parent_ = nullptr; |
| 148 } | 162 } |
| 149 | 163 |
| 150 WindowAndroid* ViewAndroid::GetWindowAndroid() const { | 164 WindowAndroid* ViewAndroid::GetWindowAndroid() const { |
| 151 return parent_ ? parent_->GetWindowAndroid() : nullptr; | 165 return parent_ ? parent_->GetWindowAndroid() : nullptr; |
| 152 } | 166 } |
| 153 | 167 |
| 168 ScopedJavaLocalRef<jobject> ViewAndroid::CreateEventHandler( | |
| 169 ViewAndroid* native_view) { | |
| 170 return parent_ ? parent_->CreateEventHandler(native_view) | |
|
boliu
2016/12/08 05:01:06
this doesn't make any sense to me, why ask the roo
Jinsuk Kim
2016/12/13 23:20:38
This is how I addressed your comment: "creating Ev
boliu
2016/12/13 23:50:41
No, I'm suggesting that you mark EventHandler.crea
Jinsuk Kim
2016/12/14 00:34:08
Ah much simpler this way. The parameter native_vie
| |
| 171 : ScopedJavaLocalRef<jobject>(); | |
| 172 } | |
| 173 | |
| 154 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate() | 174 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate() |
| 155 const { | 175 const { |
| 156 JNIEnv* env = base::android::AttachCurrentThread(); | 176 JNIEnv* env = base::android::AttachCurrentThread(); |
| 157 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); | 177 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); |
| 158 if (!delegate.is_null()) | 178 if (!delegate.is_null()) |
| 159 return delegate; | 179 return delegate; |
| 160 | 180 |
| 161 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; | 181 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; |
| 162 } | 182 } |
| 163 | 183 |
| 164 cc::Layer* ViewAndroid::GetLayer() const { | 184 cc::Layer* ViewAndroid::GetLayer() const { |
| 165 return layer_.get(); | 185 return layer_.get(); |
| 166 } | 186 } |
| 167 | 187 |
| 168 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 188 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
| 169 layer_ = layer; | 189 layer_ = layer; |
|
boliu
2016/12/08 05:01:06
need to update layer size if new layer is not null
Jinsuk Kim
2016/12/13 23:20:38
Done. Null check is performed in |UpdateLayerBound
| |
| 170 } | 190 } |
| 171 | 191 |
| 192 ScopedJavaLocalRef<jobject> ViewAndroid::GetEventHandler() { | |
| 193 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 194 const ScopedJavaLocalRef<jobject> handler = event_handler_.get(env); | |
| 195 if (!handler.is_null()) | |
| 196 return handler; | |
| 197 | |
| 198 DCHECK(!HasEventHandlerInTreeHierarchy()); | |
| 199 event_handler_ = JavaObjectWeakGlobalRef(env, CreateEventHandler(this)); | |
| 200 return event_handler_.get(env); | |
| 201 } | |
| 202 | |
| 203 bool ViewAndroid::HasEventHandlerInTreeHierarchy() { | |
| 204 ViewAndroid* view = parent_; | |
| 205 while (view) { | |
| 206 if (view->HasEventHandler()) | |
| 207 return true; | |
| 208 view = view->parent_; | |
| 209 } | |
| 210 return HasEventHandlerInSubtree(); | |
| 211 } | |
| 212 | |
| 213 bool ViewAndroid::HasEventHandlerInSubtree() { | |
| 214 if (HasEventHandler()) | |
| 215 return true; | |
| 216 for (auto& child : children_) { | |
| 217 if (child->HasEventHandlerInSubtree()) | |
| 218 return true; | |
| 219 } | |
| 220 return false; | |
| 221 } | |
| 222 | |
| 172 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 223 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
| 173 const JavaRef<jobject>& jimage) { | 224 const JavaRef<jobject>& jimage) { |
| 174 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 225 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 175 if (delegate.is_null()) | 226 if (delegate.is_null()) |
| 176 return false; | 227 return false; |
| 177 JNIEnv* env = base::android::AttachCurrentThread(); | 228 JNIEnv* env = base::android::AttachCurrentThread(); |
| 178 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, | 229 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, |
| 179 jimage); | 230 jimage); |
| 180 } | 231 } |
| 181 | 232 |
| 233 gfx::Size ViewAndroid::GetPhysicalBackingSize() { | |
| 234 return gfx::Size(physical_width_pix_, physical_height_pix_); | |
| 235 } | |
| 236 | |
| 237 void ViewAndroid::UpdateLayerBounds() { | |
| 238 if (layer_) | |
| 239 layer_->SetBounds(GetPhysicalBackingSize()); | |
| 240 } | |
| 241 | |
| 242 void ViewAndroid::OnPhysicalBackingSizeChanged(int width, int height) { | |
| 243 if (width == physical_width_pix_ && height == physical_height_pix_) | |
| 244 return; | |
| 245 | |
| 246 SetPhysicalBackingSize(width, height); | |
| 247 if (client_) | |
| 248 client_->OnPhysicalBackingSizeChanged(width, height); | |
| 249 | |
| 250 for (auto& child : children_) | |
| 251 child->OnPhysicalBackingSizeChanged(width, height); | |
| 252 } | |
| 253 | |
| 182 } // namespace ui | 254 } // namespace ui |
| OLD | NEW |