Commit 87190aa9 authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

Define CWVAutofillProfile, a wrapper around autofill::AutofillProfile.

This is needed so the client can present a list of saved autofill
profiles.

Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I95438aa53d6ee77d842892e6b7bb1b88f2e29c40
Reviewed-on: https://chromium-review.googlesource.com/1006260
Commit-Queue: John Wu <jzw@chromium.org>
Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550066}
parent 03b76edf
...@@ -74,6 +74,7 @@ if (ios_web_view_enable_autofill) { ...@@ -74,6 +74,7 @@ if (ios_web_view_enable_autofill) {
"public/cwv_autofill_controller.h", "public/cwv_autofill_controller.h",
"public/cwv_autofill_controller_delegate.h", "public/cwv_autofill_controller_delegate.h",
"public/cwv_autofill_data_manager.h", "public/cwv_autofill_data_manager.h",
"public/cwv_autofill_profile.h",
"public/cwv_autofill_suggestion.h", "public/cwv_autofill_suggestion.h",
"public/cwv_preferences_autofill.h", "public/cwv_preferences_autofill.h",
"public/cwv_web_view_autofill.h", "public/cwv_web_view_autofill.h",
...@@ -187,9 +188,11 @@ if (ios_web_view_enable_signin) { ...@@ -187,9 +188,11 @@ if (ios_web_view_enable_signin) {
if (ios_web_view_enable_autofill) { if (ios_web_view_enable_autofill) {
ios_web_view_sources += [ ios_web_view_sources += [
"internal/autofill/cwv_autofill_controller.mm", "internal/autofill/cwv_autofill_controller.mm",
"internal/autofill/cwv_autofill_controller_internal.h",
"internal/autofill/cwv_autofill_data_manager.mm", "internal/autofill/cwv_autofill_data_manager.mm",
"internal/autofill/cwv_autofill_data_manager_internal.h", "internal/autofill/cwv_autofill_data_manager_internal.h",
"internal/autofill/cwv_autofill_controller_internal.h", "internal/autofill/cwv_autofill_profile.mm",
"internal/autofill/cwv_autofill_profile_internal.h",
"internal/autofill/cwv_autofill_suggestion.mm", "internal/autofill/cwv_autofill_suggestion.mm",
"internal/autofill/cwv_autofill_suggestion_internal.h", "internal/autofill/cwv_autofill_suggestion_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_profile_internal.h"
#include <objc/runtime.h>
#include <map>
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "ios/web_view/internal/app/application_context.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
std::map<SEL, autofill::ServerFieldType> kGettersToType = {
{@selector(name), autofill::NAME_FULL},
{@selector(company), autofill::COMPANY_NAME},
{@selector(address1), autofill::ADDRESS_HOME_LINE1},
{@selector(address2), autofill::ADDRESS_HOME_LINE2},
{@selector(city), autofill::ADDRESS_HOME_CITY},
{@selector(state), autofill::ADDRESS_HOME_STATE},
{@selector(zipcode), autofill::ADDRESS_HOME_ZIP},
{@selector(country), autofill::ADDRESS_HOME_COUNTRY},
{@selector(phone), autofill::PHONE_HOME_WHOLE_NUMBER},
{@selector(email), autofill::EMAIL_ADDRESS},
};
std::map<SEL, autofill::ServerFieldType> kSettersToType = {
{@selector(setName:), autofill::NAME_FULL},
{@selector(setCompany:), autofill::COMPANY_NAME},
{@selector(setAddress1:), autofill::ADDRESS_HOME_LINE1},
{@selector(setAddress2:), autofill::ADDRESS_HOME_LINE2},
{@selector(setCity:), autofill::ADDRESS_HOME_CITY},
{@selector(setState:), autofill::ADDRESS_HOME_STATE},
{@selector(setZipcode:), autofill::ADDRESS_HOME_ZIP},
{@selector(setCountry:), autofill::ADDRESS_HOME_COUNTRY},
{@selector(setPhone:), autofill::PHONE_HOME_WHOLE_NUMBER},
{@selector(setEmail:), autofill::EMAIL_ADDRESS},
};
} // namespace
@implementation CWVAutofillProfile {
autofill::AutofillProfile _internalProfile;
}
@dynamic name, company, address1, address2, city, state, zipcode, country,
phone, email;
+ (void)initialize {
if (self != [CWVAutofillProfile class]) {
return;
}
// Add all getters and setters dynamically.
for (const auto& pair : kGettersToType) {
class_addMethod([self class], pair.first, (IMP)propertyGetter, "@@:");
}
for (const auto& pair : kSettersToType) {
class_addMethod([self class], pair.first, (IMP)propertySetter, "v@:@");
}
}
NSString* propertyGetter(CWVAutofillProfile* self, SEL _cmd) {
DCHECK(kGettersToType.find(_cmd) != kGettersToType.end());
autofill::ServerFieldType type = kGettersToType[_cmd];
const std::string& locale =
ios_web_view::ApplicationContext::GetInstance()->GetApplicationLocale();
const base::string16& value =
self->_internalProfile.GetInfo(autofill::AutofillType(type), locale);
return base::SysUTF16ToNSString(value);
}
void propertySetter(CWVAutofillProfile* self, SEL _cmd, NSString* value) {
DCHECK(kSettersToType.find(_cmd) != kSettersToType.end());
autofill::ServerFieldType type = kSettersToType[_cmd];
const std::string& locale =
ios_web_view::ApplicationContext::GetInstance()->GetApplicationLocale();
self->_internalProfile.SetInfo(type, base::SysNSStringToUTF16(value), locale);
}
- (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile {
self = [super init];
if (self) {
_internalProfile = profile;
}
return self;
}
#pragma mark - Internal Methods
- (autofill::AutofillProfile*)internalProfile {
return &_internalProfile;
}
@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_PROFILE_INTERNAL_H_
#define IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_AUTOFILL_PROFILE_INTERNAL_H_
#import "ios/web_view/public/cwv_autofill_profile.h"
namespace autofill {
class AutofillProfile;
} // namespace autofill
@interface CWVAutofillProfile ()
// The internal autofill profile that is wrapped by this object.
@property(nonatomic, readonly) autofill::AutofillProfile* internalProfile;
- (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile
NS_DESIGNATED_INITIALIZER;
@end
#endif // IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_AUTOFILL_PROFILE_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.
#ifndef IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_PROFILE_H_
#define IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_PROFILE_H_
#import <Foundation/Foundation.h>
#import "cwv_export.h"
NS_ASSUME_NONNULL_BEGIN
CWV_EXPORT
// Represents a profile for autofilling address forms.
@interface CWVAutofillProfile : NSObject
// The full name. e.g. "Homer Simpson".
@property(nonatomic, copy, nullable) NSString* name;
// The company name. e.g. "Google".
@property(nonatomic, copy, nullable) NSString* company;
// The first line of the address. e.g. "123 Main Street".
@property(nonatomic, copy, nullable) NSString* address1;
// The second line of the address. e.g. "Apt #1337".
@property(nonatomic, copy, nullable) NSString* address2;
// The city name. e.g. "Springfield".
@property(nonatomic, copy, nullable) NSString* city;
// The state name. e.g. "IL" or "Illinois".
@property(nonatomic, copy, nullable) NSString* state;
// The zipcode. e.g. "55123".
@property(nonatomic, copy, nullable) NSString* zipcode;
// The country name. e.g. "USA".
@property(nonatomic, copy, nullable) NSString* country;
// The phone number. e.g. "310-310-6000".
@property(nonatomic, copy, nullable) NSString* phone;
// The email. e.g. "hjs@aol.com".
@property(nonatomic, copy, nullable) NSString* email;
- (instancetype)init NS_UNAVAILABLE;
@end
NS_ASSUME_NONNULL_END
#endif // IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_PROFILE_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