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

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

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