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

Side by Side Diff: ui/android/view_android.cc

Issue 2595263002: Introduce ViewRoot forwarding input/view events to native (Closed)
Patch Set: tests Created 3 years, 11 months 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 unified diff | Download patch
OLDNEW
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
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);
103 DCHECK(std::find(children_.begin(), children_.end(), child) == 97 DCHECK(std::find(children_.begin(), children_.end(), child) ==
104 children_.end()); 98 children_.end());
105 DCHECK(!HasViewRootInTreeHierarchy() || 99 DCHECK(!HasViewRootInTreeHierarchy() ||
106 !child->HasViewRootInSubtree()); 100 !child->HasViewRootInSubtree());
107 101
108 children_.push_back(child); 102 children_.push_back(child);
109 if (child->parent_) 103 if (child->parent_)
110 child->RemoveFromParent(); 104 child->RemoveFromParent();
111 child->parent_ = this; 105 child->parent_ = this;
112 if (physical_width_pix_ || physical_height_pix_) { 106 if (physical_width_pix_ || physical_height_pix_) {
113 child->OnPhysicalBackingSizeChanged(physical_width_pix_, 107 child->OnPhysicalBackingSizeChangedInternal(physical_width_pix_,
114 physical_height_pix_); 108 physical_height_pix_);
115 } 109 }
116 } 110 }
117 111
118 void ViewAndroid::RemoveFromParent() { 112 void ViewAndroid::RemoveFromParent() {
119 if (parent_) 113 if (parent_)
120 parent_->RemoveChild(this); 114 parent_->RemoveChild(this);
121 } 115 }
122 116
123 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { 117 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() {
124 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); 118 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 std::find(children_.begin(), children_.end(), child); 152 std::find(children_.begin(), children_.end(), child);
159 DCHECK(it != children_.end()); 153 DCHECK(it != children_.end());
160 children_.erase(it); 154 children_.erase(it);
161 child->parent_ = nullptr; 155 child->parent_ = nullptr;
162 } 156 }
163 157
164 WindowAndroid* ViewAndroid::GetWindowAndroid() const { 158 WindowAndroid* ViewAndroid::GetWindowAndroid() const {
165 return parent_ ? parent_->GetWindowAndroid() : nullptr; 159 return parent_ ? parent_->GetWindowAndroid() : nullptr;
166 } 160 }
167 161
168 ScopedJavaLocalRef<jobject> ViewAndroid::CreateViewRoot() { 162 void ViewAndroid::SetWindowAndroid(WindowAndroid* window) {
169 JNIEnv* env = base::android::AttachCurrentThread(); 163 DCHECK(parent_) << "The root of this view tree is not present.";
170 return Java_ViewRoot_create(env, reinterpret_cast<intptr_t>(this)); 164 parent_->SetWindowAndroid(window);
171 } 165 }
172 166
173 bool ViewAndroid::HasViewRoot() { 167 bool ViewAndroid::IsViewRoot() {
174 return !view_root_.is_uninitialized(); 168 return GetViewRoot() == this;
175 } 169 }
176 170
177 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate() 171 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate()
178 const { 172 const {
179 JNIEnv* env = base::android::AttachCurrentThread(); 173 JNIEnv* env = base::android::AttachCurrentThread();
180 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); 174 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env);
181 if (!delegate.is_null()) 175 if (!delegate.is_null())
182 return delegate; 176 return delegate;
183 177
184 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; 178 return parent_ ? parent_->GetViewAndroidDelegate() : delegate;
185 } 179 }
186 180
187 cc::Layer* ViewAndroid::GetLayer() const { 181 cc::Layer* ViewAndroid::GetLayer() const {
188 return layer_.get(); 182 return layer_.get();
189 } 183 }
190 184
191 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { 185 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) {
192 layer_ = layer; 186 layer_ = layer;
193 UpdateLayerBounds(); 187 UpdateLayerBounds();
194 } 188 }
195 189
196 ScopedJavaLocalRef<jobject> ViewAndroid::GetViewRoot() { 190 ViewAndroid* ViewAndroid::GetViewRoot() {
197 JNIEnv* env = base::android::AttachCurrentThread(); 191 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 } 192 }
206 193
207 bool ViewAndroid::HasViewRootInTreeHierarchy() { 194 bool ViewAndroid::HasViewRootInTreeHierarchy() {
boliu 2017/01/03 19:16:11 I think this linear time check is unnecessary now.
Jinsuk Kim 2017/01/04 10:45:02 Done.
208 ViewAndroid* view = parent_; 195 ViewAndroid* view = parent_;
209 while (view) { 196 while (view) {
210 if (view->HasViewRoot()) 197 if (view->IsViewRoot())
211 return true; 198 return true;
212 view = view->parent_; 199 view = view->parent_;
213 } 200 }
214 return HasViewRootInSubtree(); 201 return HasViewRootInSubtree();
215 } 202 }
216 203
217 bool ViewAndroid::HasViewRootInSubtree() { 204 bool ViewAndroid::HasViewRootInSubtree() {
218 if (HasViewRoot()) 205 if (IsViewRoot())
219 return true; 206 return true;
220 for (auto& child : children_) { 207 for (auto& child : children_) {
221 if (child->HasViewRootInSubtree()) 208 if (child->HasViewRootInSubtree())
222 return true; 209 return true;
223 } 210 }
224 return false; 211 return false;
225 } 212 }
226 213
227 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, 214 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext,
228 const JavaRef<jobject>& jimage) { 215 const JavaRef<jobject>& jimage) {
229 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); 216 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
230 if (delegate.is_null()) 217 if (delegate.is_null())
231 return false; 218 return false;
232 JNIEnv* env = base::android::AttachCurrentThread(); 219 JNIEnv* env = base::android::AttachCurrentThread();
233 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, 220 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext,
234 jimage); 221 jimage);
235 } 222 }
236 223
237 gfx::Size ViewAndroid::GetPhysicalBackingSize() { 224 gfx::Size ViewAndroid::GetPhysicalBackingSize() {
238 return gfx::Size(physical_width_pix_, physical_height_pix_); 225 return gfx::Size(physical_width_pix_, physical_height_pix_);
239 } 226 }
240 227
241 void ViewAndroid::UpdateLayerBounds() { 228 void ViewAndroid::UpdateLayerBounds() {
242 if (layer_) 229 if (layer_)
243 layer_->SetBounds(GetPhysicalBackingSize()); 230 layer_->SetBounds(GetPhysicalBackingSize());
244 } 231 }
245 232
246 void ViewAndroid::OnPhysicalBackingSizeChanged(int width, int height) { 233 void ViewAndroid::OnPhysicalBackingSizeChangedInternal(int width, int height) {
247 if (width == physical_width_pix_ && height == physical_height_pix_) 234 if (width == physical_width_pix_ && height == physical_height_pix_)
248 return; 235 return;
249 236
250 physical_width_pix_ = width; 237 physical_width_pix_ = width;
251 physical_height_pix_ = height; 238 physical_height_pix_ = height;
252 UpdateLayerBounds(); 239 UpdateLayerBounds();
253 240
254 if (client_) 241 if (client_)
255 client_->OnPhysicalBackingSizeChanged(width, height); 242 client_->OnPhysicalBackingSizeChanged(width, height);
256 243
257 for (auto& child : children_) 244 for (auto& child : children_)
258 child->OnPhysicalBackingSizeChanged(width, height); 245 child->OnPhysicalBackingSizeChangedInternal(width, height);
259 } 246 }
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 247 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698