| 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 <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 typedef struct { | 29 typedef struct { |
| 30 int resource_id; | 30 int resource_id; |
| 31 const char* name; | 31 const char* name; |
| 32 int category_string_id; | 32 int category_string_id; |
| 33 } CanonicalEncodingData; | 33 } CanonicalEncodingData; |
| 34 | 34 |
| 35 // An array of all supported canonical encoding names. | 35 // An array of all supported canonical encoding names. |
| 36 const CanonicalEncodingData kCanonicalEncodingNames[] = { | 36 const CanonicalEncodingData kCanonicalEncodingNames[] = { |
| 37 { IDC_ENCODING_UTF8, "UTF-8", IDS_ENCODING_UNICODE }, | 37 { IDC_ENCODING_UTF8, "UTF-8", IDS_ENCODING_UNICODE }, |
| 38 { IDC_ENCODING_UTF16LE, "UTF-16LE", IDS_ENCODING_UNICODE }, | 38 { IDC_ENCODING_UTF16LE, "UTF-16LE", IDS_ENCODING_UNICODE }, |
| 39 { IDC_ENCODING_ISO88591, "ISO-8859-1", IDS_ENCODING_WESTERN }, | |
| 40 { IDC_ENCODING_WINDOWS1252, "windows-1252", IDS_ENCODING_WESTERN }, | 39 { IDC_ENCODING_WINDOWS1252, "windows-1252", IDS_ENCODING_WESTERN }, |
| 41 { IDC_ENCODING_GBK, "GBK", IDS_ENCODING_SIMP_CHINESE }, | 40 { IDC_ENCODING_GBK, "GBK", IDS_ENCODING_SIMP_CHINESE }, |
| 42 { IDC_ENCODING_GB18030, "gb18030", IDS_ENCODING_SIMP_CHINESE }, | 41 { IDC_ENCODING_GB18030, "gb18030", IDS_ENCODING_SIMP_CHINESE }, |
| 43 { IDC_ENCODING_BIG5, "Big5", IDS_ENCODING_TRAD_CHINESE }, | 42 { IDC_ENCODING_BIG5, "Big5", IDS_ENCODING_TRAD_CHINESE }, |
| 44 { IDC_ENCODING_BIG5HKSCS, "Big5-HKSCS", IDS_ENCODING_TRAD_CHINESE }, | 43 { IDC_ENCODING_BIG5HKSCS, "Big5-HKSCS", IDS_ENCODING_TRAD_CHINESE }, |
| 45 { IDC_ENCODING_KOREAN, "EUC-KR", IDS_ENCODING_KOREAN }, | 44 { IDC_ENCODING_KOREAN, "EUC-KR", IDS_ENCODING_KOREAN }, |
| 46 { IDC_ENCODING_SHIFTJIS, "Shift_JIS", IDS_ENCODING_JAPANESE }, | 45 { IDC_ENCODING_SHIFTJIS, "Shift_JIS", IDS_ENCODING_JAPANESE }, |
| 47 { IDC_ENCODING_EUCJP, "EUC-JP", IDS_ENCODING_JAPANESE }, | 46 { IDC_ENCODING_EUCJP, "EUC-JP", IDS_ENCODING_JAPANESE }, |
| 48 { IDC_ENCODING_ISO2022JP, "ISO-2022-JP", IDS_ENCODING_JAPANESE }, | 47 { IDC_ENCODING_ISO2022JP, "ISO-2022-JP", IDS_ENCODING_JAPANESE }, |
| 49 { IDC_ENCODING_THAI, "windows-874", IDS_ENCODING_THAI }, | 48 { IDC_ENCODING_THAI, "windows-874", IDS_ENCODING_THAI }, |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 // A static map object which contains all resourceid-nonsequenced canonical | 183 // A static map object which contains all resourceid-nonsequenced canonical |
| 185 // encoding names. | 184 // encoding names. |
| 186 CanonicalEncodingMap* CanonicalEncodingMapSingleton() { | 185 CanonicalEncodingMap* CanonicalEncodingMapSingleton() { |
| 187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 188 static CanonicalEncodingMap* singleton = new CanonicalEncodingMap; | 187 static CanonicalEncodingMap* singleton = new CanonicalEncodingMap; |
| 189 return singleton; | 188 return singleton; |
| 190 } | 189 } |
| 191 | 190 |
| 192 const int kDefaultEncodingMenus[] = { | 191 const int kDefaultEncodingMenus[] = { |
| 193 IDC_ENCODING_UTF16LE, | 192 IDC_ENCODING_UTF16LE, |
| 194 IDC_ENCODING_ISO88591, | |
| 195 IDC_ENCODING_WINDOWS1252, | 193 IDC_ENCODING_WINDOWS1252, |
| 196 IDC_ENCODING_GBK, | 194 IDC_ENCODING_GBK, |
| 197 IDC_ENCODING_GB18030, | 195 IDC_ENCODING_GB18030, |
| 198 IDC_ENCODING_BIG5, | 196 IDC_ENCODING_BIG5, |
| 199 IDC_ENCODING_BIG5HKSCS, | 197 IDC_ENCODING_BIG5HKSCS, |
| 200 IDC_ENCODING_KOREAN, | 198 IDC_ENCODING_KOREAN, |
| 201 IDC_ENCODING_SHIFTJIS, | 199 IDC_ENCODING_SHIFTJIS, |
| 202 IDC_ENCODING_EUCJP, | 200 IDC_ENCODING_EUCJP, |
| 203 IDC_ENCODING_ISO2022JP, | 201 IDC_ENCODING_ISO2022JP, |
| 204 IDC_ENCODING_THAI, | 202 IDC_ENCODING_THAI, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 if (available_list->size() == maximum_size) | 246 if (available_list->size() == maximum_size) |
| 249 return; | 247 return; |
| 250 } | 248 } |
| 251 } | 249 } |
| 252 | 250 |
| 253 base::string16 GetEncodingDisplayName(const std::string& encoding_name, | 251 base::string16 GetEncodingDisplayName(const std::string& encoding_name, |
| 254 int category_string_id) { | 252 int category_string_id) { |
| 255 base::string16 category_name = l10n_util::GetStringUTF16(category_string_id); | 253 base::string16 category_name = l10n_util::GetStringUTF16(category_string_id); |
| 256 if (category_string_id != IDS_ENCODING_KOREAN && | 254 if (category_string_id != IDS_ENCODING_KOREAN && |
| 257 category_string_id != IDS_ENCODING_THAI && | 255 category_string_id != IDS_ENCODING_THAI && |
| 258 category_string_id != IDS_ENCODING_TURKISH) { | 256 category_string_id != IDS_ENCODING_TURKISH && |
| 257 category_string_id != IDS_ENCODING_VIETNAMESE && |
| 258 category_string_id != IDS_ENCODING_ROMANIAN) { |
| 259 const CanonicalNameDisplayNameMapType* map = | 259 const CanonicalNameDisplayNameMapType* map = |
| 260 CanonicalEncodingMapSingleton()->GetCanonicalNameDisplayNameMapData(); | 260 CanonicalEncodingMapSingleton()->GetCanonicalNameDisplayNameMapData(); |
| 261 DCHECK(map); | 261 DCHECK(map); |
| 262 | 262 |
| 263 CanonicalNameDisplayNameMapType::const_iterator found_name = | 263 CanonicalNameDisplayNameMapType::const_iterator found_name = |
| 264 map->find(encoding_name); | 264 map->find(encoding_name); |
| 265 DCHECK(found_name != map->end()); | 265 DCHECK(found_name != map->end()); |
| 266 return l10n_util::GetStringFUTF16(IDS_ENCODING_DISPLAY_TEMPLATE, | 266 return l10n_util::GetStringFUTF16(IDS_ENCODING_DISPLAY_TEMPLATE, |
| 267 category_name, | 267 category_name, |
| 268 base::ASCIIToUTF16(found_name->second)); | 268 base::ASCIIToUTF16(found_name->second)); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 // If the input alias_name is already canonical encoding name, just return it. | 372 // If the input alias_name is already canonical encoding name, just return it. |
| 373 const CanonicalEncodingNameToIdMapType* map = | 373 const CanonicalEncodingNameToIdMapType* map = |
| 374 CanonicalEncodingMapSingleton()->GetCanonicalEncodingNameToIdMapData(); | 374 CanonicalEncodingMapSingleton()->GetCanonicalEncodingNameToIdMapData(); |
| 375 DCHECK(map); | 375 DCHECK(map); |
| 376 | 376 |
| 377 CanonicalEncodingNameToIdMapType::const_iterator found_id = | 377 CanonicalEncodingNameToIdMapType::const_iterator found_id = |
| 378 map->find(alias_name); | 378 map->find(alias_name); |
| 379 if (found_id != map->end()) | 379 if (found_id != map->end()) |
| 380 return alias_name; | 380 return alias_name; |
| 381 | 381 |
| 382 UErrorCode error_code = U_ZERO_ERROR; | 382 const char* standards[3] = { "HTML", "MIME", "IANA" }; |
| 383 | 383 for (size_t i = 0; i < arraysize(standards); ++i) { |
| 384 const char* canonical_name = ucnv_getCanonicalName( | 384 UErrorCode error_code = U_ZERO_ERROR; |
| 385 alias_name.c_str(), "MIME", &error_code); | 385 const char* canonical_name = ucnv_getCanonicalName( |
| 386 // If failed, then try IANA next. | 386 alias_name.c_str(), standards[i], &error_code); |
| 387 if (U_FAILURE(error_code) || !canonical_name) { | 387 if (U_SUCCESS(error_code) && canonical_name) |
| 388 error_code = U_ZERO_ERROR; | 388 return canonical_name; |
| 389 canonical_name = ucnv_getCanonicalName( | |
| 390 alias_name.c_str(), "IANA", &error_code); | |
| 391 } | 389 } |
| 392 | 390 return std::string(); |
| 393 if (canonical_name) { | |
| 394 // TODO(jnd) use a map to handle all customized {alias, canonical} | |
| 395 // encoding mappings if we have more than one pair. | |
| 396 // We don't want to add an unnecessary charset to the encoding menu, so we | |
| 397 // alias 'US-ASCII' to 'ISO-8859-1' in our UI without touching WebKit. | |
| 398 // http://crbug.com/15801. | |
| 399 if (alias_name == "US-ASCII") | |
| 400 return GetCanonicalEncodingNameByCommandId(IDC_ENCODING_ISO88591); | |
| 401 return canonical_name; | |
| 402 } else { | |
| 403 return std::string(); | |
| 404 } | |
| 405 } | 391 } |
| 406 | 392 |
| 407 // Static | 393 // Static |
| 408 // According to the behavior of user recently selected encoding short list in | 394 // According to the behavior of user recently selected encoding short list in |
| 409 // Firefox, we always put UTF-8 as top position, after then put user | 395 // Firefox, we always put UTF-8 as top position, after then put user |
| 410 // recent selected encodings, then put local dependent encoding items. | 396 // recent selected encodings, then put local dependent encoding items. |
| 411 // At last, we put all remaining encoding items. | 397 // At last, we put all remaining encoding items. |
| 412 const std::vector<CharacterEncoding::EncodingInfo>* | 398 const std::vector<CharacterEncoding::EncodingInfo>* |
| 413 CharacterEncoding::GetCurrentDisplayEncodings( | 399 CharacterEncoding::GetCurrentDisplayEncodings( |
| 414 const std::string& locale, | 400 const std::string& locale, |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 // Insert new encoding to head of selected encoding list. | 519 // Insert new encoding to head of selected encoding list. |
| 534 *selected_encodings = encoding_name; | 520 *selected_encodings = encoding_name; |
| 535 // Generate the string for rest selected encoding list. | 521 // Generate the string for rest selected encoding list. |
| 536 for (std::vector<int>::const_iterator it = selected_encoding_list.begin(); | 522 for (std::vector<int>::const_iterator it = selected_encoding_list.begin(); |
| 537 it != selected_encoding_list.end(); ++it) { | 523 it != selected_encoding_list.end(); ++it) { |
| 538 selected_encodings->append(1, L','); | 524 selected_encodings->append(1, L','); |
| 539 selected_encodings->append(GetCanonicalEncodingNameByCommandId(*it)); | 525 selected_encodings->append(GetCanonicalEncodingNameByCommandId(*it)); |
| 540 } | 526 } |
| 541 return true; | 527 return true; |
| 542 } | 528 } |
| OLD | NEW |