1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_CLIENT_H_ 6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_CLIENT_H_ 7 8 #include <memory> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "base/callback_forward.h" 14 #include "base/containers/span.h" 15 #include "base/i18n/rtl.h" 16 #include "base/memory/weak_ptr.h" 17 #include "base/strings/string16.h" 18 #include "base/util/type_safety/strong_alias.h" 19 #include "base/values.h" 20 #include "build/build_config.h" 21 #include "components/autofill/core/browser/payments/legal_message_line.h" 22 #include "components/autofill/core/browser/payments/risk_data_loader.h" 23 #include "components/autofill/core/browser/ui/popup_item_ids.h" 24 #include "components/autofill/core/browser/ui/popup_types.h" 25 #include "components/security_state/core/security_state.h" 26 #include "components/translate/core/browser/language_state.h" 27 #include "services/metrics/public/cpp/ukm_source_id.h" 28 #include "ui/base/window_open_disposition.h" 29 #include "ui/gfx/geometry/rect_f.h" 30 #include "url/gurl.h" 31 32 #if !defined(OS_IOS) 33 #include "components/autofill/core/browser/payments/internal_authenticator.h" 34 #endif 35 36 class PrefService; 37 38 namespace content { 39 class RenderFrameHost; 40 } 41 42 namespace signin { 43 class IdentityManager; 44 } 45 46 namespace syncer { 47 class SyncService; 48 } 49 50 namespace ukm { 51 class UkmRecorder; 52 } 53 54 namespace version_info { 55 enum class Channel; 56 } 57 58 namespace autofill { 59 60 class AddressNormalizer; 61 class AutocompleteHistoryManager; 62 class AutofillOfferManager; 63 class AutofillPopupDelegate; 64 class CardUnmaskDelegate; 65 class CreditCard; 66 class FormDataImporter; 67 class FormStructure; 68 class LogManager; 69 class MigratableCreditCard; 70 class PersonalDataManager; 71 class StrikeDatabase; 72 enum class WebauthnDialogCallbackType; 73 enum class WebauthnDialogState; 74 struct Suggestion; 75 76 namespace payments { 77 class PaymentsClient; 78 } 79 80 // A client interface that needs to be supplied to the Autofill component by the 81 // embedder. 82 // 83 // Each client instance is associated with a given context within which an 84 // AutofillManager is used (e.g. a single tab), so when we say "for the client" 85 // below, we mean "in the execution context the client is associated with" (e.g. 86 // for the tab the AutofillManager is attached to). 87 class AutofillClient : public RiskDataLoader { 88 public: 89 enum PaymentsRpcResult { 90 // Empty result. Used for initializing variables and should generally 91 // not be returned nor passed as arguments unless explicitly allowed by 92 // the API. 93 NONE, 94 95 // Request succeeded. 96 SUCCESS, 97 98 // Request failed; try again. 99 TRY_AGAIN_FAILURE, 100 101 // Request failed; don't try again. 102 PERMANENT_FAILURE, 103 104 // Unable to connect to Payments servers. Prompt user to check internet 105 // connection. 106 NETWORK_ERROR, 107 }; 108 109 enum SaveCardOfferUserDecision { 110 // The user accepted credit card save. 111 ACCEPTED, 112 113 // The user explicitly declined credit card save. 114 DECLINED, 115 116 // The user ignored the credit card save prompt. 117 IGNORED, 118 }; 119 120 enum UnmaskCardReason { 121 // The card is being unmasked for PaymentRequest. 122 UNMASK_FOR_PAYMENT_REQUEST, 123 124 // The card is being unmasked for Autofill. 125 UNMASK_FOR_AUTOFILL, 126 }; 127 128 // Authentication methods for card unmasking. 129 enum UnmaskAuthMethod { 130 UNKNOWN = 0, 131 // Require user to unmask via CVC. 132 CVC = 1, 133 // Suggest use of FIDO authenticator for card unmasking. 134 FIDO = 2, 135 }; 136 137 // Used for explicitly requesting the user to enter/confirm cardholder name, 138 // expiration date month and year. 139 struct UserProvidedCardDetails { 140 base::string16 cardholder_name; 141 base::string16 expiration_date_month; 142 base::string16 expiration_date_year; 143 }; 144 145 // Used for options of upload prompt. 146 struct SaveCreditCardOptions { with_from_dynamic_change_formSaveCreditCardOptions147 SaveCreditCardOptions& with_from_dynamic_change_form(bool b) { 148 from_dynamic_change_form = b; 149 return *this; 150 } 151 with_has_non_focusable_fieldSaveCreditCardOptions152 SaveCreditCardOptions& with_has_non_focusable_field(bool b) { 153 has_non_focusable_field = b; 154 return *this; 155 } 156 with_should_request_name_from_userSaveCreditCardOptions157 SaveCreditCardOptions& with_should_request_name_from_user(bool b) { 158 should_request_name_from_user = b; 159 return *this; 160 } 161 with_should_request_expiration_date_from_userSaveCreditCardOptions162 SaveCreditCardOptions& with_should_request_expiration_date_from_user( 163 bool b) { 164 should_request_expiration_date_from_user = b; 165 return *this; 166 } 167 168 SaveCreditCardOptions& with_show_prompt(bool b = true) { 169 show_prompt = b; 170 return *this; 171 } 172 173 bool from_dynamic_change_form = false; 174 bool has_non_focusable_field = false; 175 bool should_request_name_from_user = false; 176 bool should_request_expiration_date_from_user = false; 177 bool show_prompt = false; 178 }; 179 180 // Required arguments to create a dropdown showing autofill suggestions. 181 struct PopupOpenArgs { 182 using AutoselectFirstSuggestion = 183 ::util::StrongAlias<class AutoSelectFirstSuggestionTag, bool>; 184 185 PopupOpenArgs(); 186 PopupOpenArgs(const gfx::RectF& element_bounds, 187 base::i18n::TextDirection text_direction, 188 std::vector<autofill::Suggestion> suggestions, 189 AutoselectFirstSuggestion autoselect_first_suggestion, 190 PopupType popup_type); 191 PopupOpenArgs(const PopupOpenArgs&); 192 PopupOpenArgs(PopupOpenArgs&&); 193 ~PopupOpenArgs(); 194 PopupOpenArgs& operator=(const PopupOpenArgs&); 195 PopupOpenArgs& operator=(PopupOpenArgs&&); 196 197 gfx::RectF element_bounds; 198 base::i18n::TextDirection text_direction = 199 base::i18n::TextDirection::UNKNOWN_DIRECTION; 200 std::vector<autofill::Suggestion> suggestions; 201 AutoselectFirstSuggestion autoselect_first_suggestion{false}; 202 PopupType popup_type = PopupType::kUnspecified; 203 }; 204 205 // Callback to run after local credit card save is offered. Sends whether the 206 // prompt was accepted, declined, or ignored in |user_decision|. 207 typedef base::OnceCallback<void(SaveCardOfferUserDecision user_decision)> 208 LocalSaveCardPromptCallback; 209 210 // Callback to run after upload credit card save is offered. Sends whether the 211 // prompt was accepted, declined, or ignored in |user_decision|, and 212 // additional |user_provided_card_details| if applicable. 213 typedef base::OnceCallback<void( 214 SaveCardOfferUserDecision user_decision, 215 const UserProvidedCardDetails& user_provided_card_details)> 216 UploadSaveCardPromptCallback; 217 218 typedef base::OnceCallback<void(const CreditCard&)> CreditCardScanCallback; 219 220 // Callback to run if user presses the Save button in the migration dialog. 221 // Will pass a vector of GUIDs of cards that the user selected to upload to 222 // LocalCardMigrationManager. 223 typedef base::OnceCallback<void(const std::vector<std::string>&)> 224 LocalCardMigrationCallback; 225 226 // Callback to run if the user presses the trash can button in the 227 // action-required dialog. Will pass to LocalCardMigrationManager a 228 // string of GUID of the card that the user selected to delete from local 229 // storage. 230 typedef base::RepeatingCallback<void(const std::string&)> 231 MigrationDeleteCardCallback; 232 233 // Callback to run if the OK button or the cancel button in a 234 // Webauthn dialog is clicked. 235 typedef base::RepeatingCallback<void(WebauthnDialogCallbackType)> 236 WebauthnDialogCallback; 237 238 ~AutofillClient() override = default; 239 240 // Returns the channel for the installation. In branded builds, this will be 241 // version_info::Channel::{STABLE,BETA,DEV,CANARY}. In unbranded builds, or 242 // in branded builds when the channel cannot be determined, this will be 243 // version_info::Channel::UNKNOWN. 244 virtual version_info::Channel GetChannel() const; 245 246 // Gets the PersonalDataManager instance associated with the client. 247 virtual PersonalDataManager* GetPersonalDataManager() = 0; 248 249 // Gets the AutocompleteHistoryManager instance associate with the client. 250 virtual AutocompleteHistoryManager* GetAutocompleteHistoryManager() = 0; 251 252 // Gets the preferences associated with the client. 253 virtual PrefService* GetPrefs() = 0; 254 255 // Gets the sync service associated with the client. 256 virtual syncer::SyncService* GetSyncService() = 0; 257 258 // Gets the IdentityManager associated with the client. 259 virtual signin::IdentityManager* GetIdentityManager() = 0; 260 261 // Gets the FormDataImporter instance owned by the client. 262 virtual FormDataImporter* GetFormDataImporter() = 0; 263 264 // Gets the payments::PaymentsClient instance owned by the client. 265 virtual payments::PaymentsClient* GetPaymentsClient() = 0; 266 267 // Gets the StrikeDatabase associated with the client. 268 virtual StrikeDatabase* GetStrikeDatabase() = 0; 269 270 // Gets the UKM service associated with this client (for metrics). 271 virtual ukm::UkmRecorder* GetUkmRecorder() = 0; 272 273 // Gets the UKM source id associated with this client (for metrics). 274 virtual ukm::SourceId GetUkmSourceId() = 0; 275 276 // Gets an AddressNormalizer instance (can be null). 277 virtual AddressNormalizer* GetAddressNormalizer() = 0; 278 279 // Gets an AutofillOfferManager instance (can be null for unsupported 280 // platforms). 281 virtual AutofillOfferManager* GetAutofillOfferManager(); 282 283 // Gets the virtual URL of the last committed page of this client's 284 // associated WebContents. 285 virtual const GURL& GetLastCommittedURL() = 0; 286 287 // Gets the security level used for recording histograms for the current 288 // context if possible, SECURITY_LEVEL_COUNT otherwise. 289 virtual security_state::SecurityLevel GetSecurityLevelForUmaHistograms() = 0; 290 291 // Returns the language state, if available. 292 virtual const translate::LanguageState* GetLanguageState() = 0; 293 294 // Retrieves the country code of the user from Chrome variation service. 295 // If the variation service is not available, return an empty string. 296 virtual std::string GetVariationConfigCountryCode() const; 297 298 #if !defined(OS_IOS) 299 // Creates the appropriate implementation of InternalAuthenticator. May be 300 // null for platforms that don't support this, in which case standard CVC 301 // authentication will be used instead. 302 virtual std::unique_ptr<InternalAuthenticator> 303 CreateCreditCardInternalAuthenticator(content::RenderFrameHost* rfh); 304 #endif 305 306 // Causes the Autofill settings UI to be shown. If |show_credit_card_settings| 307 // is true, will show the credit card specific subpage. 308 virtual void ShowAutofillSettings(bool show_credit_card_settings) = 0; 309 310 // A user has attempted to use a masked card. Prompt them for further 311 // information to proceed. 312 virtual void ShowUnmaskPrompt(const CreditCard& card, 313 UnmaskCardReason reason, 314 base::WeakPtr<CardUnmaskDelegate> delegate) = 0; 315 virtual void OnUnmaskVerificationResult(PaymentsRpcResult result) = 0; 316 317 #if !defined(OS_ANDROID) && !defined(OS_IOS) 318 // Returns the list of allowed merchants and BIN ranges for virtual cards. 319 virtual std::vector<std::string> GetAllowedMerchantsForVirtualCards() = 0; 320 virtual std::vector<std::string> GetAllowedBinRangesForVirtualCards() = 0; 321 322 // Runs |show_migration_dialog_closure| if the user accepts the card migration 323 // offer. This causes the card migration dialog to be shown. 324 virtual void ShowLocalCardMigrationDialog( 325 base::OnceClosure show_migration_dialog_closure) = 0; 326 327 // Shows a dialog with the given |legal_message_lines| and the |user_email|. 328 // Runs |start_migrating_cards_callback| if the user would like the selected 329 // cards in the |migratable_credit_cards| to be uploaded to cloud. 330 virtual void ConfirmMigrateLocalCardToCloud( 331 const LegalMessageLines& legal_message_lines, 332 const std::string& user_email, 333 const std::vector<MigratableCreditCard>& migratable_credit_cards, 334 LocalCardMigrationCallback start_migrating_cards_callback) = 0; 335 336 // Will show a dialog containing a error message if |has_server_error| 337 // is true, or the migration results for cards in 338 // |migratable_credit_cards| otherwise. If migration succeeds the dialog will 339 // contain a |tip_message|. |migratable_credit_cards| will be used when 340 // constructing the dialog. The dialog is invoked when the migration process 341 // is finished. Runs |delete_local_card_callback| if the user chose to delete 342 // one invalid card from local storage. 343 virtual void ShowLocalCardMigrationResults( 344 const bool has_server_error, 345 const base::string16& tip_message, 346 const std::vector<MigratableCreditCard>& migratable_credit_cards, 347 MigrationDeleteCardCallback delete_local_card_callback) = 0; 348 349 // TODO(crbug.com/991037): Find a way to merge these two functions. Shouldn't 350 // use WebauthnDialogState as that state is a purely UI state (should not be 351 // accessible for managers?), and some of the states |KInactive| may be 352 // confusing here. Do we want to add another Enum? 353 354 // Will show a dialog offering the option to use device's platform 355 // authenticator in the future instead of CVC to verify the card being 356 // unmasked. Runs |offer_dialog_callback| if the OK button or the cancel 357 // button in the dialog is clicked. 358 virtual void ShowWebauthnOfferDialog( 359 WebauthnDialogCallback offer_dialog_callback) = 0; 360 361 // Will show a dialog indicating the card verification is in progress. It is 362 // shown after verification starts only if the WebAuthn is enabled. 363 virtual void ShowWebauthnVerifyPendingDialog( 364 WebauthnDialogCallback verify_pending_dialog_callback) = 0; 365 366 // Will update the WebAuthn dialog content when there is an error fetching the 367 // challenge. 368 virtual void UpdateWebauthnOfferDialogWithError() = 0; 369 370 // Will close the current visible WebAuthn dialog. Returns true if dialog was 371 // visible and has been closed. 372 virtual bool CloseWebauthnDialog() = 0; 373 374 // Prompt the user to confirm the saving of a UPI ID. 375 virtual void ConfirmSaveUpiIdLocally( 376 const std::string& upi_id, 377 base::OnceCallback<void(bool user_decision)> callback) = 0; 378 379 // Shows the dialog including all credit cards that are available to be used 380 // as a virtual card. |candidates| must not be empty and has at least one 381 // card. Runs |callback| when a card is selected. 382 virtual void OfferVirtualCardOptions( 383 const std::vector<CreditCard*>& candidates, 384 base::OnceCallback<void(const std::string&)> callback) = 0; 385 386 #else // defined(OS_ANDROID) || defined(OS_IOS) 387 // Display the cardholder name fix flow prompt and run the |callback| if 388 // the card should be uploaded to payments with updated name from the user. 389 virtual void ConfirmAccountNameFixFlow( 390 base::OnceCallback<void(const base::string16&)> callback) = 0; 391 392 // Display the expiration date fix flow prompt with the |card| details 393 // and run the |callback| if the card should be uploaded to payments with 394 // updated expiration date from the user. 395 virtual void ConfirmExpirationDateFixFlow( 396 const CreditCard& card, 397 base::OnceCallback<void(const base::string16&, const base::string16&)> 398 callback) = 0; 399 #endif 400 401 // Runs |callback| once the user makes a decision with respect to the 402 // offer-to-save prompt. On desktop, shows the offer-to-save bubble if 403 // |options.show_prompt| is true; otherwise only shows the 404 // omnibox icon. On mobile, shows the offer-to-save infobar if 405 // |options.show_prompt| is true; otherwise does not offer to 406 // save at all. 407 virtual void ConfirmSaveCreditCardLocally( 408 const CreditCard& card, 409 AutofillClient::SaveCreditCardOptions options, 410 LocalSaveCardPromptCallback callback) = 0; 411 412 // Runs |callback| once the user makes a decision with respect to the 413 // offer-to-save prompt. Displays the contents of |legal_message_lines| 414 // to the user. Displays a cardholder name textfield in the bubble if 415 // |options.should_request_name_from_user| is true. Displays 416 // a pair of expiration date dropdowns in the bubble if 417 // |should_request_expiration_date_from_user| is true. On desktop, shows the 418 // offer-to-save bubble if |options.show_prompt| is true; 419 // otherwise only shows the omnibox icon. On mobile, shows the offer-to-save 420 // infobar if |options.show_prompt| is true; otherwise does 421 // not offer to save at all. 422 virtual void ConfirmSaveCreditCardToCloud( 423 const CreditCard& card, 424 const LegalMessageLines& legal_message_lines, 425 SaveCreditCardOptions options, 426 UploadSaveCardPromptCallback callback) = 0; 427 428 // Called after credit card upload is finished. Will show upload result to 429 // users. |card_saved| indicates if the card is successfully saved. 430 virtual void CreditCardUploadCompleted(bool card_saved) = 0; 431 432 // Will show an infobar to get user consent for Credit Card assistive filling. 433 // Will run |callback| on success. 434 virtual void ConfirmCreditCardFillAssist(const CreditCard& card, 435 base::OnceClosure callback) = 0; 436 437 // Returns true if both the platform and the device support scanning credit 438 // cards. Should be called before ScanCreditCard(). 439 virtual bool HasCreditCardScanFeature() = 0; 440 441 // Shows the user interface for scanning a credit card. Invokes the |callback| 442 // when a credit card is scanned successfully. Should be called only if 443 // HasCreditCardScanFeature() returns true. 444 virtual void ScanCreditCard(CreditCardScanCallback callback) = 0; 445 446 // Shows an Autofill popup with the given |values|, |labels|, |icons|, and 447 // |identifiers| for the element at |element_bounds|. |delegate| will be 448 // notified of popup events. 449 virtual void ShowAutofillPopup( 450 const PopupOpenArgs& open_args, 451 base::WeakPtr<AutofillPopupDelegate> delegate) = 0; 452 453 // Update the data list values shown by the Autofill popup, if visible. 454 virtual void UpdateAutofillPopupDataListValues( 455 const std::vector<base::string16>& values, 456 const std::vector<base::string16>& labels) = 0; 457 458 // Informs the client that the popup needs to be kept alive. Call before 459 // |UpdatePopup| to update the open popup in-place. 460 virtual void PinPopupView() = 0; 461 462 // The returned arguments allow to reopen the dropdown with 463 // |ShowAutofillPopup| even if the controller is destroyed temporarily. 464 // This function ensures that the element's bounds are transformed back to the 465 // screen space-independent bounds. 466 virtual PopupOpenArgs GetReopenPopupArgs() const = 0; 467 468 // Returns (not elided) suggestions currently held by the UI. 469 virtual base::span<const Suggestion> GetPopupSuggestions() const = 0; 470 471 // Updates the popup contents with the newly given suggestions. 472 virtual void UpdatePopup(const std::vector<Suggestion>& suggestions, 473 PopupType popup_type) = 0; 474 475 // Hide the Autofill popup if one is currently showing. 476 virtual void HideAutofillPopup(PopupHidingReason reason) = 0; 477 478 // Whether the Autocomplete feature of Autofill should be enabled. 479 virtual bool IsAutocompleteEnabled() = 0; 480 481 // Pass the form structures to the password manager to choose correct username 482 // and to the password generation manager to detect account creation forms. 483 virtual void PropagateAutofillPredictions( 484 content::RenderFrameHost* rfh, 485 const std::vector<FormStructure*>& forms) = 0; 486 487 // Inform the client that the field has been filled. 488 virtual void DidFillOrPreviewField( 489 const base::string16& autofilled_value, 490 const base::string16& profile_full_name) = 0; 491 492 // If the context is secure. 493 virtual bool IsContextSecure() = 0; 494 495 // Whether it is appropriate to show a signin promo for this user. 496 virtual bool ShouldShowSigninPromo() = 0; 497 498 // Whether server side cards are supported by the client. If false, only 499 // local cards will be shown. 500 virtual bool AreServerCardsSupported() = 0; 501 502 // Handles simple actions for the autofill popups. 503 virtual void ExecuteCommand(int id) = 0; 504 505 // Returns a LogManager instance. May be null for platforms that don't support 506 // this. 507 virtual LogManager* GetLogManager() const; 508 509 #if defined(OS_IOS) 510 // Checks whether the qurrent query is the most recent one. 511 virtual bool IsQueryIDRelevant(int query_id) = 0; 512 #endif 513 }; 514 515 } // namespace autofill 516 517 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_CLIENT_H_ 518