password_form_helper.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_PASSWORD_FORM_HELPER_H_
  5. #define COMPONENTS_PASSWORD_MANAGER_IOS_PASSWORD_FORM_HELPER_H_
  6. #import <Foundation/Foundation.h>
  7. #include "base/memory/ref_counted_memory.h"
  8. #include "components/autofill/core/common/unique_ids.h"
  9. #import "components/autofill/ios/form_util/form_activity_observer_bridge.h"
  10. #import "ios/web/public/web_state_observer_bridge.h"
  11. #include "url/gurl.h"
  12. NS_ASSUME_NONNULL_BEGIN
  13. @class PasswordFormHelper;
  14. namespace autofill {
  15. class FieldDataManager;
  16. struct FormData;
  17. struct PasswordFormFillData;
  18. } // namespace autofill
  19. namespace password_manager {
  20. struct FillData;
  21. // Returns true if the trust level for the current page URL of |web_state| is
  22. // kAbsolute. If |page_url| is not null, fills it with the current page URL.
  23. bool GetPageURLAndCheckTrustLevel(web::WebState* web_state,
  24. GURL* __nullable page_url);
  25. } // namespace password_manager
  26. namespace web {
  27. class WebState;
  28. } // namespace web
  29. // A protocol implemented by a delegate of PasswordFormHelper.
  30. @protocol PasswordFormHelperDelegate
  31. // Called when the password form is submitted.
  32. - (void)formHelper:(PasswordFormHelper*)formHelper
  33. didSubmitForm:(const autofill::FormData&)form
  34. inMainFrame:(BOOL)inMainFrame;
  35. @end
  36. // Handles common form processing logic of password controller for both
  37. // ios/chrome and ios/web_view.
  38. // TODO(crbug.com/1097353): Consider folding this class into
  39. // SharedPasswordController.
  40. @interface PasswordFormHelper
  41. : NSObject<FormActivityObserver, CRWWebStateObserver>
  42. // Last committed URL of current web state.
  43. // Returns empty URL if current web state is not available.
  44. @property(nonatomic, readonly) const GURL& lastCommittedURL;
  45. // Maps UniqueFieldId of an input element to the pair of:
  46. // 1) The most recent text that user typed or PasswordManager autofilled in
  47. // input elements. Used for storing username/password before JavaScript
  48. // changes them.
  49. // 2) Field properties mask, i.e. whether the field was autofilled, modified
  50. // by user, etc. (see FieldPropertiesMask).
  51. @property(nonatomic, readonly) scoped_refptr<autofill::FieldDataManager>
  52. fieldDataManager;
  53. // The associated delegate.
  54. @property(nonatomic, nullable, weak) id<PasswordFormHelperDelegate> delegate;
  55. // Uses JavaScript to find password forms. Calls |completionHandler| with the
  56. // extracted information used for matching and saving passwords. Calls
  57. // |completionHandler| with an empty vector if no password forms are found.
  58. - (void)findPasswordFormsWithCompletionHandler:
  59. (nullable void (^)(const std::vector<autofill::FormData>&,
  60. uint32_t))completionHandler;
  61. // Autofills credentials into the page. Credentials and input fields are
  62. // specified by |formData|. Invokes |completionHandler| when finished with YES
  63. // if successful and NO otherwise.
  64. // TODO(crbug.com/1344776) after the cross origin iframe support is implemented
  65. // to trigger filling directly in |frame| instead of triggering the filling
  66. // recursively from the main frame.
  67. - (void)fillPasswordForm:(const autofill::PasswordFormFillData&)formData
  68. inFrame:(web::WebFrame*)frame
  69. completionHandler:(nullable void (^)(BOOL))completionHandler;
  70. // Fills new password field for (optional as @"") |newPasswordIdentifier| and
  71. // for (optional as @"") confirm password field |confirmPasswordIdentifier| in
  72. // the form identified by |formData|. Invokes |completionHandler| with true if
  73. // any fields were filled, false otherwise.
  74. // TODO(crbug.com/1344776) after the cross origin iframe support is implemented
  75. // to trigger filling directly in |frame| instead of triggering the filling
  76. // recursively from the main frame.
  77. - (void)fillPasswordForm:(autofill::FormRendererId)formIdentifier
  78. inFrame:(web::WebFrame*)frame
  79. newPasswordIdentifier:(autofill::FieldRendererId)newPasswordIdentifier
  80. confirmPasswordIdentifier:
  81. (autofill::FieldRendererId)confirmPasswordIdentifier
  82. generatedPassword:(NSString*)generatedPassword
  83. completionHandler:(nullable void (^)(BOOL))completionHandler;
  84. // Autofills credentials into the page on credential suggestion selection.
  85. // Credentials and input fields are specified by |fillData|. |uniqueFieldID|
  86. // specifies the unput on which the suggestion was accepted. Invokes
  87. // |completionHandler| when finished with YES if successful and NO otherwise.
  88. // TODO(crbug.com/1344776) after the cross origin iframe support is implemented
  89. // to trigger filling directly in |frame| instead of triggering the filling
  90. // recursively from the main frame.
  91. - (void)fillPasswordFormWithFillData:(const password_manager::FillData&)fillData
  92. inFrame:(web::WebFrame*)frame
  93. triggeredOnField:(autofill::FieldRendererId)uniqueFieldID
  94. completionHandler:(nullable void (^)(BOOL))completionHandler;
  95. // Finds the password form with unique ID |formIdentifier| and calls
  96. // |completionHandler| with the populated |FormData| data structure. |found| is
  97. // YES if the current form was found successfully, NO otherwise.
  98. - (void)extractPasswordFormData:(autofill::FormRendererId)formIdentifier
  99. completionHandler:
  100. (void (^)(BOOL found,
  101. const autofill::FormData& form))completionHandler;
  102. // Sets up the next available numeric value for setting unique renderer ids
  103. // in |frame|.
  104. - (void)setUpForUniqueIDsWithInitialState:(uint32_t)nextAvailableID
  105. inFrame:(web::WebFrame*)frame;
  106. // Updates the stored field data. In case if there is a presaved credential it
  107. // updates the presaved credential.
  108. - (void)updateFieldDataOnUserInput:(autofill::FieldRendererId)field_id
  109. inputValue:(NSString*)field_value;
  110. // Creates a instance with the given |webState|.
  111. - (instancetype)initWithWebState:(web::WebState*)webState
  112. NS_DESIGNATED_INITIALIZER;
  113. - (instancetype)init NS_UNAVAILABLE;
  114. @end
  115. NS_ASSUME_NONNULL_END
  116. #endif // COMPONENTS_PASSWORD_MANAGER_IOS_PASSWORD_FORM_HELPER_H_