Commit e62e4fc4 authored by Tao Bai's avatar Tao Bai Committed by Commit Bot

Moved more form cache code to AutofillHandler

Bug: 849913
Change-Id: I6f1bd722bc2af136c76653311e91b7a7ad1df0b2
Reviewed-on: https://chromium-review.googlesource.com/1157520Reviewed-by: default avatarRoger McFarlane <rogerm@chromium.org>
Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Commit-Queue: Tao Bai <michaelbai@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580356}
parent 11b2bb0c
......@@ -136,6 +136,65 @@ void AutofillHandler::SendFormDataToRenderer(
driver_->SendFormDataToRenderer(query_id, action, data);
}
bool AutofillHandler::GetCachedFormAndField(const FormData& form,
const FormFieldData& field,
FormStructure** form_structure,
AutofillField** autofill_field) {
// Find the FormStructure that corresponds to |form|.
// If we do not have this form in our cache but it is parseable, we'll add it
// in the call to |UpdateCachedForm()|.
if (!FindCachedForm(form, form_structure) &&
!FormStructure(form).ShouldBeParsed()) {
return false;
}
// Update the cached form to reflect any dynamic changes to the form data, if
// necessary.
if (!UpdateCachedForm(form, *form_structure, form_structure))
return false;
// No data to return if there are no auto-fillable fields.
if (!(*form_structure)->autofill_count())
return false;
// Find the AutofillField that corresponds to |field|.
*autofill_field = nullptr;
for (const auto& current : **form_structure) {
if (current->SameFieldAs(field)) {
*autofill_field = current.get();
break;
}
}
// Even though we always update the cache, the field might not exist if the
// website disables autocomplete while the user is interacting with the form.
// See http://crbug.com/160476
return *autofill_field != nullptr;
}
bool AutofillHandler::UpdateCachedForm(const FormData& live_form,
const FormStructure* cached_form,
FormStructure** updated_form) {
bool needs_update =
(!cached_form || live_form.fields.size() != cached_form->field_count());
for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i)
needs_update = !cached_form->field(i)->SameFieldAs(live_form.fields[i]);
if (!needs_update)
return true;
// Note: We _must not_ remove the original version of the cached form from
// the list of |form_structures_|. Otherwise, we break parsing of the
// crowdsourcing server's response to our query.
if (!ParseForm(live_form, cached_form, updated_form))
return false;
// Annotate the updated form with its predicted types.
driver_->SendAutofillTypePredictionsToRenderer({*updated_form});
return true;
}
bool AutofillHandler::FindCachedForm(const FormData& form,
FormStructure** form_structure) const {
// Find the FormStructure that corresponds to |form|.
......
......@@ -21,6 +21,7 @@ class RectF;
namespace autofill {
class AutofillField;
struct FormData;
struct FormFieldData;
class FormStructure;
......@@ -160,6 +161,23 @@ class AutofillHandler {
AutofillDriver* driver() { return driver_; }
// Fills |form_structure| and |autofill_field| with the cached elements
// corresponding to |form| and |field|. This might have the side-effect of
// updating the cache. Returns false if the |form| is not autofillable, or if
// it is not already present in the cache and the cache is full.
bool GetCachedFormAndField(const FormData& form,
const FormFieldData& field,
FormStructure** form_structure,
AutofillField** autofill_field) WARN_UNUSED_RESULT;
// Re-parses |live_form| and adds the result to |form_structures_|.
// |cached_form| should be a pointer to the existing version of the form, or
// NULL if no cached version exists. The updated form is then written into
// |updated_form|. Returns false if the cache could not be updated.
bool UpdateCachedForm(const FormData& live_form,
const FormStructure* cached_form,
FormStructure** updated_form) WARN_UNUSED_RESULT;
// Fills |form_structure| cached element corresponding to |form|.
// Returns false if the cached element was not found.
bool FindCachedForm(const FormData& form,
......
......@@ -1411,42 +1411,6 @@ std::unique_ptr<FormStructure> AutofillManager::ValidateSubmittedForm(
return submitted_form;
}
bool AutofillManager::GetCachedFormAndField(const FormData& form,
const FormFieldData& field,
FormStructure** form_structure,
AutofillField** autofill_field) {
// Find the FormStructure that corresponds to |form|.
// If we do not have this form in our cache but it is parseable, we'll add it
// in the call to |UpdateCachedForm()|.
if (!FindCachedForm(form, form_structure) &&
!FormStructure(form).ShouldBeParsed()) {
return false;
}
// Update the cached form to reflect any dynamic changes to the form data, if
// necessary.
if (!UpdateCachedForm(form, *form_structure, form_structure))
return false;
// No data to return if there are no auto-fillable fields.
if (!(*form_structure)->autofill_count())
return false;
// Find the AutofillField that corresponds to |field|.
*autofill_field = nullptr;
for (const auto& current : **form_structure) {
if (current->SameFieldAs(field)) {
*autofill_field = current.get();
break;
}
}
// Even though we always update the cache, the field might not exist if the
// website disables autocomplete while the user is interacting with the form.
// See http://crbug.com/160476
return *autofill_field != nullptr;
}
AutofillField* AutofillManager::GetAutofillField(const FormData& form,
const FormFieldData& field) {
if (!personal_data_)
......@@ -1475,29 +1439,6 @@ bool AutofillManager::FormHasAddressField(const FormData& form) {
return false;
}
bool AutofillManager::UpdateCachedForm(const FormData& live_form,
const FormStructure* cached_form,
FormStructure** updated_form) {
bool needs_update =
(!cached_form || live_form.fields.size() != cached_form->field_count());
for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i)
needs_update = !cached_form->field(i)->SameFieldAs(live_form.fields[i]);
if (!needs_update)
return true;
// Note: We _must not_ remove the original version of the cached form from
// the list of |form_structures_|. Otherwise, we break parsing of the
// crowdsourcing server's response to our query.
if (!ParseForm(live_form, cached_form, updated_form))
return false;
// Annotate the updated form with its predicted types.
driver()->SendAutofillTypePredictionsToRenderer({*updated_form});
return true;
}
std::vector<Suggestion> AutofillManager::GetProfileSuggestions(
const FormStructure& form,
const FormFieldData& field,
......
......@@ -409,15 +409,6 @@ class AutofillManager : public AutofillHandler,
// or personal data.
std::unique_ptr<FormStructure> ValidateSubmittedForm(const FormData& form);
// Fills |form_structure| and |autofill_field| with the cached elements
// corresponding to |form| and |field|. This might have the side-effect of
// updating the cache. Returns false if the |form| is not autofillable, or if
// it is not already present in the cache and the cache is full.
bool GetCachedFormAndField(const FormData& form,
const FormFieldData& field,
FormStructure** form_structure,
AutofillField** autofill_field) WARN_UNUSED_RESULT;
// Returns the field corresponding to |form| and |field| that can be
// autofilled. Returns NULL if the field cannot be autofilled.
AutofillField* GetAutofillField(const FormData& form,
......@@ -428,14 +419,6 @@ class AutofillManager : public AutofillHandler,
// |FieldTypeGroup|.
bool FormHasAddressField(const FormData& form) WARN_UNUSED_RESULT;
// Re-parses |live_form| and adds the result to |form_structures_|.
// |cached_form| should be a pointer to the existing version of the form, or
// NULL if no cached version exists. The updated form is then written into
// |updated_form|. Returns false if the cache could not be updated.
bool UpdateCachedForm(const FormData& live_form,
const FormStructure* cached_form,
FormStructure** updated_form) WARN_UNUSED_RESULT;
// Returns a list of values from the stored profiles that match |type| and the
// value of |field| and returns the labels of the matching profiles. |labels|
// is filled with the Profile label.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment