1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_
#define COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_
#include <map>
#include <vector>
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/autofill/core/common/password_form_fill_data.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/render_view_observer.h"
#include "third_party/WebKit/public/web/WebInputElement.h"
namespace blink {
class WebInputElement;
class WebKeyboardEvent;
class WebSecurityOrigin;
class WebView;
}
namespace autofill {
// This class is responsible for filling password forms.
class PasswordAutofillAgent : public content::RenderFrameObserver {
public:
explicit PasswordAutofillAgent(content::RenderFrame* render_frame);
~PasswordAutofillAgent() override;
// WebFrameClient editor related calls forwarded by AutofillAgent.
// If they return true, it indicates the event was consumed and should not
// be used for any other autofill activity.
bool TextFieldDidEndEditing(const blink::WebInputElement& element);
bool TextDidChangeInTextField(const blink::WebInputElement& element);
bool TextFieldHandlingKeyDown(const blink::WebInputElement& element,
const blink::WebKeyboardEvent& event);
// Fills the username and password fields of this form with the given values.
// Returns true if the fields were filled, false otherwise.
bool FillSuggestion(const blink::WebNode& node,
const blink::WebString& username,
const blink::WebString& password);
// Previews the username and password fields of this form with the given
// values. Returns true if the fields were previewed, false otherwise.
bool PreviewSuggestion(const blink::WebNode& node,
const blink::WebString& username,
const blink::WebString& password);
// Clears the preview for the username and password fields, restoring both to
// their previous filled state. Return false if no login information was
// found for the form.
bool DidClearAutofillSelection(const blink::WebNode& node);
// Shows an Autofill popup with username suggestions for |element|. If
// |show_all| is |true|, will show all possible suggestions for that element,
// otherwise shows suggestions based on current value of |element|.
// Returns true if any suggestions were shown, false otherwise.
bool ShowSuggestions(const blink::WebInputElement& element, bool show_all);
// Called when new form controls are inserted.
void OnDynamicFormsSeen();
// Called when the user first interacts with the page after a load. This is a
// signal to make autofilled values of password input elements accessible to
// JavaScript.
void FirstUserGestureObserved();
protected:
virtual bool OriginCanAccessPasswordManager(
const blink::WebSecurityOrigin& origin);
private:
enum OtherPossibleUsernamesUsage {
NOTHING_TO_AUTOFILL,
OTHER_POSSIBLE_USERNAMES_ABSENT,
OTHER_POSSIBLE_USERNAMES_PRESENT,
OTHER_POSSIBLE_USERNAME_SHOWN,
OTHER_POSSIBLE_USERNAME_SELECTED,
OTHER_POSSIBLE_USERNAMES_MAX
};
// Ways to restrict which passwords are saved in ProvisionallySavePassword.
enum ProvisionallySaveRestriction {
RESTRICTION_NONE,
RESTRICTION_NON_EMPTY_PASSWORD
};
struct PasswordInfo {
blink::WebInputElement password_field;
PasswordFormFillData fill_data;
bool backspace_pressed_last;
// The user manually edited the password more recently than the username was
// changed.
bool password_was_edited_last;
PasswordInfo();
};
typedef std::map<blink::WebInputElement, PasswordInfo> LoginToPasswordInfoMap;
typedef std::map<blink::WebElement, int> LoginToPasswordInfoKeyMap;
typedef std::map<blink::WebInputElement, blink::WebInputElement>
PasswordToLoginMap;
// This class keeps track of autofilled password input elements and makes sure
// the autofilled password value is not accessible to JavaScript code until
// the user interacts with the page.
class PasswordValueGatekeeper {
public:
PasswordValueGatekeeper();
~PasswordValueGatekeeper();
// Call this for every autofilled password field, so that the gatekeeper
// protects the value accordingly.
void RegisterElement(blink::WebInputElement* element);
// Call this to notify the gatekeeper that the user interacted with the
// page.
void OnUserGesture();
// Call this to reset the gatekeeper on a new page navigation.
void Reset();
private:
// Make the value of |element| accessible to JavaScript code.
void ShowValue(blink::WebInputElement* element);
bool was_user_gesture_seen_;
std::vector<blink::WebInputElement> elements_;
DISALLOW_COPY_AND_ASSIGN(PasswordValueGatekeeper);
};
// Thunk class for RenderViewObserver methods that haven't yet been migrated
// to RenderFrameObserver. Should eventually be removed.
// http://crbug.com/433486
class LegacyPasswordAutofillAgent : public content::RenderViewObserver {
public:
LegacyPasswordAutofillAgent(content::RenderView* render_view,
PasswordAutofillAgent* agent);
~LegacyPasswordAutofillAgent() override;
// RenderViewObserver:
void OnDestruct() override;
void DidStartLoading() override;
void DidStopLoading() override;
void DidStartProvisionalLoad(blink::WebLocalFrame* frame) override;
private:
PasswordAutofillAgent* agent_;
DISALLOW_COPY_AND_ASSIGN(LegacyPasswordAutofillAgent);
};
friend class LegacyPasswordAutofillAgent;
// RenderFrameObserver:
bool OnMessageReceived(const IPC::Message& message) override;
void DidFinishDocumentLoad() override;
void DidFinishLoad() override;
void FrameDetached() override;
void FrameWillClose() override;
void DidCommitProvisionalLoad(bool is_new_navigation) override;
void WillSendSubmitEvent(const blink::WebFormElement& form) override;
void WillSubmitForm(const blink::WebFormElement& form) override;
// Legacy RenderViewObserver:
void DidStartLoading();
void DidStopLoading();
void LegacyDidStartProvisionalLoad(blink::WebLocalFrame* frame);
// RenderView IPC handlers:
void OnFillPasswordForm(int key, const PasswordFormFillData& form_data);
void OnSetLoggingState(bool active);
// Scans the given frame for password forms and sends them up to the browser.
// If |only_visible| is true, only forms visible in the layout are sent.
void SendPasswordForms(bool only_visible);
// Instructs the browser to show a pop-up suggesting which credentials could
// be filled. |show_in_password_field| should indicate whether the pop-up is
// to be shown on the password field instead of on the username field. If the
// username exists, it should be passed as |user_input|. If there is no
// username, pass the password field in |user_input|. In the latter case, no
// username value will be shown in the pop-up.
bool ShowSuggestionPopup(const PasswordFormFillData& fill_data,
const blink::WebInputElement& user_input,
bool show_all,
bool show_on_password_field);
// Finds the PasswordInfo that corresponds to the passed in element. The
// passed in element can be either a username element or a password element.
// If a PasswordInfo was found, returns |true| and also assigns the
// corresponding username WebInputElement and PasswordInfo into
// username_element and pasword_info, respectively. Note, that
// |username_element->isNull()| can be true for forms without a username
// field.
bool FindPasswordInfoForElement(
const blink::WebInputElement& element,
const blink::WebInputElement** username_element,
PasswordInfo** password_info);
// Fills |login_input| and |password| with the most relevant suggestion from
// |fill_data| and shows a popup with other suggestions.
void PerformInlineAutocomplete(
const blink::WebInputElement& username,
const blink::WebInputElement& password,
const PasswordFormFillData& fill_data);
// Invoked when the frame is closing.
void FrameClosing();
// Finds login information for a |node| that was previously filled.
bool FindLoginInfo(const blink::WebNode& node,
blink::WebInputElement* found_input,
PasswordInfo** found_password);
// Clears the preview for the username and password fields, restoring both to
// their previous filled state.
void ClearPreview(blink::WebInputElement* username,
blink::WebInputElement* password);
// Extracts a PasswordForm from |form| and saves it as
// |provisionally_saved_form_|, as long as it satisfies |restriction|.
void ProvisionallySavePassword(const blink::WebFormElement& form,
ProvisionallySaveRestriction restriction);
// Passes through |RenderViewObserver| method to |this|.
LegacyPasswordAutofillAgent legacy_;
// The logins we have filled so far with their associated info.
LoginToPasswordInfoMap login_to_password_info_;
// And the keys under which PasswordAutofillManager can find the same info.
LoginToPasswordInfoKeyMap login_to_password_info_key_;
// A (sort-of) reverse map to |login_to_password_info_|.
PasswordToLoginMap password_to_username_;
// Used for UMA stats.
OtherPossibleUsernamesUsage usernames_usage_;
// Pointer to the WebView. Used to access page scale factor.
blink::WebView* web_view_;
// Set if the user might be submitting a password form on the current page,
// but the submit may still fail (i.e. doesn't pass JavaScript validation).
scoped_ptr<PasswordForm> provisionally_saved_form_;
// Contains the most recent text that user typed in input elements. Used for
// storing username/password before JavaScript changes them.
std::map<const blink::WebInputElement, blink::WebString>
user_modified_elements_;
PasswordValueGatekeeper gatekeeper_;
// True indicates that user debug information should be logged.
bool logging_state_active_;
// True indicates that the username field was autofilled, false otherwise.
bool was_username_autofilled_;
// True indicates that the password field was autofilled, false otherwise.
bool was_password_autofilled_;
// Records original starting point of username element's selection range
// before preview.
int username_selection_start_;
// True indicates that all frames in a page have been rendered.
bool did_stop_loading_;
// True indicates that there is a command line flag to enable showing of
// save password prompt on in-page navigations.
bool save_password_on_in_page_navigation_;
base::WeakPtrFactory<PasswordAutofillAgent> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgent);
};
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_
|