Home | History | Annotate | Download | only in renderer
      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_AUTOFILL_AGENT_H_
      6 #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/gtest_prod_util.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/time/time.h"
     15 #include "base/timer/timer.h"
     16 #include "components/autofill/content/renderer/form_cache.h"
     17 #include "components/autofill/content/renderer/page_click_listener.h"
     18 #include "components/autofill/core/common/forms_seen_state.h"
     19 #include "content/public/renderer/render_view_observer.h"
     20 #include "third_party/WebKit/public/web/WebAutofillClient.h"
     21 #include "third_party/WebKit/public/web/WebFormElement.h"
     22 #include "third_party/WebKit/public/web/WebInputElement.h"
     23 
     24 namespace blink {
     25 class WebNode;
     26 class WebView;
     27 }
     28 
     29 namespace autofill {
     30 
     31 struct FormData;
     32 struct FormFieldData;
     33 struct WebElementDescriptor;
     34 class PasswordAutofillAgent;
     35 
     36 // AutofillAgent deals with Autofill related communications between WebKit and
     37 // the browser.  There is one AutofillAgent per RenderView.
     38 // This code was originally part of RenderView.
     39 // Note that Autofill encompasses:
     40 // - single text field suggestions, that we usually refer to as Autocomplete,
     41 // - password form fill, refered to as Password Autofill, and
     42 // - entire form fill based on one field entry, referred to as Form Autofill.
     43 
     44 class AutofillAgent : public content::RenderViewObserver,
     45                       public PageClickListener,
     46                       public blink::WebAutofillClient {
     47  public:
     48   // PasswordAutofillAgent is guaranteed to outlive AutofillAgent.
     49   AutofillAgent(content::RenderView* render_view,
     50                 PasswordAutofillAgent* password_autofill_manager);
     51   virtual ~AutofillAgent();
     52 
     53  private:
     54   enum AutofillAction {
     55     AUTOFILL_NONE,     // No state set.
     56     AUTOFILL_FILL,     // Fill the Autofill form data.
     57     AUTOFILL_PREVIEW,  // Preview the Autofill form data.
     58   };
     59 
     60   // RenderView::Observer:
     61   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     62   virtual void DidFinishDocumentLoad(blink::WebFrame* frame) OVERRIDE;
     63   virtual void DidCommitProvisionalLoad(blink::WebFrame* frame,
     64                                         bool is_new_navigation) OVERRIDE;
     65   virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE;
     66   virtual void WillSubmitForm(blink::WebFrame* frame,
     67                               const blink::WebFormElement& form) OVERRIDE;
     68   virtual void ZoomLevelChanged() OVERRIDE;
     69   virtual void DidChangeScrollOffset(blink::WebFrame* frame) OVERRIDE;
     70   virtual void FocusedNodeChanged(const blink::WebNode& node) OVERRIDE;
     71   virtual void OrientationChangeEvent(int orientation) OVERRIDE;
     72 
     73   // PageClickListener:
     74   virtual void InputElementClicked(const blink::WebInputElement& element,
     75                                    bool was_focused,
     76                                    bool is_focused) OVERRIDE;
     77   virtual void InputElementLostFocus() OVERRIDE;
     78 
     79   // blink::WebAutofillClient:
     80   virtual void didClearAutofillSelection(const blink::WebNode& node) OVERRIDE;
     81   virtual void textFieldDidEndEditing(
     82       const blink::WebInputElement& element) OVERRIDE;
     83   virtual void textFieldDidChange(
     84       const blink::WebInputElement& element) OVERRIDE;
     85   virtual void textFieldDidReceiveKeyDown(
     86       const blink::WebInputElement& element,
     87       const blink::WebKeyboardEvent& event) OVERRIDE;
     88   virtual void didRequestAutocomplete(
     89       blink::WebFrame* frame,
     90       const blink::WebFormElement& form) OVERRIDE;
     91   virtual void setIgnoreTextChanges(bool ignore) OVERRIDE;
     92   virtual void didAssociateFormControls(
     93       const blink::WebVector<blink::WebNode>& nodes) OVERRIDE;
     94   virtual void openTextDataListChooser(const blink::WebInputElement& element);
     95 
     96   void OnFormDataFilled(int query_id, const FormData& form);
     97   void OnFieldTypePredictionsAvailable(
     98       const std::vector<FormDataPredictions>& forms);
     99 
    100   // For external Autofill selection.
    101   void OnSetAutofillActionFill();
    102   void OnClearForm();
    103   void OnSetAutofillActionPreview();
    104   void OnClearPreviewedForm();
    105   void OnSetNodeText(const base::string16& value);
    106   void OnAcceptDataListSuggestion(const base::string16& value);
    107   void OnAcceptPasswordAutofillSuggestion(const base::string16& username);
    108 
    109   // Called when interactive autocomplete finishes.
    110   void OnRequestAutocompleteResult(
    111       blink::WebFormElement::AutocompleteResult result,
    112       const FormData& form_data);
    113 
    114   // Called when an autocomplete request succeeds or fails with the |result|.
    115   void FinishAutocompleteRequest(
    116       blink::WebFormElement::AutocompleteResult result);
    117 
    118   // Called when the page is actually shown in the browser, as opposed to simply
    119   // being preloaded.
    120   void OnPageShown();
    121 
    122   // Called in a posted task by textFieldDidChange() to work-around a WebKit bug
    123   // http://bugs.webkit.org/show_bug.cgi?id=16976
    124   void TextFieldDidChangeImpl(const blink::WebInputElement& element);
    125 
    126   // Shows the autofill suggestions for |element|.
    127   // This call is asynchronous and may or may not lead to the showing of a
    128   // suggestion popup (no popup is shown if there are no available suggestions).
    129   // |autofill_on_empty_values| specifies whether suggestions should be shown
    130   // when |element| contains no text.
    131   // |requires_caret_at_end| specifies whether suggestions should be shown when
    132   // the caret is not after the last character in |element|.
    133   // |display_warning_if_disabled| specifies whether a warning should be
    134   // displayed to the user if Autofill has suggestions available, but cannot
    135   // fill them because it is disabled (e.g. when trying to fill a credit card
    136   // form on a non-secure website).
    137   // |datalist_only| specifies whether all of <datalist> suggestions and no
    138   // autofill suggestions are shown. |autofill_on_empty_values| and
    139   // |requires_caret_at_end| are ignored if |datalist_only| is true.
    140   void ShowSuggestions(const blink::WebInputElement& element,
    141                        bool autofill_on_empty_values,
    142                        bool requires_caret_at_end,
    143                        bool display_warning_if_disabled,
    144                        bool datalist_only);
    145 
    146   // Queries the browser for Autocomplete and Autofill suggestions for the given
    147   // |element|.
    148   void QueryAutofillSuggestions(const blink::WebInputElement& element,
    149                                 bool display_warning_if_disabled,
    150                                 bool datalist_only);
    151 
    152   // Sets the element value to reflect the selected |suggested_value|.
    153   void AcceptDataListSuggestion(const base::string16& suggested_value);
    154 
    155   // Queries the AutofillManager for form data for the form containing |node|.
    156   // |value| is the current text in the field, and |unique_id| is the selected
    157   // profile's unique ID.  |action| specifies whether to Fill or Preview the
    158   // values returned from the AutofillManager.
    159   void FillAutofillFormData(const blink::WebNode& node,
    160                             int unique_id,
    161                             AutofillAction action);
    162 
    163   // Fills |form| and |field| with the FormData and FormField corresponding to
    164   // |node|. Returns true if the data was found; and false otherwise.
    165   bool FindFormAndFieldForNode(
    166       const blink::WebNode& node,
    167       FormData* form,
    168       FormFieldData* field) WARN_UNUSED_RESULT;
    169 
    170   // Set |node| to display the given |value|.
    171   void SetNodeText(const base::string16& value, blink::WebInputElement* node);
    172 
    173   // Hides any currently showing Autofill UI.
    174   void HideAutofillUI();
    175 
    176   FormCache form_cache_;
    177 
    178   PasswordAutofillAgent* password_autofill_agent_;  // WEAK reference.
    179 
    180   // The ID of the last request sent for form field Autofill.  Used to ignore
    181   // out of date responses.
    182   int autofill_query_id_;
    183 
    184   // The element corresponding to the last request sent for form field Autofill.
    185   blink::WebInputElement element_;
    186 
    187   // The form element currently requesting an interactive autocomplete.
    188   blink::WebFormElement in_flight_request_form_;
    189 
    190   // All the form elements seen in the top frame.
    191   std::vector<blink::WebFormElement> form_elements_;
    192 
    193   // The action to take when receiving Autofill data from the AutofillManager.
    194   AutofillAction autofill_action_;
    195 
    196   // Pointer to the WebView. Used to access page scale factor.
    197   blink::WebView* web_view_;
    198 
    199   // Should we display a warning if autofill is disabled?
    200   bool display_warning_if_disabled_;
    201 
    202   // Was the query node autofilled prior to previewing the form?
    203   bool was_query_node_autofilled_;
    204 
    205   // Have we already shown Autofill suggestions for the field the user is
    206   // currently editing?  Used to keep track of state for metrics logging.
    207   bool has_shown_autofill_popup_for_current_edit_;
    208 
    209   // If true we just set the node text so we shouldn't show the popup.
    210   bool did_set_node_text_;
    211 
    212   // Whether or not new forms/fields have been dynamically added
    213   // since the last loaded forms were sent to the browser process.
    214   bool has_new_forms_for_browser_;
    215 
    216   // Whether or not to ignore text changes.  Useful for when we're committing
    217   // a composition when we are defocusing the WebView and we don't want to
    218   // trigger an autofill popup to show.
    219   bool ignore_text_changes_;
    220 
    221   // Timestamp of first time forms are seen.
    222   base::TimeTicks forms_seen_timestamp_;
    223 
    224   base::WeakPtrFactory<AutofillAgent> weak_ptr_factory_;
    225 
    226   friend class PasswordAutofillAgentTest;
    227   FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest, FillFormElement);
    228   FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest, SendForms);
    229   FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest, SendDynamicForms);
    230   FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest, ShowAutofillWarning);
    231   FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, WaitUsername);
    232   FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, SuggestionAccept);
    233   FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, SuggestionSelect);
    234 
    235   DISALLOW_COPY_AND_ASSIGN(AutofillAgent);
    236 };
    237 
    238 }  // namespace autofill
    239 
    240 #endif  // COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_
    241