Commit 2885b88f authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

Introduce CWVAutofillForm to represent a HTML form.

CWVAutofillControllerDelegate can be notified of forms found on the page
via a new delegate method. This can be used to better understand the
forms on the page and measure the success of the feature.

Bug: 905546
Change-Id: Ie9798387c678d6aac0ca10516331f7b1e6b8a5a1
Reviewed-on: https://chromium-review.googlesource.com/c/1337290Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: John Wu <jzw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608593}
parent 7d3f5923
......@@ -82,6 +82,7 @@ if (ios_web_view_enable_autofill) {
"public/cwv_autofill_controller_delegate.h",
"public/cwv_autofill_data_manager.h",
"public/cwv_autofill_data_manager_observer.h",
"public/cwv_autofill_form.h",
"public/cwv_autofill_profile.h",
"public/cwv_autofill_suggestion.h",
"public/cwv_credit_card.h",
......@@ -237,6 +238,8 @@ if (ios_web_view_enable_autofill) {
"internal/autofill/cwv_autofill_controller_internal.h",
"internal/autofill/cwv_autofill_data_manager.mm",
"internal/autofill/cwv_autofill_data_manager_internal.h",
"internal/autofill/cwv_autofill_form.mm",
"internal/autofill/cwv_autofill_form_internal.h",
"internal/autofill/cwv_autofill_profile.mm",
"internal/autofill/cwv_autofill_profile_internal.h",
"internal/autofill/cwv_autofill_suggestion.mm",
......@@ -385,6 +388,7 @@ test("ios_web_view_unittests") {
sources = [
"internal/autofill/cwv_autofill_controller_unittest.mm",
"internal/autofill/cwv_autofill_data_manager_unittest.mm",
"internal/autofill/cwv_autofill_form_unittest.mm",
"internal/autofill/cwv_autofill_profile_unittest.mm",
"internal/autofill/cwv_autofill_suggestion_unittest.mm",
"internal/autofill/cwv_credit_card_unittest.mm",
......
......@@ -14,6 +14,7 @@
namespace autofill {
class CreditCard;
class FormStructure;
} // namespace autofill
// WebView extension of AutofillClientIOSBridge.
......@@ -36,6 +37,10 @@ showUnmaskPromptForCard:(const autofill::CreditCard&)creditCard
// Bridge for AutofillClient's method |LoadRiskData|.
- (void)loadRiskData:(base::OnceCallback<void(const std::string&)>)callback;
// Bridge for AutofillClient's method |PropagateAutofillPredictions|.
- (void)propagateAutofillPredictionsForForms:
(const std::vector<autofill::FormStructure*>&)forms;
@end
#endif // IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_AUTOFILL_CLIENT_IOS_BRIDGE_H_
......@@ -11,6 +11,7 @@
#include "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/autofill_manager.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/popup_item_ids.h"
#import "components/autofill/ios/browser/autofill_agent.h"
#include "components/autofill/ios/browser/autofill_driver_ios.h"
......@@ -29,6 +30,7 @@
#import "ios/web/public/web_state/web_state_observer_bridge.h"
#include "ios/web_view/internal/app/application_context.h"
#import "ios/web_view/internal/autofill/cwv_autofill_client_ios_bridge.h"
#import "ios/web_view/internal/autofill/cwv_autofill_form_internal.h"
#import "ios/web_view/internal/autofill/cwv_autofill_suggestion_internal.h"
#import "ios/web_view/internal/autofill/cwv_credit_card_internal.h"
#import "ios/web_view/internal/autofill/cwv_credit_card_verifier_internal.h"
......@@ -436,6 +438,22 @@ showUnmaskPromptForCard:(const autofill::CreditCard&)creditCard
[_verifier loadRiskData:std::move(callback)];
}
- (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]];
}
#pragma mark - AutofillDriverIOSBridge
- (void)onFormDataFilled:(uint16_t)query_id
......
// Copyright 2018 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.
#import "ios/web_view/internal/autofill/cwv_autofill_form_internal.h"
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/form_structure.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation CWVAutofillForm
- (instancetype)initWithFormStructure:
(const autofill::FormStructure&)formStructure {
self = [super init];
if (self) {
_name = base::SysUTF16ToNSString(formStructure.form_name());
_type = CWVAutofillFormTypeUnknown;
std::set<autofill::FormType> formTypes = formStructure.GetFormTypes();
if (formTypes.find(autofill::ADDRESS_FORM) != formTypes.end()) {
_type |= CWVAutofillFormTypeAddresses;
}
if (formTypes.find(autofill::CREDIT_CARD_FORM) != formTypes.end()) {
_type |= CWVAutofillFormTypeCreditCards;
}
if (formTypes.find(autofill::PASSWORD_FORM) != formTypes.end()) {
_type |= CWVAutofillFormTypePasswords;
}
}
return self;
}
@end
// Copyright 2018 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 IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_AUTOFILL_FORM_INTERNAL_H_
#define IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_AUTOFILL_FORM_INTERNAL_H_
#import "ios/web_view/public/cwv_autofill_form.h"
NS_ASSUME_NONNULL_BEGIN
namespace autofill {
class FormStructure;
}
@interface CWVAutofillForm ()
// |formStructure| is only used to populate properties of this class and will
// not be used by this class past initialization.
- (instancetype)initWithFormStructure:
(const autofill::FormStructure&)formStructure NS_DESIGNATED_INITIALIZER;
@end
NS_ASSUME_NONNULL_END
#endif // IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_AUTOFILL_FORM_INTERNAL_H_
// Copyright 2018 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.
#import "ios/web_view/internal/autofill/cwv_autofill_form_internal.h"
#import <Foundation/Foundation.h>
#include <memory>
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/form_structure.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace ios_web_view {
using CWVAutofillFormTest = PlatformTest;
// Tests CWVAutofillForm initialization.
TEST_F(CWVAutofillFormTest, Initialization) {
autofill::FormData form_data;
autofill::test::CreateTestAddressFormData(&form_data);
std::unique_ptr<autofill::FormStructure> form_structure =
std::make_unique<autofill::FormStructure>(form_data);
form_structure->DetermineHeuristicTypes();
CWVAutofillForm* form =
[[CWVAutofillForm alloc] initWithFormStructure:*form_structure];
EXPECT_NSEQ(base::SysUTF16ToNSString(form_data.name), form.name);
EXPECT_TRUE(form.type & CWVAutofillFormTypeAddresses);
EXPECT_FALSE(form.type & CWVAutofillFormTypeCreditCards);
EXPECT_FALSE(form.type & CWVAutofillFormTypePasswords);
}
} // namespace ios_web_view
......@@ -212,7 +212,9 @@ void WebViewAutofillClientIOS::UpdateAutofillPopupDataListValues(
void WebViewAutofillClientIOS::PropagateAutofillPredictions(
content::RenderFrameHost* rfh,
const std::vector<FormStructure*>& forms) {}
const std::vector<FormStructure*>& forms) {
[bridge_ propagateAutofillPredictionsForForms:forms];
}
void WebViewAutofillClientIOS::DidFillOrPreviewField(
const base::string16& autofilled_value,
......
......@@ -10,6 +10,7 @@
NS_ASSUME_NONNULL_BEGIN
@class CWVAutofillController;
@class CWVAutofillForm;
@class CWVAutofillFormSuggestion;
@class CWVCreditCard;
@class CWVCreditCardVerifier;
......@@ -43,6 +44,12 @@ typedef NS_ENUM(NSInteger, CWVPasswordUserDecision) {
@optional
// 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.
- (void)autofillController:(CWVAutofillController*)autofillController
didFindAutofillableForms:(NSArray<CWVAutofillForm*>*)forms;
// Called when a form field element receives a "focus" event.
- (void)autofillController:(CWVAutofillController*)autofillController
didFocusOnFieldWithIdentifier:(NSString*)fieldIdentifier
......
// Copyright 2018 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 IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_FORM_H_
#define IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_FORM_H_
#import <Foundation/Foundation.h>
#import "cwv_export.h"
NS_ASSUME_NONNULL_BEGIN
// The different form types used in autofilling.
typedef NS_OPTIONS(NSInteger, CWVAutofillFormType) {
// The type of form is unknown.
CWVAutofillFormTypeUnknown = 0,
// Address forms that can be autofilled with saved profiles.
CWVAutofillFormTypeAddresses = 1 << 0,
// Credit card forms that can be autofilled with saved credit cards.
CWVAutofillFormTypeCreditCards = 1 << 1,
// Log in forms that can be autofilled with saved credentials.
CWVAutofillFormTypePasswords = 1 << 2
};
// Contains information on a HTML <form> that may be autofilled.
CWV_EXPORT
@interface CWVAutofillForm : NSObject
// The name attribute of the form.
@property(nullable, nonatomic, copy, readonly) NSString* name;
// Indicates the types of form this may be. Note that a form may contain
// addresses, credit cards, and passwords.
@property(nonatomic, readonly) CWVAutofillFormType type;
- (instancetype)init NS_UNAVAILABLE;
@end
NS_ASSUME_NONNULL_END
#endif // IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_FORM_H_
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