account_select_fill_data.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2018 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. #ifndef COMPONENTS_PASSWORD_MANAGER_IOS_ACCOUNT_SELECT_FILL_DATA_H_
  5. #define COMPONENTS_PASSWORD_MANAGER_IOS_ACCOUNT_SELECT_FILL_DATA_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "components/autofill/core/common/unique_ids.h"
  11. #include "url/gurl.h"
  12. namespace autofill {
  13. struct PasswordFormFillData;
  14. }
  15. namespace password_manager {
  16. struct UsernameAndRealm {
  17. std::u16string username;
  18. std::string realm;
  19. };
  20. // Keeps all required for filling information from Password Form.
  21. struct FormInfo {
  22. FormInfo();
  23. ~FormInfo();
  24. FormInfo(const FormInfo&);
  25. GURL origin;
  26. autofill::FormRendererId form_id;
  27. autofill::FieldRendererId username_element_id;
  28. autofill::FieldRendererId password_element_id;
  29. };
  30. struct Credential {
  31. Credential(const std::u16string& username,
  32. const std::u16string& password,
  33. const std::string& realm);
  34. ~Credential();
  35. std::u16string username;
  36. std::u16string password;
  37. std::string realm;
  38. };
  39. // Contains all information whis is required for filling the password form.
  40. // TODO(crbug.com/1075444): Remove form name and field identifiers once
  41. // unique IDs are used in filling.
  42. struct FillData {
  43. FillData();
  44. ~FillData();
  45. GURL origin;
  46. autofill::FormRendererId form_id;
  47. autofill::FieldRendererId username_element_id;
  48. std::u16string username_value;
  49. autofill::FieldRendererId password_element_id;
  50. std::u16string password_value;
  51. };
  52. // Handles data and logic for filling on account select. This class stores 2
  53. // types of independent data - forms on the page and credentials saved for the
  54. // current page. Based on the user action (clicks, typing values, choosing
  55. // suggestions) this class decides which suggestions should be shown and which
  56. // credentials should be filled.
  57. class AccountSelectFillData {
  58. public:
  59. AccountSelectFillData();
  60. AccountSelectFillData(const AccountSelectFillData&) = delete;
  61. AccountSelectFillData& operator=(const AccountSelectFillData&) = delete;
  62. ~AccountSelectFillData();
  63. // Adds form structure from |form_data| to internal lists of known forms and
  64. // overrides known credentials with credentials from |form_data|. So only the
  65. // credentials from the latest |form_data| will be shown to the user.
  66. void Add(const autofill::PasswordFormFillData& form_data);
  67. void Reset();
  68. bool Empty() const;
  69. // Returns whether suggestions are available for field with id
  70. // |field_identifier| which is in the form with id |form_identifier|.
  71. bool IsSuggestionsAvailable(autofill::FormRendererId form_identifier,
  72. autofill::FieldRendererId field_identifier,
  73. bool is_password_field) const;
  74. // Returns suggestions for field with id |field_identifier| which is in the
  75. // form with id |form_identifier|.
  76. std::vector<UsernameAndRealm> RetrieveSuggestions(
  77. autofill::FormRendererId form_identifier,
  78. autofill::FieldRendererId field_identifier,
  79. bool is_password_field);
  80. // Returns data for password form filling based on |username| chosen by the
  81. // user.
  82. // RetrieveSuggestions should be called before in order to specify on which
  83. // field the user clicked.
  84. std::unique_ptr<FillData> GetFillData(const std::u16string& username) const;
  85. private:
  86. // Keeps data about all known forms. The key is the pair (form_id, username
  87. // field_name).
  88. std::map<autofill::FormRendererId, FormInfo> forms_;
  89. // Keeps all known credentials.
  90. std::vector<Credential> credentials_;
  91. // Mutable because it's updated from RetrieveSuggestions, which is logically
  92. // should be const.
  93. // Keeps information about last form that was requested in
  94. // RetrieveSuggestions.
  95. mutable const FormInfo* last_requested_form_ = nullptr;
  96. // Keeps id of the last requested field if it was password otherwise the empty
  97. // string.
  98. autofill::FieldRendererId last_requested_password_field_id_;
  99. // Returns form information from |forms_| that has id |form_identifier|.
  100. // If |is_password_field| == false and |field_identifier| is not equal to
  101. // form username_element null is returned. If |is_password_field| == true then
  102. // |field_identifier| is ignored. That corresponds to the logic, that
  103. // suggestions should be shown on any password fields.
  104. const FormInfo* GetFormInfo(autofill::FormRendererId form_identifier,
  105. autofill::FieldRendererId field_identifier,
  106. bool is_password_field) const;
  107. };
  108. } // namespace password_manager
  109. #endif // COMPONENTS_PASSWORD_MANAGER_IOS_ACCOUNT_SELECT_FILL_DATA_H_