password_manager_java_script_feature.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2021 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_MANAGER_JAVA_SCRIPT_FEATURE_H_
  5. #define COMPONENTS_PASSWORD_MANAGER_IOS_PASSWORD_MANAGER_JAVA_SCRIPT_FEATURE_H_
  6. #include "base/callback.h"
  7. #include "base/no_destructor.h"
  8. #include "components/autofill/core/common/unique_ids.h"
  9. #import "ios/web/public/js_messaging/java_script_feature.h"
  10. namespace autofill {
  11. struct PasswordFormFillData;
  12. } // namespace autofill
  13. namespace web {
  14. class WebFrame;
  15. } // namespace web
  16. namespace password_manager {
  17. struct FillData;
  18. // Communicates with the JavaScript file password_controller.js, which
  19. // contains password form parsing and autofill functions.
  20. class PasswordManagerJavaScriptFeature : public web::JavaScriptFeature {
  21. public:
  22. // This feature holds no state, so only a single static instance is ever
  23. // needed.
  24. static PasswordManagerJavaScriptFeature* GetInstance();
  25. // Finds any password forms on the web page.
  26. // |callback| is then called with the JSON string result (which can
  27. // be a zero-length string if there was an error). |callback| cannot be null.
  28. // For example the JSON string for a form with a single password field is:
  29. // [{"action":null,"method":null,"usernameElement":"","usernameValue":"","
  30. // passwords":[{"element":"","value":"asd"}]}]
  31. void FindPasswordFormsInFrame(web::WebFrame* frame,
  32. base::OnceCallback<void(NSString*)> callback);
  33. // Extracts the password form with the given |form_identifier| from a web
  34. // page. |callback| is called with the JSON string containing the info about
  35. // submitted password forms from a web page (it can be zero-length if there
  36. // was an error). |callback| cannot be null. For example. the JSON string for
  37. // a form with a single password field is:
  38. // {"action":null,"method":null,"usernameElement":"","usernameValue":"",
  39. // "passwords":[{"element":"","value":"asd"}]}
  40. void ExtractForm(web::WebFrame* frame,
  41. autofill::FormRendererId form_identifier,
  42. base::OnceCallback<void(NSString*)> callback);
  43. // Fills in the form specified by |form| with the given |password|.
  44. // |username| will be filled in if and only if |fill_username| is true.
  45. // Assumes JavaScript has been injected previously by calling
  46. // |FindPasswordFormsInFrame| or |ExtractForm|. Calls |callback|
  47. // with YES if the filling of the password was successful, NO otherwise.
  48. // |callback| cannot be null.
  49. void FillPasswordForm(web::WebFrame* frame,
  50. const password_manager::FillData& form,
  51. BOOL fill_username,
  52. const std::string& username,
  53. const std::string& password,
  54. base::OnceCallback<void(BOOL)> callback);
  55. // Fills in the form specified by |fill_data| with the given |username| and
  56. // |password|. Assumes JavaScript has been injected previously by calling
  57. // |FindPasswordFormsInFrame| or |ExtractForm|. Calls |callback|
  58. // with YES if the filling of the password was successful, NO otherwise.
  59. // |callback| cannot be null.
  60. void FillPasswordForm(web::WebFrame* frame,
  61. const autofill::PasswordFormFillData& fill_data,
  62. const std::string& username,
  63. const std::string& password,
  64. base::OnceCallback<void(BOOL)> callback);
  65. // Fills new password field for (optional) |new_password_identifier| and for
  66. // (optional) confirm password field |confirm_password_identifier| in the form
  67. // identified by |form_identifier|. Invokes |callback| with YES if any fields
  68. // were filled, false otherwise.
  69. void FillPasswordForm(web::WebFrame* frame,
  70. autofill::FormRendererId form_identifier,
  71. autofill::FieldRendererId new_password_identifier,
  72. autofill::FieldRendererId confirm_password_identifier,
  73. NSString* generated_password,
  74. base::OnceCallback<void(BOOL)> callback);
  75. private:
  76. friend class base::NoDestructor<PasswordManagerJavaScriptFeature>;
  77. PasswordManagerJavaScriptFeature();
  78. ~PasswordManagerJavaScriptFeature() override;
  79. PasswordManagerJavaScriptFeature(const PasswordManagerJavaScriptFeature&) =
  80. delete;
  81. PasswordManagerJavaScriptFeature& operator=(
  82. const PasswordManagerJavaScriptFeature&) = delete;
  83. // Calls the "passwords.fillPasswordForm" JavaScript function to fill the form
  84. // described by |form_value| with |username| and |password|.
  85. void FillPasswordForm(web::WebFrame* frame,
  86. std::unique_ptr<base::Value> form_value,
  87. const std::string& username,
  88. const std::string& password,
  89. base::OnceCallback<void(BOOL)> callback);
  90. };
  91. } // namespace password_manager
  92. #endif // COMPONENTS_PASSWORD_MANAGER_IOS_PASSWORD_MANAGER_JAVA_SCRIPT_FEATURE_H_