• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #include "chrome/browser/autofill/autofill_country.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <map>
10 #include <utility>
11 
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/singleton.h"
15 #include "base/stl_util-inl.h"
16 #include "base/string_util.h"
17 #include "base/utf_string_conversions.h"
18 #ifndef ANDROID
19 #include "chrome/browser/browser_process.h"
20 #endif
21 #include "grit/generated_resources.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "unicode/coll.h"
24 #include "unicode/locid.h"
25 #include "unicode/ucol.h"
26 #include "unicode/uloc.h"
27 #include "unicode/unistr.h"
28 #include "unicode/urename.h"
29 #include "unicode/utypes.h"
30 
31 namespace {
32 
33 struct CountryData {
34   std::string country_code;
35   int postal_code_label_id;
36   int state_label_id;
37 };
38 
39 // The maximum capacity needed to store a locale up to the country code.
40 const size_t kLocaleCapacity =
41     ULOC_LANG_CAPACITY + ULOC_SCRIPT_CAPACITY + ULOC_COUNTRY_CAPACITY + 1;
42 
43 // Maps country codes to localized label string identifiers.
44 const CountryData kCountryData[] = {
45   {"AD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PARISH},
46   {"AE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_EMIRATE},
47   {"AF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
48   {"AG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
49   {"AI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
50   {"AL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
51   {"AM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
52   {"AN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
53   {"AO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
54   {"AQ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
55   {"AR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE},
56   {"AS", IDS_AUTOFILL_DIALOG_ZIP_CODE,    IDS_AUTOFILL_DIALOG_STATE},
57   {"AT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
58   {"AU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE},
59   {"AW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
60   {"AX", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
61   {"AZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
62   {"BA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
63   {"BB", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PARISH},
64   {"BD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
65   {"BE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
66   {"BF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
67   {"BG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
68   {"BH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
69   {"BI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
70   {"BJ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
71   {"BL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
72   {"BM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
73   {"BN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
74   {"BO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
75   {"BR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE},
76   {"BS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND},
77   {"BT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
78   {"BV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
79   {"BW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
80   {"BY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
81   {"BZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
82   {"CA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
83   {"CC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
84   {"CD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
85   {"CF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
86   {"CG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
87   {"CH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
88   {"CI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
89   {"CK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
90   {"CL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE},
91   {"CM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
92   {"CN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
93   {"CO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
94   {"CR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
95   {"CV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND},
96   {"CX", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
97   {"CY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
98   {"CZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
99   {"DE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
100   {"DJ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
101   {"DK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
102   {"DM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
103   {"DO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
104   {"DZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
105   {"EC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
106   {"EE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
107   {"EG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
108   {"EH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
109   {"ER", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
110   {"ES", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
111   {"ET", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
112   {"FI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
113   {"FJ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
114   {"FK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
115   {"FM", IDS_AUTOFILL_DIALOG_ZIP_CODE,    IDS_AUTOFILL_DIALOG_STATE},
116   {"FO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
117   {"FR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
118   {"GA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
119   {"GB", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_COUNTY},
120   {"GD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
121   {"GE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
122   {"GF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
123   {"GG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
124   {"GH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
125   {"GI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
126   {"GL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
127   {"GM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
128   {"GN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
129   {"GP", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
130   {"GQ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
131   {"GR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
132   {"GS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
133   {"GT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
134   {"GU", IDS_AUTOFILL_DIALOG_ZIP_CODE,    IDS_AUTOFILL_DIALOG_STATE},
135   {"GW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
136   {"GY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
137   {"HK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_AREA},
138   {"HM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
139   {"HN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
140   {"HR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
141   {"HT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
142   {"HU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
143   {"ID", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
144   {"IE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_COUNTY},
145   {"IL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
146   {"IM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
147   {"IN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE},
148   {"IO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
149   {"IQ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
150   {"IS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
151   {"IT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
152   {"JE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
153   {"JM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PARISH},
154   {"JO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
155   {"JP", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PREFECTURE},
156   {"KE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
157   {"KG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
158   {"KH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
159   {"KI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND},
160   {"KM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
161   {"KN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND},
162   {"KP", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
163   {"KR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
164   {"KW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
165   {"KY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND},
166   {"KZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
167   {"LA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
168   {"LB", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
169   {"LC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
170   {"LI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
171   {"LK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
172   {"LR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
173   {"LS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
174   {"LT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
175   {"LU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
176   {"LV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
177   {"LY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
178   {"MA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
179   {"MC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
180   {"MD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
181   {"ME", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
182   {"MF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
183   {"MG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
184   {"MH", IDS_AUTOFILL_DIALOG_ZIP_CODE,    IDS_AUTOFILL_DIALOG_STATE},
185   {"MK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
186   {"ML", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
187   {"MN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
188   {"MO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
189   {"MP", IDS_AUTOFILL_DIALOG_ZIP_CODE,    IDS_AUTOFILL_DIALOG_STATE},
190   {"MQ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
191   {"MR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
192   {"MS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
193   {"MT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
194   {"MU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
195   {"MV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
196   {"MW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
197   {"MX", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE},
198   {"MY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE},
199   {"MZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
200   {"NA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
201   {"NC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
202   {"NE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
203   {"NF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
204   {"NG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE},
205   {"NI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_DEPARTMENT},
206   {"NL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
207   {"NO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
208   {"NP", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
209   {"NR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_DISTRICT},
210   {"NU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
211   {"NZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
212   {"OM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
213   {"PA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
214   {"PE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
215   {"PF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND},
216   {"PG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
217   {"PH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
218   {"PK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
219   {"PL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
220   {"PM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
221   {"PN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
222   {"PR", IDS_AUTOFILL_DIALOG_ZIP_CODE,    IDS_AUTOFILL_DIALOG_PROVINCE},
223   {"PS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
224   {"PT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
225   {"PW", IDS_AUTOFILL_DIALOG_ZIP_CODE,    IDS_AUTOFILL_DIALOG_STATE},
226   {"PY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
227   {"QA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
228   {"RE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
229   {"RO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
230   {"RS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
231   {"RU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
232   {"RW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
233   {"SA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
234   {"SB", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
235   {"SC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND},
236   {"SE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
237   {"SG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
238   {"SH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
239   {"SI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
240   {"SJ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
241   {"SK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
242   {"SL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
243   {"SM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
244   {"SN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
245   {"SO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
246   {"SR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
247   {"ST", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
248   {"SV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
249   {"SZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
250   {"TC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
251   {"TD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
252   {"TF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
253   {"TG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
254   {"TH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
255   {"TJ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
256   {"TK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
257   {"TL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
258   {"TM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
259   {"TN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
260   {"TO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
261   {"TR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
262   {"TT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
263   {"TV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND},
264   {"TW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_COUNTY},
265   {"TZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
266   {"UA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
267   {"UG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
268   {"UM", IDS_AUTOFILL_DIALOG_ZIP_CODE,    IDS_AUTOFILL_DIALOG_STATE},
269   {"US", IDS_AUTOFILL_DIALOG_ZIP_CODE,    IDS_AUTOFILL_DIALOG_STATE},
270   {"UY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
271   {"UZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
272   {"VA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
273   {"VC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
274   {"VE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
275   {"VG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
276   {"VI", IDS_AUTOFILL_DIALOG_ZIP_CODE,    IDS_AUTOFILL_DIALOG_STATE},
277   {"VN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
278   {"VU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
279   {"WF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
280   {"WS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
281   {"YE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
282   {"YT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
283   {"ZA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
284   {"ZM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
285   {"ZW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE},
286 };
287 
288 // A singleton class that encapsulates a map from country codes to country data.
289 class CountryDataMap {
290  public:
291   // A const iterator over the wrapped map data.
292   typedef std::map<std::string, CountryData>::const_iterator Iterator;
293 
294   static CountryDataMap* GetInstance();
295   static const Iterator Begin();
296   static const Iterator End();
297   static const Iterator Find(const std::string& country_code);
298 
299  private:
300   CountryDataMap();
301   friend struct DefaultSingletonTraits<CountryDataMap>;
302 
303   std::map<std::string, CountryData> country_data_;
304 
305   DISALLOW_COPY_AND_ASSIGN(CountryDataMap);
306 };
307 
308 // static
GetInstance()309 CountryDataMap* CountryDataMap::GetInstance() {
310   return Singleton<CountryDataMap>::get();
311 }
312 
CountryDataMap()313 CountryDataMap::CountryDataMap() {
314   // Add all the countries we have explicit data for.
315   for (size_t i = 0; i < arraysize(kCountryData); ++i) {
316     const CountryData& data = kCountryData[i];
317     country_data_.insert(std::make_pair(data.country_code, data));
318   }
319 
320   // Add any other countries that ICU knows about, falling back to default data
321   // values.
322   for (const char* const* country_pointer = icu::Locale::getISOCountries();
323        *country_pointer;
324        ++country_pointer) {
325     std::string country_code = *country_pointer;
326     if (!country_data_.count(country_code)) {
327       CountryData data = {
328         country_code,
329         IDS_AUTOFILL_DIALOG_POSTAL_CODE,
330         IDS_AUTOFILL_DIALOG_PROVINCE
331       };
332       country_data_.insert(std::make_pair(country_code, data));
333     }
334   }
335 }
336 
Begin()337 const CountryDataMap::Iterator CountryDataMap::Begin() {
338   return GetInstance()->country_data_.begin();
339 }
340 
End()341 const CountryDataMap::Iterator CountryDataMap::End() {
342   return GetInstance()->country_data_.end();
343 }
344 
Find(const std::string & country_code)345 const CountryDataMap::Iterator CountryDataMap::Find(
346     const std::string& country_code) {
347   return GetInstance()->country_data_.find(country_code);
348 }
349 
350 // A singleton class that encapsulates mappings from country names to their
351 // corresponding country codes.
352 class CountryNames {
353  public:
354   static CountryNames* GetInstance();
355 
356   // Returns the country code corresponding to |country|, which should be a
357   // country code or country name localized to |locale|.
358   const std::string GetCountryCode(const string16& country,
359                                    const std::string& locale);
360 
361  private:
362   CountryNames();
363   ~CountryNames();
364   friend struct DefaultSingletonTraits<CountryNames>;
365 
366   // Populates |locales_to_localized_names_| with the mapping of country names
367   // localized to |locale| to their corresponding country codes.
368   void AddLocalizedNamesForLocale(const std::string& locale);
369 
370   // Interprets |country_name| as a full country name localized to the given
371   // |locale| and returns the corresponding country code stored in
372   // |locales_to_localized_names_|, or an empty string if there is none.
373   const std::string GetCountryCodeForLocalizedName(const string16& country_name,
374                                                    const std::string& locale);
375 
376   // Returns an ICU collator -- i.e. string comparator -- appropriate for the
377   // given |locale|.
378   icu::Collator* GetCollatorForLocale(const std::string& locale);
379 
380   // Returns the ICU sort key corresponding to |str| for the given |collator|.
381   // Uses |buffer| as temporary storage, and might resize |buffer| as a side-
382   // effect. |buffer_size| should specify the |buffer|'s size, and is updated if
383   // the |buffer| is resized.
384   const std::string GetSortKey(const icu::Collator& collator,
385                                const icu::UnicodeString& str,
386                                scoped_array<uint8_t>* buffer,
387                                int32_t* buffer_size) const;
388 
389 
390   // Maps from common country names, including 2- and 3-letter country codes,
391   // to the corresponding 2-letter country codes. The keys are uppercase ASCII
392   // strings.
393   std::map<std::string, std::string> common_names_;
394 
395   // The outer map keys are ICU locale identifiers.
396   // The inner maps map from localized country names to their corresponding
397   // country codes. The inner map keys are ICU collation sort keys corresponding
398   // to the target localized country name.
399   std::map<std::string, std::map<std::string, std::string> >
400       locales_to_localized_names_;
401 
402   // Maps ICU locale names to their corresponding collators.
403   std::map<std::string, icu::Collator*> collators_;
404 
405   DISALLOW_COPY_AND_ASSIGN(CountryNames);
406 };
407 
408 // static
GetInstance()409 CountryNames* CountryNames::GetInstance() {
410   return Singleton<CountryNames>::get();
411 }
412 
CountryNames()413 CountryNames::CountryNames() {
414   // Add 2- and 3-letter ISO country codes.
415   for (CountryDataMap::Iterator it = CountryDataMap::Begin();
416        it != CountryDataMap::End();
417        ++it) {
418     const std::string& country_code = it->first;
419     std::string iso3_country_code =
420     icu::Locale(NULL, country_code.c_str()).getISO3Country();
421 
422     common_names_.insert(std::make_pair(country_code, country_code));
423     common_names_.insert(std::make_pair(iso3_country_code, country_code));
424   }
425 
426   // Add a few other common synonyms.
427   common_names_.insert(std::make_pair("UNITED STATES OF AMERICA", "US"));
428   common_names_.insert(std::make_pair("GREAT BRITAIN", "GB"));
429   common_names_.insert(std::make_pair("UK", "GB"));
430   common_names_.insert(std::make_pair("BRASIL", "BR"));
431   common_names_.insert(std::make_pair("DEUTSCHLAND", "DE"));
432 }
433 
~CountryNames()434 CountryNames::~CountryNames() {
435   STLDeleteContainerPairSecondPointers(collators_.begin(),
436                                        collators_.end());
437 }
438 
GetCountryCode(const string16 & country,const std::string & locale)439 const std::string CountryNames::GetCountryCode(const string16& country,
440                                                const std::string& locale) {
441   // First, check common country names, including 2- and 3-letter country codes.
442   std::string country_utf8 = UTF16ToUTF8(StringToUpperASCII(country));
443   std::map<std::string, std::string>::const_iterator result =
444       common_names_.find(country_utf8);
445   if (result != common_names_.end())
446     return result->second;
447 
448   // Next, check country names localized to |locale|.
449   std::string country_code = GetCountryCodeForLocalizedName(country, locale);
450   if (!country_code.empty())
451     return country_code;
452 
453   // Finally, check country names localized to US English.
454   return GetCountryCodeForLocalizedName(country, "en_US");
455 }
456 
AddLocalizedNamesForLocale(const std::string & locale)457 void CountryNames::AddLocalizedNamesForLocale(const std::string& locale) {
458   // Nothing to do if we've previously added the localized names for the given
459   // |locale|.
460   if (locales_to_localized_names_.count(locale))
461     return;
462 
463   std::map<std::string, std::string> localized_names;
464 
465   icu::Locale icu_locale(locale.c_str());
466   const icu::Collator* collator = GetCollatorForLocale(locale);
467 
468   int32_t buffer_size = 1000;
469   scoped_array<uint8_t> buffer(new uint8_t[buffer_size]);
470 
471   for (CountryDataMap::Iterator it = CountryDataMap::Begin();
472        it != CountryDataMap::End();
473        ++it) {
474     const std::string& country_code = it->first;
475 
476     icu::Locale country_locale(NULL, country_code.c_str());
477     icu::UnicodeString country_name;
478     country_locale.getDisplayName(icu_locale, country_name);
479     std::string sort_key = GetSortKey(*collator,
480                                       country_name,
481                                       &buffer,
482                                       &buffer_size);
483 
484     localized_names.insert(std::make_pair(sort_key, country_code));
485   }
486 
487   locales_to_localized_names_.insert(std::make_pair(locale, localized_names));
488 }
489 
GetCountryCodeForLocalizedName(const string16 & country_name,const std::string & locale)490 const std::string CountryNames::GetCountryCodeForLocalizedName(
491     const string16& country_name,
492     const std::string& locale) {
493   AddLocalizedNamesForLocale(locale);
494 
495   icu::Collator* collator = GetCollatorForLocale(locale);
496 
497   // As recommended[1] by ICU, initialize the buffer size to four times the
498   // source string length.
499   // [1] http://userguide.icu-project.org/collation/api#TOC-Examples
500   int32_t buffer_size = country_name.size() * 4;
501   scoped_array<uint8_t> buffer(new uint8_t[buffer_size]);
502   std::string sort_key = GetSortKey(*collator,
503                                     country_name.c_str(),
504                                     &buffer,
505                                     &buffer_size);
506 
507   const std::map<std::string, std::string>& localized_names =
508       locales_to_localized_names_[locale];
509   std::map<std::string, std::string>::const_iterator result =
510       localized_names.find(sort_key);
511 
512   if (result != localized_names.end())
513     return result->second;
514 
515   return std::string();
516 }
517 
GetCollatorForLocale(const std::string & locale)518 icu::Collator* CountryNames::GetCollatorForLocale(const std::string& locale) {
519   if (!collators_.count(locale)) {
520     icu::Locale icu_locale(locale.c_str());
521     UErrorCode ignored = U_ZERO_ERROR;
522     icu::Collator* collator(icu::Collator::createInstance(icu_locale, ignored));
523 
524     // Compare case-insensitively and ignoring punctuation.
525     ignored = U_ZERO_ERROR;
526     collator->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, ignored);
527     ignored = U_ZERO_ERROR;
528     collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored);
529 
530     collators_.insert(std::make_pair(locale, collator));
531   }
532 
533   return collators_[locale];
534 }
535 
GetSortKey(const icu::Collator & collator,const icu::UnicodeString & str,scoped_array<uint8_t> * buffer,int32_t * buffer_size) const536 const std::string CountryNames::GetSortKey(const icu::Collator& collator,
537                                            const icu::UnicodeString& str,
538                                            scoped_array<uint8_t>* buffer,
539                                            int32_t* buffer_size) const {
540   DCHECK(buffer);
541   DCHECK(buffer_size);
542 
543   int32_t expected_size = collator.getSortKey(str, buffer->get(), *buffer_size);
544   if (expected_size > *buffer_size) {
545     // If there wasn't enough space, grow the buffer and try again.
546     *buffer_size = expected_size;
547     buffer->reset(new uint8_t[*buffer_size]);
548     DCHECK(buffer->get());
549 
550     expected_size = collator.getSortKey(str, buffer->get(), *buffer_size);
551     DCHECK_EQ(*buffer_size, expected_size);
552   }
553 
554   return std::string(reinterpret_cast<const char*>(buffer->get()));
555 }
556 
557 // Returns the country name corresponding to |country_code|, localized to the
558 // |display_locale|.
GetDisplayName(const std::string & country_code,const icu::Locale & display_locale)559 string16 GetDisplayName(const std::string& country_code,
560                         const icu::Locale& display_locale) {
561   icu::Locale country_locale(NULL, country_code.c_str());
562   icu::UnicodeString name;
563   country_locale.getDisplayName(display_locale, name);
564 
565   DCHECK_GT(name.length(), 0);
566   return string16(name.getBuffer(), name.length());
567 }
568 
569 }  // namespace
570 
AutofillCountry(const std::string & country_code,const std::string & locale)571 AutofillCountry::AutofillCountry(const std::string& country_code,
572                                  const std::string& locale) {
573   const CountryDataMap::Iterator result = CountryDataMap::Find(country_code);
574   DCHECK(result != CountryDataMap::End());
575   const CountryData& data = result->second;
576 
577   country_code_ = country_code;
578   name_ = GetDisplayName(country_code, icu::Locale(locale.c_str()));
579   postal_code_label_ = l10n_util::GetStringUTF16(data.postal_code_label_id);
580   state_label_ = l10n_util::GetStringUTF16(data.state_label_id);
581 }
582 
~AutofillCountry()583 AutofillCountry::~AutofillCountry() {
584 }
585 
586 // static
GetAvailableCountries(std::vector<std::string> * country_codes)587 void AutofillCountry::GetAvailableCountries(
588     std::vector<std::string>* country_codes) {
589   DCHECK(country_codes);
590 
591   for (CountryDataMap::Iterator it = CountryDataMap::Begin();
592        it != CountryDataMap::End();
593        ++it) {
594     country_codes->push_back(it->first);
595   }
596 }
597 
598 // static
CountryCodeForLocale(const std::string & locale)599 const std::string AutofillCountry::CountryCodeForLocale(
600     const std::string& locale) {
601   // Add likely subtags to the locale. In particular, add any likely country
602   // subtags -- e.g. for locales like "ru" that only include the language.
603   std::string likely_locale;
604   UErrorCode error_ignored = U_ZERO_ERROR;
605   uloc_addLikelySubtags(locale.c_str(),
606                         WriteInto(&likely_locale, kLocaleCapacity),
607                         kLocaleCapacity,
608                         &error_ignored);
609 
610   // Extract the country code.
611   std::string country_code = icu::Locale(likely_locale.c_str()).getCountry();
612 
613   // TODO(isherman): Return an empty string and update the clients instead.
614   // Default to the United States if we have no better guess.
615   if (CountryDataMap::Find(country_code) == CountryDataMap::End())
616     return "US";
617 
618   return country_code;
619 }
620 
621 // static
GetCountryCode(const string16 & country,const std::string & locale)622 const std::string AutofillCountry::GetCountryCode(const string16& country,
623                                                   const std::string& locale) {
624   return CountryNames::GetInstance()->GetCountryCode(country, locale);
625 }
626 
627 // static
ApplicationLocale()628 const std::string AutofillCountry::ApplicationLocale() {
629 #ifdef ANDROID
630   return l10n_util::GetApplicationLocale();
631 #else
632   return g_browser_process->GetApplicationLocale();
633 #endif
634 }
635 
AutofillCountry(const std::string & country_code,const string16 & name,const string16 & postal_code_label,const string16 & state_label)636 AutofillCountry::AutofillCountry(const std::string& country_code,
637                                  const string16& name,
638                                  const string16& postal_code_label,
639                                  const string16& state_label)
640     : country_code_(country_code),
641       name_(name),
642       postal_code_label_(postal_code_label),
643       state_label_(state_label) {
644 }
645