Commit 5bddaead authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

New API for finding forms in CWVAutofillController

Converting from a public method to a delegate based one so we can share
the form fetching implementation with AutofillAgent.

Bug: 1030451
Change-Id: I5e5c7e9ee3abac5769b79dc20d96f532a6a19d6c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1952030
Commit-Queue: John Wu <jzw@chromium.org>
Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721836}
parent e3088be5
......@@ -348,46 +348,6 @@ fetchNonPasswordSuggestionsForFormWithName:(NSString*)formName
completionHandler:completionHandler];
}
- (void)findAllFormsWithCompletionHandler:
(void (^)(NSArray<CWVAutofillForm*>*))completionHandler {
web::WebFramesManager* framesManager = _webState->GetWebFramesManager();
DCHECK(framesManager);
web::WebFrame* webFrame = framesManager->GetMainWebFrame();
if (!webFrame) {
completionHandler(nil);
return;
}
GURL pageURL = _webState->GetLastCommittedURL();
GURL frameOrigin = webFrame->GetSecurityOrigin();
id fetchCompletionHandler = ^(NSString* formJSON) {
std::vector<autofill::FormData> formDataVector;
bool success = autofill::ExtractFormsData(
formJSON, /*filtered=*/NO, /*form_name=*/base::string16(), pageURL,
frameOrigin, &formDataVector);
if (!success) {
completionHandler(nil);
return;
}
NSMutableArray<CWVAutofillForm*>* autofillForms = [NSMutableArray array];
for (const autofill::FormData& formData : formDataVector) {
autofill::FormStructure formStructure(formData);
formStructure.DetermineHeuristicTypes();
CWVAutofillForm* autofillForm =
[[CWVAutofillForm alloc] initWithFormStructure:formStructure];
[autofillForms addObject:autofillForm];
}
completionHandler([autofillForms copy]);
};
// Ignore empty forms.
NSUInteger minRequiredFieldsCount = 1;
[_JSAutofillManager
fetchFormsWithMinimumRequiredFieldsCount:minRequiredFieldsCount
inFrame:webFrame
completionHandler:fetchCompletionHandler];
}
#pragma mark - Utility Methods
- (autofill::AutofillManager*)autofillManagerForFrame:(web::WebFrame*)frame {
......@@ -541,7 +501,20 @@ showUnmaskPromptForCard:(const autofill::CreditCard&)creditCard
- (void)handleParsedForms:(const std::vector<autofill::FormStructure*>&)forms
inFrame:(web::WebFrame*)frame {
// TODO(crbug.com/1030451): Pass through in CVWAutofillControllerDelegate.
if (![_delegate respondsToSelector:@selector(autofillController:
didFindForms:frameID:)]) {
return;
}
NSMutableArray<CWVAutofillForm*>* autofillForms = [NSMutableArray array];
for (autofill::FormStructure* form : forms) {
CWVAutofillForm* autofillForm =
[[CWVAutofillForm alloc] initWithFormStructure:*form];
[autofillForms addObject:autofillForm];
}
[_delegate autofillController:self
didFindForms:autofillForms
frameID:base::SysUTF8ToNSString(frame->GetFrameId())];
}
- (void)fillFormDataPredictions:
......@@ -552,22 +525,6 @@ showUnmaskPromptForCard:(const autofill::CreditCard&)creditCard
#pragma mark - CRWWebStateObserver
- (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success {
if (!success) {
return;
}
web::WebFramesManager* framesManager = _webState->GetWebFramesManager();
DCHECK(framesManager);
web::WebFrame* webFrame = framesManager->GetMainWebFrame();
if (!webFrame) {
return;
}
// Start listening for any form mutations.
[_JSAutofillManager toggleTrackingFormMutations:YES inFrame:webFrame];
}
- (void)webState:(web::WebState*)webState
didRegisterFormActivity:(const autofill::FormActivityParams&)params
inFrame:(web::WebFrame*)frame {
......@@ -614,11 +571,6 @@ showUnmaskPromptForCard:(const autofill::CreditCard&)creditCard
frameID:nsFrameID
value:nsValue];
}
} else if (params.type == "form_changed") {
if ([_delegate respondsToSelector:@selector
(autofillControllerDidInsertFormElements:)]) {
[_delegate autofillControllerDidInsertFormElements:self];
}
}
}
......
......@@ -339,26 +339,4 @@ TEST_F(CWVAutofillControllerTest, SubmitCallback) {
}
}
// Tests CWVAutofillController delegate form insertion callback is invoked.
TEST_F(CWVAutofillControllerTest, DidInsertFormElementsCallback) {
id delegate = OCMProtocolMock(@protocol(CWVAutofillControllerDelegate));
autofill_controller_.delegate = delegate;
// [delegate expect] returns an autoreleased object, but it must be destroyed
// before this test exits to avoid holding on to |autofill_controller_|.
@autoreleasepool {
[[delegate expect]
autofillControllerDidInsertFormElements:autofill_controller_];
autofill::FormActivityParams params;
params.frame_id = base::SysNSStringToUTF8(kTestFrameId);
params.type = "form_changed";
web::FakeWebFrame frame(base::SysNSStringToUTF8(kTestFrameId), true,
GURL::EmptyGURL());
test_form_activity_tab_helper_->FormActivityRegistered(&frame, params);
[delegate verify];
}
}
} // namespace ios_web_view
......@@ -83,12 +83,6 @@ CWV_EXPORT
- (void)checkIfPreviousAndNextFieldsAreAvailableForFocusWithCompletionHandler:
(void (^)(BOOL previous, BOOL next))completionHandler;
// Finds all non-empty (at least 1 field) forms in the current page.
// |completionHandler| will be called with an array if successful, nil
// otherwise.
- (void)findAllFormsWithCompletionHandler:
(void (^)(NSArray<CWVAutofillForm*>* _Nullable forms))completionHandler;
@end
NS_ASSUME_NONNULL_END
......
......@@ -10,6 +10,7 @@
NS_ASSUME_NONNULL_BEGIN
@class CWVAutofillController;
@class CWVAutofillForm;
@class CWVAutofillFormSuggestion;
@class CWVAutofillProfile;
@class CWVCreditCard;
......@@ -71,12 +72,12 @@ typedef NS_ENUM(NSInteger, CWVPasswordUserDecision) {
userInitiated:(BOOL)userInitiated
isMainFrame:(BOOL)isMainFrame;
// Called when a form related element is inserted into the DOM.
// A form related element includes forms, inputs, selects, and options.
// This callback is throttled and will group together multiple inserts that are
// close together in time into one invocation.
- (void)autofillControllerDidInsertFormElements:
(CWVAutofillController*)autofillController;
// Called when |forms| are found in a frame with |frameID|.
// Will be called after initial load and after any form mutations.
// Always includes all forms in the frame.
- (void)autofillController:(CWVAutofillController*)autofillController
didFindForms:(NSArray<CWVAutofillForm*>*)forms
frameID:(NSString*)frameID;
// Called when it is possible to save a new autofill profile used in filling
// address forms. This is usually invoked after successfully submitting an
......
......@@ -108,9 +108,16 @@
// Not implemented.
}
- (void)autofillControllerDidInsertFormElements:
(CWVAutofillController*)autofillController {
// Not implemented.
- (void)autofillController:(CWVAutofillController*)autofillController
didFindForms:(NSArray<CWVAutofillForm*>*)forms
frameID:(NSString*)frameID {
if (forms.count == 0) {
return;
}
NSArray<NSString*>* debugDescriptions =
[forms valueForKey:NSStringFromSelector(@selector(debugDescription))];
NSLog(@"Found forms in frame %@\n%@", frameID, debugDescriptions);
}
- (void)autofillController:(CWVAutofillController*)autofillController
......
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