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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_GENERATION_AGENT_H_
6 #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_GENERATION_AGENT_H_
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "content/public/renderer/render_frame_observer.h"
15 #include "third_party/WebKit/public/web/WebInputElement.h"
26 class PasswordAutofillAgent
;
28 // This class is responsible for controlling communication for password
29 // generation between the browser (which shows the popup and generates
30 // passwords) and WebKit (shows the generation icon in the password field).
31 class PasswordGenerationAgent
: public content::RenderFrameObserver
{
33 PasswordGenerationAgent(content::RenderFrame
* render_frame
,
34 PasswordAutofillAgent
* password_agent
);
35 ~PasswordGenerationAgent() override
;
37 // Returns true if the field being changed is one where a generated password
38 // is being offered. Updates the state of the popup if necessary.
39 bool TextDidChangeInTextField(const blink::WebInputElement
& element
);
41 // Returns true if the newly focused node caused the generation UI to show.
42 bool FocusedNodeHasChanged(const blink::WebNode
& node
);
44 // Called when new form controls are inserted.
45 void OnDynamicFormsSeen();
47 // The length that a password can be before the UI is hidden.
48 static const size_t kMaximumOfferSize
= 5;
51 // Returns true if the document for |render_frame()| is one that we should
52 // consider analyzing. Virtual so that it can be overriden during testing.
53 virtual bool ShouldAnalyzeDocument() const;
55 // RenderViewObserver:
56 bool OnMessageReceived(const IPC::Message
& message
) override
;
58 // Use to force enable during testing.
59 void set_enabled(bool enabled
) { enabled_
= enabled
; }
62 struct AccountCreationFormData
{
63 linked_ptr
<PasswordForm
> form
;
64 std::vector
<blink::WebInputElement
> password_elements
;
66 AccountCreationFormData(
67 linked_ptr
<PasswordForm
> form
,
68 std::vector
<blink::WebInputElement
> password_elements
);
69 ~AccountCreationFormData();
72 typedef std::vector
<AccountCreationFormData
> AccountCreationFormDataList
;
74 // RenderFrameObserver:
75 void DidFinishDocumentLoad() override
;
78 void OnFormNotBlacklisted(const PasswordForm
& form
);
79 void OnPasswordAccepted(const base::string16
& password
);
80 void OnAccountCreationFormsDetected(
81 const std::vector
<autofill::FormData
>& forms
);
83 // Helper function that will try and populate |password_elements_| and
84 // |possible_account_creation_form_|.
85 void FindPossibleGenerationForm();
87 // Helper function to decide if |passwords_| contains password fields for
88 // an account creation form. Sets |generation_element_| to the field that
89 // we want to trigger the generation UI on.
90 void DetermineGenerationElement();
92 // Show password generation UI anchored at |generation_element_|.
93 void ShowGenerationPopup();
95 // Show UI for editing a generated password at |generation_element_|.
96 void ShowEditingPopup();
98 // Hides a password generation popup if one exists.
101 // Stores forms that are candidates for account creation.
102 AccountCreationFormDataList possible_account_creation_forms_
;
104 // Stores the origins of the password forms confirmed not to be blacklisted
105 // by the browser. A form can be blacklisted if a user chooses "never save
106 // passwords for this site".
107 std::vector
<GURL
> not_blacklisted_password_form_origins_
;
109 // Stores each password form for which the Autofill server classifies one of
110 // the form's fields as an ACCOUNT_CREATION_PASSWORD. These forms will
111 // not be sent if the feature is disabled.
112 std::vector
<autofill::FormData
> generation_enabled_forms_
;
114 // Data for form which generation is allowed on.
115 scoped_ptr
<AccountCreationFormData
> generation_form_data_
;
117 // Element where we want to trigger password generation UI.
118 blink::WebInputElement generation_element_
;
120 // If the password field at |generation_element_| contains a generated
122 bool password_is_generated_
;
124 // True if a password was generated and the user edited it. Used for UMA
126 bool password_edited_
;
128 // True if the generation popup was shown during this navigation. Used to
129 // track UMA stats per page visit rather than per display, since the former
130 // is more interesting.
131 bool generation_popup_shown_
;
133 // True if the editing popup was shown during this navigation. Used to track
134 // UMA stats per page rather than per display, since the former is more
136 bool editing_popup_shown_
;
138 // If this feature is enabled. Controlled by Finch.
141 // Unowned pointer. Used to notify PassowrdAutofillAgent when values
142 // in password fields are updated.
143 PasswordAutofillAgent
* password_agent_
;
145 DISALLOW_COPY_AND_ASSIGN(PasswordGenerationAgent
);
148 } // namespace autofill
150 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_GENERATION_AGENT_H_