OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/character_encoding.h" | 5 #include "chrome/browser/character_encoding.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
(...skipping 19 matching lines...) Expand all Loading... | |
30 const size_t kUserSelectedEncodingsMaxLength = 3; | 30 const size_t kUserSelectedEncodingsMaxLength = 3; |
31 | 31 |
32 typedef struct { | 32 typedef struct { |
33 int resource_id; | 33 int resource_id; |
34 const char* name; | 34 const char* name; |
35 int category_string_id; | 35 int category_string_id; |
36 } CanonicalEncodingData; | 36 } CanonicalEncodingData; |
37 | 37 |
38 // An array of all supported canonical encoding names. | 38 // An array of all supported canonical encoding names. |
39 const CanonicalEncodingData kCanonicalEncodingNames[] = { | 39 const CanonicalEncodingData kCanonicalEncodingNames[] = { |
40 { IDC_ENCODING_UTF8, "UTF-8", IDS_ENCODING_UNICODE }, | 40 { IDC_ENCODING_UTF8, "UTF-8", IDS_ENCODING_UNICODE }, |
msw
2016/08/22 21:59:53
Can we remove more here? Is GetCurrentDisplayEncod
Jinsuk Kim
2016/08/23 07:09:22
Good catch. Removed the method. The array is neces
| |
41 { IDC_ENCODING_UTF16LE, "UTF-16LE", IDS_ENCODING_UNICODE }, | 41 { IDC_ENCODING_UTF16LE, "UTF-16LE", IDS_ENCODING_UNICODE }, |
42 { IDC_ENCODING_WINDOWS1252, "windows-1252", IDS_ENCODING_WESTERN }, | 42 { IDC_ENCODING_WINDOWS1252, "windows-1252", IDS_ENCODING_WESTERN }, |
43 { IDC_ENCODING_GBK, "GBK", IDS_ENCODING_SIMP_CHINESE }, | 43 { IDC_ENCODING_GBK, "GBK", IDS_ENCODING_SIMP_CHINESE }, |
44 { IDC_ENCODING_GB18030, "gb18030", IDS_ENCODING_SIMP_CHINESE }, | 44 { IDC_ENCODING_GB18030, "gb18030", IDS_ENCODING_SIMP_CHINESE }, |
45 { IDC_ENCODING_BIG5, "Big5", IDS_ENCODING_TRAD_CHINESE }, | 45 { IDC_ENCODING_BIG5, "Big5", IDS_ENCODING_TRAD_CHINESE }, |
46 { IDC_ENCODING_KOREAN, "EUC-KR", IDS_ENCODING_KOREAN }, | 46 { IDC_ENCODING_KOREAN, "EUC-KR", IDS_ENCODING_KOREAN }, |
47 { IDC_ENCODING_SHIFTJIS, "Shift_JIS", IDS_ENCODING_JAPANESE }, | 47 { IDC_ENCODING_SHIFTJIS, "Shift_JIS", IDS_ENCODING_JAPANESE }, |
48 { IDC_ENCODING_EUCJP, "EUC-JP", IDS_ENCODING_JAPANESE }, | 48 { IDC_ENCODING_EUCJP, "EUC-JP", IDS_ENCODING_JAPANESE }, |
49 { IDC_ENCODING_ISO2022JP, "ISO-2022-JP", IDS_ENCODING_JAPANESE }, | 49 { IDC_ENCODING_ISO2022JP, "ISO-2022-JP", IDS_ENCODING_JAPANESE }, |
50 { IDC_ENCODING_THAI, "windows-874", IDS_ENCODING_THAI }, | 50 { IDC_ENCODING_THAI, "windows-874", IDS_ENCODING_THAI }, |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
334 DCHECK(map); | 334 DCHECK(map); |
335 | 335 |
336 IdToCanonicalEncodingNameMapType::const_iterator found_name = map->find(id); | 336 IdToCanonicalEncodingNameMapType::const_iterator found_name = map->find(id); |
337 if (found_name != map->end()) | 337 if (found_name != map->end()) |
338 return GetEncodingDisplayName(found_name->second.first, | 338 return GetEncodingDisplayName(found_name->second.first, |
339 found_name->second.second); | 339 found_name->second.second); |
340 return base::string16(); | 340 return base::string16(); |
341 } | 341 } |
342 | 342 |
343 // Static. | 343 // Static. |
344 // Return count number of all supported canonical encoding. | |
345 int CharacterEncoding::GetSupportCanonicalEncodingCount() { | |
346 return kCanonicalEncodingNamesLength; | |
347 } | |
348 | |
349 // Static. | |
350 std::string CharacterEncoding::GetCanonicalEncodingNameByIndex(int index) { | |
351 if (index < kCanonicalEncodingNamesLength) | |
352 return kCanonicalEncodingNames[index].name; | |
353 return std::string(); | |
354 } | |
355 | |
356 // Static. | |
357 base::string16 CharacterEncoding::GetCanonicalEncodingDisplayNameByIndex( | |
358 int index) { | |
359 if (index < kCanonicalEncodingNamesLength) | |
360 return GetEncodingDisplayName(kCanonicalEncodingNames[index].name, | |
361 kCanonicalEncodingNames[index].category_string_id); | |
362 return base::string16(); | |
363 } | |
364 | |
365 // Static. | |
366 int CharacterEncoding::GetEncodingCommandIdByIndex(int index) { | |
367 if (index < kCanonicalEncodingNamesLength) | |
368 return kCanonicalEncodingNames[index].resource_id; | |
369 return 0; | |
370 } | |
371 | |
372 // Static. | |
373 std::string CharacterEncoding::GetCanonicalEncodingNameByAliasName( | 344 std::string CharacterEncoding::GetCanonicalEncodingNameByAliasName( |
374 const std::string& alias_name) { | 345 const std::string& alias_name) { |
375 // If the input alias_name is already canonical encoding name, just return it. | 346 // If the input alias_name is already canonical encoding name, just return it. |
376 const CanonicalEncodingNameToIdMapType* map = | 347 const CanonicalEncodingNameToIdMapType* map = |
377 CanonicalEncodingMapSingleton()->GetCanonicalEncodingNameToIdMapData(); | 348 CanonicalEncodingMapSingleton()->GetCanonicalEncodingNameToIdMapData(); |
378 DCHECK(map); | 349 DCHECK(map); |
379 | 350 |
380 CanonicalEncodingNameToIdMapType::const_iterator found_id = | 351 CanonicalEncodingNameToIdMapType::const_iterator found_id = |
381 map->find(alias_name); | 352 map->find(alias_name); |
382 if (found_id != map->end()) | 353 if (found_id != map->end()) |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 // Sort the encoding list. | 448 // Sort the encoding list. |
478 l10n_util::SortVectorWithStringKey(locale, | 449 l10n_util::SortVectorWithStringKey(locale, |
479 encoding_list, | 450 encoding_list, |
480 start_sorted_index, | 451 start_sorted_index, |
481 encoding_list->size(), | 452 encoding_list->size(), |
482 true); | 453 true); |
483 } | 454 } |
484 DCHECK(!encoding_list->empty()); | 455 DCHECK(!encoding_list->empty()); |
485 return encoding_list; | 456 return encoding_list; |
486 } | 457 } |
487 | |
488 // Static | |
489 bool CharacterEncoding::UpdateRecentlySelectedEncoding( | |
490 const std::string& original_selected_encodings, | |
491 int new_selected_encoding_id, | |
492 std::string* selected_encodings) { | |
493 // Get encoding name. | |
494 std::string encoding_name = | |
495 GetCanonicalEncodingNameByCommandId(new_selected_encoding_id); | |
496 DCHECK(!encoding_name.empty()); | |
497 // Check whether the new encoding is in local dependent encodings or original | |
498 // recently selected encodings. If yes, do not add it. | |
499 std::vector<int>* locale_dependent_encoding_list = | |
500 CanonicalEncodingMapSingleton()->locale_dependent_encoding_ids(); | |
501 DCHECK(locale_dependent_encoding_list); | |
502 std::vector<int> selected_encoding_list; | |
503 ParseEncodingListSeparatedWithComma(original_selected_encodings, | |
504 &selected_encoding_list, | |
505 kUserSelectedEncodingsMaxLength); | |
506 // Put 'cached encodings' (dynamic encoding list) after 'local dependent | |
507 // encoding list' for check. | |
508 std::vector<int> top_encoding_list(*locale_dependent_encoding_list); | |
509 // UTF8 is always in our optimized encoding list. | |
510 top_encoding_list.insert(top_encoding_list.begin(), IDC_ENCODING_UTF8); | |
511 top_encoding_list.insert(top_encoding_list.end(), | |
512 selected_encoding_list.begin(), | |
513 selected_encoding_list.end()); | |
514 for (std::vector<int>::const_iterator it = top_encoding_list.begin(); | |
515 it != top_encoding_list.end(); ++it) | |
516 if (*it == new_selected_encoding_id) | |
517 return false; | |
518 // Need to add the encoding id to recently selected encoding list. | |
519 // Remove the last encoding in original list. | |
520 if (selected_encoding_list.size() == kUserSelectedEncodingsMaxLength) | |
521 selected_encoding_list.pop_back(); | |
522 // Insert new encoding to head of selected encoding list. | |
523 *selected_encodings = encoding_name; | |
524 // Generate the string for rest selected encoding list. | |
525 for (std::vector<int>::const_iterator it = selected_encoding_list.begin(); | |
526 it != selected_encoding_list.end(); ++it) { | |
527 selected_encodings->append(1, L','); | |
528 selected_encodings->append(GetCanonicalEncodingNameByCommandId(*it)); | |
529 } | |
530 return true; | |
531 } | |
OLD | NEW |