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

Remove dynamic method definitions in CWVAutofillProfile.

Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I33931655a1e138c1caa96016ca1639f17a0355ad
Reviewed-on: https://chromium-review.googlesource.com/1014657
Commit-Queue: John Wu <jzw@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551395}
parent fd6fa0b5
...@@ -4,10 +4,6 @@ ...@@ -4,10 +4,6 @@
#import "ios/web_view/internal/autofill/cwv_autofill_profile_internal.h" #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 "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/autofill_profile.h" #include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_type.h" #include "components/autofill/core/browser/autofill_type.h"
...@@ -17,80 +13,107 @@ ...@@ -17,80 +13,107 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
namespace { @interface CWVAutofillProfile ()
std::map<SEL, autofill::ServerFieldType> kGettersToType = {
{@selector(name), autofill::NAME_FULL}, // Sets |value| for |type| in |_internalProfile|.
{@selector(company), autofill::COMPANY_NAME}, - (void)setValue:(NSString*)value forType:(autofill::ServerFieldType)type;
{@selector(address1), autofill::ADDRESS_HOME_LINE1}, // Gets |value| for |type| from |_internalProfile|.
{@selector(address2), autofill::ADDRESS_HOME_LINE2}, - (NSString*)valueForType:(autofill::ServerFieldType)type;
{@selector(city), autofill::ADDRESS_HOME_CITY},
{@selector(state), autofill::ADDRESS_HOME_STATE}, @end
{@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 { @implementation CWVAutofillProfile {
autofill::AutofillProfile _internalProfile; autofill::AutofillProfile _internalProfile;
} }
@dynamic name, company, address1, address2, city, state, zipcode, country, - (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile {
phone, email; self = [super init];
if (self) {
+ (void)initialize { _internalProfile = profile;
if (self != [CWVAutofillProfile class]) {
return;
} }
return self;
}
// Add all getters and setters dynamically. #pragma mark - Public Methods
for (const auto& pair : kGettersToType) {
class_addMethod([self class], pair.first, (IMP)propertyGetter, "@@:"); - (NSString*)name {
} return [self valueForType:autofill::NAME_FULL];
for (const auto& pair : kSettersToType) {
class_addMethod([self class], pair.first, (IMP)propertySetter, "v@:@");
}
} }
NSString* propertyGetter(CWVAutofillProfile* self, SEL _cmd) { - (void)setName:(NSString*)name {
DCHECK(kGettersToType.find(_cmd) != kGettersToType.end()); [self setValue:name forType:autofill::NAME_FULL];
}
autofill::ServerFieldType type = kGettersToType[_cmd]; - (NSString*)company {
const std::string& locale = return [self valueForType:autofill::COMPANY_NAME];
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) { - (void)setCompany:(NSString*)company {
DCHECK(kSettersToType.find(_cmd) != kSettersToType.end()); [self setValue:company forType:autofill::COMPANY_NAME];
}
autofill::ServerFieldType type = kSettersToType[_cmd]; - (NSString*)address1 {
const std::string& locale = return [self valueForType:autofill::ADDRESS_HOME_LINE1];
ios_web_view::ApplicationContext::GetInstance()->GetApplicationLocale();
self->_internalProfile.SetInfo(type, base::SysNSStringToUTF16(value), locale);
} }
- (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile { - (void)setAddress1:(NSString*)address1 {
self = [super init]; [self setValue:address1 forType:autofill::ADDRESS_HOME_LINE1];
if (self) { }
_internalProfile = profile;
} - (NSString*)address2 {
return self; return [self valueForType:autofill::ADDRESS_HOME_LINE2];
}
- (void)setAddress2:(NSString*)address2 {
[self setValue:address2 forType:autofill::ADDRESS_HOME_LINE2];
}
- (NSString*)city {
return [self valueForType:autofill::ADDRESS_HOME_CITY];
}
- (void)setCity:(NSString*)city {
[self setValue:city forType:autofill::ADDRESS_HOME_CITY];
}
- (NSString*)state {
return [self valueForType:autofill::ADDRESS_HOME_STATE];
}
- (void)setState:(NSString*)state {
[self setValue:state forType:autofill::ADDRESS_HOME_STATE];
}
- (NSString*)zipcode {
return [self valueForType:autofill::ADDRESS_HOME_ZIP];
}
- (void)setZipcode:(NSString*)zipcode {
[self setValue:zipcode forType:autofill::ADDRESS_HOME_ZIP];
}
- (NSString*)country {
return [self valueForType:autofill::ADDRESS_HOME_COUNTRY];
}
- (void)setCountry:(NSString*)country {
[self setValue:country forType:autofill::ADDRESS_HOME_COUNTRY];
}
- (NSString*)phone {
return [self valueForType:autofill::PHONE_HOME_WHOLE_NUMBER];
}
- (void)setPhone:(NSString*)phone {
[self setValue:phone forType:autofill::PHONE_HOME_WHOLE_NUMBER];
}
- (NSString*)email {
return [self valueForType:autofill::EMAIL_ADDRESS];
}
- (void)setEmail:(NSString*)email {
[self setValue:email forType:autofill::EMAIL_ADDRESS];
} }
#pragma mark - Internal Methods #pragma mark - Internal Methods
...@@ -99,4 +122,18 @@ void propertySetter(CWVAutofillProfile* self, SEL _cmd, NSString* value) { ...@@ -99,4 +122,18 @@ void propertySetter(CWVAutofillProfile* self, SEL _cmd, NSString* value) {
return &_internalProfile; return &_internalProfile;
} }
#pragma mark - Private Methods
- (void)setValue:(NSString*)value forType:(autofill::ServerFieldType)type {
const std::string& locale =
ios_web_view::ApplicationContext::GetInstance()->GetApplicationLocale();
_internalProfile.SetInfo(type, base::SysNSStringToUTF16(value), locale);
}
- (NSString*)valueForType:(autofill::ServerFieldType)type {
const std::string& locale =
ios_web_view::ApplicationContext::GetInstance()->GetApplicationLocale();
return base::SysUTF16ToNSString(_internalProfile.GetInfo(type, locale));
}
@end @end
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