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/linked_ptr.h" 12 #include "base/memory/weak_ptr.h" 13 #include "components/autofill/core/common/password_form_fill_data.h" 14 #include "content/public/renderer/render_view_observer.h" 15 #include "third_party/WebKit/public/web/WebInputElement.h" 16 17 namespace blink { 18 class WebInputElement; 19 class WebKeyboardEvent; 20 class WebSecurityOrigin; 21 class WebView; 22 } 23 24 namespace autofill { 25 26 // This class is responsible for filling password forms. 27 // There is one PasswordAutofillAgent per RenderView. 28 class PasswordAutofillAgent : public content::RenderViewObserver { 29 public: 30 explicit PasswordAutofillAgent(content::RenderView* render_view); 31 virtual ~PasswordAutofillAgent(); 32 33 // WebViewClient editor related calls forwarded by the RenderView. 34 // If they return true, it indicates the event was consumed and should not 35 // be used for any other autofill activity. 36 bool TextFieldDidEndEditing(const blink::WebInputElement& element); 37 bool TextDidChangeInTextField(const blink::WebInputElement& element); 38 bool TextFieldHandlingKeyDown(const blink::WebInputElement& element, 39 const blink::WebKeyboardEvent& event); 40 41 // Fills the password associated with user name |username|. Returns true if 42 // the username and password fields were filled, false otherwise. 43 bool DidAcceptAutofillSuggestion(const blink::WebNode& node, 44 const blink::WebString& username); 45 // A no-op. Password forms are not previewed, so they do not need to be 46 // cleared when the selection changes. However, this method returns 47 // true when |node| is fillable by password Autofill. 48 bool DidClearAutofillSelection(const blink::WebNode& node); 49 // Shows an Autofill popup with username suggestions for |element|. 50 // Returns true if any suggestions were shown, false otherwise. 51 bool ShowSuggestions(const blink::WebInputElement& element); 52 53 // Called when new form controls are inserted. 54 void OnDynamicFormsSeen(blink::WebFrame* frame); 55 56 protected: 57 virtual bool OriginCanAccessPasswordManager( 58 const blink::WebSecurityOrigin& origin); 59 60 private: 61 friend class PasswordAutofillAgentTest; 62 63 enum OtherPossibleUsernamesUsage { 64 NOTHING_TO_AUTOFILL, 65 OTHER_POSSIBLE_USERNAMES_ABSENT, 66 OTHER_POSSIBLE_USERNAMES_PRESENT, 67 OTHER_POSSIBLE_USERNAME_SHOWN, 68 OTHER_POSSIBLE_USERNAME_SELECTED, 69 OTHER_POSSIBLE_USERNAMES_MAX 70 }; 71 72 struct PasswordInfo { 73 blink::WebInputElement password_field; 74 PasswordFormFillData fill_data; 75 bool backspace_pressed_last; 76 PasswordInfo() : backspace_pressed_last(false) {} 77 }; 78 typedef std::map<blink::WebElement, PasswordInfo> LoginToPasswordInfoMap; 79 typedef std::map<blink::WebFrame*, 80 linked_ptr<PasswordForm> > FrameToPasswordFormMap; 81 82 // RenderViewObserver: 83 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 84 virtual void DidStartProvisionalLoad(blink::WebFrame* frame) OVERRIDE; 85 virtual void DidStartLoading() OVERRIDE; 86 virtual void DidFinishDocumentLoad(blink::WebFrame* frame) OVERRIDE; 87 virtual void DidFinishLoad(blink::WebFrame* frame) OVERRIDE; 88 virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE; 89 virtual void FrameWillClose(blink::WebFrame* frame) OVERRIDE; 90 virtual void WillSendSubmitEvent(blink::WebFrame* frame, 91 const blink::WebFormElement& form) OVERRIDE; 92 virtual void WillSubmitForm(blink::WebFrame* frame, 93 const blink::WebFormElement& form) OVERRIDE; 94 95 // RenderView IPC handlers: 96 void OnFillPasswordForm(const PasswordFormFillData& form_data); 97 98 // Scans the given frame for password forms and sends them up to the browser. 99 // If |only_visible| is true, only forms visible in the layout are sent. 100 void SendPasswordForms(blink::WebFrame* frame, bool only_visible); 101 102 void GetSuggestions(const PasswordFormFillData& fill_data, 103 const base::string16& input, 104 std::vector<base::string16>* suggestions, 105 std::vector<base::string16>* realms); 106 107 bool ShowSuggestionPopup(const PasswordFormFillData& fill_data, 108 const blink::WebInputElement& user_input); 109 110 // Attempts to fill |username_element| and |password_element| with the 111 // |fill_data|. Will use the data corresponding to the preferred username, 112 // unless the |username_element| already has a value set. In that case, 113 // attempts to fill the password matching the already filled username, if 114 // such a password exists. 115 void FillFormOnPasswordRecieved(const PasswordFormFillData& fill_data, 116 blink::WebInputElement username_element, 117 blink::WebInputElement password_element); 118 119 bool FillUserNameAndPassword( 120 blink::WebInputElement* username_element, 121 blink::WebInputElement* password_element, 122 const PasswordFormFillData& fill_data, 123 bool exact_username_match, 124 bool set_selection); 125 126 // Fills |login_input| and |password| with the most relevant suggestion from 127 // |fill_data| and shows a popup with other suggestions. 128 void PerformInlineAutocomplete( 129 const blink::WebInputElement& username, 130 const blink::WebInputElement& password, 131 const PasswordFormFillData& fill_data); 132 133 // Invoked when the passed frame is closing. Gives us a chance to clear any 134 // reference we may have to elements in that frame. 135 void FrameClosing(const blink::WebFrame* frame); 136 137 // Finds login information for a |node| that was previously filled. 138 bool FindLoginInfo(const blink::WebNode& node, 139 blink::WebInputElement* found_input, 140 PasswordInfo* found_password); 141 142 // If |provisionally_saved_forms_| contains a form for |current_frame| or its 143 // children, return such frame. 144 blink::WebFrame* CurrentOrChildFrameWithSavedForms( 145 const blink::WebFrame* current_frame); 146 147 // The logins we have filled so far with their associated info. 148 LoginToPasswordInfoMap login_to_password_info_; 149 150 // Used for UMA stats. 151 OtherPossibleUsernamesUsage usernames_usage_; 152 153 // Pointer to the WebView. Used to access page scale factor. 154 blink::WebView* web_view_; 155 156 // Set if the user might be submitting a password form on the current page, 157 // but the submit may still fail (i.e. doesn't pass JavaScript validation). 158 FrameToPasswordFormMap provisionally_saved_forms_; 159 160 base::WeakPtrFactory<PasswordAutofillAgent> weak_ptr_factory_; 161 162 DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgent); 163 }; 164 165 } // namespace autofill 166 167 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_ 168