Commit aef87c58 authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

Reimplement finding form logic.

The previous implementation relied on autofill_agent finding forms, but
it is only interested in address and credit card forms. Instead, we call
the javascript function in CWVAutofillController to obtain all forms we
care about.

Also renamed delegate method from didFind to didScan since the former
implies that at least one was found but in many cases there are 0.

Bug: 905546
Change-Id: Ibb3144e767fe2257c20176d61315568cfde076cb
Reviewed-on: https://chromium-review.googlesource.com/c/1339322
Commit-Queue: John Wu <jzw@chromium.org>
Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609048}
parent 07be96d8
......@@ -6,6 +6,7 @@
#include <memory>
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/mac/foundation_util.h"
......@@ -17,6 +18,7 @@
#include "components/autofill/ios/browser/autofill_driver_ios.h"
#include "components/autofill/ios/browser/autofill_driver_ios_bridge.h"
#include "components/autofill/ios/browser/autofill_switches.h"
#import "components/autofill/ios/browser/autofill_util.h"
#import "components/autofill/ios/browser/js_autofill_manager.h"
#import "components/autofill/ios/browser/js_suggestion_manager.h"
#import "components/autofill/ios/form_util/form_activity_observer_bridge.h"
......@@ -440,18 +442,7 @@ showUnmaskPromptForCard:(const autofill::CreditCard&)creditCard
- (void)propagateAutofillPredictionsForForms:
(const std::vector<autofill::FormStructure*>&)forms {
if (![_delegate respondsToSelector:@selector
(autofillController:didFindAutofillableForms:)]) {
return;
}
NSMutableArray<CWVAutofillForm*>* autofillForms = [NSMutableArray array];
for (autofill::FormStructure* formStructure : forms) {
CWVAutofillForm* autofillForm =
[[CWVAutofillForm alloc] initWithFormStructure:*formStructure];
[autofillForms addObject:autofillForm];
}
[_delegate autofillController:self
didFindAutofillableForms:[autofillForms copy]];
// Not supported.
}
#pragma mark - AutofillDriverIOSBridge
......@@ -474,6 +465,57 @@ showUnmaskPromptForCard:(const autofill::CreditCard&)creditCard
#pragma mark - CRWWebStateObserver
- (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success {
if (!success) {
return;
}
// Find all forms in the page and notify |delegate|.
if (![_delegate respondsToSelector:@selector
(autofillController:didScanForAutofillableForms:)]) {
return;
}
web::WebFramesManager* framesManager =
web::WebFramesManager::FromWebState(_webState);
DCHECK(framesManager);
web::WebFrame* webFrame = framesManager->GetMainWebFrame();
if (!webFrame) {
return;
}
GURL pageURL = _webState->GetLastCommittedURL();
GURL frameOrigin = webFrame->GetSecurityOrigin();
id completionHandler = ^(NSString* formJSON) {
std::vector<autofill::FormData> formDataVector;
bool success = autofill::ExtractFormsData(
formJSON, /*filtered=*/NO, /*form_name=*/base::string16(), pageURL,
frameOrigin, &formDataVector);
if (!success) {
return;
}
NSMutableArray<CWVAutofillForm*>* autofillForms = [NSMutableArray array];
for (const autofill::FormData& formData : formDataVector) {
autofill::FormStructure formStructure(formData);
formStructure.DetermineHeuristicTypes();
CWVAutofillForm* autofillForm =
[[CWVAutofillForm alloc] initWithFormStructure:formStructure];
if (autofillForm.type != CWVAutofillFormTypeUnknown) {
[autofillForms addObject:autofillForm];
}
}
[_delegate autofillController:self
didScanForAutofillableForms:[autofillForms copy]];
};
// Ignore empty forms.
NSUInteger minRequiredFieldsCount = 1;
[_JSAutofillManager
fetchFormsWithMinimumRequiredFieldsCount:minRequiredFieldsCount
inFrame:webFrame
completionHandler:completionHandler];
}
- (void)webState:(web::WebState*)webState
didRegisterFormActivity:(const autofill::FormActivityParams&)params
inFrame:(web::WebFrame*)frame {
......
......@@ -52,10 +52,18 @@ void WebViewWebMainParts::PreCreateThreads() {
switches::kSyncSupportSecondaryAccount.name,
switches::kSyncUSSAutofillWalletData.name},
",");
std::string disabled_features = base::JoinString(
{// TODO(crbug.com/873790): Remove after supporting user consents.
switches::kSyncUserConsentSeparateType.name,
// Allows form_structure.cc to run heuristics on single field forms.
// This is needed to find autofillable password forms with less than 3
// fields in CWVAutofillControllerDelegate's
// |autofillController:didScanForAutofillableForms:| method.
autofill::features::kAutofillEnforceMinRequiredFieldsForHeuristics.name},
",");
feature_list->InitializeFromCommandLine(
/*enable_features=*/enable_features,
// TODO(crbug.com/873790): Figure out if USER_CONSENTS need to be enabled.
/*disable_features=*/switches::kSyncUserConsentSeparateType.name);
/*disable_features=*/disabled_features);
base::FeatureList::SetInstance(std::move(feature_list));
#endif // BUILDFLAG(IOS_WEB_VIEW_ENABLE_SYNC)
}
......
......@@ -47,8 +47,9 @@ typedef NS_ENUM(NSInteger, CWVPasswordUserDecision) {
// Called to notify of all autofillable forms in the document after page load.
// Autofillable forms are any form that has the potential to be autofilled
// using a CWVAutofillSuggestion, regardless if any such suggestions exist yet.
// Note that |forms| may be empty, indicating no forms were found.
- (void)autofillController:(CWVAutofillController*)autofillController
didFindAutofillableForms:(NSArray<CWVAutofillForm*>*)forms;
didScanForAutofillableForms:(NSArray<CWVAutofillForm*>*)forms;
// Called when a form field element receives a "focus" event.
- (void)autofillController:(CWVAutofillController*)autofillController
......
......@@ -40,6 +40,11 @@
#pragma mark - CWVAutofillControllerDelegate methods
- (void)autofillController:(CWVAutofillController*)autofillController
didScanForAutofillableForms:(NSArray<CWVAutofillForm*>*)forms {
NSLog(@"%@ found %lu forms", NSStringFromSelector(_cmd), forms.count);
}
- (void)autofillController:(CWVAutofillController*)autofillController
didFocusOnFieldWithIdentifier:(NSString*)fieldIdentifier
fieldType:(NSString*)fieldType
......
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