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 "jni/ViewRoot_jni.h" | |
| 13 #include "ui/android/view_client.h" | 12 #include "ui/android/view_client.h" |
| 14 #include "ui/android/window_android.h" | 13 #include "ui/android/window_android.h" |
| 15 #include "ui/display/display.h" | 14 #include "ui/display/display.h" |
| 16 #include "ui/display/screen.h" | 15 #include "ui/display/screen.h" |
| 17 | 16 |
| 18 namespace ui { | 17 namespace ui { |
| 19 | 18 |
| 20 using base::android::JavaParamRef; | 19 using base::android::JavaParamRef; |
| 21 using base::android::JavaRef; | 20 using base::android::JavaRef; |
| 22 using base::android::ScopedJavaLocalRef; | 21 using base::android::ScopedJavaLocalRef; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 physical_height_pix_(0) {} | 76 physical_height_pix_(0) {} |
| 78 ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {} | 77 ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {} |
| 79 | 78 |
| 80 ViewAndroid::~ViewAndroid() { | 79 ViewAndroid::~ViewAndroid() { |
| 81 RemoveFromParent(); | 80 RemoveFromParent(); |
| 82 | 81 |
| 83 for (auto& child : children_) { | 82 for (auto& child : children_) { |
| 84 DCHECK_EQ(child->parent_, this); | 83 DCHECK_EQ(child->parent_, this); |
| 85 child->parent_ = nullptr; | 84 child->parent_ = nullptr; |
| 86 } | 85 } |
| 87 | |
| 88 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 89 const ScopedJavaLocalRef<jobject> view_root = view_root_.get(env); | |
| 90 if (!view_root.is_null()) | |
| 91 Java_ViewRoot_onDestroyNativeView(env, view_root); | |
| 92 } | 86 } |
| 93 | 87 |
| 94 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { | 88 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { |
| 95 // A ViewAndroid may have its own delegate or otherwise will | 89 // A ViewAndroid may have its own delegate or otherwise will |
| 96 // use the next available parent's delegate. | 90 // use the next available parent's delegate. |
| 97 JNIEnv* env = base::android::AttachCurrentThread(); | 91 JNIEnv* env = base::android::AttachCurrentThread(); |
| 98 delegate_ = JavaObjectWeakGlobalRef(env, delegate); | 92 delegate_ = JavaObjectWeakGlobalRef(env, delegate); |
| 99 } | 93 } |
| 100 | 94 |
| 101 void ViewAndroid::AddChild(ViewAndroid* child) { | 95 void ViewAndroid::AddChild(ViewAndroid* child) { |
| 102 DCHECK(child); | 96 DCHECK(child); |
| 97 DCHECK(child->GetViewRoot() != child); // ViewRoot cannot be a child. | |
|
boliu
2017/01/04 18:58:06
just have a "bool IsViewRoot" method instead
Jinsuk Kim
2017/01/05 11:03:12
Done.
| |
| 103 DCHECK(std::find(children_.begin(), children_.end(), child) == | 98 DCHECK(std::find(children_.begin(), children_.end(), child) == |
| 104 children_.end()); | 99 children_.end()); |
| 105 DCHECK(!HasViewRootInTreeHierarchy() || | |
| 106 !child->HasViewRootInSubtree()); | |
| 107 | 100 |
| 108 children_.push_back(child); | 101 children_.push_back(child); |
| 109 if (child->parent_) | 102 if (child->parent_) |
| 110 child->RemoveFromParent(); | 103 child->RemoveFromParent(); |
| 111 child->parent_ = this; | 104 child->parent_ = this; |
| 112 if (physical_width_pix_ || physical_height_pix_) { | 105 if (physical_width_pix_ || physical_height_pix_) { |
| 113 child->OnPhysicalBackingSizeChanged(physical_width_pix_, | 106 child->OnPhysicalBackingSizeChangedInternal(physical_width_pix_, |
| 114 physical_height_pix_); | 107 physical_height_pix_); |
| 115 } | 108 } |
| 116 } | 109 } |
| 117 | 110 |
| 118 void ViewAndroid::RemoveFromParent() { | 111 void ViewAndroid::RemoveFromParent() { |
| 119 if (parent_) | 112 if (parent_) |
| 120 parent_->RemoveChild(this); | 113 parent_->RemoveChild(this); |
| 121 } | 114 } |
| 122 | 115 |
| 123 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { | 116 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { |
| 124 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 117 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 std::find(children_.begin(), children_.end(), child); | 151 std::find(children_.begin(), children_.end(), child); |
| 159 DCHECK(it != children_.end()); | 152 DCHECK(it != children_.end()); |
| 160 children_.erase(it); | 153 children_.erase(it); |
| 161 child->parent_ = nullptr; | 154 child->parent_ = nullptr; |
| 162 } | 155 } |
| 163 | 156 |
| 164 WindowAndroid* ViewAndroid::GetWindowAndroid() const { | 157 WindowAndroid* ViewAndroid::GetWindowAndroid() const { |
| 165 return parent_ ? parent_->GetWindowAndroid() : nullptr; | 158 return parent_ ? parent_->GetWindowAndroid() : nullptr; |
| 166 } | 159 } |
| 167 | 160 |
| 168 ScopedJavaLocalRef<jobject> ViewAndroid::CreateViewRoot() { | |
| 169 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 170 return Java_ViewRoot_create(env, reinterpret_cast<intptr_t>(this)); | |
| 171 } | |
| 172 | |
| 173 bool ViewAndroid::HasViewRoot() { | |
| 174 return !view_root_.is_uninitialized(); | |
| 175 } | |
| 176 | |
| 177 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate() | 161 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate() |
| 178 const { | 162 const { |
| 179 JNIEnv* env = base::android::AttachCurrentThread(); | 163 JNIEnv* env = base::android::AttachCurrentThread(); |
| 180 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); | 164 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); |
| 181 if (!delegate.is_null()) | 165 if (!delegate.is_null()) |
| 182 return delegate; | 166 return delegate; |
| 183 | 167 |
| 184 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; | 168 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; |
| 185 } | 169 } |
| 186 | 170 |
| 187 cc::Layer* ViewAndroid::GetLayer() const { | 171 cc::Layer* ViewAndroid::GetLayer() const { |
| 188 return layer_.get(); | 172 return layer_.get(); |
| 189 } | 173 } |
| 190 | 174 |
| 191 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 175 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
| 192 layer_ = layer; | 176 layer_ = layer; |
| 193 UpdateLayerBounds(); | 177 UpdateLayerBounds(); |
| 194 } | 178 } |
| 195 | 179 |
| 196 ScopedJavaLocalRef<jobject> ViewAndroid::GetViewRoot() { | 180 ViewAndroid* ViewAndroid::GetViewRoot() { |
| 197 JNIEnv* env = base::android::AttachCurrentThread(); | 181 return parent_ ? parent_->GetViewRoot() : nullptr; |
| 198 const ScopedJavaLocalRef<jobject> view_root = view_root_.get(env); | |
| 199 if (!view_root.is_null()) | |
| 200 return view_root; | |
| 201 | |
| 202 DCHECK(!HasViewRootInTreeHierarchy()); | |
| 203 view_root_ = JavaObjectWeakGlobalRef(env, CreateViewRoot()); | |
| 204 return view_root_.get(env); | |
| 205 } | |
| 206 | |
| 207 bool ViewAndroid::HasViewRootInTreeHierarchy() { | |
| 208 ViewAndroid* view = parent_; | |
| 209 while (view) { | |
| 210 if (view->HasViewRoot()) | |
| 211 return true; | |
| 212 view = view->parent_; | |
| 213 } | |
| 214 return HasViewRootInSubtree(); | |
| 215 } | |
| 216 | |
| 217 bool ViewAndroid::HasViewRootInSubtree() { | |
| 218 if (HasViewRoot()) | |
| 219 return true; | |
| 220 for (auto& child : children_) { | |
| 221 if (child->HasViewRootInSubtree()) | |
| 222 return true; | |
| 223 } | |
| 224 return false; | |
| 225 } | 182 } |
| 226 | 183 |
| 227 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 184 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
| 228 const JavaRef<jobject>& jimage) { | 185 const JavaRef<jobject>& jimage) { |
| 229 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 186 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 230 if (delegate.is_null()) | 187 if (delegate.is_null()) |
| 231 return false; | 188 return false; |
| 232 JNIEnv* env = base::android::AttachCurrentThread(); | 189 JNIEnv* env = base::android::AttachCurrentThread(); |
| 233 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, | 190 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, |
| 234 jimage); | 191 jimage); |
| 235 } | 192 } |
| 236 | 193 |
| 237 gfx::Size ViewAndroid::GetPhysicalBackingSize() { | 194 gfx::Size ViewAndroid::GetPhysicalBackingSize() { |
| 238 return gfx::Size(physical_width_pix_, physical_height_pix_); | 195 return gfx::Size(physical_width_pix_, physical_height_pix_); |
| 239 } | 196 } |
| 240 | 197 |
| 241 void ViewAndroid::UpdateLayerBounds() { | 198 void ViewAndroid::UpdateLayerBounds() { |
| 242 if (layer_) | 199 if (layer_) |
| 243 layer_->SetBounds(GetPhysicalBackingSize()); | 200 layer_->SetBounds(GetPhysicalBackingSize()); |
| 244 } | 201 } |
| 245 | 202 |
| 246 void ViewAndroid::OnPhysicalBackingSizeChanged(int width, int height) { | 203 void ViewAndroid::OnPhysicalBackingSizeChangedInternal(int width, int height) { |
| 247 if (width == physical_width_pix_ && height == physical_height_pix_) | 204 if (width == physical_width_pix_ && height == physical_height_pix_) |
| 248 return; | 205 return; |
| 249 | 206 |
| 250 physical_width_pix_ = width; | 207 physical_width_pix_ = width; |
| 251 physical_height_pix_ = height; | 208 physical_height_pix_ = height; |
| 252 UpdateLayerBounds(); | 209 UpdateLayerBounds(); |
| 253 | 210 |
| 254 if (client_) | 211 if (client_) |
| 255 client_->OnPhysicalBackingSizeChanged(width, height); | 212 client_->OnPhysicalBackingSizeChanged(width, height); |
| 256 | 213 |
| 257 for (auto& child : children_) | 214 for (auto& child : children_) |
| 258 child->OnPhysicalBackingSizeChanged(width, height); | 215 child->OnPhysicalBackingSizeChangedInternal(width, height); |
| 259 } | 216 } |
| 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 RegisterViewRoot(JNIEnv* env) { | |
| 272 return RegisterNativesImpl(env); | |
| 273 } | |
| 274 | |
| 275 } // namespace ui | 217 } // namespace ui |
| OLD | NEW |