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

Side by Side Diff: chrome/browser/ui/autofill/autofill_popup_controller_impl.cc

Issue 2762233004: Fix autofill popup controller key press callback registration (Closed)
Patch Set: Fixed browsertests Created 3 years, 8 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" 5 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/i18n/rtl.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/optional.h" 14 #include "base/optional.h"
14 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
15 #include "build/build_config.h" 16 #include "build/build_config.h"
16 #include "chrome/browser/ui/autofill/autofill_popup_view.h" 17 #include "chrome/browser/ui/autofill/autofill_popup_view.h"
18 #include "components/autofill/content/browser/content_autofill_driver.h"
17 #include "components/autofill/core/browser/autofill_popup_delegate.h" 19 #include "components/autofill/core/browser/autofill_popup_delegate.h"
18 #include "components/autofill/core/browser/popup_item_ids.h" 20 #include "components/autofill/core/browser/popup_item_ids.h"
19 #include "components/autofill/core/browser/suggestion.h" 21 #include "components/autofill/core/browser/suggestion.h"
20 #include "content/public/browser/native_web_keyboard_event.h" 22 #include "content/public/browser/native_web_keyboard_event.h"
21 #include "ui/events/event.h" 23 #include "ui/events/event.h"
22 #include "ui/gfx/canvas.h" 24 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/text_elider.h" 25 #include "ui/gfx/text_elider.h"
24 #include "ui/gfx/text_utils.h" 26 #include "ui/gfx/text_utils.h"
25 27
26 using base::WeakPtr; 28 using base::WeakPtr;
27 29
28 namespace autofill { 30 namespace autofill {
29 31
30 // static 32 // static
31 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetOrCreate( 33 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetOrCreate(
32 WeakPtr<AutofillPopupControllerImpl> previous, 34 WeakPtr<AutofillPopupControllerImpl> previous,
33 WeakPtr<AutofillPopupDelegate> delegate, 35 WeakPtr<AutofillPopupDelegate> delegate,
34 content::WebContents* web_contents, 36 content::WebContents* web_contents,
35 gfx::NativeView container_view, 37 gfx::NativeView container_view,
36 const gfx::RectF& element_bounds, 38 const gfx::RectF& element_bounds,
37 base::i18n::TextDirection text_direction) { 39 base::i18n::TextDirection text_direction) {
38 if (previous.get() && previous->web_contents() == web_contents && 40 if (previous.get() && previous->delegate_.get() == delegate.get() &&
vabr (Chromium) 2017/04/05 14:53:34 The same delegate implies the same web_contents (d
39 previous->delegate_.get() == delegate.get() &&
40 previous->container_view() == container_view && 41 previous->container_view() == container_view &&
41 previous->element_bounds() == element_bounds) { 42 previous->element_bounds() == element_bounds) {
42 previous->ClearState(); 43 previous->ClearState();
43 return previous; 44 return previous;
44 } 45 }
45 46
46 if (previous.get()) 47 if (previous.get())
47 previous->Hide(); 48 previous->Hide();
48 49
49 AutofillPopupControllerImpl* controller = 50 AutofillPopupControllerImpl* controller =
50 new AutofillPopupControllerImpl( 51 new AutofillPopupControllerImpl(
51 delegate, web_contents, container_view, element_bounds, 52 delegate, web_contents, container_view, element_bounds,
52 text_direction); 53 text_direction);
53 return controller->GetWeakPtr(); 54 return controller->GetWeakPtr();
54 } 55 }
55 56
56 AutofillPopupControllerImpl::AutofillPopupControllerImpl( 57 AutofillPopupControllerImpl::AutofillPopupControllerImpl(
57 base::WeakPtr<AutofillPopupDelegate> delegate, 58 base::WeakPtr<AutofillPopupDelegate> delegate,
58 content::WebContents* web_contents, 59 content::WebContents* web_contents,
59 gfx::NativeView container_view, 60 gfx::NativeView container_view,
60 const gfx::RectF& element_bounds, 61 const gfx::RectF& element_bounds,
61 base::i18n::TextDirection text_direction) 62 base::i18n::TextDirection text_direction)
62 : controller_common_(new PopupControllerCommon(element_bounds, 63 : controller_common_(element_bounds, text_direction, container_view),
63 text_direction,
64 container_view,
65 web_contents)),
66 view_(NULL), 64 view_(NULL),
67 layout_model_(this, delegate->IsCreditCardPopup()), 65 layout_model_(this, delegate->IsCreditCardPopup()),
68 delegate_(delegate), 66 delegate_(delegate),
69 weak_ptr_factory_(this) { 67 weak_ptr_factory_(this) {
70 ClearState(); 68 ClearState();
71 controller_common_->SetKeyPressCallback(
72 base::Bind(&AutofillPopupControllerImpl::HandleKeyPressEvent,
73 base::Unretained(this)));
74 } 69 }
75 70
76 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() {} 71 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() {}
77 72
78 void AutofillPopupControllerImpl::Show( 73 void AutofillPopupControllerImpl::Show(
79 const std::vector<autofill::Suggestion>& suggestions) { 74 const std::vector<autofill::Suggestion>& suggestions) {
80 SetValues(suggestions); 75 SetValues(suggestions);
81 DCHECK_EQ(suggestions_.size(), elided_values_.size()); 76 DCHECK_EQ(suggestions_.size(), elided_values_.size());
82 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); 77 DCHECK_EQ(suggestions_.size(), elided_labels_.size());
83 78
(...skipping 26 matching lines...) Expand all
110 105
111 if (just_created) { 106 if (just_created) {
112 view_->Show(); 107 view_->Show();
113 } else { 108 } else {
114 if (selected_line_ && *selected_line_ >= GetLineCount()) 109 if (selected_line_ && *selected_line_ >= GetLineCount())
115 selected_line_.reset(); 110 selected_line_.reset();
116 111
117 OnSuggestionsChanged(); 112 OnSuggestionsChanged();
118 } 113 }
119 114
120 controller_common_->RegisterKeyPressCallback(); 115 static_cast<ContentAutofillDriver*>(delegate_->GetAutofillDriver())
116 ->RegisterKeyPressHandler(
117 base::Bind(&AutofillPopupControllerImpl::HandleKeyPressEvent,
118 base::Unretained(this)));
121 delegate_->OnPopupShown(); 119 delegate_->OnPopupShown();
122 120
123 DCHECK_EQ(suggestions_.size(), elided_values_.size()); 121 DCHECK_EQ(suggestions_.size(), elided_values_.size());
124 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); 122 DCHECK_EQ(suggestions_.size(), elided_labels_.size());
125 } 123 }
126 124
127 void AutofillPopupControllerImpl::UpdateDataListValues( 125 void AutofillPopupControllerImpl::UpdateDataListValues(
128 const std::vector<base::string16>& values, 126 const std::vector<base::string16>& values,
129 const std::vector<base::string16>& labels) { 127 const std::vector<base::string16>& labels) {
130 DCHECK_EQ(suggestions_.size(), elided_values_.size()); 128 DCHECK_EQ(suggestions_.size(), elided_values_.size());
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 elided_values_[i] = values[i]; 180 elided_values_[i] = values[i];
183 elided_labels_[i] = labels[i]; 181 elided_labels_[i] = labels[i];
184 } 182 }
185 183
186 OnSuggestionsChanged(); 184 OnSuggestionsChanged();
187 DCHECK_EQ(suggestions_.size(), elided_values_.size()); 185 DCHECK_EQ(suggestions_.size(), elided_values_.size());
188 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); 186 DCHECK_EQ(suggestions_.size(), elided_labels_.size());
189 } 187 }
190 188
191 void AutofillPopupControllerImpl::Hide() { 189 void AutofillPopupControllerImpl::Hide() {
192 controller_common_->RemoveKeyPressCallback(); 190 if (delegate_) {
193 if (delegate_)
194 delegate_->OnPopupHidden(); 191 delegate_->OnPopupHidden();
192 static_cast<ContentAutofillDriver*>(delegate_->GetAutofillDriver())
193 ->RemoveKeyPressHandler();
194 }
195 195
196 if (view_) 196 if (view_)
197 view_->Hide(); 197 view_->Hide();
198 198
199 delete this; 199 delete this;
200 } 200 }
201 201
202 void AutofillPopupControllerImpl::ViewDestroyed() { 202 void AutofillPopupControllerImpl::ViewDestroyed() {
203 // The view has already been destroyed so clear the reference to it. 203 // The view has already been destroyed so clear the reference to it.
204 view_ = NULL; 204 view_ = NULL;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 void AutofillPopupControllerImpl::AcceptSuggestion(int index) { 280 void AutofillPopupControllerImpl::AcceptSuggestion(int index) {
281 const autofill::Suggestion& suggestion = suggestions_[index]; 281 const autofill::Suggestion& suggestion = suggestions_[index];
282 delegate_->DidAcceptSuggestion(suggestion.value, suggestion.frontend_id, 282 delegate_->DidAcceptSuggestion(suggestion.value, suggestion.frontend_id,
283 index); 283 index);
284 } 284 }
285 285
286 gfx::Rect AutofillPopupControllerImpl::popup_bounds() const { 286 gfx::Rect AutofillPopupControllerImpl::popup_bounds() const {
287 return layout_model_.popup_bounds(); 287 return layout_model_.popup_bounds();
288 } 288 }
289 289
290 content::WebContents* AutofillPopupControllerImpl::web_contents() {
291 return controller_common_->web_contents();
292 }
293
294 gfx::NativeView AutofillPopupControllerImpl::container_view() { 290 gfx::NativeView AutofillPopupControllerImpl::container_view() {
295 return controller_common_->container_view(); 291 return controller_common_.container_view;
296 } 292 }
297 293
298 const gfx::RectF& AutofillPopupControllerImpl::element_bounds() const { 294 const gfx::RectF& AutofillPopupControllerImpl::element_bounds() const {
299 return controller_common_->element_bounds(); 295 return controller_common_.element_bounds;
300 } 296 }
301 297
302 bool AutofillPopupControllerImpl::IsRTL() const { 298 bool AutofillPopupControllerImpl::IsRTL() const {
303 return controller_common_->is_rtl(); 299 return controller_common_.text_direction == base::i18n::RIGHT_TO_LEFT;
304 } 300 }
305 301
306 const std::vector<autofill::Suggestion> 302 const std::vector<autofill::Suggestion>
307 AutofillPopupControllerImpl::GetSuggestions() { 303 AutofillPopupControllerImpl::GetSuggestions() {
308 return suggestions_; 304 return suggestions_;
309 } 305 }
310 306
311 #if !defined(OS_ANDROID) 307 #if !defined(OS_ANDROID)
312 int AutofillPopupControllerImpl::GetElidedValueWidthForRow(int row) { 308 int AutofillPopupControllerImpl::GetElidedValueWidthForRow(int row) {
313 return gfx::GetStringWidth(GetElidedValueAt(row), 309 return gfx::GetStringWidth(GetElidedValueAt(row),
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 // Don't clear view_, because otherwise the popup will have to get regenerated 508 // Don't clear view_, because otherwise the popup will have to get regenerated
513 // and this will cause flickering. 509 // and this will cause flickering.
514 suggestions_.clear(); 510 suggestions_.clear();
515 elided_values_.clear(); 511 elided_values_.clear();
516 elided_labels_.clear(); 512 elided_labels_.clear();
517 513
518 selected_line_.reset(); 514 selected_line_.reset();
519 } 515 }
520 516
521 } // namespace autofill 517 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698