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/EventHandler_jni.h" | |
| 11 #include "jni/ViewAndroidDelegate_jni.h" | 12 #include "jni/ViewAndroidDelegate_jni.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), |
| 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 Java_EventHandler_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 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 170 return Java_EventHandler_create(env, reinterpret_cast<intptr_t>(this)); | |
| 171 } | |
| 172 | |
| 173 bool ViewAndroid::HasEventHandler() { | |
| 174 return !event_handler_.is_uninitialized(); | |
| 175 } | |
| 176 | |
| 154 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate() | 177 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate() |
| 155 const { | 178 const { |
| 156 JNIEnv* env = base::android::AttachCurrentThread(); | 179 JNIEnv* env = base::android::AttachCurrentThread(); |
| 157 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); | 180 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); |
| 158 if (!delegate.is_null()) | 181 if (!delegate.is_null()) |
| 159 return delegate; | 182 return delegate; |
| 160 | 183 |
| 161 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; | 184 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; |
| 162 } | 185 } |
| 163 | 186 |
| 164 cc::Layer* ViewAndroid::GetLayer() const { | 187 cc::Layer* ViewAndroid::GetLayer() const { |
| 165 return layer_.get(); | 188 return layer_.get(); |
| 166 } | 189 } |
| 167 | 190 |
| 168 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 191 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
| 169 layer_ = layer; | 192 layer_ = layer; |
| 193 UpdateLayerBounds(); | |
| 194 } | |
| 195 | |
| 196 ScopedJavaLocalRef<jobject> ViewAndroid::GetEventHandler() { | |
| 197 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 198 const ScopedJavaLocalRef<jobject> handler = event_handler_.get(env); | |
| 199 if (!handler.is_null()) | |
| 200 return handler; | |
| 201 | |
| 202 DCHECK(!HasEventHandlerInTreeHierarchy()); | |
| 203 event_handler_ = JavaObjectWeakGlobalRef(env, CreateEventHandler()); | |
| 204 return event_handler_.get(env); | |
| 205 } | |
| 206 | |
| 207 bool ViewAndroid::HasEventHandlerInTreeHierarchy() { | |
| 208 ViewAndroid* view = parent_; | |
| 209 while (view) { | |
| 210 if (view->HasEventHandler()) | |
| 211 return true; | |
| 212 view = view->parent_; | |
| 213 } | |
| 214 return HasEventHandlerInSubtree(); | |
| 215 } | |
| 216 | |
| 217 bool ViewAndroid::HasEventHandlerInSubtree() { | |
| 218 if (HasEventHandler()) | |
| 219 return true; | |
| 220 for (auto& child : children_) { | |
| 221 if (child->HasEventHandlerInSubtree()) | |
| 222 return true; | |
| 223 } | |
| 224 return false; | |
| 170 } | 225 } |
| 171 | 226 |
| 172 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 227 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
| 173 const JavaRef<jobject>& jimage) { | 228 const JavaRef<jobject>& jimage) { |
| 174 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 229 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 175 if (delegate.is_null()) | 230 if (delegate.is_null()) |
| 176 return false; | 231 return false; |
| 177 JNIEnv* env = base::android::AttachCurrentThread(); | 232 JNIEnv* env = base::android::AttachCurrentThread(); |
| 178 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, | 233 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, |
| 179 jimage); | 234 jimage); |
| 180 } | 235 } |
| 181 | 236 |
| 237 gfx::Size ViewAndroid::GetPhysicalBackingSize() { | |
| 238 return gfx::Size(physical_width_pix_, physical_height_pix_); | |
| 239 } | |
| 240 | |
| 241 void ViewAndroid::UpdateLayerBounds() { | |
| 242 if (layer_) | |
| 243 layer_->SetBounds(GetPhysicalBackingSize()); | |
| 244 } | |
| 245 | |
| 246 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
| |
| 247 if (width == physical_width_pix_ && height == physical_height_pix_) | |
| 248 return; | |
| 249 | |
| 250 physical_width_pix_ = width; | |
| 251 physical_height_pix_ = height; | |
| 252 UpdateLayerBounds(); | |
| 253 | |
| 254 if (client_) | |
| 255 client_->OnPhysicalBackingSizeChanged(width, height); | |
| 256 | |
| 257 for (auto& child : children_) | |
| 258 child->OnPhysicalBackingSizeChanged(width, height); | |
| 259 } | |
| 260 | |
| 261 // static | |
| 262 void OnPhysicalBackingSizeChanged(JNIEnv* env, | |
| 263 const JavaParamRef<jclass>& jcaller, | |
| 264 jlong native_view, | |
| 265 int width, | |
| 266 int height) { | |
| 267 ViewAndroid* view_android = reinterpret_cast<ViewAndroid*>(native_view); | |
| 268 view_android->OnPhysicalBackingSizeChanged(width, height); | |
| 269 } | |
| 270 | |
| 271 bool RegisterEventHandler(JNIEnv* env) { | |
| 272 return RegisterNativesImpl(env); | |
| 273 } | |
| 274 | |
| 182 } // namespace ui | 275 } // namespace ui |
| OLD | NEW |