Commit 1976e41c authored by Tao Bai's avatar Tao Bai Committed by Commit Bot

[Autofill] Parse forms in AutofillHandler::OnFormsSeen.

This patch parses forms in AutofillHandler::OnFormsSeen(), so the
code could be shared between chrome and WebView autofill.

The parse is disabled in WebView autofill now.

Bug: 849913
Change-Id: I25260fa87eb59c2cc2d9060a1d92acbca0254e84
Reviewed-on: https://chromium-review.googlesource.com/1108921Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: default avatarRoger McFarlane <rogerm@chromium.org>
Commit-Queue: Tao Bai <michaelbai@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570533}
parent 01e8678c
...@@ -34,6 +34,36 @@ void AutofillHandler::OnFormSubmitted(const FormData& form, ...@@ -34,6 +34,36 @@ void AutofillHandler::OnFormSubmitted(const FormData& form,
OnFormSubmittedImpl(form, known_success, source, timestamp); OnFormSubmittedImpl(form, known_success, source, timestamp);
} }
void AutofillHandler::OnFormsSeen(const std::vector<FormData>& forms,
const base::TimeTicks timestamp) {
if (!IsValidFormDataVector(forms) || !driver_->RendererIsAvailable())
return;
// This should be called even forms is empty, AutofillProviderAndroid uses
// this event to detect form submission.
if (!ShouldParseForms(forms, timestamp))
return;
if (forms.empty())
return;
std::vector<FormStructure*> form_structures;
for (const FormData& form : forms) {
const auto parse_form_start_time = TimeTicks::Now();
FormStructure* form_structure = nullptr;
if (!ParseForm(form, /*cached_form=*/nullptr, &form_structure))
continue;
DCHECK(form_structure);
if (form_structure == nullptr)
continue;
form_structures.push_back(form_structure);
AutofillMetrics::LogParseFormTiming(TimeTicks::Now() -
parse_form_start_time);
}
if (!form_structures.empty())
OnFormsParsed(form_structures, timestamp);
}
void AutofillHandler::OnTextFieldDidChange(const FormData& form, void AutofillHandler::OnTextFieldDidChange(const FormData& form,
const FormFieldData& field, const FormFieldData& field,
const gfx::RectF& bounding_box, const gfx::RectF& bounding_box,
......
...@@ -72,6 +72,10 @@ class AutofillHandler { ...@@ -72,6 +72,10 @@ class AutofillHandler {
SubmissionSource source, SubmissionSource source,
base::TimeTicks timestamp); base::TimeTicks timestamp);
// Invoked when |forms| has been detected.
void OnFormsSeen(const std::vector<FormData>& forms,
const base::TimeTicks timestamp);
// Invoked when focus is no longer on form. // Invoked when focus is no longer on form.
virtual void OnFocusNoLongerOnForm() = 0; virtual void OnFocusNoLongerOnForm() = 0;
...@@ -83,10 +87,6 @@ class AutofillHandler { ...@@ -83,10 +87,6 @@ class AutofillHandler {
// Invoked when preview autofill value has been shown. // Invoked when preview autofill value has been shown.
virtual void OnDidPreviewAutofillFormData() = 0; virtual void OnDidPreviewAutofillFormData() = 0;
// Invoked when |forms| has been detected.
virtual void OnFormsSeen(const std::vector<FormData>& forms,
const base::TimeTicks timestamp) = 0;
// Invoked when textfeild editing ended // Invoked when textfeild editing ended
virtual void OnDidEndTextFieldEditing() = 0; virtual void OnDidEndTextFieldEditing() = 0;
...@@ -146,6 +146,15 @@ class AutofillHandler { ...@@ -146,6 +146,15 @@ class AutofillHandler {
const FormFieldData& field, const FormFieldData& field,
const gfx::RectF& bounding_box) = 0; const gfx::RectF& bounding_box) = 0;
// Return whether the |forms| from OnFormSeen() should be parsed to
// form_structures.
virtual bool ShouldParseForms(const std::vector<FormData>& forms,
const base::TimeTicks timestamp) = 0;
// Invoked when forms from OnFormsSeen() has been parsed to |form_structures|.
virtual void OnFormsParsed(const std::vector<FormStructure*>& form_structures,
const base::TimeTicks timestamp) = 0;
AutofillDriver* driver() { return driver_; } AutofillDriver* driver() { return driver_; }
// Fills |form_structure| cached element corresponding to |form|. // Fills |form_structure| cached element corresponding to |form|.
......
...@@ -61,6 +61,17 @@ void AutofillHandlerProxy::OnSelectControlDidChangeImpl( ...@@ -61,6 +61,17 @@ void AutofillHandlerProxy::OnSelectControlDidChangeImpl(
provider_->OnSelectControlDidChange(this, form, field, bounding_box); provider_->OnSelectControlDidChange(this, form, field, bounding_box);
} }
bool AutofillHandlerProxy::ShouldParseForms(const std::vector<FormData>& forms,
const base::TimeTicks timestamp) {
provider_->OnFormsSeen(this, forms, timestamp);
// Don't use form_structure.
return false;
}
void AutofillHandlerProxy::OnFormsParsed(
const std::vector<FormStructure*>& form_structures,
const base::TimeTicks timestamp) {}
void AutofillHandlerProxy::OnFocusNoLongerOnForm() { void AutofillHandlerProxy::OnFocusNoLongerOnForm() {
provider_->OnFocusNoLongerOnForm(this); provider_->OnFocusNoLongerOnForm(this);
} }
...@@ -73,11 +84,6 @@ void AutofillHandlerProxy::OnDidFillAutofillFormData( ...@@ -73,11 +84,6 @@ void AutofillHandlerProxy::OnDidFillAutofillFormData(
void AutofillHandlerProxy::OnDidPreviewAutofillFormData() {} void AutofillHandlerProxy::OnDidPreviewAutofillFormData() {}
void AutofillHandlerProxy::OnFormsSeen(const std::vector<FormData>& forms,
const base::TimeTicks timestamp) {
provider_->OnFormsSeen(this, forms, timestamp);
}
void AutofillHandlerProxy::OnDidEndTextFieldEditing() {} void AutofillHandlerProxy::OnDidEndTextFieldEditing() {}
void AutofillHandlerProxy::OnHidePopup() {} void AutofillHandlerProxy::OnHidePopup() {}
......
...@@ -24,10 +24,6 @@ class AutofillHandlerProxy : public AutofillHandler { ...@@ -24,10 +24,6 @@ class AutofillHandlerProxy : public AutofillHandler {
const base::TimeTicks timestamp) override; const base::TimeTicks timestamp) override;
void OnDidPreviewAutofillFormData() override; void OnDidPreviewAutofillFormData() override;
void OnFormsSeen(const std::vector<FormData>& forms,
const base::TimeTicks timestamp) override;
void OnDidEndTextFieldEditing() override; void OnDidEndTextFieldEditing() override;
void OnHidePopup() override; void OnHidePopup() override;
void OnSetDataList(const std::vector<base::string16>& values, void OnSetDataList(const std::vector<base::string16>& values,
...@@ -68,6 +64,12 @@ class AutofillHandlerProxy : public AutofillHandler { ...@@ -68,6 +64,12 @@ class AutofillHandlerProxy : public AutofillHandler {
const FormFieldData& field, const FormFieldData& field,
const gfx::RectF& bounding_box) override; const gfx::RectF& bounding_box) override;
bool ShouldParseForms(const std::vector<FormData>& forms,
const base::TimeTicks timestamp) override;
void OnFormsParsed(const std::vector<FormStructure*>& form_structures,
const base::TimeTicks timestamp) override;
private: private:
AutofillProvider* provider_; AutofillProvider* provider_;
base::WeakPtrFactory<AutofillHandlerProxy> weak_ptr_factory_; base::WeakPtrFactory<AutofillHandlerProxy> weak_ptr_factory_;
......
...@@ -328,28 +328,15 @@ bool AutofillManager::ShouldShowCreditCardSigninPromo( ...@@ -328,28 +328,15 @@ bool AutofillManager::ShouldShowCreditCardSigninPromo(
return false; return false;
} }
void AutofillManager::OnFormsSeen(const std::vector<FormData>& forms, bool AutofillManager::ShouldParseForms(const std::vector<FormData>& forms,
const TimeTicks timestamp) { const base::TimeTicks timestamp) {
if (!IsValidFormDataVector(forms))
return;
if (!driver()->RendererIsAvailable())
return;
bool enabled = IsAutofillEnabled(); bool enabled = IsAutofillEnabled();
if (!has_logged_autofill_enabled_) { if (!has_logged_autofill_enabled_) {
AutofillMetrics::LogIsAutofillEnabledAtPageLoad(enabled); AutofillMetrics::LogIsAutofillEnabledAtPageLoad(enabled);
has_logged_autofill_enabled_ = true; has_logged_autofill_enabled_ = true;
} }
if (!enabled) return enabled;
return;
for (const FormData& form : forms) {
forms_loaded_timestamps_[form] = timestamp;
}
ParseForms(forms);
} }
void AutofillManager::OnFormSubmittedImpl(const FormData& form, void AutofillManager::OnFormSubmittedImpl(const FormData& form,
...@@ -1575,25 +1562,23 @@ std::vector<Suggestion> AutofillManager::GetCreditCardSuggestions( ...@@ -1575,25 +1562,23 @@ std::vector<Suggestion> AutofillManager::GetCreditCardSuggestions(
return suggestions; return suggestions;
} }
void AutofillManager::ParseForms(const std::vector<FormData>& forms) { void AutofillManager::OnFormsParsed(
if (forms.empty()) const std::vector<FormStructure*>& form_structures,
return; const base::TimeTicks timestamp) {
DCHECK(!form_structures.empty());
// Setup the url for metrics that we will collect for this form. // Setup the url for metrics that we will collect for this form.
form_interactions_ukm_logger_->OnFormsParsed( form_interactions_ukm_logger_->OnFormsParsed(
forms[0].main_frame_origin.GetURL(), client_->GetUkmSourceId()); form_structures[0]->ToFormData().main_frame_origin.GetURL(),
client_->GetUkmSourceId());
std::vector<FormStructure*> non_queryable_forms; std::vector<FormStructure*> non_queryable_forms;
std::vector<FormStructure*> queryable_forms; std::vector<FormStructure*> queryable_forms;
std::set<FormType> form_types; std::set<FormType> form_types;
for (const FormData& form : forms) { for (FormStructure* form_structure : form_structures) {
const auto parse_form_start_time = TimeTicks::Now(); form_structure->DetermineHeuristicTypes(client_->GetUkmRecorder(),
client_->GetUkmSourceId());
FormStructure* form_structure = nullptr; forms_loaded_timestamps_[form_structure->ToFormData()] = timestamp;
if (!ParseFormInternal(form, /*cached_form=*/nullptr, &form_structure))
continue;
DCHECK(form_structure);
std::set<FormType> current_form_types = form_structure->GetFormTypes(); std::set<FormType> current_form_types = form_structure->GetFormTypes();
form_types.insert(current_form_types.begin(), current_form_types.end()); form_types.insert(current_form_types.begin(), current_form_types.end());
// Set aside forms with method GET or author-specified types, so that they // Set aside forms with method GET or author-specified types, so that they
...@@ -1603,9 +1588,6 @@ void AutofillManager::ParseForms(const std::vector<FormData>& forms) { ...@@ -1603,9 +1588,6 @@ void AutofillManager::ParseForms(const std::vector<FormData>& forms) {
else else
non_queryable_forms.push_back(form_structure); non_queryable_forms.push_back(form_structure);
AutofillMetrics::LogParseFormTiming(TimeTicks::Now() -
parse_form_start_time);
// If a form with the same name was previously filled, and there has not // If a form with the same name was previously filled, and there has not
// been a refill attempt on that form yet, start the process of triggering a // been a refill attempt on that form yet, start the process of triggering a
// refill. // refill.
...@@ -1620,12 +1602,13 @@ void AutofillManager::ParseForms(const std::vector<FormData>& forms) { ...@@ -1620,12 +1602,13 @@ void AutofillManager::ParseForms(const std::vector<FormData>& forms) {
if (filling_context->on_refill_timer.IsRunning()) if (filling_context->on_refill_timer.IsRunning())
filling_context->on_refill_timer.AbandonAndStop(); filling_context->on_refill_timer.AbandonAndStop();
// Can TriggerRefill get FormData from FormStructure?
filling_context->on_refill_timer.Start( filling_context->on_refill_timer.Start(
FROM_HERE, FROM_HERE,
base::TimeDelta::FromMilliseconds(kWaitTimeForDynamicFormsMs), base::TimeDelta::FromMilliseconds(kWaitTimeForDynamicFormsMs),
base::BindRepeating(&AutofillManager::TriggerRefill, base::BindRepeating(&AutofillManager::TriggerRefill,
weak_ptr_factory_.GetWeakPtr(), form, weak_ptr_factory_.GetWeakPtr(),
form_structure)); form_structure->ToFormData(), form_structure));
} }
} }
...@@ -1647,7 +1630,8 @@ void AutofillManager::ParseForms(const std::vector<FormData>& forms) { ...@@ -1647,7 +1630,8 @@ void AutofillManager::ParseForms(const std::vector<FormData>& forms) {
// prompt for credit card assisted filling. Upon accepting the infobar, the // prompt for credit card assisted filling. Upon accepting the infobar, the
// form will automatically be filled with the user's information through this // form will automatically be filled with the user's information through this
// class' FillCreditCardForm(). // class' FillCreditCardForm().
if (autofill_assistant_.CanShowCreditCardAssist(form_structures())) { if (autofill_assistant_.CanShowCreditCardAssist(
AutofillHandler::form_structures())) {
const std::vector<CreditCard*> cards = const std::vector<CreditCard*> cards =
personal_data_->GetCreditCardsToSuggest( personal_data_->GetCreditCardsToSuggest(
client_->AreServerCardsSupported()); client_->AreServerCardsSupported());
......
...@@ -180,8 +180,6 @@ class AutofillManager : public AutofillHandler, ...@@ -180,8 +180,6 @@ class AutofillManager : public AutofillHandler,
void OnDidFillAutofillFormData(const FormData& form, void OnDidFillAutofillFormData(const FormData& form,
const base::TimeTicks timestamp) override; const base::TimeTicks timestamp) override;
void OnDidPreviewAutofillFormData() override; void OnDidPreviewAutofillFormData() override;
void OnFormsSeen(const std::vector<FormData>& forms,
const base::TimeTicks timestamp) override;
void OnDidEndTextFieldEditing() override; void OnDidEndTextFieldEditing() override;
void OnHidePopup() override; void OnHidePopup() override;
void OnSetDataList(const std::vector<base::string16>& values, void OnSetDataList(const std::vector<base::string16>& values,
...@@ -259,6 +257,10 @@ class AutofillManager : public AutofillHandler, ...@@ -259,6 +257,10 @@ class AutofillManager : public AutofillHandler,
void OnSelectControlDidChangeImpl(const FormData& form, void OnSelectControlDidChangeImpl(const FormData& form,
const FormFieldData& field, const FormFieldData& field,
const gfx::RectF& bounding_box) override; const gfx::RectF& bounding_box) override;
bool ShouldParseForms(const std::vector<FormData>& forms,
const base::TimeTicks timestamp) override;
void OnFormsParsed(const std::vector<FormStructure*>& form_structures,
const base::TimeTicks timestamp) override;
AutofillMetrics::FormInteractionsUkmLogger* form_interactions_ukm_logger() { AutofillMetrics::FormInteractionsUkmLogger* form_interactions_ukm_logger() {
return form_interactions_ukm_logger_.get(); return form_interactions_ukm_logger_.get();
......
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