Home | History | Annotate | Download | only in autofill
      1 /*
      2  * Copyright (c) 2010 The Chromium Authors. All rights reserved.
      3  * Copyright 2010, The Android Open Source Project
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #ifndef FormManagerAndroid_h
     28 #define FormManagerAndroid_h
     29 
     30 #include "ChromiumIncludes.h"
     31 
     32 #include <map>
     33 #include <vector>
     34 
     35 // TODO: This file is taken from chromium/chrome/renderer/form_manager.h and
     36 // customised to use WebCore types rather than WebKit API types. It would be
     37 // nice and would ease future merge pain if the two could be combined.
     38 
     39 namespace webkit_glue {
     40 struct FormData;
     41 struct FormField;
     42 }  // namespace webkit_glue
     43 
     44 namespace WebCore {
     45 class Frame;
     46 class HTMLFormControlElement;
     47 class HTMLFormElement;
     48 class Node;
     49 }
     50 
     51 using WebCore::Frame;
     52 using WebCore::HTMLFormControlElement;
     53 using WebCore::HTMLFormElement;
     54 using WebCore::Node;
     55 
     56 namespace android {
     57 
     58 // Manages the forms in a Document.
     59 class FormManager {
     60 public:
     61     // A bit field mask for form requirements.
     62     enum RequirementsMask {
     63         REQUIRE_NONE         = 0,          // No requirements.
     64         REQUIRE_AUTOCOMPLETE = 1 << 0,     // Require that autocomplete != off.
     65         REQUIRE_ENABLED      = 1 << 1,     // Require that disabled attribute is off.
     66         REQUIRE_EMPTY        = 1 << 2,     // Require that the fields are empty.
     67     };
     68 
     69     // A bit field mask to extract data from HTMLFormControlElement.
     70     enum ExtractMask {
     71         EXTRACT_NONE        = 0,
     72         EXTRACT_VALUE       = 1 << 0, // Extract value from HTMLFormControlElement.
     73         EXTRACT_OPTION_TEXT = 1 << 1, // Extract option text from HTMLFormSelectElement. Only valid when |EXTRACT_VALUE| is set. This is used for form submission where humand readable value is captured.
     74         EXTRACT_OPTIONS     = 1 << 2, // Extract options from HTMLFormControlElement.
     75     };
     76 
     77     FormManager();
     78     virtual ~FormManager();
     79 
     80     // Fills out a FormField object from a given HTMLFormControlElement.
     81     // |extract_mask|: See the enum ExtractMask above for details.
     82     static void HTMLFormControlElementToFormField(HTMLFormControlElement* element, ExtractMask extract_mask, webkit_glue::FormField* field);
     83 
     84     // Returns the corresponding label for |element|.  WARNING: This method can
     85     // potentially be very slow.  Do not use during any code paths where the page
     86     // is loading.
     87     static string16 LabelForElement(const HTMLFormControlElement& element);
     88 
     89     // Fills out a FormData object from a given WebFormElement.  If |get_values|
     90     // is true, the fields in |form| will have the values filled out.  Returns
     91     // true if |form| is filled out; it's possible that |element| won't meet the
     92     // requirements in |requirements|.  This also returns false if there are no
     93     // fields in |form|.
     94     // TODO: Remove the user of this in RenderView and move this to
     95     // private.
     96     static bool HTMLFormElementToFormData(HTMLFormElement* element, RequirementsMask requirements, ExtractMask extract_mask, webkit_glue::FormData* form);
     97 
     98     // Scans the DOM in |frame| extracting and storing forms.
     99     void ExtractForms(Frame* frame);
    100 
    101     // Returns a vector of forms in |frame| that match |requirements|.
    102     void GetFormsInFrame(const Frame* frame, RequirementsMask requirements, std::vector<webkit_glue::FormData>* forms);
    103 
    104     // Finds the form that contains |element| and returns it in |form|. Returns
    105     // false if the form is not found.
    106     bool FindFormWithFormControlElement(HTMLFormControlElement* element, RequirementsMask requirements, webkit_glue::FormData* form);
    107 
    108     // Fills the form represented by |form|. |node| is the input element that
    109     // initiated the auto-fill process.
    110     bool FillForm(const webkit_glue::FormData& form, Node* node);
    111 
    112     // Previews the form represented by |form|.  |node| is the input element
    113     // that initiated the preview process.
    114     bool PreviewForm(const webkit_glue::FormData& form, Node* node);
    115 
    116     // Clears the values of all input elements in the form that contains |node|.
    117     // Returns false if the form is not found.
    118     bool ClearFormWithNode(Node* node);
    119 
    120     // Clears the placeholder values and the auto-filled background for any fields
    121     // in the form containing |node| that have been previewed. Resets the
    122     // autofilled state of |node| to |was_autofilled|. Returns false if the form
    123     // is not found.
    124     bool ClearPreviewedFormWithNode(Node* node, bool was_autofilled);
    125 
    126     // Resets the stored set of forms.
    127     void Reset();
    128 
    129     // Resets the forms for the specified |frame|.
    130     void ResetFrame(const Frame* frame);
    131 
    132     // Returns true if |form| has any auto-filled fields.
    133     bool FormWithNodeIsAutofilled(Node* node);
    134 
    135 private:
    136     // Stores the HTMLFormElement and the form control elements for a form.
    137     // Original form values are stored so when we clear a form we can reset
    138     // <select> values to their original value.
    139     struct FormElement;
    140 
    141     // Type for cache of FormElement objects.
    142     typedef ScopedVector<FormElement> FormElementList;
    143 
    144     // The callback type used by ForEachMatchingFormField().
    145     typedef Callback3<HTMLFormControlElement*, const webkit_glue::FormField*, bool>::Type Callback;
    146 
    147     // Finds the cached FormElement that contains |node|.
    148     bool FindCachedFormElementWithNode(Node* node, FormElement** form_element);
    149 
    150     // Uses the data in |form| to find the cached FormElement.
    151     bool FindCachedFormElement(const webkit_glue::FormData& form, FormElement** form_element);
    152 
    153     // For each field in |data| that matches the corresponding field in |form|
    154     // and meets the |requirements|, |callback| is called with the actual
    155     // WebFormControlElement and the FormField data from |form|. The field that
    156     // matches |node| is not required to be empty if |requirements| includes
    157     // REQUIRE_EMPTY.  This method owns |callback|.
    158     void ForEachMatchingFormField(FormElement* form, Node* node, RequirementsMask requirements, const webkit_glue::FormData& data, Callback* callback);
    159 
    160     // A ForEachMatchingFormField() callback that sets |field|'s value using the
    161     // value in |data|.  This method also sets the autofill attribute, causing the
    162     // background to be yellow.
    163     void FillFormField(HTMLFormControlElement* field, const webkit_glue::FormField* data, bool is_initiating_node);
    164 
    165     // A ForEachMatchingFormField() callback that sets |field|'s placeholder value
    166     // using the value in |data|, causing the test to be greyed-out.  This method
    167     // also sets the autofill attribute, causing the background to be yellow.
    168     void PreviewFormField(HTMLFormControlElement* field, const webkit_glue::FormField* data, bool is_initiaiting_node);
    169 
    170     // The cached FormElement objects.
    171     FormElementList form_elements_;
    172 
    173     DISALLOW_COPY_AND_ASSIGN(FormManager);
    174 };
    175 
    176 } // namespace android
    177 
    178 #endif  // FormManagerAndroid_h
    179