| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 13 #include "base/string_tokenizer.h" | 13 #include "base/string_tokenizer.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "chrome/app/chrome_dll_resource.h" | 15 #include "chrome/app/chrome_dll_resource.h" |
| 16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 17 #include "unicode/ucnv.h" | 17 #include "unicode/ucnv.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // The maximum length of short list of recently user selected encodings is 3. | 21 // The maximum length of short list of recently user selected encodings is 3. |
| 22 const size_t kUserSelectedEncodingsMaxLength = 3; | 22 const size_t kUserSelectedEncodingsMaxLength = 3; |
| 23 | 23 |
| 24 typedef struct { | 24 typedef struct { |
| 25 int resource_id; | 25 int resource_id; |
| 26 const wchar_t* name; | 26 const char* name; |
| 27 int category_string_id; | 27 int category_string_id; |
| 28 } CanonicalEncodingData; | 28 } CanonicalEncodingData; |
| 29 | 29 |
| 30 // An array of all supported canonical encoding names. | 30 // An array of all supported canonical encoding names. |
| 31 static CanonicalEncodingData canonical_encoding_names[] = { | 31 static CanonicalEncodingData canonical_encoding_names[] = { |
| 32 { IDC_ENCODING_UTF8, L"UTF-8", IDS_ENCODING_UNICODE }, | 32 { IDC_ENCODING_UTF8, "UTF-8", IDS_ENCODING_UNICODE }, |
| 33 { IDC_ENCODING_UTF16LE, L"UTF-16LE", IDS_ENCODING_UNICODE }, | 33 { IDC_ENCODING_UTF16LE, "UTF-16LE", IDS_ENCODING_UNICODE }, |
| 34 { IDC_ENCODING_ISO88591, L"ISO-8859-1", IDS_ENCODING_WESTERN }, | 34 { IDC_ENCODING_ISO88591, "ISO-8859-1", IDS_ENCODING_WESTERN }, |
| 35 { IDC_ENCODING_WINDOWS1252, L"windows-1252", IDS_ENCODING_WESTERN }, | 35 { IDC_ENCODING_WINDOWS1252, "windows-1252", IDS_ENCODING_WESTERN }, |
| 36 { IDC_ENCODING_GBK, L"GBK", IDS_ENCODING_SIMP_CHINESE }, | 36 { IDC_ENCODING_GBK, "GBK", IDS_ENCODING_SIMP_CHINESE }, |
| 37 { IDC_ENCODING_GB18030, L"gb18030", IDS_ENCODING_SIMP_CHINESE }, | 37 { IDC_ENCODING_GB18030, "gb18030", IDS_ENCODING_SIMP_CHINESE }, |
| 38 { IDC_ENCODING_BIG5, L"Big5", IDS_ENCODING_TRAD_CHINESE }, | 38 { IDC_ENCODING_BIG5, "Big5", IDS_ENCODING_TRAD_CHINESE }, |
| 39 { IDC_ENCODING_BIG5HKSCS, L"Big5-HKSCS", IDS_ENCODING_TRAD_CHINESE }, | 39 { IDC_ENCODING_BIG5HKSCS, "Big5-HKSCS", IDS_ENCODING_TRAD_CHINESE }, |
| 40 { IDC_ENCODING_KOREAN, L"windows-949", IDS_ENCODING_KOREAN }, | 40 { IDC_ENCODING_KOREAN, "windows-949", IDS_ENCODING_KOREAN }, |
| 41 { IDC_ENCODING_SHIFTJIS, L"Shift_JIS", IDS_ENCODING_JAPANESE }, | 41 { IDC_ENCODING_SHIFTJIS, "Shift_JIS", IDS_ENCODING_JAPANESE }, |
| 42 { IDC_ENCODING_EUCJP, L"EUC-JP", IDS_ENCODING_JAPANESE }, | 42 { IDC_ENCODING_EUCJP, "EUC-JP", IDS_ENCODING_JAPANESE }, |
| 43 { IDC_ENCODING_ISO2022JP, L"ISO-2022-JP", IDS_ENCODING_JAPANESE }, | 43 { IDC_ENCODING_ISO2022JP, "ISO-2022-JP", IDS_ENCODING_JAPANESE }, |
| 44 { IDC_ENCODING_THAI, L"windows-874", IDS_ENCODING_THAI }, | 44 { IDC_ENCODING_THAI, "windows-874", IDS_ENCODING_THAI }, |
| 45 { IDC_ENCODING_ISO885915, L"ISO-8859-15", IDS_ENCODING_WESTERN }, | 45 { IDC_ENCODING_ISO885915, "ISO-8859-15", IDS_ENCODING_WESTERN }, |
| 46 { IDC_ENCODING_MACINTOSH, L"macintosh", IDS_ENCODING_WESTERN }, | 46 { IDC_ENCODING_MACINTOSH, "macintosh", IDS_ENCODING_WESTERN }, |
| 47 { IDC_ENCODING_ISO88592, L"ISO-8859-2", IDS_ENCODING_CENTRAL_EUROPEAN }, | 47 { IDC_ENCODING_ISO88592, "ISO-8859-2", IDS_ENCODING_CENTRAL_EUROPEAN }, |
| 48 { IDC_ENCODING_WINDOWS1250, L"windows-1250", IDS_ENCODING_CENTRAL_EUROPEAN }, | 48 { IDC_ENCODING_WINDOWS1250, "windows-1250", IDS_ENCODING_CENTRAL_EUROPEAN }, |
| 49 { IDC_ENCODING_ISO88595, L"ISO-8859-5", IDS_ENCODING_CYRILLIC }, | 49 { IDC_ENCODING_ISO88595, "ISO-8859-5", IDS_ENCODING_CYRILLIC }, |
| 50 { IDC_ENCODING_WINDOWS1251, L"windows-1251", IDS_ENCODING_CYRILLIC }, | 50 { IDC_ENCODING_WINDOWS1251, "windows-1251", IDS_ENCODING_CYRILLIC }, |
| 51 { IDC_ENCODING_KOI8R, L"KOI8-R", IDS_ENCODING_CYRILLIC }, | 51 { IDC_ENCODING_KOI8R, "KOI8-R", IDS_ENCODING_CYRILLIC }, |
| 52 { IDC_ENCODING_KOI8U, L"KOI8-U", IDS_ENCODING_CYRILLIC }, | 52 { IDC_ENCODING_KOI8U, "KOI8-U", IDS_ENCODING_CYRILLIC }, |
| 53 { IDC_ENCODING_ISO88597, L"ISO-8859-7", IDS_ENCODING_GREEK }, | 53 { IDC_ENCODING_ISO88597, "ISO-8859-7", IDS_ENCODING_GREEK }, |
| 54 { IDC_ENCODING_WINDOWS1253, L"windows-1253", IDS_ENCODING_GREEK }, | 54 { IDC_ENCODING_WINDOWS1253, "windows-1253", IDS_ENCODING_GREEK }, |
| 55 { IDC_ENCODING_WINDOWS1254, L"windows-1254", IDS_ENCODING_TURKISH }, | 55 { IDC_ENCODING_WINDOWS1254, "windows-1254", IDS_ENCODING_TURKISH }, |
| 56 { IDC_ENCODING_WINDOWS1256, L"windows-1256", IDS_ENCODING_ARABIC }, | 56 { IDC_ENCODING_WINDOWS1256, "windows-1256", IDS_ENCODING_ARABIC }, |
| 57 { IDC_ENCODING_ISO88596, L"ISO-8859-6", IDS_ENCODING_ARABIC }, | 57 { IDC_ENCODING_ISO88596, "ISO-8859-6", IDS_ENCODING_ARABIC }, |
| 58 { IDC_ENCODING_WINDOWS1255, L"windows-1255", IDS_ENCODING_HEBREW }, | 58 { IDC_ENCODING_WINDOWS1255, "windows-1255", IDS_ENCODING_HEBREW }, |
| 59 { IDC_ENCODING_ISO88598I, L"ISO-8859-8-I", IDS_ENCODING_HEBREW }, | 59 { IDC_ENCODING_ISO88598I, "ISO-8859-8-I", IDS_ENCODING_HEBREW }, |
| 60 { IDC_ENCODING_ISO88598, L"ISO-8859-8", IDS_ENCODING_HEBREW }, | 60 { IDC_ENCODING_ISO88598, "ISO-8859-8", IDS_ENCODING_HEBREW }, |
| 61 { IDC_ENCODING_WINDOWS1258, L"windows-1258", IDS_ENCODING_VIETNAMESE }, | 61 { IDC_ENCODING_WINDOWS1258, "windows-1258", IDS_ENCODING_VIETNAMESE }, |
| 62 { IDC_ENCODING_ISO88594, L"ISO-8859-4", IDS_ENCODING_BALTIC }, | 62 { IDC_ENCODING_ISO88594, "ISO-8859-4", IDS_ENCODING_BALTIC }, |
| 63 { IDC_ENCODING_ISO885913, L"ISO-8859-13", IDS_ENCODING_BALTIC }, | 63 { IDC_ENCODING_ISO885913, "ISO-8859-13", IDS_ENCODING_BALTIC }, |
| 64 { IDC_ENCODING_WINDOWS1257, L"windows-1257", IDS_ENCODING_BALTIC }, | 64 { IDC_ENCODING_WINDOWS1257, "windows-1257", IDS_ENCODING_BALTIC }, |
| 65 { IDC_ENCODING_ISO88593, L"ISO-8859-3", IDS_ENCODING_SOUTH_EUROPEAN }, | 65 { IDC_ENCODING_ISO88593, "ISO-8859-3", IDS_ENCODING_SOUTH_EUROPEAN }, |
| 66 { IDC_ENCODING_ISO885910, L"ISO-8859-10", IDS_ENCODING_NORDIC }, | 66 { IDC_ENCODING_ISO885910, "ISO-8859-10", IDS_ENCODING_NORDIC }, |
| 67 { IDC_ENCODING_ISO885914, L"ISO-8859-14", IDS_ENCODING_CELTIC }, | 67 { IDC_ENCODING_ISO885914, "ISO-8859-14", IDS_ENCODING_CELTIC }, |
| 68 { IDC_ENCODING_ISO885916, L"ISO-8859-16", IDS_ENCODING_ROMANIAN }, | 68 { IDC_ENCODING_ISO885916, "ISO-8859-16", IDS_ENCODING_ROMANIAN }, |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 static const int canonical_encoding_names_length = | 71 static const int canonical_encoding_names_length = |
| 72 arraysize(canonical_encoding_names); | 72 arraysize(canonical_encoding_names); |
| 73 | 73 |
| 74 typedef std::map<int, std::pair<const wchar_t*, int> > | 74 typedef std::map<int, std::pair<const char*, int> > |
| 75 IdToCanonicalEncodingNameMapType; | 75 IdToCanonicalEncodingNameMapType; |
| 76 typedef std::map<const std::wstring, int> CanonicalEncodingNameToIdMapType; | 76 typedef std::map<const std::string, int> CanonicalEncodingNameToIdMapType; |
| 77 | 77 |
| 78 class CanonicalEncodingMap { | 78 class CanonicalEncodingMap { |
| 79 public: | 79 public: |
| 80 CanonicalEncodingMap() | 80 CanonicalEncodingMap() |
| 81 : id_to_encoding_name_map_(NULL), | 81 : id_to_encoding_name_map_(NULL), |
| 82 encoding_name_to_id_map_(NULL) { } | 82 encoding_name_to_id_map_(NULL) { } |
| 83 const IdToCanonicalEncodingNameMapType* GetIdToCanonicalEncodingNameMapData(); | 83 const IdToCanonicalEncodingNameMapType* GetIdToCanonicalEncodingNameMapData(); |
| 84 const CanonicalEncodingNameToIdMapType* GetCanonicalEncodingNameToIdMapData(); | 84 const CanonicalEncodingNameToIdMapType* GetCanonicalEncodingNameToIdMapData(); |
| 85 std::vector<int>* const locale_dependent_encoding_ids() { | 85 std::vector<int>* const locale_dependent_encoding_ids() { |
| 86 return &locale_dependent_encoding_ids_; | 86 return &locale_dependent_encoding_ids_; |
| 87 } | 87 } |
| 88 | 88 |
| 89 std::vector<CharacterEncoding::EncodingInfo>* const | 89 std::vector<CharacterEncoding::EncodingInfo>* const |
| 90 current_display_encodings() { | 90 current_display_encodings() { |
| 91 return ¤t_display_encodings_; | 91 return ¤t_display_encodings_; |
| 92 } | 92 } |
| 93 | 93 |
| 94 private: | 94 private: |
| 95 scoped_ptr<IdToCanonicalEncodingNameMapType> id_to_encoding_name_map_; | 95 scoped_ptr<IdToCanonicalEncodingNameMapType> id_to_encoding_name_map_; |
| 96 scoped_ptr<CanonicalEncodingNameToIdMapType> encoding_name_to_id_map_; | 96 scoped_ptr<CanonicalEncodingNameToIdMapType> encoding_name_to_id_map_; |
| 97 std::vector<int> locale_dependent_encoding_ids_; | 97 std::vector<int> locale_dependent_encoding_ids_; |
| 98 std::vector<CharacterEncoding::EncodingInfo> current_display_encodings_; | 98 std::vector<CharacterEncoding::EncodingInfo> current_display_encodings_; |
| 99 | 99 |
| 100 DISALLOW_EVIL_CONSTRUCTORS(CanonicalEncodingMap); | 100 DISALLOW_COPY_AND_ASSIGN(CanonicalEncodingMap); |
| 101 }; | 101 }; |
| 102 | 102 |
| 103 const IdToCanonicalEncodingNameMapType* | 103 const IdToCanonicalEncodingNameMapType* |
| 104 CanonicalEncodingMap::GetIdToCanonicalEncodingNameMapData() { | 104 CanonicalEncodingMap::GetIdToCanonicalEncodingNameMapData() { |
| 105 // Testing and building map is not thread safe, this function is supposed to | 105 // Testing and building map is not thread safe, this function is supposed to |
| 106 // only run in UI thread. Myabe I should add a lock in here for making it as | 106 // only run in UI thread. Myabe I should add a lock in here for making it as |
| 107 // thread safe. | 107 // thread safe. |
| 108 if (!id_to_encoding_name_map_.get()) { | 108 if (!id_to_encoding_name_map_.get()) { |
| 109 id_to_encoding_name_map_.reset(new IdToCanonicalEncodingNameMapType); | 109 id_to_encoding_name_map_.reset(new IdToCanonicalEncodingNameMapType); |
| 110 for (int i = 0; i < canonical_encoding_names_length; ++i) { | 110 for (int i = 0; i < canonical_encoding_names_length; ++i) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 IDC_ENCODING_ISO885916, | 172 IDC_ENCODING_ISO885916, |
| 173 }; | 173 }; |
| 174 | 174 |
| 175 const int default_encoding_menus_length = arraysize(default_encoding_menus); | 175 const int default_encoding_menus_length = arraysize(default_encoding_menus); |
| 176 | 176 |
| 177 // Parse the input |encoding_list| which is a encoding list separated with | 177 // Parse the input |encoding_list| which is a encoding list separated with |
| 178 // comma, get available encoding ids and save them to |available_list|. | 178 // comma, get available encoding ids and save them to |available_list|. |
| 179 // The parameter |maximum_size| indicates maximum size of encoding items we | 179 // The parameter |maximum_size| indicates maximum size of encoding items we |
| 180 // want to get from the |encoding_list|. | 180 // want to get from the |encoding_list|. |
| 181 void ParseEncodingListSeparatedWithComma( | 181 void ParseEncodingListSeparatedWithComma( |
| 182 const std::wstring& encoding_list, std::vector<int>* const available_list, | 182 const std::string& encoding_list, std::vector<int>* const available_list, |
| 183 size_t maximum_size) { | 183 size_t maximum_size) { |
| 184 WStringTokenizer tokenizer(encoding_list, L","); | 184 StringTokenizer tokenizer(encoding_list, ","); |
| 185 while (tokenizer.GetNext()) { | 185 while (tokenizer.GetNext()) { |
| 186 int id = CharacterEncoding::GetCommandIdByCanonicalEncodingName( | 186 int id = CharacterEncoding::GetCommandIdByCanonicalEncodingName( |
| 187 tokenizer.token()); | 187 tokenizer.token()); |
| 188 // Ignore invalid encoding. | 188 // Ignore invalid encoding. |
| 189 if (!id) | 189 if (!id) |
| 190 continue; | 190 continue; |
| 191 available_list->push_back(id); | 191 available_list->push_back(id); |
| 192 if (available_list->size() == maximum_size) | 192 if (available_list->size() == maximum_size) |
| 193 return; | 193 return; |
| 194 } | 194 } |
| 195 } | 195 } |
| 196 | 196 |
| 197 std::wstring GetEncodingDisplayName(std::wstring encoding_name, | 197 string16 GetEncodingDisplayName(std::string encoding_name, |
| 198 int category_string_id) { | 198 int category_string_id) { |
| 199 std::wstring category_name = l10n_util::GetString(category_string_id); | 199 string16 category_name = l10n_util::GetStringUTF16(category_string_id); |
| 200 if (category_string_id != IDS_ENCODING_KOREAN && | 200 if (category_string_id != IDS_ENCODING_KOREAN && |
| 201 category_string_id != IDS_ENCODING_THAI && | 201 category_string_id != IDS_ENCODING_THAI && |
| 202 category_string_id != IDS_ENCODING_TURKISH) { | 202 category_string_id != IDS_ENCODING_TURKISH) { |
| 203 return l10n_util::GetStringF(IDS_ENCODING_DISPLAY_TEMPLATE, | 203 return l10n_util::GetStringFUTF16(IDS_ENCODING_DISPLAY_TEMPLATE, |
| 204 category_name, | 204 category_name, |
| 205 encoding_name); | 205 ASCIIToUTF16(encoding_name)); |
| 206 } | 206 } |
| 207 return category_name; | 207 return category_name; |
| 208 } | 208 } |
| 209 | 209 |
| 210 int GetEncodingCategoryStringIdByCommandId(int id) { | 210 int GetEncodingCategoryStringIdByCommandId(int id) { |
| 211 const IdToCanonicalEncodingNameMapType* map = | 211 const IdToCanonicalEncodingNameMapType* map = |
| 212 canonical_encoding_name_map_singleton. | 212 canonical_encoding_name_map_singleton. |
| 213 GetIdToCanonicalEncodingNameMapData(); | 213 GetIdToCanonicalEncodingNameMapData(); |
| 214 DCHECK(map); | 214 DCHECK(map); |
| 215 | 215 |
| 216 IdToCanonicalEncodingNameMapType::const_iterator found_name = map->find(id); | 216 IdToCanonicalEncodingNameMapType::const_iterator found_name = map->find(id); |
| 217 if (found_name != map->end()) | 217 if (found_name != map->end()) |
| 218 return found_name->second.second; | 218 return found_name->second.second; |
| 219 return 0; | 219 return 0; |
| 220 } | 220 } |
| 221 | 221 |
| 222 std::wstring GetEncodingCategoryStringByCommandId(int id) { | 222 std::string GetEncodingCategoryStringByCommandId(int id) { |
| 223 int category_id = GetEncodingCategoryStringIdByCommandId(id); | 223 int category_id = GetEncodingCategoryStringIdByCommandId(id); |
| 224 if (category_id) | 224 if (category_id) |
| 225 return l10n_util::GetString(category_id); | 225 return l10n_util::GetStringUTF8(category_id); |
| 226 return std::wstring(); | 226 return std::string(); |
| 227 } | 227 } |
| 228 | 228 |
| 229 } // namespace | 229 } // namespace |
| 230 | 230 |
| 231 CharacterEncoding::EncodingInfo::EncodingInfo(int id) | 231 CharacterEncoding::EncodingInfo::EncodingInfo(int id) |
| 232 : encoding_id(id) { | 232 : encoding_id(id) { |
| 233 encoding_category_name = GetEncodingCategoryStringByCommandId(id), | 233 encoding_category_name = UTF8ToWide(GetEncodingCategoryStringByCommandId(id)); |
| 234 encoding_display_name = GetCanonicalEncodingDisplayNameByCommandId(id); | 234 encoding_display_name = UTF16ToWideHack( |
| 235 GetCanonicalEncodingDisplayNameByCommandId(id)); |
| 235 } | 236 } |
| 236 | 237 |
| 237 // Static. | 238 // Static. |
| 238 int CharacterEncoding::GetCommandIdByCanonicalEncodingName( | 239 int CharacterEncoding::GetCommandIdByCanonicalEncodingName( |
| 239 const std::wstring& encoding_name) { | 240 const std::string& encoding_name) { |
| 240 const CanonicalEncodingNameToIdMapType* map = | 241 const CanonicalEncodingNameToIdMapType* map = |
| 241 canonical_encoding_name_map_singleton. | 242 canonical_encoding_name_map_singleton. |
| 242 GetCanonicalEncodingNameToIdMapData(); | 243 GetCanonicalEncodingNameToIdMapData(); |
| 243 DCHECK(map); | 244 DCHECK(map); |
| 244 | 245 |
| 245 CanonicalEncodingNameToIdMapType::const_iterator found_id = | 246 CanonicalEncodingNameToIdMapType::const_iterator found_id = |
| 246 map->find(encoding_name); | 247 map->find(encoding_name); |
| 247 if (found_id != map->end()) | 248 if (found_id != map->end()) |
| 248 return found_id->second; | 249 return found_id->second; |
| 249 return 0; | 250 return 0; |
| 250 } | 251 } |
| 251 | 252 |
| 252 // Static. | 253 // Static. |
| 253 std::wstring CharacterEncoding::GetCanonicalEncodingNameByCommandId(int id) { | 254 std::string CharacterEncoding::GetCanonicalEncodingNameByCommandId(int id) { |
| 254 const IdToCanonicalEncodingNameMapType* map = | 255 const IdToCanonicalEncodingNameMapType* map = |
| 255 canonical_encoding_name_map_singleton. | 256 canonical_encoding_name_map_singleton. |
| 256 GetIdToCanonicalEncodingNameMapData(); | 257 GetIdToCanonicalEncodingNameMapData(); |
| 257 DCHECK(map); | 258 DCHECK(map); |
| 258 | 259 |
| 259 IdToCanonicalEncodingNameMapType::const_iterator found_name = map->find(id); | 260 IdToCanonicalEncodingNameMapType::const_iterator found_name = map->find(id); |
| 260 if (found_name != map->end()) | 261 if (found_name != map->end()) |
| 261 return found_name->second.first; | 262 return found_name->second.first; |
| 262 return std::wstring(); | 263 return std::string(); |
| 263 } | 264 } |
| 264 | 265 |
| 265 // Static. | 266 // Static. |
| 266 std::wstring CharacterEncoding::GetCanonicalEncodingDisplayNameByCommandId( | 267 string16 CharacterEncoding::GetCanonicalEncodingDisplayNameByCommandId( |
| 267 int id) { | 268 int id) { |
| 268 const IdToCanonicalEncodingNameMapType* map = | 269 const IdToCanonicalEncodingNameMapType* map = |
| 269 canonical_encoding_name_map_singleton. | 270 canonical_encoding_name_map_singleton. |
| 270 GetIdToCanonicalEncodingNameMapData(); | 271 GetIdToCanonicalEncodingNameMapData(); |
| 271 DCHECK(map); | 272 DCHECK(map); |
| 272 | 273 |
| 273 IdToCanonicalEncodingNameMapType::const_iterator found_name = map->find(id); | 274 IdToCanonicalEncodingNameMapType::const_iterator found_name = map->find(id); |
| 274 if (found_name != map->end()) | 275 if (found_name != map->end()) |
| 275 return GetEncodingDisplayName(found_name->second.first, | 276 return GetEncodingDisplayName(found_name->second.first, |
| 276 found_name->second.second); | 277 found_name->second.second); |
| 277 return std::wstring(); | 278 return string16(); |
| 278 } | 279 } |
| 279 | 280 |
| 280 // Static. | 281 // Static. |
| 281 // Return count number of all supported canonical encoding. | 282 // Return count number of all supported canonical encoding. |
| 282 int CharacterEncoding::GetSupportCanonicalEncodingCount() { | 283 int CharacterEncoding::GetSupportCanonicalEncodingCount() { |
| 283 return canonical_encoding_names_length; | 284 return canonical_encoding_names_length; |
| 284 } | 285 } |
| 285 | 286 |
| 286 // Static. | 287 // Static. |
| 287 std::wstring CharacterEncoding::GetCanonicalEncodingNameByIndex(int index) { | 288 std::string CharacterEncoding::GetCanonicalEncodingNameByIndex(int index) { |
| 288 if (index < canonical_encoding_names_length) | 289 if (index < canonical_encoding_names_length) |
| 289 return canonical_encoding_names[index].name; | 290 return canonical_encoding_names[index].name; |
| 290 return std::wstring(); | 291 return std::string(); |
| 291 } | 292 } |
| 292 | 293 |
| 293 // Static. | 294 // Static. |
| 294 std::wstring CharacterEncoding::GetCanonicalEncodingDisplayNameByIndex( | 295 string16 CharacterEncoding::GetCanonicalEncodingDisplayNameByIndex( |
| 295 int index) { | 296 int index) { |
| 296 if (index < canonical_encoding_names_length) | 297 if (index < canonical_encoding_names_length) |
| 297 return GetEncodingDisplayName(canonical_encoding_names[index].name, | 298 return GetEncodingDisplayName(canonical_encoding_names[index].name, |
| 298 canonical_encoding_names[index].category_string_id); | 299 canonical_encoding_names[index].category_string_id); |
| 299 return std::wstring(); | 300 return string16(); |
| 300 } | 301 } |
| 301 | 302 |
| 302 // Static. | 303 // Static. |
| 303 int CharacterEncoding::GetEncodingCommandIdByIndex(int index) { | 304 int CharacterEncoding::GetEncodingCommandIdByIndex(int index) { |
| 304 if (index < canonical_encoding_names_length) | 305 if (index < canonical_encoding_names_length) |
| 305 return canonical_encoding_names[index].resource_id; | 306 return canonical_encoding_names[index].resource_id; |
| 306 return 0; | 307 return 0; |
| 307 } | 308 } |
| 308 | 309 |
| 309 // Static. | 310 // Static. |
| 310 std::wstring CharacterEncoding::GetCanonicalEncodingNameByAliasName( | 311 std::string CharacterEncoding::GetCanonicalEncodingNameByAliasName( |
| 311 const std::wstring& alias_name) { | 312 const std::string& alias_name) { |
| 312 // If the input alias_name is already canonical encoding name, just return it. | 313 // If the input alias_name is already canonical encoding name, just return it. |
| 313 const CanonicalEncodingNameToIdMapType* map = | 314 const CanonicalEncodingNameToIdMapType* map = |
| 314 canonical_encoding_name_map_singleton. | 315 canonical_encoding_name_map_singleton. |
| 315 GetCanonicalEncodingNameToIdMapData(); | 316 GetCanonicalEncodingNameToIdMapData(); |
| 316 DCHECK(map); | 317 DCHECK(map); |
| 317 | 318 |
| 318 CanonicalEncodingNameToIdMapType::const_iterator found_id = | 319 CanonicalEncodingNameToIdMapType::const_iterator found_id = |
| 319 map->find(alias_name); | 320 map->find(alias_name); |
| 320 if (found_id != map->end()) | 321 if (found_id != map->end()) |
| 321 return alias_name; | 322 return alias_name; |
| 322 | 323 |
| 323 UErrorCode error_code = U_ZERO_ERROR; | 324 UErrorCode error_code = U_ZERO_ERROR; |
| 324 | 325 |
| 325 const char* canonical_name = ucnv_getCanonicalName( | 326 const char* canonical_name = ucnv_getCanonicalName( |
| 326 WideToASCII(alias_name).c_str(), "MIME", &error_code); | 327 alias_name.c_str(), "MIME", &error_code); |
| 327 // If failed, then try IANA next. | 328 // If failed, then try IANA next. |
| 328 if (U_FAILURE(error_code) || !canonical_name) { | 329 if (U_FAILURE(error_code) || !canonical_name) { |
| 329 error_code = U_ZERO_ERROR; | 330 error_code = U_ZERO_ERROR; |
| 330 canonical_name = ucnv_getCanonicalName( | 331 canonical_name = ucnv_getCanonicalName( |
| 331 WideToASCII(alias_name).c_str(), "IANA", &error_code); | 332 alias_name.c_str(), "IANA", &error_code); |
| 332 } | 333 } |
| 333 | 334 |
| 334 if (canonical_name) { | 335 if (canonical_name) { |
| 335 // TODO(jnd) use a map to handle all customized {alias, canonical} | 336 // TODO(jnd) use a map to handle all customized {alias, canonical} |
| 336 // encoding mappings if we have more than one pair. | 337 // encoding mappings if we have more than one pair. |
| 337 // We don't want to add an unnecessary charset to the encoding menu, so we | 338 // We don't want to add an unnecessary charset to the encoding menu, so we |
| 338 // alias 'US-ASCII' to 'ISO-8859-1' in our UI without touching WebKit. | 339 // alias 'US-ASCII' to 'ISO-8859-1' in our UI without touching WebKit. |
| 339 // http://crbug.com/15801. | 340 // http://crbug.com/15801. |
| 340 if (alias_name == L"US-ASCII") | 341 if (alias_name == "US-ASCII") |
| 341 return GetCanonicalEncodingNameByCommandId(IDC_ENCODING_ISO88591); | 342 return GetCanonicalEncodingNameByCommandId(IDC_ENCODING_ISO88591); |
| 342 return ASCIIToWide(canonical_name); | 343 return canonical_name; |
| 343 } else { | 344 } else { |
| 344 return std::wstring(); | 345 return std::string(); |
| 345 } | 346 } |
| 346 } | 347 } |
| 347 | 348 |
| 348 // Static | 349 // Static |
| 349 // According to the behavior of user recently selected encoding short list in | 350 // According to the behavior of user recently selected encoding short list in |
| 350 // FireFox, we always put UTF-8 as toppest position, after then put user | 351 // FireFox, we always put UTF-8 as toppest position, after then put user |
| 351 // recently selected encodings, then put local dependent encoding items. | 352 // recently selected encodings, then put local dependent encoding items. |
| 352 // At last, we put all rest encoding items. | 353 // At last, we put all rest encoding items. |
| 353 const std::vector<CharacterEncoding::EncodingInfo>* | 354 const std::vector<CharacterEncoding::EncodingInfo>* |
| 354 CharacterEncoding::GetCurrentDisplayEncodings( | 355 CharacterEncoding::GetCurrentDisplayEncodings( |
| 355 const std::string& locale, | 356 const std::string& locale, |
| 356 const std::wstring& locale_encodings, | 357 const std::string& locale_encodings, |
| 357 const std::wstring& recently_select_encodings) { | 358 const std::string& recently_select_encodings) { |
| 358 std::vector<int>* const locale_dependent_encoding_list = | 359 std::vector<int>* const locale_dependent_encoding_list = |
| 359 canonical_encoding_name_map_singleton.locale_dependent_encoding_ids(); | 360 canonical_encoding_name_map_singleton.locale_dependent_encoding_ids(); |
| 360 std::vector<CharacterEncoding::EncodingInfo>* const encoding_list = | 361 std::vector<CharacterEncoding::EncodingInfo>* const encoding_list = |
| 361 canonical_encoding_name_map_singleton.current_display_encodings(); | 362 canonical_encoding_name_map_singleton.current_display_encodings(); |
| 362 | 363 |
| 363 // Initialize locale dependent static encoding list. | 364 // Initialize locale dependent static encoding list. |
| 364 if (locale_dependent_encoding_list->empty() && !locale_encodings.empty()) | 365 if (locale_dependent_encoding_list->empty() && !locale_encodings.empty()) |
| 365 ParseEncodingListSeparatedWithComma(locale_encodings, | 366 ParseEncodingListSeparatedWithComma(locale_encodings, |
| 366 locale_dependent_encoding_list, | 367 locale_dependent_encoding_list, |
| 367 kUserSelectedEncodingsMaxLength); | 368 kUserSelectedEncodingsMaxLength); |
| 368 | 369 |
| 369 static std::wstring cached_user_selected_encodings; | 370 static std::string cached_user_selected_encodings; |
| 370 // Build current display encoding list. | 371 // Build current display encoding list. |
| 371 if (encoding_list->empty() || | 372 if (encoding_list->empty() || |
| 372 cached_user_selected_encodings != recently_select_encodings) { | 373 cached_user_selected_encodings != recently_select_encodings) { |
| 373 // Update user recently selected encodings. | 374 // Update user recently selected encodings. |
| 374 cached_user_selected_encodings = recently_select_encodings; | 375 cached_user_selected_encodings = recently_select_encodings; |
| 375 // Clear old encoding list since user recently selected encodings changed. | 376 // Clear old encoding list since user recently selected encodings changed. |
| 376 encoding_list->clear(); | 377 encoding_list->clear(); |
| 377 // Always add UTF-8 to first encoding position. | 378 // Always add UTF-8 to first encoding position. |
| 378 encoding_list->push_back(EncodingInfo(IDC_ENCODING_UTF8)); | 379 encoding_list->push_back(EncodingInfo(IDC_ENCODING_UTF8)); |
| 379 std::set<int> inserted_encoding; | 380 std::set<int> inserted_encoding; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 start_sorted_index, | 433 start_sorted_index, |
| 433 encoding_list->size(), | 434 encoding_list->size(), |
| 434 true); | 435 true); |
| 435 } | 436 } |
| 436 DCHECK(!encoding_list->empty()); | 437 DCHECK(!encoding_list->empty()); |
| 437 return encoding_list; | 438 return encoding_list; |
| 438 } | 439 } |
| 439 | 440 |
| 440 // Static | 441 // Static |
| 441 bool CharacterEncoding::UpdateRecentlySelectdEncoding( | 442 bool CharacterEncoding::UpdateRecentlySelectdEncoding( |
| 442 const std::wstring& original_selected_encodings, | 443 const std::string& original_selected_encodings, |
| 443 int new_selected_encoding_id, | 444 int new_selected_encoding_id, |
| 444 std::wstring* selected_encodings) { | 445 std::string* selected_encodings) { |
| 445 // Get encoding name. | 446 // Get encoding name. |
| 446 std::wstring encoding_name = | 447 std::string encoding_name = |
| 447 GetCanonicalEncodingNameByCommandId(new_selected_encoding_id); | 448 GetCanonicalEncodingNameByCommandId(new_selected_encoding_id); |
| 448 DCHECK(!encoding_name.empty()); | 449 DCHECK(!encoding_name.empty()); |
| 449 // Check whether the new encoding is in local dependent encodings or original | 450 // Check whether the new encoding is in local dependent encodings or original |
| 450 // recently selected encodings. If yes, do not add it. | 451 // recently selected encodings. If yes, do not add it. |
| 451 std::vector<int>* locale_dependent_encoding_list = | 452 std::vector<int>* locale_dependent_encoding_list = |
| 452 canonical_encoding_name_map_singleton.locale_dependent_encoding_ids(); | 453 canonical_encoding_name_map_singleton.locale_dependent_encoding_ids(); |
| 453 DCHECK(locale_dependent_encoding_list); | 454 DCHECK(locale_dependent_encoding_list); |
| 454 std::vector<int> selected_encoding_list; | 455 std::vector<int> selected_encoding_list; |
| 455 ParseEncodingListSeparatedWithComma(original_selected_encodings, | 456 ParseEncodingListSeparatedWithComma(original_selected_encodings, |
| 456 &selected_encoding_list, | 457 &selected_encoding_list, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 474 // Insert new encoding to head of selected encoding list. | 475 // Insert new encoding to head of selected encoding list. |
| 475 *selected_encodings = encoding_name; | 476 *selected_encodings = encoding_name; |
| 476 // Generate the string for rest selected encoding list. | 477 // Generate the string for rest selected encoding list. |
| 477 for (std::vector<int>::const_iterator it = selected_encoding_list.begin(); | 478 for (std::vector<int>::const_iterator it = selected_encoding_list.begin(); |
| 478 it != selected_encoding_list.end(); ++it) { | 479 it != selected_encoding_list.end(); ++it) { |
| 479 selected_encodings->append(1, L','); | 480 selected_encodings->append(1, L','); |
| 480 selected_encodings->append(GetCanonicalEncodingNameByCommandId(*it)); | 481 selected_encodings->append(GetCanonicalEncodingNameByCommandId(*it)); |
| 481 } | 482 } |
| 482 return true; | 483 return true; |
| 483 } | 484 } |
| OLD | NEW |