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