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

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

Issue 772253003: Create an autofill Suggestion class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self review 2 Created 6 years 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_dialog_controller_impl.h" 5 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
(...skipping 2110 matching lines...) Expand 10 before | Expand all | Expand 10 after
2121 return; 2121 return;
2122 } 2122 }
2123 2123
2124 // If the user clicks while the popup is already showing, be sure to hide 2124 // If the user clicks while the popup is already showing, be sure to hide
2125 // it. 2125 // it.
2126 if (!was_edit && popup_controller_.get()) { 2126 if (!was_edit && popup_controller_.get()) {
2127 HidePopup(); 2127 HidePopup();
2128 return; 2128 return;
2129 } 2129 }
2130 2130
2131 std::vector<base::string16> popup_values, popup_labels, popup_icons; 2131 std::vector<autofill::Suggestion> popup_suggestions;
2132 if (common::IsCreditCardType(type)) { 2132 if (common::IsCreditCardType(type)) {
2133 GetManager()->GetCreditCardSuggestions(AutofillType(type), 2133 GetManager()->GetCreditCardSuggestions(AutofillType(type),
2134 field_contents, 2134 field_contents,
2135 &popup_values, 2135 &popup_suggestions);
2136 &popup_labels, 2136 for (size_t i = 0; i < popup_suggestions.size(); i++)
Evan Stade 2014/12/10 23:11:14 range based loop?
2137 &popup_icons, 2137 popup_suggestion_ids_.push_back(popup_suggestions[i].backend_id);
2138 &popup_guids_);
2139 } else { 2138 } else {
2140 GetManager()->GetProfileSuggestions( 2139 GetManager()->GetProfileSuggestions(
2141 AutofillType(type), 2140 AutofillType(type),
2142 field_contents, 2141 field_contents,
2143 false, 2142 false,
2144 RequestedTypesForSection(section), 2143 RequestedTypesForSection(section),
2145 base::Bind(&AutofillDialogControllerImpl::ShouldSuggestProfile, 2144 base::Bind(&AutofillDialogControllerImpl::ShouldSuggestProfile,
2146 base::Unretained(this), section), 2145 base::Unretained(this), section),
2147 &popup_values, 2146 &popup_suggestions);
2148 &popup_labels, 2147 for (size_t i = 0; i < popup_suggestions.size(); i++)
2149 &popup_icons, 2148 popup_suggestion_ids_.push_back(popup_suggestions[i].backend_id);
2150 &popup_guids_);
2151 2149
2152 GetI18nValidatorSuggestions(section, type, &popup_values, &popup_labels, 2150 // This will append to the popup_suggestions but not the IDs since there
2153 &popup_icons); 2151 // are no backend IDs for the I18N validator suggestions.
2152 GetI18nValidatorSuggestions(section, type, &popup_suggestions);
2154 } 2153 }
2155 2154
2156 if (popup_values.empty()) { 2155 if (popup_suggestions.empty()) {
2157 HidePopup(); 2156 HidePopup();
2158 return; 2157 return;
2159 } 2158 }
2160 2159
2161 // |popup_input_type_| must be set before calling |Show()|. 2160 // |popup_input_type_| must be set before calling |Show()|.
2162 popup_input_type_ = type; 2161 popup_input_type_ = type;
2163 popup_section_ = section; 2162 popup_section_ = section;
2164 2163
2164 // Use our own 0-based IDs for the items.
2165 // TODO(estade): do we need separators and control rows like 'Clear 2165 // TODO(estade): do we need separators and control rows like 'Clear
2166 // Form'? 2166 // Form'?
2167 std::vector<int> popup_ids; 2167 for (size_t i = 0; i < popup_suggestions.size(); ++i) {
2168 for (size_t i = 0; i < popup_values.size(); ++i) { 2168 popup_suggestions[i].frontend_id = i;
2169 popup_ids.push_back(i);
2170 } 2169 }
2171 2170
2172 popup_controller_ = AutofillPopupControllerImpl::GetOrCreate( 2171 popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
2173 popup_controller_, 2172 popup_controller_,
2174 weak_ptr_factory_.GetWeakPtr(), 2173 weak_ptr_factory_.GetWeakPtr(),
2175 NULL, 2174 NULL,
2176 parent_view, 2175 parent_view,
2177 content_bounds, 2176 content_bounds,
2178 base::i18n::IsRTL() ? 2177 base::i18n::IsRTL() ?
2179 base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT); 2178 base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT);
2180 popup_controller_->Show(popup_values, 2179 popup_controller_->Show(popup_suggestions);
2181 popup_labels,
2182 popup_icons,
2183 popup_ids);
2184 } 2180 }
2185 2181
2186 void AutofillDialogControllerImpl::FocusMoved() { 2182 void AutofillDialogControllerImpl::FocusMoved() {
2187 HidePopup(); 2183 HidePopup();
2188 } 2184 }
2189 2185
2190 bool AutofillDialogControllerImpl::ShouldShowErrorBubble() const { 2186 bool AutofillDialogControllerImpl::ShouldShowErrorBubble() const {
2191 return popup_input_type_ == UNKNOWN_TYPE; 2187 return popup_input_type_ == UNKNOWN_TYPE;
2192 } 2188 }
2193 2189
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
2407 const base::string16& value, 2403 const base::string16& value,
2408 int identifier) { 2404 int identifier) {
2409 DCHECK_NE(UNKNOWN_TYPE, popup_input_type_); 2405 DCHECK_NE(UNKNOWN_TYPE, popup_input_type_);
2410 // Because |HidePopup()| can be called from |UpdateSection()|, remember the 2406 // Because |HidePopup()| can be called from |UpdateSection()|, remember the
2411 // type of the input for later here. 2407 // type of the input for later here.
2412 const ServerFieldType popup_input_type = popup_input_type_; 2408 const ServerFieldType popup_input_type = popup_input_type_;
2413 2409
2414 ScopedViewUpdates updates(view_.get()); 2410 ScopedViewUpdates updates(view_.get());
2415 scoped_ptr<DataModelWrapper> wrapper; 2411 scoped_ptr<DataModelWrapper> wrapper;
2416 2412
2417 if (static_cast<size_t>(identifier) < popup_guids_.size()) { 2413 if (static_cast<size_t>(identifier) < popup_suggestion_ids_.size()) {
2418 const PersonalDataManager::GUIDPair& pair = popup_guids_[identifier]; 2414 const SuggestionBackendID& sid = popup_suggestion_ids_[identifier];
2419 if (common::IsCreditCardType(popup_input_type)) { 2415 if (common::IsCreditCardType(popup_input_type)) {
2420 wrapper.reset(new AutofillCreditCardWrapper( 2416 wrapper.reset(new AutofillCreditCardWrapper(
2421 GetManager()->GetCreditCardByGUID(pair.first))); 2417 GetManager()->GetCreditCardByGUID(sid.guid)));
2422 } else { 2418 } else {
2423 wrapper.reset(new AutofillProfileWrapper( 2419 wrapper.reset(new AutofillProfileWrapper(
2424 GetManager()->GetProfileByGUID(pair.first), 2420 GetManager()->GetProfileByGUID(sid.guid),
2425 AutofillType(popup_input_type), 2421 AutofillType(popup_input_type),
2426 pair.second)); 2422 sid.variant));
2427 } 2423 }
2428 } else { 2424 } else {
2429 wrapper.reset(new I18nAddressDataWrapper( 2425 wrapper.reset(new I18nAddressDataWrapper(
2430 &i18n_validator_suggestions_[identifier - popup_guids_.size()])); 2426 &i18n_validator_suggestions_[
2427 identifier - popup_suggestion_ids_.size()]));
2431 } 2428 }
2432 2429
2433 // If the user hasn't switched away from the default country and |wrapper|'s 2430 // If the user hasn't switched away from the default country and |wrapper|'s
2434 // country differs from the |view_|'s, rebuild inputs and restore user data. 2431 // country differs from the |view_|'s, rebuild inputs and restore user data.
2435 const FieldValueMap snapshot = TakeUserInputSnapshot(); 2432 const FieldValueMap snapshot = TakeUserInputSnapshot();
2436 bool billing_rebuilt = false, shipping_rebuilt = false; 2433 bool billing_rebuilt = false, shipping_rebuilt = false;
2437 2434
2438 base::string16 billing_country = 2435 base::string16 billing_country =
2439 wrapper->GetInfo(AutofillType(ADDRESS_BILLING_COUNTRY)); 2436 wrapper->GetInfo(AutofillType(ADDRESS_BILLING_COUNTRY));
2440 if (popup_section_ == ActiveBillingSection() && 2437 if (popup_section_ == ActiveBillingSection() &&
(...skipping 945 matching lines...) Expand 10 before | Expand all | Expand 10 after
3386 3383
3387 if (section == SECTION_SHIPPING) 3384 if (section == SECTION_SHIPPING)
3388 return shipping_country_combobox_model_.get(); 3385 return shipping_country_combobox_model_.get();
3389 3386
3390 return NULL; 3387 return NULL;
3391 } 3388 }
3392 3389
3393 void AutofillDialogControllerImpl::GetI18nValidatorSuggestions( 3390 void AutofillDialogControllerImpl::GetI18nValidatorSuggestions(
3394 DialogSection section, 3391 DialogSection section,
3395 ServerFieldType type, 3392 ServerFieldType type,
3396 std::vector<base::string16>* popup_values, 3393 std::vector<autofill::Suggestion>* popup_suggestions) {
3397 std::vector<base::string16>* popup_labels,
3398 std::vector<base::string16>* popup_icons) {
3399 AddressField focused_field; 3394 AddressField focused_field;
3400 if (!i18n::FieldForType(type, &focused_field)) 3395 if (!i18n::FieldForType(type, &focused_field))
3401 return; 3396 return;
3402 3397
3403 FieldValueMap inputs; 3398 FieldValueMap inputs;
3404 view_->GetUserInput(section, &inputs); 3399 view_->GetUserInput(section, &inputs);
3405 3400
3406 AutofillProfile profile; 3401 AutofillProfile profile;
3407 FillFormGroupFromOutputs(inputs, &profile); 3402 FillFormGroupFromOutputs(inputs, &profile);
3408 3403
3409 scoped_ptr<AddressData> user_input = 3404 scoped_ptr<AddressData> user_input =
3410 i18n::CreateAddressDataFromAutofillProfile( 3405 i18n::CreateAddressDataFromAutofillProfile(
3411 profile, g_browser_process->GetApplicationLocale()); 3406 profile, g_browser_process->GetApplicationLocale());
3412 user_input->language_code = AddressLanguageCodeForSection(section); 3407 user_input->language_code = AddressLanguageCodeForSection(section);
3413 3408
3414 static const size_t kSuggestionsLimit = 10; 3409 static const size_t kSuggestionsLimit = 10;
3415 AddressValidator::Status status = GetValidator()->GetSuggestions( 3410 AddressValidator::Status status = GetValidator()->GetSuggestions(
3416 *user_input, focused_field, kSuggestionsLimit, 3411 *user_input, focused_field, kSuggestionsLimit,
3417 &i18n_validator_suggestions_); 3412 &i18n_validator_suggestions_);
3418 3413
3419 if (status != AddressValidator::SUCCESS) 3414 if (status != AddressValidator::SUCCESS)
3420 return; 3415 return;
3421 3416
3422 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) { 3417 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) {
3423 popup_values->push_back(base::UTF8ToUTF16( 3418 popup_suggestions->push_back(autofill::Suggestion(
3424 i18n_validator_suggestions_[i].GetFieldValue(focused_field))); 3419 base::UTF8ToUTF16(
3420 i18n_validator_suggestions_[i].GetFieldValue(focused_field))));
3425 3421
3426 // Disambiguate the suggestion by showing the smallest administrative 3422 // Disambiguate the suggestion by showing the smallest administrative
3427 // region of the suggested address: 3423 // region of the suggested address:
3428 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY 3424 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY
3429 popup_labels->push_back(base::string16());
3430 for (int field = DEPENDENT_LOCALITY; field >= ADMIN_AREA; --field) { 3425 for (int field = DEPENDENT_LOCALITY; field >= ADMIN_AREA; --field) {
3431 const std::string& field_value = 3426 const std::string& field_value =
3432 i18n_validator_suggestions_[i].GetFieldValue( 3427 i18n_validator_suggestions_[i].GetFieldValue(
3433 static_cast<AddressField>(field)); 3428 static_cast<AddressField>(field));
3434 if (focused_field != field && !field_value.empty()) { 3429 if (focused_field != field && !field_value.empty()) {
3435 popup_labels->back().assign(base::UTF8ToUTF16(field_value)); 3430 popup_suggestions->back().label = base::UTF8ToUTF16(field_value);
3436 break; 3431 break;
3437 } 3432 }
3438 } 3433 }
3439 } 3434 }
3440 popup_icons->resize(popup_values->size());
3441 } 3435 }
3442 3436
3443 DetailInputs* AutofillDialogControllerImpl::MutableRequestedFieldsForSection( 3437 DetailInputs* AutofillDialogControllerImpl::MutableRequestedFieldsForSection(
3444 DialogSection section) { 3438 DialogSection section) {
3445 return const_cast<DetailInputs*>(&RequestedFieldsForSection(section)); 3439 return const_cast<DetailInputs*>(&RequestedFieldsForSection(section));
3446 } 3440 }
3447 3441
3448 std::string* AutofillDialogControllerImpl::MutableAddressLanguageCodeForSection( 3442 std::string* AutofillDialogControllerImpl::MutableAddressLanguageCodeForSection(
3449 DialogSection section) { 3443 DialogSection section) {
3450 switch (section) { 3444 switch (section) {
(...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after
4155 view_->UpdateButtonStrip(); 4149 view_->UpdateButtonStrip();
4156 } 4150 }
4157 4151
4158 void AutofillDialogControllerImpl::FetchWalletCookie() { 4152 void AutofillDialogControllerImpl::FetchWalletCookie() {
4159 net::URLRequestContextGetter* request_context = profile_->GetRequestContext(); 4153 net::URLRequestContextGetter* request_context = profile_->GetRequestContext();
4160 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context)); 4154 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context));
4161 signin_helper_->StartWalletCookieValueFetch(); 4155 signin_helper_->StartWalletCookieValueFetch();
4162 } 4156 }
4163 4157
4164 } // namespace autofill 4158 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698