1// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "ios/web_view/internal/autofill/cwv_autofill_profile_internal.h"
6
7#include "base/strings/sys_string_conversions.h"
8#include "components/autofill/core/browser/autofill_type.h"
9#include "components/autofill/core/browser/data_model/autofill_profile.h"
10#include "ios/web_view/internal/app/application_context.h"
11#import "ios/web_view/internal/utils/nsobject_description_utils.h"
12
13#if !defined(__has_feature) || !__has_feature(objc_arc)
14#error "This file requires ARC support."
15#endif
16
17@interface CWVAutofillProfile ()
18
19// Sets |value| for |type| in |_internalProfile|.
20- (void)setValue:(NSString*)value forType:(autofill::ServerFieldType)type;
21// Gets |value| for |type| from |_internalProfile|.
22- (NSString*)valueForType:(autofill::ServerFieldType)type;
23
24@end
25
26@implementation CWVAutofillProfile {
27  autofill::AutofillProfile _internalProfile;
28}
29
30- (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile {
31  self = [super init];
32  if (self) {
33    _internalProfile = profile;
34  }
35  return self;
36}
37
38#pragma mark - Public Methods
39
40- (NSString*)name {
41  return [self valueForType:autofill::NAME_FULL];
42}
43
44- (void)setName:(NSString*)name {
45  [self setValue:name forType:autofill::NAME_FULL];
46}
47
48- (NSString*)company {
49  return [self valueForType:autofill::COMPANY_NAME];
50}
51
52- (void)setCompany:(NSString*)company {
53  [self setValue:company forType:autofill::COMPANY_NAME];
54}
55
56- (NSString*)address1 {
57  return [self valueForType:autofill::ADDRESS_HOME_LINE1];
58}
59
60- (void)setAddress1:(NSString*)address1 {
61  [self setValue:address1 forType:autofill::ADDRESS_HOME_LINE1];
62}
63
64- (NSString*)address2 {
65  return [self valueForType:autofill::ADDRESS_HOME_LINE2];
66}
67
68- (void)setAddress2:(NSString*)address2 {
69  [self setValue:address2 forType:autofill::ADDRESS_HOME_LINE2];
70}
71
72- (NSString*)city {
73  return [self valueForType:autofill::ADDRESS_HOME_CITY];
74}
75
76- (void)setCity:(NSString*)city {
77  [self setValue:city forType:autofill::ADDRESS_HOME_CITY];
78}
79
80- (NSString*)state {
81  return [self valueForType:autofill::ADDRESS_HOME_STATE];
82}
83
84- (void)setState:(NSString*)state {
85  [self setValue:state forType:autofill::ADDRESS_HOME_STATE];
86}
87
88- (NSString*)zipcode {
89  return [self valueForType:autofill::ADDRESS_HOME_ZIP];
90}
91
92- (void)setZipcode:(NSString*)zipcode {
93  [self setValue:zipcode forType:autofill::ADDRESS_HOME_ZIP];
94}
95
96- (NSString*)country {
97  return [self valueForType:autofill::ADDRESS_HOME_COUNTRY];
98}
99
100- (void)setCountry:(NSString*)country {
101  [self setValue:country forType:autofill::ADDRESS_HOME_COUNTRY];
102}
103
104- (NSString*)phone {
105  return [self valueForType:autofill::PHONE_HOME_WHOLE_NUMBER];
106}
107
108- (void)setPhone:(NSString*)phone {
109  [self setValue:phone forType:autofill::PHONE_HOME_WHOLE_NUMBER];
110}
111
112- (NSString*)email {
113  return [self valueForType:autofill::EMAIL_ADDRESS];
114}
115
116- (void)setEmail:(NSString*)email {
117  [self setValue:email forType:autofill::EMAIL_ADDRESS];
118}
119
120#pragma mark - NSObject
121
122- (NSString*)debugDescription {
123  NSString* debugDescription = [super debugDescription];
124  return [debugDescription
125      stringByAppendingFormat:@"\n%@", CWVPropertiesDescription(self)];
126}
127
128#pragma mark - Internal Methods
129
130- (autofill::AutofillProfile*)internalProfile {
131  return &_internalProfile;
132}
133
134#pragma mark - Private Methods
135
136- (void)setValue:(NSString*)value forType:(autofill::ServerFieldType)type {
137  const std::string& locale =
138      ios_web_view::ApplicationContext::GetInstance()->GetApplicationLocale();
139  _internalProfile.SetInfo(type, base::SysNSStringToUTF16(value), locale);
140}
141
142- (NSString*)valueForType:(autofill::ServerFieldType)type {
143  const std::string& locale =
144      ios_web_view::ApplicationContext::GetInstance()->GetApplicationLocale();
145  return base::SysUTF16ToNSString(_internalProfile.GetInfo(type, locale));
146}
147
148@end
149