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

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

Issue 2861413002: Refined DCHECK preventing multiple event forwarders in VA tree path (Closed)
Patch Set: simpler 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(!RootPathHasEventForwarder(parent_) &&
boliu 2017/05/11 00:30:27 nit: maybe split this into two DCHECKs
108 << "Root of the ViewAndroid can have at most one handler."; 108 !SubtreeHasEventForwarder(this))
109 << "The view tree path already has an event forwarder.";
109 event_forwarder_.reset(new EventForwarder(this)); 110 event_forwarder_.reset(new EventForwarder(this));
110 } 111 }
111 return event_forwarder_->GetJavaObject(); 112 return event_forwarder_->GetJavaObject();
112 } 113 }
113 114
114 void ViewAndroid::AddChild(ViewAndroid* child) { 115 void ViewAndroid::AddChild(ViewAndroid* child) {
115 DCHECK(child); 116 DCHECK(child);
116 DCHECK(std::find(children_.begin(), children_.end(), child) == 117 DCHECK(std::find(children_.begin(), children_.end(), child) ==
117 children_.end()); 118 children_.end());
118 DCHECK(!SubtreeHasEventForwarder(child) || !ViewTreeHasEventForwarder(this)) 119 DCHECK(!RootPathHasEventForwarder(this) || !SubtreeHasEventForwarder(child))
119 << "Only one event handler is allowed."; 120 << "Some view tree path will have more than one event forwarder "
121 "if the child is added.";
120 122
121 // The new child goes to the top, which is the end of the list. 123 // The new child goes to the top, which is the end of the list.
122 children_.push_back(child); 124 children_.push_back(child);
123 if (child->parent_) 125 if (child->parent_)
124 child->RemoveFromParent(); 126 child->RemoveFromParent();
125 child->parent_ = this; 127 child->parent_ = this;
126 } 128 }
127 129
128 // static 130 // static
129 bool ViewAndroid::ViewTreeHasEventForwarder(ViewAndroid* view) { 131 bool ViewAndroid::RootPathHasEventForwarder(ViewAndroid* view) {
130 ViewAndroid* v = view; 132 while (view) {
131 do { 133 if (view->has_event_forwarder())
132 if (v->has_event_forwarder())
133 return true; 134 return true;
134 v = v->parent_; 135 view = view->parent_;
135 } while (v); 136 }
136 return SubtreeHasEventForwarder(view); 137
138 return false;
137 } 139 }
138 140
139 // static 141 // static
140 bool ViewAndroid::SubtreeHasEventForwarder(ViewAndroid* view) { 142 bool ViewAndroid::SubtreeHasEventForwarder(ViewAndroid* view) {
141 if (view->has_event_forwarder()) 143 if (view->has_event_forwarder())
142 return true; 144 return true;
145
143 for (auto* child : view->children_) { 146 for (auto* child : view->children_) {
144 if (SubtreeHasEventForwarder(child)) 147 if (SubtreeHasEventForwarder(child))
145 return true; 148 return true;
146 } 149 }
147 return false; 150 return false;
148 } 151 }
149 152
150 void ViewAndroid::MoveToFront(ViewAndroid* child) { 153 void ViewAndroid::MoveToFront(ViewAndroid* child) {
151 DCHECK(child); 154 DCHECK(child);
152 auto it = std::find(children_.begin(), children_.end(), child); 155 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)); 335 matched = bound.Contains(e->GetX(0), e->GetY(0));
333 } 336 }
334 if (matched && child->HitTest(send_to_client, *e)) 337 if (matched && child->HitTest(send_to_client, *e))
335 return true; 338 return true;
336 } 339 }
337 } 340 }
338 return false; 341 return false;
339 } 342 }
340 343
341 } // namespace ui 344 } // 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