Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(257)

Side by Side Diff: chrome/browser/autofill/autofill_country.cc

Issue 12434004: Move remaining Autofill code to //components/autofill. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix long lines Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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.h"
16 #include "base/string_util.h"
17 #include "base/threading/thread_checker.h"
18 #include "base/utf_string_conversions.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/content_browser_client.h"
21 #include "grit/generated_resources.h"
22 #include "third_party/icu/public/common/unicode/locid.h"
23 #include "third_party/icu/public/common/unicode/uloc.h"
24 #include "third_party/icu/public/common/unicode/unistr.h"
25 #include "third_party/icu/public/common/unicode/urename.h"
26 #include "third_party/icu/public/common/unicode/utypes.h"
27 #include "third_party/icu/public/i18n/unicode/coll.h"
28 #include "third_party/icu/public/i18n/unicode/ucol.h"
29 #include "ui/base/l10n/l10n_util.h"
30
31 using content::BrowserThread;
32
33 namespace {
34
35 // The maximum capacity needed to store a locale up to the country code.
36 const size_t kLocaleCapacity =
37 ULOC_LANG_CAPACITY + ULOC_SCRIPT_CAPACITY + ULOC_COUNTRY_CAPACITY + 1;
38
39 struct CountryData {
40 int postal_code_label_id;
41 int state_label_id;
42 };
43
44 struct StaticCountryData {
45 char country_code[3];
46 CountryData country_data;
47 };
48
49 // Maps country codes to localized label string identifiers.
50 const StaticCountryData kCountryData[] = {
51 { "AD", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
52 IDS_AUTOFILL_FIELD_LABEL_PARISH } },
53 { "AE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
54 IDS_AUTOFILL_FIELD_LABEL_EMIRATE } },
55 { "AF", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
56 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
57 { "AG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
58 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
59 { "AI", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
60 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
61 { "AL", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
62 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
63 { "AM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
64 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
65 { "AN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
66 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
67 { "AO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
68 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
69 { "AQ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
70 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
71 { "AR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
72 IDS_AUTOFILL_FIELD_LABEL_STATE } },
73 { "AS", { IDS_AUTOFILL_FIELD_LABEL_ZIP_CODE,
74 IDS_AUTOFILL_FIELD_LABEL_STATE } },
75 { "AT", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
76 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
77 { "AU", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
78 IDS_AUTOFILL_FIELD_LABEL_STATE } },
79 { "AW", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
80 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
81 { "AX", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
82 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
83 { "AZ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
84 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
85 { "BA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
86 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
87 { "BB", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
88 IDS_AUTOFILL_FIELD_LABEL_PARISH } },
89 { "BD", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
90 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
91 { "BE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
92 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
93 { "BF", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
94 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
95 { "BG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
96 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
97 { "BH", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
98 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
99 { "BI", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
100 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
101 { "BJ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
102 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
103 { "BL", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
104 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
105 { "BM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
106 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
107 { "BN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
108 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
109 { "BO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
110 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
111 { "BR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
112 IDS_AUTOFILL_FIELD_LABEL_STATE } },
113 { "BS", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
114 IDS_AUTOFILL_FIELD_LABEL_ISLAND } },
115 { "BT", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
116 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
117 { "BV", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
118 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
119 { "BW", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
120 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
121 { "BY", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
122 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
123 { "BZ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
124 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
125 { "CA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
126 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
127 { "CC", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
128 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
129 { "CD", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
130 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
131 { "CF", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
132 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
133 { "CG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
134 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
135 { "CH", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
136 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
137 { "CI", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
138 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
139 { "CK", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
140 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
141 { "CL", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
142 IDS_AUTOFILL_FIELD_LABEL_STATE } },
143 { "CM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
144 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
145 { "CN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
146 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
147 { "CO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
148 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
149 { "CR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
150 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
151 { "CV", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
152 IDS_AUTOFILL_FIELD_LABEL_ISLAND } },
153 { "CX", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
154 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
155 { "CY", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
156 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
157 { "CZ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
158 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
159 { "DE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
160 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
161 { "DJ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
162 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
163 { "DK", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
164 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
165 { "DM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
166 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
167 { "DO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
168 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
169 { "DZ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
170 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
171 { "EC", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
172 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
173 { "EE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
174 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
175 { "EG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
176 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
177 { "EH", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
178 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
179 { "ER", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
180 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
181 { "ES", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
182 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
183 { "ET", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
184 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
185 { "FI", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
186 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
187 { "FJ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
188 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
189 { "FK", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
190 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
191 { "FM", { IDS_AUTOFILL_FIELD_LABEL_ZIP_CODE,
192 IDS_AUTOFILL_FIELD_LABEL_STATE } },
193 { "FO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
194 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
195 { "FR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
196 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
197 { "GA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
198 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
199 { "GB", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
200 IDS_AUTOFILL_FIELD_LABEL_COUNTY } },
201 { "GD", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
202 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
203 { "GE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
204 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
205 { "GF", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
206 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
207 { "GG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
208 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
209 { "GH", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
210 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
211 { "GI", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
212 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
213 { "GL", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
214 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
215 { "GM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
216 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
217 { "GN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
218 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
219 { "GP", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
220 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
221 { "GQ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
222 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
223 { "GR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
224 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
225 { "GS", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
226 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
227 { "GT", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
228 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
229 { "GU", { IDS_AUTOFILL_FIELD_LABEL_ZIP_CODE,
230 IDS_AUTOFILL_FIELD_LABEL_STATE } },
231 { "GW", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
232 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
233 { "GY", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
234 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
235 { "HK", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
236 IDS_AUTOFILL_FIELD_LABEL_AREA } },
237 { "HM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
238 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
239 { "HN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
240 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
241 { "HR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
242 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
243 { "HT", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
244 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
245 { "HU", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
246 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
247 { "ID", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
248 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
249 { "IE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
250 IDS_AUTOFILL_FIELD_LABEL_COUNTY } },
251 { "IL", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
252 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
253 { "IM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
254 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
255 { "IN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
256 IDS_AUTOFILL_FIELD_LABEL_STATE } },
257 { "IO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
258 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
259 { "IQ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
260 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
261 { "IS", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
262 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
263 { "IT", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
264 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
265 { "JE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
266 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
267 { "JM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
268 IDS_AUTOFILL_FIELD_LABEL_PARISH } },
269 { "JO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
270 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
271 { "JP", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
272 IDS_AUTOFILL_FIELD_LABEL_PREFECTURE } },
273 { "KE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
274 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
275 { "KG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
276 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
277 { "KH", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
278 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
279 { "KI", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
280 IDS_AUTOFILL_FIELD_LABEL_ISLAND } },
281 { "KM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
282 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
283 { "KN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
284 IDS_AUTOFILL_FIELD_LABEL_ISLAND } },
285 { "KP", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
286 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
287 { "KR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
288 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
289 { "KW", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
290 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
291 { "KY", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
292 IDS_AUTOFILL_FIELD_LABEL_ISLAND } },
293 { "KZ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
294 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
295 { "LA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
296 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
297 { "LB", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
298 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
299 { "LC", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
300 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
301 { "LI", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
302 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
303 { "LK", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
304 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
305 { "LR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
306 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
307 { "LS", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
308 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
309 { "LT", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
310 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
311 { "LU", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
312 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
313 { "LV", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
314 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
315 { "LY", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
316 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
317 { "MA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
318 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
319 { "MC", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
320 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
321 { "MD", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
322 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
323 { "ME", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
324 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
325 { "MF", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
326 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
327 { "MG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
328 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
329 { "MH", { IDS_AUTOFILL_FIELD_LABEL_ZIP_CODE,
330 IDS_AUTOFILL_FIELD_LABEL_STATE } },
331 { "MK", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
332 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
333 { "ML", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
334 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
335 { "MN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
336 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
337 { "MO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
338 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
339 { "MP", { IDS_AUTOFILL_FIELD_LABEL_ZIP_CODE,
340 IDS_AUTOFILL_FIELD_LABEL_STATE } },
341 { "MQ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
342 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
343 { "MR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
344 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
345 { "MS", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
346 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
347 { "MT", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
348 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
349 { "MU", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
350 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
351 { "MV", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
352 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
353 { "MW", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
354 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
355 { "MX", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
356 IDS_AUTOFILL_FIELD_LABEL_STATE } },
357 { "MY", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
358 IDS_AUTOFILL_FIELD_LABEL_STATE } },
359 { "MZ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
360 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
361 { "NA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
362 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
363 { "NC", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
364 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
365 { "NE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
366 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
367 { "NF", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
368 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
369 { "NG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
370 IDS_AUTOFILL_FIELD_LABEL_STATE } },
371 { "NI", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
372 IDS_AUTOFILL_FIELD_LABEL_DEPARTMENT } },
373 { "NL", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
374 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
375 { "NO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
376 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
377 { "NP", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
378 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
379 { "NR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
380 IDS_AUTOFILL_FIELD_LABEL_DISTRICT } },
381 { "NU", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
382 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
383 { "NZ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
384 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
385 { "OM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
386 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
387 { "PA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
388 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
389 { "PE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
390 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
391 { "PF", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
392 IDS_AUTOFILL_FIELD_LABEL_ISLAND } },
393 { "PG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
394 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
395 { "PH", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
396 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
397 { "PK", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
398 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
399 { "PL", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
400 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
401 { "PM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
402 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
403 { "PN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
404 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
405 { "PR", { IDS_AUTOFILL_FIELD_LABEL_ZIP_CODE,
406 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
407 { "PS", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
408 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
409 { "PT", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
410 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
411 { "PW", { IDS_AUTOFILL_FIELD_LABEL_ZIP_CODE,
412 IDS_AUTOFILL_FIELD_LABEL_STATE } },
413 { "PY", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
414 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
415 { "QA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
416 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
417 { "RE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
418 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
419 { "RO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
420 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
421 { "RS", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
422 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
423 { "RU", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
424 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
425 { "RW", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
426 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
427 { "SA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
428 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
429 { "SB", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
430 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
431 { "SC", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
432 IDS_AUTOFILL_FIELD_LABEL_ISLAND } },
433 { "SE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
434 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
435 { "SG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
436 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
437 { "SH", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
438 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
439 { "SI", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
440 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
441 { "SJ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
442 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
443 { "SK", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
444 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
445 { "SL", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
446 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
447 { "SM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
448 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
449 { "SN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
450 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
451 { "SO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
452 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
453 { "SR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
454 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
455 { "ST", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
456 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
457 { "SV", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
458 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
459 { "SZ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
460 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
461 { "TC", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
462 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
463 { "TD", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
464 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
465 { "TF", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
466 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
467 { "TG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
468 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
469 { "TH", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
470 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
471 { "TJ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
472 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
473 { "TK", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
474 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
475 { "TL", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
476 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
477 { "TM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
478 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
479 { "TN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
480 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
481 { "TO", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
482 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
483 { "TR", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
484 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
485 { "TT", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
486 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
487 { "TV", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
488 IDS_AUTOFILL_FIELD_LABEL_ISLAND } },
489 { "TW", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
490 IDS_AUTOFILL_FIELD_LABEL_COUNTY } },
491 { "TZ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
492 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
493 { "UA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
494 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
495 { "UG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
496 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
497 { "UM", { IDS_AUTOFILL_FIELD_LABEL_ZIP_CODE,
498 IDS_AUTOFILL_FIELD_LABEL_STATE } },
499 { "US", { IDS_AUTOFILL_FIELD_LABEL_ZIP_CODE,
500 IDS_AUTOFILL_FIELD_LABEL_STATE } },
501 { "UY", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
502 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
503 { "UZ", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
504 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
505 { "VA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
506 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
507 { "VC", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
508 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
509 { "VE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
510 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
511 { "VG", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
512 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
513 { "VI", { IDS_AUTOFILL_FIELD_LABEL_ZIP_CODE,
514 IDS_AUTOFILL_FIELD_LABEL_STATE } },
515 { "VN", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
516 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
517 { "VU", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
518 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
519 { "WF", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
520 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
521 { "WS", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
522 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
523 { "YE", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
524 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
525 { "YT", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
526 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
527 { "ZA", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
528 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
529 { "ZM", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
530 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
531 { "ZW", { IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
532 IDS_AUTOFILL_FIELD_LABEL_PROVINCE } },
533 };
534
535 // A singleton class that encapsulates a map from country codes to country data.
536 class CountryDataMap {
537 public:
538 // A const iterator over the wrapped map data.
539 typedef std::map<std::string, CountryData>::const_iterator Iterator;
540
541 static CountryDataMap* GetInstance();
542 static const Iterator Begin();
543 static const Iterator End();
544 static const Iterator Find(const std::string& country_code);
545
546 private:
547 CountryDataMap();
548 friend struct DefaultSingletonTraits<CountryDataMap>;
549
550 std::map<std::string, CountryData> country_data_;
551
552 DISALLOW_COPY_AND_ASSIGN(CountryDataMap);
553 };
554
555 // static
556 CountryDataMap* CountryDataMap::GetInstance() {
557 return Singleton<CountryDataMap>::get();
558 }
559
560 CountryDataMap::CountryDataMap() {
561 // Add all the countries we have explicit data for.
562 for (size_t i = 0; i < arraysize(kCountryData); ++i) {
563 const StaticCountryData& static_data = kCountryData[i];
564 country_data_.insert(std::make_pair(static_data.country_code,
565 static_data.country_data));
566 }
567
568 // Add any other countries that ICU knows about, falling back to default data
569 // values.
570 for (const char* const* country_pointer = icu::Locale::getISOCountries();
571 *country_pointer;
572 ++country_pointer) {
573 std::string country_code = *country_pointer;
574 if (!country_data_.count(country_code)) {
575 CountryData data = {
576 IDS_AUTOFILL_FIELD_LABEL_POSTAL_CODE,
577 IDS_AUTOFILL_FIELD_LABEL_PROVINCE
578 };
579 country_data_.insert(std::make_pair(country_code, data));
580 }
581 }
582 }
583
584 const CountryDataMap::Iterator CountryDataMap::Begin() {
585 return GetInstance()->country_data_.begin();
586 }
587
588 const CountryDataMap::Iterator CountryDataMap::End() {
589 return GetInstance()->country_data_.end();
590 }
591
592 const CountryDataMap::Iterator CountryDataMap::Find(
593 const std::string& country_code) {
594 return GetInstance()->country_data_.find(country_code);
595 }
596
597 // A singleton class that encapsulates mappings from country names to their
598 // corresponding country codes.
599 class CountryNames {
600 public:
601 static CountryNames* GetInstance();
602
603 // Returns the application locale.
604 const std::string ApplicationLocale();
605
606 // Returns the country code corresponding to |country|, which should be a
607 // country code or country name localized to |locale|.
608 const std::string GetCountryCode(const string16& country,
609 const std::string& locale);
610
611 private:
612 CountryNames();
613 ~CountryNames();
614 friend struct DefaultSingletonTraits<CountryNames>;
615
616 // Populates |locales_to_localized_names_| with the mapping of country names
617 // localized to |locale| to their corresponding country codes.
618 void AddLocalizedNamesForLocale(const std::string& locale);
619
620 // Interprets |country_name| as a full country name localized to the given
621 // |locale| and returns the corresponding country code stored in
622 // |locales_to_localized_names_|, or an empty string if there is none.
623 const std::string GetCountryCodeForLocalizedName(const string16& country_name,
624 const std::string& locale);
625
626 // Returns an ICU collator -- i.e. string comparator -- appropriate for the
627 // given |locale|.
628 icu::Collator* GetCollatorForLocale(const std::string& locale);
629
630 // Returns the ICU sort key corresponding to |str| for the given |collator|.
631 // Uses |buffer| as temporary storage, and might resize |buffer| as a side-
632 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if
633 // the |buffer| is resized.
634 const std::string GetSortKey(const icu::Collator& collator,
635 const string16& str,
636 scoped_array<uint8_t>* buffer,
637 int32_t* buffer_size) const;
638
639 // Maps from common country names, including 2- and 3-letter country codes,
640 // to the corresponding 2-letter country codes. The keys are uppercase ASCII
641 // strings.
642 std::map<std::string, std::string> common_names_;
643
644 // The outer map keys are ICU locale identifiers.
645 // The inner maps map from localized country names to their corresponding
646 // country codes. The inner map keys are ICU collation sort keys corresponding
647 // to the target localized country name.
648 std::map<std::string, std::map<std::string, std::string> >
649 locales_to_localized_names_;
650
651 // Maps ICU locale names to their corresponding collators.
652 std::map<std::string, icu::Collator*> collators_;
653
654 // Verifies thread-safety of accesses to the application locale.
655 base::ThreadChecker thread_checker_;
656
657 // Caches the application locale, for thread-safe access.
658 std::string application_locale_;
659
660 DISALLOW_COPY_AND_ASSIGN(CountryNames);
661 };
662
663 // static
664 CountryNames* CountryNames::GetInstance() {
665 return Singleton<CountryNames>::get();
666 }
667
668 const std::string CountryNames::ApplicationLocale() {
669 if (application_locale_.empty()) {
670 // In production code, this class is always constructed on the UI thread, so
671 // the two conditions in the below DCHECK are identical. In test code,
672 // sometimes there is a UI thread, and sometimes there is just the unnamed
673 // main thread. Since this class is a singleton, it needs to support both
674 // cases. Hence, the somewhat strange looking DCHECK below.
675 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
676 thread_checker_.CalledOnValidThread());
677 application_locale_ =
678 content::GetContentClient()->browser()->GetApplicationLocale();
679 }
680
681 return application_locale_;
682 }
683
684 CountryNames::CountryNames() {
685 // Add 2- and 3-letter ISO country codes.
686 for (CountryDataMap::Iterator it = CountryDataMap::Begin();
687 it != CountryDataMap::End();
688 ++it) {
689 const std::string& country_code = it->first;
690 std::string iso3_country_code =
691 icu::Locale(NULL, country_code.c_str()).getISO3Country();
692
693 common_names_.insert(std::make_pair(country_code, country_code));
694 common_names_.insert(std::make_pair(iso3_country_code, country_code));
695 }
696
697 // Add a few other common synonyms.
698 common_names_.insert(std::make_pair("UNITED STATES OF AMERICA", "US"));
699 common_names_.insert(std::make_pair("U.S.A.", "US"));
700 common_names_.insert(std::make_pair("GREAT BRITAIN", "GB"));
701 common_names_.insert(std::make_pair("UK", "GB"));
702 common_names_.insert(std::make_pair("BRASIL", "BR"));
703 common_names_.insert(std::make_pair("DEUTSCHLAND", "DE"));
704 }
705
706 CountryNames::~CountryNames() {
707 STLDeleteContainerPairSecondPointers(collators_.begin(),
708 collators_.end());
709 }
710
711 const std::string CountryNames::GetCountryCode(const string16& country,
712 const std::string& locale) {
713 // First, check common country names, including 2- and 3-letter country codes.
714 std::string country_utf8 = UTF16ToUTF8(StringToUpperASCII(country));
715 std::map<std::string, std::string>::const_iterator result =
716 common_names_.find(country_utf8);
717 if (result != common_names_.end())
718 return result->second;
719
720 // Next, check country names localized to |locale|.
721 std::string country_code = GetCountryCodeForLocalizedName(country, locale);
722 if (!country_code.empty())
723 return country_code;
724
725 // Finally, check country names localized to US English.
726 return GetCountryCodeForLocalizedName(country, "en_US");
727 }
728
729 void CountryNames::AddLocalizedNamesForLocale(const std::string& locale) {
730 // Nothing to do if we've previously added the localized names for the given
731 // |locale|.
732 if (locales_to_localized_names_.count(locale))
733 return;
734
735 std::map<std::string, std::string> localized_names;
736 const icu::Collator* collator = GetCollatorForLocale(locale);
737 int32_t buffer_size = 1000;
738 scoped_array<uint8_t> buffer(new uint8_t[buffer_size]);
739
740 for (CountryDataMap::Iterator it = CountryDataMap::Begin();
741 it != CountryDataMap::End();
742 ++it) {
743 const std::string& country_code = it->first;
744 string16 country_name = l10n_util::GetDisplayNameForCountry(country_code,
745 locale);
746 std::string sort_key = GetSortKey(*collator,
747 country_name,
748 &buffer,
749 &buffer_size);
750
751 localized_names.insert(std::make_pair(sort_key, country_code));
752 }
753
754 locales_to_localized_names_.insert(std::make_pair(locale, localized_names));
755 }
756
757 const std::string CountryNames::GetCountryCodeForLocalizedName(
758 const string16& country_name,
759 const std::string& locale) {
760 AddLocalizedNamesForLocale(locale);
761
762 icu::Collator* collator = GetCollatorForLocale(locale);
763
764 // As recommended[1] by ICU, initialize the buffer size to four times the
765 // source string length.
766 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples
767 int32_t buffer_size = country_name.size() * 4;
768 scoped_array<uint8_t> buffer(new uint8_t[buffer_size]);
769 std::string sort_key = GetSortKey(*collator,
770 country_name,
771 &buffer,
772 &buffer_size);
773
774 const std::map<std::string, std::string>& localized_names =
775 locales_to_localized_names_[locale];
776 std::map<std::string, std::string>::const_iterator result =
777 localized_names.find(sort_key);
778
779 if (result != localized_names.end())
780 return result->second;
781
782 return std::string();
783 }
784
785 icu::Collator* CountryNames::GetCollatorForLocale(const std::string& locale) {
786 if (!collators_.count(locale)) {
787 icu::Locale icu_locale(locale.c_str());
788 UErrorCode ignored = U_ZERO_ERROR;
789 icu::Collator* collator(icu::Collator::createInstance(icu_locale, ignored));
790
791 // Compare case-insensitively and ignoring punctuation.
792 ignored = U_ZERO_ERROR;
793 collator->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, ignored);
794 ignored = U_ZERO_ERROR;
795 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored);
796
797 collators_.insert(std::make_pair(locale, collator));
798 }
799
800 return collators_[locale];
801 }
802
803 const std::string CountryNames::GetSortKey(const icu::Collator& collator,
804 const string16& str,
805 scoped_array<uint8_t>* buffer,
806 int32_t* buffer_size) const {
807 DCHECK(buffer);
808 DCHECK(buffer_size);
809
810 icu::UnicodeString icu_str(str.c_str(), str.length());
811 int32_t expected_size = collator.getSortKey(icu_str, buffer->get(),
812 *buffer_size);
813 if (expected_size > *buffer_size) {
814 // If there wasn't enough space, grow the buffer and try again.
815 *buffer_size = expected_size;
816 buffer->reset(new uint8_t[*buffer_size]);
817 DCHECK(buffer->get());
818
819 expected_size = collator.getSortKey(icu_str, buffer->get(), *buffer_size);
820 DCHECK_EQ(*buffer_size, expected_size);
821 }
822
823 return std::string(reinterpret_cast<const char*>(buffer->get()));
824 }
825
826 } // namespace
827
828 AutofillCountry::AutofillCountry(const std::string& country_code,
829 const std::string& locale) {
830 const CountryDataMap::Iterator result = CountryDataMap::Find(country_code);
831 DCHECK(result != CountryDataMap::End());
832 const CountryData& data = result->second;
833
834 country_code_ = country_code;
835 name_ = l10n_util::GetDisplayNameForCountry(country_code, locale);
836 postal_code_label_ = l10n_util::GetStringUTF16(data.postal_code_label_id);
837 state_label_ = l10n_util::GetStringUTF16(data.state_label_id);
838 }
839
840 AutofillCountry::~AutofillCountry() {
841 }
842
843 // static
844 void AutofillCountry::GetAvailableCountries(
845 std::vector<std::string>* country_codes) {
846 DCHECK(country_codes);
847
848 for (CountryDataMap::Iterator it = CountryDataMap::Begin();
849 it != CountryDataMap::End();
850 ++it) {
851 country_codes->push_back(it->first);
852 }
853 }
854
855 // static
856 const std::string AutofillCountry::CountryCodeForLocale(
857 const std::string& locale) {
858 // Add likely subtags to the locale. In particular, add any likely country
859 // subtags -- e.g. for locales like "ru" that only include the language.
860 std::string likely_locale;
861 UErrorCode error_ignored = U_ZERO_ERROR;
862 uloc_addLikelySubtags(locale.c_str(),
863 WriteInto(&likely_locale, kLocaleCapacity),
864 kLocaleCapacity,
865 &error_ignored);
866
867 // Extract the country code.
868 std::string country_code = icu::Locale(likely_locale.c_str()).getCountry();
869
870 // Default to the United States if we have no better guess.
871 if (CountryDataMap::Find(country_code) == CountryDataMap::End())
872 return "US";
873
874 return country_code;
875 }
876
877 // static
878 const std::string AutofillCountry::GetCountryCode(const string16& country,
879 const std::string& locale) {
880 return CountryNames::GetInstance()->GetCountryCode(country, locale);
881 }
882
883 // static
884 const std::string AutofillCountry::ApplicationLocale() {
885 return CountryNames::GetInstance()->ApplicationLocale();
886 }
887
888 AutofillCountry::AutofillCountry(const std::string& country_code,
889 const string16& name,
890 const string16& postal_code_label,
891 const string16& state_label)
892 : country_code_(country_code),
893 name_(name),
894 postal_code_label_(postal_code_label),
895 state_label_(state_label) {
896 }
OLDNEW
« no previous file with comments | « chrome/browser/autofill/autofill_country.h ('k') | chrome/browser/autofill/autofill_country_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698