Home | History | Annotate | Download | only in browser
      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_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
      6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/strings/string16.h"
     13 #include "components/autofill/core/browser/autofill_popup_delegate.h"
     14 #include "components/autofill/core/browser/password_autofill_manager.h"
     15 #include "components/autofill/core/common/form_data.h"
     16 #include "components/autofill/core/common/form_field_data.h"
     17 #include "components/autofill/core/common/password_form_fill_data.h"
     18 #include "ui/gfx/rect.h"
     19 
     20 namespace gfx {
     21 class Rect;
     22 }
     23 
     24 namespace autofill {
     25 
     26 class AutofillDriver;
     27 class AutofillManager;
     28 
     29 // TODO(csharp): A lot of the logic in this class is copied from autofillagent.
     30 // Once Autofill is moved out of WebKit this class should be the only home for
     31 // this logic. See http://crbug.com/51644
     32 
     33 // Delegate for in-browser Autocomplete and Autofill display and selection.
     34 class AutofillExternalDelegate
     35     : public AutofillPopupDelegate {
     36  public:
     37   // Creates an AutofillExternalDelegate for the specified AutofillManager and
     38   // AutofillDriver.
     39   AutofillExternalDelegate(AutofillManager* autofill_manager,
     40                            AutofillDriver* autofill_driver);
     41   virtual ~AutofillExternalDelegate();
     42 
     43   // AutofillPopupDelegate implementation.
     44   virtual void OnPopupShown() OVERRIDE;
     45   virtual void OnPopupHidden() OVERRIDE;
     46   virtual bool ShouldRepostEvent(const ui::MouseEvent& event) OVERRIDE;
     47   virtual void DidSelectSuggestion(int identifier) OVERRIDE;
     48   virtual void DidAcceptSuggestion(const base::string16& value,
     49                                    int identifier) OVERRIDE;
     50   virtual void RemoveSuggestion(const base::string16& value,
     51                                 int identifier) OVERRIDE;
     52   virtual void ClearPreviewedForm() OVERRIDE;
     53 
     54   // Records and associates a query_id with web form data.  Called
     55   // when the renderer posts an Autofill query to the browser. |bounds|
     56   // is window relative. |display_warning_if_disabled| tells us if we should
     57   // display warnings (such as autofill is disabled, but had suggestions).
     58   // We might not want to display the warning if a website has disabled
     59   // Autocomplete because they have their own popup, and showing our popup
     60   // on to of theirs would be a poor user experience.
     61   virtual void OnQuery(int query_id,
     62                        const FormData& form,
     63                        const FormFieldData& field,
     64                        const gfx::RectF& element_bounds,
     65                        bool display_warning_if_disabled);
     66 
     67   // Records query results and correctly formats them before sending them off
     68   // to be displayed.  Called when an Autofill query result is available.
     69   virtual void OnSuggestionsReturned(
     70       int query_id,
     71       const std::vector<base::string16>& autofill_values,
     72       const std::vector<base::string16>& autofill_labels,
     73       const std::vector<base::string16>& autofill_icons,
     74       const std::vector<int>& autofill_unique_ids);
     75 
     76   // Show password suggestions in the popup.
     77   void OnShowPasswordSuggestions(const std::vector<base::string16>& suggestions,
     78                                  const std::vector<base::string16>& realms,
     79                                  const FormFieldData& field,
     80                                  const gfx::RectF& bounds);
     81 
     82   // Set the data list value associated with the current field.
     83   void SetCurrentDataListValues(
     84       const std::vector<base::string16>& data_list_values,
     85       const std::vector<base::string16>& data_list_labels);
     86 
     87   // Inform the delegate that the text field editing has ended. This is
     88   // used to help record the metrics of when a new popup is shown.
     89   void DidEndTextFieldEditing();
     90 
     91   // Returns the delegate to its starting state by removing any page specific
     92   // values or settings.
     93   void Reset();
     94 
     95   // Inform the Password Manager of a filled form.
     96   void AddPasswordFormMapping(
     97       const FormFieldData& form,
     98       const PasswordFormFillData& fill_data);
     99 
    100  protected:
    101   base::WeakPtr<AutofillExternalDelegate> GetWeakPtr();
    102 
    103  private:
    104   // Fills the form with the Autofill data corresponding to |unique_id|.
    105   // If |is_preview| is true then this is just a preview to show the user what
    106   // would be selected and if |is_preview| is false then the user has selected
    107   // this data.
    108   void FillAutofillFormData(int unique_id, bool is_preview);
    109 
    110   // Handle applying any Autofill warnings to the Autofill popup.
    111   void ApplyAutofillWarnings(std::vector<base::string16>* autofill_values,
    112                              std::vector<base::string16>* autofill_labels,
    113                              std::vector<base::string16>* autofill_icons,
    114                              std::vector<int>* autofill_unique_ids);
    115 
    116   // Handle applying any Autofill option listings to the Autofill popup.
    117   // This function should only get called when there is at least one
    118   // multi-field suggestion in the list of suggestions.
    119   void ApplyAutofillOptions(std::vector<base::string16>* autofill_values,
    120                             std::vector<base::string16>* autofill_labels,
    121                             std::vector<base::string16>* autofill_icons,
    122                             std::vector<int>* autofill_unique_ids);
    123 
    124   // Insert the data list values at the start of the given list, including
    125   // any required separators.
    126   void InsertDataListValues(std::vector<base::string16>* autofill_values,
    127                             std::vector<base::string16>* autofill_labels,
    128                             std::vector<base::string16>* autofill_icons,
    129                             std::vector<int>* autofill_unique_ids);
    130 
    131   AutofillManager* autofill_manager_;  // weak.
    132 
    133   // Provides driver-level context to the shared code of the component. Must
    134   // outlive this object.
    135   AutofillDriver* autofill_driver_;  // weak
    136 
    137   // Password Autofill manager, handles all password-related Autofilling.
    138   PasswordAutofillManager password_autofill_manager_;
    139 
    140   // The ID of the last request sent for form field Autofill.  Used to ignore
    141   // out of date responses.
    142   int autofill_query_id_;
    143 
    144   // The current form and field selected by Autofill.
    145   FormData autofill_query_form_;
    146   FormFieldData autofill_query_field_;
    147 
    148   // The bounds of the form field that user is interacting with.
    149   gfx::RectF element_bounds_;
    150 
    151   // Should we display a warning if Autofill is disabled?
    152   bool display_warning_if_disabled_;
    153 
    154   // Does the popup include any Autofill profile or credit card suggestions?
    155   bool has_autofill_suggestion_;
    156 
    157   // Have we already shown Autofill suggestions for the field the user is
    158   // currently editing?  Used to keep track of state for metrics logging.
    159   bool has_shown_autofill_popup_for_current_edit_;
    160 
    161   // The current data list values.
    162   std::vector<base::string16> data_list_values_;
    163   std::vector<base::string16> data_list_labels_;
    164 
    165   base::WeakPtrFactory<AutofillExternalDelegate> weak_ptr_factory_;
    166 
    167   DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegate);
    168 };
    169 
    170 }  // namespace autofill
    171 
    172 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
    173