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

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

Issue 2861413002: Refined DCHECK preventing multiple event forwarders in VA tree path (Closed)
Patch Set: renamed methods Created 3 years, 7 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
« no previous file with comments | « ui/android/view_android.h ('k') | ui/android/view_android_unittests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 JNIEnv* env = base::android::AttachCurrentThread(); 97 JNIEnv* env = base::android::AttachCurrentThread();
98 delegate_ = JavaObjectWeakGlobalRef(env, delegate); 98 delegate_ = JavaObjectWeakGlobalRef(env, delegate);
99 } 99 }
100 100
101 float ViewAndroid::GetDipScale() { 101 float ViewAndroid::GetDipScale() {
102 return ui::GetScaleFactorForNativeView(this); 102 return ui::GetScaleFactorForNativeView(this);
103 } 103 }
104 104
105 ScopedJavaLocalRef<jobject> ViewAndroid::GetEventForwarder() { 105 ScopedJavaLocalRef<jobject> ViewAndroid::GetEventForwarder() {
106 if (!event_forwarder_) { 106 if (!event_forwarder_) {
107 DCHECK(!ViewTreeHasEventForwarder(this)) 107 DCHECK(!OtherNodesHaveEventForwarder(this))
108 << "Root of the ViewAndroid can have at most one handler."; 108 << "The view tree path already has an event forwarder.";
109 event_forwarder_.reset(new EventForwarder(this)); 109 event_forwarder_.reset(new EventForwarder(this));
110 } 110 }
111 return event_forwarder_->GetJavaObject(); 111 return event_forwarder_->GetJavaObject();
112 } 112 }
113 113
114 void ViewAndroid::AddChild(ViewAndroid* child) { 114 void ViewAndroid::AddChild(ViewAndroid* child) {
115 DCHECK(child); 115 DCHECK(child);
116 DCHECK(std::find(children_.begin(), children_.end(), child) == 116 DCHECK(std::find(children_.begin(), children_.end(), child) ==
117 children_.end()); 117 children_.end());
118 DCHECK(!SubtreeHasEventForwarder(child) || !ViewTreeHasEventForwarder(this)) 118 DCHECK(!OtherNodesHaveEventForwarder(child) || !has_event_forwarder())
119 << "Only one event handler is allowed."; 119 << "Some view tree path will have more than one event forwarder "
120 "if the child is added.";
120 121
121 // The new child goes to the top, which is the end of the list. 122 // The new child goes to the top, which is the end of the list.
122 children_.push_back(child); 123 children_.push_back(child);
123 if (child->parent_) 124 if (child->parent_)
124 child->RemoveFromParent(); 125 child->RemoveFromParent();
125 child->parent_ = this; 126 child->parent_ = this;
126 } 127 }
127 128
128 // static 129 bool ViewAndroid::OtherNodesHaveEventForwarder(ViewAndroid* tree) {
129 bool ViewAndroid::ViewTreeHasEventForwarder(ViewAndroid* view) { 130 // Note that it's |this| node's parents that are being checked here,
130 ViewAndroid* v = view; 131 // not those of the |tree|.
131 do { 132 return ParentsHaveEventForwarder() || SubtreeHasEventForwarder(tree);
boliu 2017/05/10 22:48:43 this method is still super confusing, and can easi
Jinsuk Kim 2017/05/10 23:17:56 Done.
132 if (v->has_event_forwarder()) 133 }
134
135 bool ViewAndroid::ParentsHaveEventForwarder() {
136 ViewAndroid* view = parent_;
137 while (view) {
138 if (view->has_event_forwarder())
133 return true; 139 return true;
134 v = v->parent_; 140 view = view->parent_;
135 } while (v); 141 }
136 return SubtreeHasEventForwarder(view); 142 return false;
137 } 143 }
138 144
139 // static 145 // static
140 bool ViewAndroid::SubtreeHasEventForwarder(ViewAndroid* view) { 146 bool ViewAndroid::SubtreeHasEventForwarder(ViewAndroid* view) {
141 if (view->has_event_forwarder()) 147 if (view->has_event_forwarder())
142 return true; 148 return true;
149
143 for (auto* child : view->children_) { 150 for (auto* child : view->children_) {
144 if (SubtreeHasEventForwarder(child)) 151 if (SubtreeHasEventForwarder(child))
145 return true; 152 return true;
146 } 153 }
147 return false; 154 return false;
148 } 155 }
149 156
150 void ViewAndroid::MoveToFront(ViewAndroid* child) { 157 void ViewAndroid::MoveToFront(ViewAndroid* child) {
151 DCHECK(child); 158 DCHECK(child);
152 auto it = std::find(children_.begin(), children_.end(), child); 159 auto it = std::find(children_.begin(), children_.end(), child);
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 matched = bound.Contains(e->GetX(0), e->GetY(0)); 339 matched = bound.Contains(e->GetX(0), e->GetY(0));
333 } 340 }
334 if (matched && child->HitTest(send_to_client, *e)) 341 if (matched && child->HitTest(send_to_client, *e))
335 return true; 342 return true;
336 } 343 }
337 } 344 }
338 return false; 345 return false;
339 } 346 }
340 347
341 } // namespace ui 348 } // namespace ui
OLDNEW
« no previous file with comments | « ui/android/view_android.h ('k') | ui/android/view_android_unittests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698