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/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_ViewRoot_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(!HasViewRootInTreeHierarchy() || | |
106 !child->HasViewRootInSubtree()); | |
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::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 !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::GetViewRoot() { | |
197 JNIEnv* env = base::android::AttachCurrentThread(); | |
198 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.
| |
199 DCHECK(!HasViewRootInTreeHierarchy()); | |
200 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.
| |
201 } | |
202 return event_handler_.get(env); | |
203 } | |
204 | |
205 bool ViewAndroid::HasViewRootInTreeHierarchy() { | |
206 ViewAndroid* view = parent_; | |
207 while (view) { | |
208 if (view->HasViewRoot()) | |
209 return true; | |
210 view = view->parent_; | |
211 } | |
212 return HasViewRootInSubtree(); | |
213 } | |
214 | |
215 bool ViewAndroid::HasViewRootInSubtree() { | |
216 if (HasViewRoot()) | |
217 return true; | |
218 for (auto& child : children_) { | |
219 if (child->HasViewRootInSubtree()) | |
220 return true; | |
221 } | |
222 return false; | |
170 } | 223 } |
171 | 224 |
172 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 225 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
173 const JavaRef<jobject>& jimage) { | 226 const JavaRef<jobject>& jimage) { |
174 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 227 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
175 if (delegate.is_null()) | 228 if (delegate.is_null()) |
176 return false; | 229 return false; |
177 JNIEnv* env = base::android::AttachCurrentThread(); | 230 JNIEnv* env = base::android::AttachCurrentThread(); |
178 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, | 231 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, |
179 jimage); | 232 jimage); |
180 } | 233 } |
181 | 234 |
235 gfx::Size ViewAndroid::GetPhysicalBackingSize() { | |
236 return gfx::Size(physical_width_pix_, physical_height_pix_); | |
237 } | |
238 | |
239 void ViewAndroid::UpdateLayerBounds() { | |
240 if (layer_) | |
241 layer_->SetBounds(GetPhysicalBackingSize()); | |
242 } | |
243 | |
244 void ViewAndroid::OnPhysicalBackingSizeChanged(int width, int height) { | |
245 if (width == physical_width_pix_ && height == physical_height_pix_) | |
246 return; | |
247 | |
248 physical_width_pix_ = width; | |
249 physical_height_pix_ = height; | |
250 UpdateLayerBounds(); | |
251 | |
252 if (client_) | |
253 client_->OnPhysicalBackingSizeChanged(width, height); | |
254 | |
255 for (auto& child : children_) | |
256 child->OnPhysicalBackingSizeChanged(width, height); | |
257 } | |
258 | |
259 // static | |
260 void OnPhysicalBackingSizeChanged(JNIEnv* env, | |
261 const JavaParamRef<jclass>& jcaller, | |
262 jlong native_view, | |
263 int width, | |
264 int height) { | |
265 ViewAndroid* view_android = reinterpret_cast<ViewAndroid*>(native_view); | |
266 view_android->OnPhysicalBackingSizeChanged(width, height); | |
267 } | |
268 | |
269 bool RegisterViewRoot(JNIEnv* env) { | |
270 return RegisterNativesImpl(env); | |
271 } | |
272 | |
182 } // namespace ui | 273 } // namespace ui |
OLD | NEW |