password_autofill_agent.h revision 868fa2fe829687343ffae624259930155e16dbd8
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_AUTOFILL_AGENT_H_
6#define COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_
7
8#include <map>
9#include <vector>
10
11#include "base/memory/weak_ptr.h"
12#include "components/autofill/common/password_form_fill_data.h"
13#include "content/public/renderer/render_view_observer.h"
14#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
15
16namespace WebKit {
17class WebInputElement;
18class WebKeyboardEvent;
19class WebView;
20}
21
22namespace autofill {
23
24// This class is responsible for filling password forms.
25// There is one PasswordAutofillAgent per RenderView.
26class PasswordAutofillAgent : public content::RenderViewObserver {
27 public:
28  explicit PasswordAutofillAgent(content::RenderView* render_view);
29  virtual ~PasswordAutofillAgent();
30
31  // WebViewClient editor related calls forwarded by the RenderView.
32  // If they return true, it indicates the event was consumed and should not
33  // be used for any other autofill activity.
34  bool TextFieldDidEndEditing(const WebKit::WebInputElement& element);
35  bool TextDidChangeInTextField(const WebKit::WebInputElement& element);
36  bool TextFieldHandlingKeyDown(const WebKit::WebInputElement& element,
37                                const WebKit::WebKeyboardEvent& event);
38
39  // Fills the password associated with user name |value|. Returns true if the
40  // username and password fields were filled, false otherwise.
41  bool DidAcceptAutofillSuggestion(const WebKit::WebNode& node,
42                                   const WebKit::WebString& value);
43  // A no-op.  No filling happens for selection.  But this method returns
44  // true when |node| is fillable by password Autofill.
45  bool DidSelectAutofillSuggestion(const WebKit::WebNode& node);
46  // A no-op.  Password forms are not previewed, so they do not need to be
47  // cleared when the selection changes.  However, this method returns
48  // true when |node| is fillable by password Autofill.
49  bool DidClearAutofillSelection(const WebKit::WebNode& node);
50  // Shows an Autofill popup with username suggestions for |element|.
51  // Returns true if any suggestions were shown, false otherwise.
52  bool ShowSuggestions(const WebKit::WebInputElement& element);
53
54 private:
55  friend class PasswordAutofillAgentTest;
56
57  enum OtherPossibleUsernamesUsage {
58    NOTHING_TO_AUTOFILL,
59    OTHER_POSSIBLE_USERNAMES_ABSENT,
60    OTHER_POSSIBLE_USERNAMES_PRESENT,
61    OTHER_POSSIBLE_USERNAME_SHOWN,
62    OTHER_POSSIBLE_USERNAME_SELECTED,
63    OTHER_POSSIBLE_USERNAMES_MAX
64  };
65
66  struct PasswordInfo {
67    WebKit::WebInputElement password_field;
68    PasswordFormFillData fill_data;
69    bool backspace_pressed_last;
70    PasswordInfo() : backspace_pressed_last(false) {}
71  };
72  typedef std::map<WebKit::WebElement, PasswordInfo> LoginToPasswordInfoMap;
73
74  // RenderViewObserver:
75  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
76  virtual void DidStartLoading() OVERRIDE;
77  virtual void DidFinishDocumentLoad(WebKit::WebFrame* frame) OVERRIDE;
78  virtual void DidFinishLoad(WebKit::WebFrame* frame) OVERRIDE;
79  virtual void FrameDetached(WebKit::WebFrame* frame) OVERRIDE;
80  virtual void FrameWillClose(WebKit::WebFrame* frame) OVERRIDE;
81
82  // RenderView IPC handlers:
83  void OnFillPasswordForm(const PasswordFormFillData& form_data,
84                          bool disable_popup);
85
86  // Scans the given frame for password forms and sends them up to the browser.
87  // If |only_visible| is true, only forms visible in the layout are sent.
88  void SendPasswordForms(WebKit::WebFrame* frame, bool only_visible);
89
90  void GetSuggestions(const PasswordFormFillData& fill_data,
91                      const base::string16& input,
92                      std::vector<base::string16>* suggestions);
93
94  bool ShowSuggestionPopup(const PasswordFormFillData& fill_data,
95                           const WebKit::WebInputElement& user_input);
96
97  bool FillUserNameAndPassword(
98      WebKit::WebInputElement* username_element,
99      WebKit::WebInputElement* password_element,
100      const PasswordFormFillData& fill_data,
101      bool exact_username_match,
102      bool set_selection);
103
104  // Fills |login_input| and |password| with the most relevant suggestion from
105  // |fill_data| and shows a popup with other suggestions.
106  void PerformInlineAutocomplete(
107      const WebKit::WebInputElement& username,
108      const WebKit::WebInputElement& password,
109      const PasswordFormFillData& fill_data);
110
111  // Invoked when the passed frame is closing.  Gives us a chance to clear any
112  // reference we may have to elements in that frame.
113  void FrameClosing(const WebKit::WebFrame* frame);
114
115  // Finds login information for a |node| that was previously filled.
116  bool FindLoginInfo(const WebKit::WebNode& node,
117                     WebKit::WebInputElement* found_input,
118                     PasswordInfo* found_password);
119
120  // The logins we have filled so far with their associated info.
121  LoginToPasswordInfoMap login_to_password_info_;
122
123  // Used to disable and hide the popup.
124  bool disable_popup_;
125
126  // Used for UMA stats.
127  OtherPossibleUsernamesUsage usernames_usage_;
128
129  // Pointer to the WebView. Used to access page scale factor.
130  WebKit::WebView* web_view_;
131
132  base::WeakPtrFactory<PasswordAutofillAgent> weak_ptr_factory_;
133
134  DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgent);
135};
136
137}  // namespace autofill
138
139#endif  // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_
140