// 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_CORE_BROWSER_FORM_STRUCTURE_H_ #define COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_ #include #include #include #include #include "base/callback.h" #include "base/gtest_prod_util.h" #include "base/macros.h" #include "base/memory/scoped_ptr.h" #include "base/memory/scoped_vector.h" #include "base/strings/string16.h" #include "base/strings/string_piece.h" #include "components/autofill/core/browser/autofill_field.h" #include "components/autofill/core/browser/autofill_type.h" #include "components/autofill/core/browser/field_types.h" #include "components/autofill/core/browser/proto/server.pb.h" #include "url/gurl.h" class XmlWriter; enum UploadRequired { UPLOAD_NOT_REQUIRED, UPLOAD_REQUIRED, USE_UPLOAD_RATES }; namespace base { class TimeTicks; } namespace buzz { class XmlElement; } namespace rappor { class RapporService; } namespace autofill { struct FormData; struct FormDataPredictions; // FormStructure stores a single HTML form together with the values entered // in the fields along with additional information needed by Autofill. class FormStructure { public: explicit FormStructure(const FormData& form); virtual ~FormStructure(); // Runs several heuristics against the form fields to determine their possible // types. void DetermineHeuristicTypes(); // Encodes the proto |upload| request from this FormStructure. // In some cases, a |login_form_signature| is included as part of the upload. // This field is empty when sending upload requests for non-login forms. bool EncodeUploadRequest(const ServerFieldTypeSet& available_field_types, bool form_was_autofilled, const std::string& login_form_signature, bool observed_submission, autofill::AutofillUploadContents* upload) const; // Encodes the proto |query| request for the set of |forms| that are valid // (see implementation for details on which forms are not included in the // query). The form signatures used in the Query request are output in // |encoded_signatures|. All valid fields are encoded in |query|. static bool EncodeQueryRequest(const std::vector& forms, std::vector* encoded_signatures, autofill::AutofillQueryContents* query); // Parses the field types from the server query response. |forms| must be the // same as the one passed to EncodeQueryRequest when constructing the query. // |rappor_service| may be null. static void ParseQueryResponse(std::string response, const std::vector& forms, rappor::RapporService* rappor_service); // Returns predictions using the details from the given |form_structures| and // their fields' predicted types. static std::vector GetFieldTypePredictions( const std::vector& form_structures); // The unique signature for this form, composed of the target url domain, // the form name, and the form field names in a 64-bit hash. std::string FormSignature() const; // Runs a quick heuristic to rule out forms that are obviously not // auto-fillable, like google/yahoo/msn search, etc. bool IsAutofillable() const; // Resets |autofill_count_| and counts the number of auto-fillable fields. // This is used when we receive server data for form fields. At that time, // we may have more known fields than just the number of fields we matched // heuristically. void UpdateAutofillCount(); // Returns true if this form matches the structural requirements for Autofill. bool ShouldBeParsed() const; // Returns true if we should query the crowdsourcing server to determine this // form's field types. If the form includes author-specified types, this will // return false unless there are password fields in the form. If there are no // password fields the assumption is that the author has expressed their // intent and crowdsourced data should not be used to override this. Password // fields are different because there is no way to specify password generation // directly. bool ShouldBeCrowdsourced() const; // Sets the field types to be those set for |cached_form|. void UpdateFromCache(const FormStructure& cached_form); // Logs quality metrics for |this|, which should be a user-submitted form. // This method should only be called after the possible field types have been // set for each field. |interaction_time| should be a timestamp corresponding // to the user's first interaction with the form. |submission_time| should be // a timestamp corresponding to the form's submission. |observed_submission| // indicates whether this method is called as a result of observing a // submission event (otherwise, it may be that an upload was triggered after // a form was unfocused or a navigation occurred). void LogQualityMetrics(const base::TimeTicks& load_time, const base::TimeTicks& interaction_time, const base::TimeTicks& submission_time, rappor::RapporService* rappor_service, bool did_show_suggestions, bool observed_submission) const; // Classifies each field in |fields_| based upon its |autocomplete| attribute, // if the attribute is available. The association is stored into the field's // |heuristic_type|. // Fills |has_author_specified_types_| with |true| if the attribute is // available and neither empty nor set to the special values "on" or "off" for // at least one field. // Fills |has_author_specified_sections_| with |true| if the attribute // specifies a section for at least one field. void ParseFieldTypesFromAutocompleteAttributes(); // Determines whether |type| and |field| match. typedef base::Callback InputFieldComparator; // Fills in |fields_| that match |types| (via |matches|) with info from // |get_info|. Uses |address_language_code| to determine line separators when // collapsing street address lines into a single-line input text field. bool FillFields( const std::vector& types, const InputFieldComparator& matches, const base::Callback& get_info, const std::string& address_language_code, const std::string& app_locale); // Returns the values that can be filled into the form structure for the // given type. For example, there's no way to fill in a value of "The Moon" // into ADDRESS_HOME_STATE if the form only has a // ). // All returned values are standardized to upper case. std::set PossibleValues(ServerFieldType type); // Gets the form's current value for |type|. For example, it may return // the contents of a text input or the currently selected