• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_GENERATION_AGENT_H_
6 #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_GENERATION_AGENT_H_
7 
8 #include <map>
9 #include <utility>
10 #include <vector>
11 
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"
15 #include "url/gurl.h"
16 
17 namespace blink {
18 class WebCString;
19 class WebDocument;
20 }
21 
22 namespace autofill {
23 
24 struct FormData;
25 struct PasswordForm;
26 
27 // This class is responsible for controlling communication for password
28 // generation between the browser (which shows the popup and generates
29 // passwords) and WebKit (shows the generation icon in the password field).
30 class PasswordGenerationAgent : public content::RenderViewObserver {
31  public:
32   explicit PasswordGenerationAgent(content::RenderView* render_view);
33   virtual ~PasswordGenerationAgent();
34 
35   // Returns true if the field being changed is one where a generated password
36   // is being offered. Updates the state of the popup if necessary.
37   bool TextDidChangeInTextField(const blink::WebInputElement& element);
38 
39   // Returns true if the newly focused node caused the generation UI to show.
40   bool FocusedNodeHasChanged(const blink::WebNode& node);
41 
42  protected:
43   // Returns true if this document is one that we should consider analyzing.
44   // Virtual so that it can be overriden during testing.
45   virtual bool ShouldAnalyzeDocument(const blink::WebDocument& document) const;
46 
47   // RenderViewObserver:
48   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
49 
50   // Use to force enable during testing.
set_enabled(bool enabled)51   void set_enabled(bool enabled) { enabled_ = enabled; }
52 
53  private:
54   // RenderViewObserver:
55   virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) OVERRIDE;
56   virtual void DidFinishLoad(blink::WebLocalFrame* frame) OVERRIDE;
57 
58   // Message handlers.
59   void OnFormNotBlacklisted(const PasswordForm& form);
60   void OnPasswordAccepted(const base::string16& password);
61   void OnAccountCreationFormsDetected(
62       const std::vector<autofill::FormData>& forms);
63 
64   // Helper function to decide if |passwords_| contains password fields for
65   // an account creation form. Sets |generation_element_| to the field that
66   // we want to trigger the generation UI on.
67   void DetermineGenerationElement();
68 
69   // Show password generation UI anchored at |generation_element_|.
70   void ShowGenerationPopup();
71 
72   // Show UI for editing a generated password at |generation_element_|.
73   void ShowEditingPopup();
74 
75   // Hides a password generation popup if one exists.
76   void HidePopup();
77 
78   content::RenderView* render_view_;
79 
80   // Stores the origin of the account creation form we detected.
81   scoped_ptr<PasswordForm> possible_account_creation_form_;
82 
83   // Stores the origins of the password forms confirmed not to be blacklisted
84   // by the browser. A form can be blacklisted if a user chooses "never save
85   // passwords for this site".
86   std::vector<GURL> not_blacklisted_password_form_origins_;
87 
88   // Stores each password form for which the Autofill server classifies one of
89   // the form's fields as an ACCOUNT_CREATION_PASSWORD. These forms will
90   // not be sent if the feature is disabled.
91   std::vector<autofill::FormData> generation_enabled_forms_;
92 
93   // Password elements that may be part of an account creation form.
94   std::vector<blink::WebInputElement> password_elements_;
95 
96   // Element where we want to trigger password generation UI.
97   blink::WebInputElement generation_element_;
98 
99   // If the password field at |generation_element_| contains a generated
100   // password.
101   bool password_is_generated_;
102 
103   // True if a password was generated and the user edited it. Used for UMA
104   // stats.
105   bool password_edited_;
106 
107   // If this feature is enabled. Controlled by Finch.
108   bool enabled_;
109 
110   DISALLOW_COPY_AND_ASSIGN(PasswordGenerationAgent);
111 };
112 
113 }  // namespace autofill
114 
115 #endif  // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_GENERATION_AGENT_H_
116