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/scoped_ptr.h"
13 #include "content/public/renderer/render_view_observer.h"
14 #include "third_party/WebKit/public/web/WebInputElement.h"
26 // This class is responsible for controlling communication for password
27 // generation between the browser (which shows the popup and generates
28 // passwords) and WebKit (shows the generation icon in the password field).
29 class PasswordGenerationAgent
: public content::RenderViewObserver
{
31 explicit PasswordGenerationAgent(content::RenderView
* render_view
);
32 ~PasswordGenerationAgent() override
;
34 // Returns true if the field being changed is one where a generated password
35 // is being offered. Updates the state of the popup if necessary.
36 bool TextDidChangeInTextField(const blink::WebInputElement
& element
);
38 // Returns true if the newly focused node caused the generation UI to show.
39 bool FocusedNodeHasChanged(const blink::WebNode
& node
);
41 // Called when new form controls are inserted.
42 void OnDynamicFormsSeen(blink::WebLocalFrame
* frame
);
44 // The length that a password can be before the UI is hidden.
45 static const size_t kMaximumOfferSize
= 5;
48 // Returns true if this document is one that we should consider analyzing.
49 // Virtual so that it can be overriden during testing.
50 virtual bool ShouldAnalyzeDocument(const blink::WebDocument
& document
) const;
52 // RenderViewObserver:
53 bool OnMessageReceived(const IPC::Message
& message
) override
;
55 // Use to force enable during testing.
56 void set_enabled(bool enabled
) { enabled_
= enabled
; }
59 // RenderViewObserver:
60 void DidFinishDocumentLoad(blink::WebLocalFrame
* frame
) override
;
61 void DidFinishLoad(blink::WebLocalFrame
* frame
) override
;
64 void OnFormNotBlacklisted(const PasswordForm
& form
);
65 void OnPasswordAccepted(const base::string16
& password
);
66 void OnAccountCreationFormsDetected(
67 const std::vector
<autofill::FormData
>& forms
);
69 // Helper function that will try and populate |password_elements_| and
70 // |possible_account_creation_form_|.
71 void FindPossibleGenerationForm(blink::WebLocalFrame
* frame
);
73 // Helper function to decide if |passwords_| contains password fields for
74 // an account creation form. Sets |generation_element_| to the field that
75 // we want to trigger the generation UI on.
76 void DetermineGenerationElement();
78 // Show password generation UI anchored at |generation_element_|.
79 void ShowGenerationPopup();
81 // Show UI for editing a generated password at |generation_element_|.
82 void ShowEditingPopup();
84 // Hides a password generation popup if one exists.
87 content::RenderView
* render_view_
;
89 // Stores the origin of the account creation form we detected.
90 scoped_ptr
<PasswordForm
> possible_account_creation_form_
;
92 // Stores the origins of the password forms confirmed not to be blacklisted
93 // by the browser. A form can be blacklisted if a user chooses "never save
94 // passwords for this site".
95 std::vector
<GURL
> not_blacklisted_password_form_origins_
;
97 // Stores each password form for which the Autofill server classifies one of
98 // the form's fields as an ACCOUNT_CREATION_PASSWORD. These forms will
99 // not be sent if the feature is disabled.
100 std::vector
<autofill::FormData
> generation_enabled_forms_
;
102 // Password elements that may be part of an account creation form.
103 std::vector
<blink::WebInputElement
> password_elements_
;
105 // Element where we want to trigger password generation UI.
106 blink::WebInputElement generation_element_
;
108 // If the password field at |generation_element_| contains a generated
110 bool password_is_generated_
;
112 // True if a password was generated and the user edited it. Used for UMA
114 bool password_edited_
;
116 // True if the generation popup was shown during this navigation. Used to
117 // track UMA stats per page visit rather than per display, since the former
118 // is more interesting.
119 bool generation_popup_shown_
;
121 // True if the editing popup was shown during this navigation. Used to track
122 // UMA stats per page rather than per display, since the former is more
124 bool editing_popup_shown_
;
126 // If this feature is enabled. Controlled by Finch.
129 DISALLOW_COPY_AND_ASSIGN(PasswordGenerationAgent
);
132 } // namespace autofill
134 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_GENERATION_AGENT_H_