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

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

Issue 2861413002: Refined DCHECK preventing multiple event forwarders in VA tree path (Closed)
Patch Set: comment 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(!HasMultipleEventForwarder(this, true, this))
108 << "Root of the ViewAndroid can have at most one handler."; 108 << "The view tree path already has an event handler.";
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(!HasMultipleEventForwarder(this, has_event_forwarder(), child))
119 << "Only one event handler is allowed."; 119 << "Some view tree path will have more than one event handler after "
120 "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 // static
129 bool ViewAndroid::ViewTreeHasEventForwarder(ViewAndroid* view) { 130 bool ViewAndroid::HasMultipleEventForwarder(ViewAndroid* node,
boliu 2017/05/10 01:57:16 this is not correct afaict, if there's a single fo
Jinsuk Kim 2017/05/10 06:36:12 Fixed the bug, and simplified the logic.
130 ViewAndroid* v = view; 131 bool has_forwarder,
131 do { 132 ViewAndroid* tree) {
132 if (v->has_event_forwarder()) 133 ViewAndroid* v = node->parent_;
134 bool forwarder_found = has_forwarder;
135
136 // Checks if there are multiple event forwarders from here to the root node.
137 while (v) {
138 if (forwarder_found && v->has_event_forwarder())
133 return true; 139 return true;
140 forwarder_found |= v->has_event_forwarder();
134 v = v->parent_; 141 v = v->parent_;
135 } while (v); 142 }
136 return SubtreeHasEventForwarder(view); 143
144 // Checks of there is any path from |tree| to leaf nodes that has multiple
145 // event forwarders.
146 return HasMultipleEventForwarderInAnyPath(tree, forwarder_found);
137 } 147 }
138 148
139 // static 149 // static
140 bool ViewAndroid::SubtreeHasEventForwarder(ViewAndroid* view) { 150 bool ViewAndroid::HasMultipleEventForwarderInAnyPath(ViewAndroid* node,
141 if (view->has_event_forwarder()) 151 bool forwarder_found) {
152 bool has_forwarder = node->has_event_forwarder();
153 if (forwarder_found && has_forwarder)
142 return true; 154 return true;
143 for (auto* child : view->children_) { 155
144 if (SubtreeHasEventForwarder(child)) 156 forwarder_found |= has_forwarder;
157 for (auto* child : node->children_) {
158 if (HasMultipleEventForwarderInAnyPath(child, forwarder_found))
145 return true; 159 return true;
146 } 160 }
147 return false; 161 return false;
148 } 162 }
149 163
150 void ViewAndroid::MoveToFront(ViewAndroid* child) { 164 void ViewAndroid::MoveToFront(ViewAndroid* child) {
151 DCHECK(child); 165 DCHECK(child);
152 auto it = std::find(children_.begin(), children_.end(), child); 166 auto it = std::find(children_.begin(), children_.end(), child);
153 DCHECK(it != children_.end()); 167 DCHECK(it != children_.end());
154 168
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 matched = bound.Contains(e->GetX(0), e->GetY(0)); 346 matched = bound.Contains(e->GetX(0), e->GetY(0));
333 } 347 }
334 if (matched && child->HitTest(send_to_client, *e)) 348 if (matched && child->HitTest(send_to_client, *e))
335 return true; 349 return true;
336 } 350 }
337 } 351 }
338 return false; 352 return false;
339 } 353 }
340 354
341 } // namespace ui 355 } // 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