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

Side by Side Diff: chrome/browser/character_encoding.cc

Issue 21414: We will sort the encoding menu items according to current used UI language ex... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 10 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
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 "base/logging.h" 10 #include "base/logging.h"
(...skipping 22 matching lines...) Expand all
33 { IDC_ENCODING_UTF8, L"UTF-8", IDS_ENCODING_UNICODE }, 33 { IDC_ENCODING_UTF8, L"UTF-8", IDS_ENCODING_UNICODE },
34 { IDC_ENCODING_UTF16LE, L"UTF-16LE", IDS_ENCODING_UNICODE }, 34 { IDC_ENCODING_UTF16LE, L"UTF-16LE", IDS_ENCODING_UNICODE },
35 { IDC_ENCODING_ISO88591, L"ISO-8859-1", IDS_ENCODING_WESTERN }, 35 { IDC_ENCODING_ISO88591, L"ISO-8859-1", IDS_ENCODING_WESTERN },
36 { IDC_ENCODING_WINDOWS1252, L"windows-1252", IDS_ENCODING_WESTERN }, 36 { IDC_ENCODING_WINDOWS1252, L"windows-1252", IDS_ENCODING_WESTERN },
37 { IDC_ENCODING_GBK, L"GBK", IDS_ENCODING_SIMP_CHINESE }, 37 { IDC_ENCODING_GBK, L"GBK", IDS_ENCODING_SIMP_CHINESE },
38 { IDC_ENCODING_GB18030, L"gb18030", IDS_ENCODING_SIMP_CHINESE }, 38 { IDC_ENCODING_GB18030, L"gb18030", IDS_ENCODING_SIMP_CHINESE },
39 { IDC_ENCODING_BIG5, L"Big5", IDS_ENCODING_TRAD_CHINESE }, 39 { IDC_ENCODING_BIG5, L"Big5", IDS_ENCODING_TRAD_CHINESE },
40 { IDC_ENCODING_BIG5HKSCS, L"Big5-HKSCS", IDS_ENCODING_TRAD_CHINESE }, 40 { IDC_ENCODING_BIG5HKSCS, L"Big5-HKSCS", IDS_ENCODING_TRAD_CHINESE },
41 { IDC_ENCODING_KOREAN, L"windows-949", IDS_ENCODING_KOREAN }, 41 { IDC_ENCODING_KOREAN, L"windows-949", IDS_ENCODING_KOREAN },
42 { IDC_ENCODING_SHIFTJIS, L"Shift_JIS", IDS_ENCODING_JAPANESE }, 42 { IDC_ENCODING_SHIFTJIS, L"Shift_JIS", IDS_ENCODING_JAPANESE },
43 { IDC_ENCODING_EUCJP, L"EUC-JP", IDS_ENCODING_JAPANESE },
43 { IDC_ENCODING_ISO2022JP, L"ISO-2022-JP", IDS_ENCODING_JAPANESE }, 44 { IDC_ENCODING_ISO2022JP, L"ISO-2022-JP", IDS_ENCODING_JAPANESE },
44 { IDC_ENCODING_EUCJP, L"EUC-JP", IDS_ENCODING_JAPANESE },
45 { IDC_ENCODING_THAI, L"windows-874", IDS_ENCODING_THAI }, 45 { IDC_ENCODING_THAI, L"windows-874", IDS_ENCODING_THAI },
46 { IDC_ENCODING_ISO885915, L"ISO-8859-15", IDS_ENCODING_WESTERN }, 46 { IDC_ENCODING_ISO885915, L"ISO-8859-15", IDS_ENCODING_WESTERN },
47 { IDC_ENCODING_MACINTOSH, L"macintosh", IDS_ENCODING_WESTERN }, 47 { IDC_ENCODING_MACINTOSH, L"macintosh", IDS_ENCODING_WESTERN },
48 { IDC_ENCODING_ISO88592, L"ISO-8859-2", IDS_ENCODING_CENTRAL_EUROPEAN }, 48 { IDC_ENCODING_ISO88592, L"ISO-8859-2", IDS_ENCODING_CENTRAL_EUROPEAN },
49 { IDC_ENCODING_WINDOWS1250, L"windows-1250", IDS_ENCODING_CENTRAL_EUROPEAN }, 49 { IDC_ENCODING_WINDOWS1250, L"windows-1250", IDS_ENCODING_CENTRAL_EUROPEAN },
50 { IDC_ENCODING_ISO88595, L"ISO-8859-5", IDS_ENCODING_CYRILLIC }, 50 { IDC_ENCODING_ISO88595, L"ISO-8859-5", IDS_ENCODING_CYRILLIC },
51 { IDC_ENCODING_WINDOWS1251, L"windows-1251", IDS_ENCODING_CYRILLIC }, 51 { IDC_ENCODING_WINDOWS1251, L"windows-1251", IDS_ENCODING_CYRILLIC },
52 { IDC_ENCODING_KOI8R, L"KOI8-R", IDS_ENCODING_CYRILLIC }, 52 { IDC_ENCODING_KOI8R, L"KOI8-R", IDS_ENCODING_CYRILLIC },
53 { IDC_ENCODING_KOI8U, L"KOI8-U", IDS_ENCODING_CYRILLIC }, 53 { IDC_ENCODING_KOI8U, L"KOI8-U", IDS_ENCODING_CYRILLIC },
54 { IDC_ENCODING_ISO88597, L"ISO-8859-7", IDS_ENCODING_GREEK }, 54 { IDC_ENCODING_ISO88597, L"ISO-8859-7", IDS_ENCODING_GREEK },
55 { IDC_ENCODING_WINDOWS1253, L"windows-1253", IDS_ENCODING_GREEK }, 55 { IDC_ENCODING_WINDOWS1253, L"windows-1253", IDS_ENCODING_GREEK },
56 { IDC_ENCODING_WINDOWS1254, L"windows-1254", IDS_ENCODING_TURKISH }, 56 { IDC_ENCODING_WINDOWS1254, L"windows-1254", IDS_ENCODING_TURKISH },
57 { IDC_ENCODING_ISO88596, L"ISO-8859-6", IDS_ENCODING_ARABIC }, 57 { IDC_ENCODING_ISO88596, L"ISO-8859-6", IDS_ENCODING_ARABIC },
58 { IDC_ENCODING_WINDOWS1256, L"windows-1256", IDS_ENCODING_ARABIC }, 58 { IDC_ENCODING_WINDOWS1256, L"windows-1256", IDS_ENCODING_ARABIC },
59 { IDC_ENCODING_ISO88598, L"ISO-8859-8", IDS_ENCODING_HEBREW }, 59 { IDC_ENCODING_ISO88598, L"ISO-8859-8", IDS_ENCODING_HEBREW },
60 { IDC_ENCODING_WINDOWS1255, L"windows-1255", IDS_ENCODING_HEBREW }, 60 { IDC_ENCODING_WINDOWS1255, L"windows-1255", IDS_ENCODING_HEBREW },
61 { IDC_ENCODING_WINDOWS1258, L"windows-1258", IDS_ENCODING_VIETNAMESE }, 61 { IDC_ENCODING_WINDOWS1258, L"windows-1258", IDS_ENCODING_VIETNAMESE },
62 { IDC_ENCODING_ISO88594, L"ISO-8859-4", IDS_ENCODING_BALTIC }, 62 { IDC_ENCODING_ISO88594, L"ISO-8859-4", IDS_ENCODING_BALTIC },
63 { IDC_ENCODING_ISO885913, L"ISO-8859-13", IDS_ENCODING_BALTIC }, 63 { IDC_ENCODING_ISO885913, L"ISO-8859-13", IDS_ENCODING_BALTIC },
64 { IDC_ENCODING_WINDOWS1257, L"windows-1257", IDS_ENCODING_BALTIC }, 64 { IDC_ENCODING_WINDOWS1257, L"windows-1257", IDS_ENCODING_BALTIC },
65 { IDC_ENCODING_ISO88593, L"ISO-8859-3", IDS_ENCODING_SOUTH_EUROPEAN }, 65 { IDC_ENCODING_ISO88593, L"ISO-8859-3", IDS_ENCODING_SOUTH_EUROPEAN },
66 { IDC_ENCODING_ISO885910, L"ISO-8859-10", IDS_ENCODING_NORDIC }, 66 { IDC_ENCODING_ISO885910, L"ISO-8859-10", IDS_ENCODING_NORDIC },
67 { IDC_ENCODING_ISO885914, L"ISO-8859-14", IDS_ENCODING_CELTIC }, 67 { IDC_ENCODING_ISO885914, L"ISO-8859-14", IDS_ENCODING_CELTIC },
68 { IDC_ENCODING_ISO885916, L"ISO-8859-16", IDS_ENCODING_ROMANIAN }, 68 { IDC_ENCODING_ISO885916, L"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> > IdToCanonicalEncodingName MapType; 74 typedef std::map<int, std::pair<const wchar_t*, int> >
75 IdToCanonicalEncodingNameMapType;
75 typedef std::map<const std::wstring, int> CanonicalEncodingNameToIdMapType; 76 typedef std::map<const std::wstring, int> CanonicalEncodingNameToIdMapType;
76 77
77 class CanonicalEncodingMap { 78 class CanonicalEncodingMap {
78 public: 79 public:
79 CanonicalEncodingMap() 80 CanonicalEncodingMap()
80 : id_to_encoding_name_map_(NULL), 81 : id_to_encoding_name_map_(NULL),
81 encoding_name_to_id_map_(NULL) { } 82 encoding_name_to_id_map_(NULL) { }
82 const IdToCanonicalEncodingNameMapType* GetIdToCanonicalEncodingNameMapData(); 83 const IdToCanonicalEncodingNameMapType* GetIdToCanonicalEncodingNameMapData();
83 const CanonicalEncodingNameToIdMapType* GetCanonicalEncodingNameToIdMapData(); 84 const CanonicalEncodingNameToIdMapType* GetCanonicalEncodingNameToIdMapData();
84 std::vector<int>* const locale_dependent_encoding_ids() { 85 std::vector<int>* const locale_dependent_encoding_ids() {
85 return &locale_dependent_encoding_ids_; 86 return &locale_dependent_encoding_ids_;
86 } 87 }
87 88
88 std::vector<int>* const current_display_encoding_ids() { 89 std::vector<CharacterEncoding::EncodingInfo>* const current_display_encodings( ) {
89 return &current_display_encoding_ids_; 90 return &current_display_encodings_;
90 } 91 }
91 92
92 private: 93 private:
93 scoped_ptr<IdToCanonicalEncodingNameMapType> id_to_encoding_name_map_; 94 scoped_ptr<IdToCanonicalEncodingNameMapType> id_to_encoding_name_map_;
94 scoped_ptr<CanonicalEncodingNameToIdMapType> encoding_name_to_id_map_; 95 scoped_ptr<CanonicalEncodingNameToIdMapType> encoding_name_to_id_map_;
95 std::vector<int> locale_dependent_encoding_ids_; 96 std::vector<int> locale_dependent_encoding_ids_;
96 std::vector<int> current_display_encoding_ids_; 97 std::vector<CharacterEncoding::EncodingInfo> current_display_encodings_;
97 98
98 DISALLOW_EVIL_CONSTRUCTORS(CanonicalEncodingMap); 99 DISALLOW_EVIL_CONSTRUCTORS(CanonicalEncodingMap);
99 }; 100 };
100 101
101 const IdToCanonicalEncodingNameMapType* CanonicalEncodingMap::GetIdToCanonicalEn codingNameMapData() { 102 const IdToCanonicalEncodingNameMapType* CanonicalEncodingMap::GetIdToCanonicalEn codingNameMapData() {
102 // Testing and building map is not thread safe, this function is supposed to 103 // Testing and building map is not thread safe, this function is supposed to
103 // only run in UI thread. Myabe I should add a lock in here for making it as 104 // only run in UI thread. Myabe I should add a lock in here for making it as
104 // thread safe. 105 // thread safe.
105 if (!id_to_encoding_name_map_.get()) { 106 if (!id_to_encoding_name_map_.get()) {
106 id_to_encoding_name_map_.reset(new IdToCanonicalEncodingNameMapType); 107 id_to_encoding_name_map_.reset(new IdToCanonicalEncodingNameMapType);
(...skipping 17 matching lines...) Expand all
124 } 125 }
125 return encoding_name_to_id_map_.get(); 126 return encoding_name_to_id_map_.get();
126 } 127 }
127 128
128 // A static map object which contains all resourceid-nonsequenced canonical 129 // A static map object which contains all resourceid-nonsequenced canonical
129 // encoding names. 130 // encoding names.
130 static CanonicalEncodingMap canonical_encoding_name_map_singleton; 131 static CanonicalEncodingMap canonical_encoding_name_map_singleton;
131 132
132 const int default_encoding_menus[] = { 133 const int default_encoding_menus[] = {
133 IDC_ENCODING_UTF16LE, 134 IDC_ENCODING_UTF16LE,
134 0,
135 IDC_ENCODING_ISO88591, 135 IDC_ENCODING_ISO88591,
136 IDC_ENCODING_WINDOWS1252, 136 IDC_ENCODING_WINDOWS1252,
137 0,
138 IDC_ENCODING_GBK, 137 IDC_ENCODING_GBK,
139 IDC_ENCODING_GB18030, 138 IDC_ENCODING_GB18030,
140 IDC_ENCODING_BIG5, 139 IDC_ENCODING_BIG5,
141 IDC_ENCODING_BIG5HKSCS, 140 IDC_ENCODING_BIG5HKSCS,
142 0,
143 IDC_ENCODING_KOREAN, 141 IDC_ENCODING_KOREAN,
144 0,
145 IDC_ENCODING_SHIFTJIS, 142 IDC_ENCODING_SHIFTJIS,
143 IDC_ENCODING_EUCJP,
146 IDC_ENCODING_ISO2022JP, 144 IDC_ENCODING_ISO2022JP,
147 IDC_ENCODING_EUCJP,
148 0,
149 IDC_ENCODING_THAI, 145 IDC_ENCODING_THAI,
150 0,
151 IDC_ENCODING_ISO885915, 146 IDC_ENCODING_ISO885915,
152 IDC_ENCODING_MACINTOSH, 147 IDC_ENCODING_MACINTOSH,
153 IDC_ENCODING_ISO88592, 148 IDC_ENCODING_ISO88592,
154 IDC_ENCODING_WINDOWS1250, 149 IDC_ENCODING_WINDOWS1250,
155 0,
156 IDC_ENCODING_ISO88595, 150 IDC_ENCODING_ISO88595,
157 IDC_ENCODING_WINDOWS1251, 151 IDC_ENCODING_WINDOWS1251,
158 IDC_ENCODING_KOI8R, 152 IDC_ENCODING_KOI8R,
159 IDC_ENCODING_KOI8U, 153 IDC_ENCODING_KOI8U,
160 0,
161 IDC_ENCODING_ISO88597, 154 IDC_ENCODING_ISO88597,
162 IDC_ENCODING_WINDOWS1253, 155 IDC_ENCODING_WINDOWS1253,
163 IDC_ENCODING_WINDOWS1254, 156 IDC_ENCODING_WINDOWS1254,
164 IDC_ENCODING_ISO88596, 157 IDC_ENCODING_ISO88596,
165 IDC_ENCODING_WINDOWS1256, 158 IDC_ENCODING_WINDOWS1256,
166 IDC_ENCODING_ISO88598, 159 IDC_ENCODING_ISO88598,
167 IDC_ENCODING_WINDOWS1255, 160 IDC_ENCODING_WINDOWS1255,
168 IDC_ENCODING_WINDOWS1258, 161 IDC_ENCODING_WINDOWS1258,
169
170 IDC_ENCODING_ISO88594, 162 IDC_ENCODING_ISO88594,
171 IDC_ENCODING_ISO885913, 163 IDC_ENCODING_ISO885913,
172 IDC_ENCODING_WINDOWS1257, 164 IDC_ENCODING_WINDOWS1257,
173 IDC_ENCODING_ISO88593, 165 IDC_ENCODING_ISO88593,
174 IDC_ENCODING_ISO885910, 166 IDC_ENCODING_ISO885910,
175 IDC_ENCODING_ISO885914, 167 IDC_ENCODING_ISO885914,
176 IDC_ENCODING_ISO885916, 168 IDC_ENCODING_ISO885916,
177 }; 169 };
178 170
179 const int default_encoding_menus_length = arraysize(default_encoding_menus); 171 const int default_encoding_menus_length = arraysize(default_encoding_menus);
180 172
181 // Parse the input |encoding_list| which is a encoding list separated with 173 // Parse the input |encoding_list| which is a encoding list separated with
182 // comma, get available encoding ids and save them to |available_list|. 174 // comma, get available encoding ids and save them to |available_list|.
183 // The parameter |maximum_size| indicates maximum size of encoding items we 175 // The parameter |maximum_size| indicates maximum size of encoding items we
184 // want to get from the |encoding_list|. 176 // want to get from the |encoding_list|.
185 static void ParseEncodingListSeparatedWithComma( 177 void ParseEncodingListSeparatedWithComma(
186 const std::wstring& encoding_list, std::vector<int>* const available_list, 178 const std::wstring& encoding_list, std::vector<int>* const available_list,
187 size_t maximum_size) { 179 size_t maximum_size) {
188 WStringTokenizer tokenizer(encoding_list, L","); 180 WStringTokenizer tokenizer(encoding_list, L",");
189 while (tokenizer.GetNext()) { 181 while (tokenizer.GetNext()) {
190 int id = CharacterEncoding::GetCommandIdByCanonicalEncodingName( 182 int id = CharacterEncoding::GetCommandIdByCanonicalEncodingName(
191 tokenizer.token()); 183 tokenizer.token());
192 // Ignore invalid encoding. 184 // Ignore invalid encoding.
193 if (!id) 185 if (!id)
194 continue; 186 continue;
195 available_list->push_back(id); 187 available_list->push_back(id);
196 if (available_list->size() == maximum_size) 188 if (available_list->size() == maximum_size)
197 return; 189 return;
198 } 190 }
199 } 191 }
200 192
201 std::wstring GetEncodingDisplayName(std::wstring encoding_name, 193 std::wstring GetEncodingDisplayName(std::wstring encoding_name,
202 int category_string_id) { 194 int category_string_id) {
203 std::wstring category_name = l10n_util::GetString(category_string_id); 195 std::wstring category_name = l10n_util::GetString(category_string_id);
204 if (category_string_id != IDS_ENCODING_KOREAN && 196 if (category_string_id != IDS_ENCODING_KOREAN &&
205 category_string_id != IDS_ENCODING_THAI && 197 category_string_id != IDS_ENCODING_THAI &&
206 category_string_id != IDS_ENCODING_TURKISH) { 198 category_string_id != IDS_ENCODING_TURKISH) {
207 return l10n_util::GetStringF(IDS_ENCODING_DISPLAY_TEMPLATE, 199 return l10n_util::GetStringF(IDS_ENCODING_DISPLAY_TEMPLATE,
208 category_name, 200 category_name,
209 encoding_name); 201 encoding_name);
210 } 202 }
211 return category_name; 203 return category_name;
212 } 204 }
213 205
206 int GetEncodingCategoryStringIdByCommandId(int id) {
207 const IdToCanonicalEncodingNameMapType* map =
208 canonical_encoding_name_map_singleton.
209 GetIdToCanonicalEncodingNameMapData();
210 DCHECK(map);
211
212 IdToCanonicalEncodingNameMapType::const_iterator found_name = map->find(id);
213 if (found_name != map->end())
214 return found_name->second.second;
215 return 0;
216 }
217
218 std::wstring GetEncodingCategoryStringByCommandId(int id) {
219 int category_id = GetEncodingCategoryStringIdByCommandId(id);
220 if (category_id)
221 return l10n_util::GetString(category_id);
222 return std::wstring();
223 }
224
214 } // namespace 225 } // namespace
215 226
227 CharacterEncoding::EncodingInfo::EncodingInfo(int id)
228 : encoding_id(id) {
229 encoding_category_name = GetEncodingCategoryStringByCommandId(id),
230 encoding_display_name = GetCanonicalEncodingDisplayNameByCommandId(id);
231 }
232
216 // Static. 233 // Static.
217 int CharacterEncoding::GetCommandIdByCanonicalEncodingName( 234 int CharacterEncoding::GetCommandIdByCanonicalEncodingName(
218 const std::wstring& encoding_name) { 235 const std::wstring& encoding_name) {
219 const CanonicalEncodingNameToIdMapType* map = 236 const CanonicalEncodingNameToIdMapType* map =
220 canonical_encoding_name_map_singleton. 237 canonical_encoding_name_map_singleton.
221 GetCanonicalEncodingNameToIdMapData(); 238 GetCanonicalEncodingNameToIdMapData();
222 DCHECK(map); 239 DCHECK(map);
223 240
224 CanonicalEncodingNameToIdMapType::const_iterator found_id = 241 CanonicalEncodingNameToIdMapType::const_iterator found_id =
225 map->find(encoding_name); 242 map->find(encoding_name);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 return ASCIIToWide(canonical_name); 324 return ASCIIToWide(canonical_name);
308 else 325 else
309 return std::wstring(); 326 return std::wstring();
310 } 327 }
311 328
312 // Static 329 // Static
313 // According to the behavior of user recently selected encoding short list in 330 // According to the behavior of user recently selected encoding short list in
314 // FireFox, we always put UTF-8 as toppest position, after then put user 331 // FireFox, we always put UTF-8 as toppest position, after then put user
315 // recently selected encodings, then put local dependent encoding items. 332 // recently selected encodings, then put local dependent encoding items.
316 // At last, we put all rest encoding items. 333 // At last, we put all rest encoding items.
317 const std::vector<int>* CharacterEncoding::GetCurrentDisplayEncodings( 334 const std::vector<CharacterEncoding::EncodingInfo>* CharacterEncoding::GetCurren tDisplayEncodings(
335 const std::wstring& locale,
318 const std::wstring& locale_encodings, 336 const std::wstring& locale_encodings,
319 const std::wstring& recently_select_encodings) { 337 const std::wstring& recently_select_encodings) {
320 std::vector<int>* const locale_dependent_encoding_list = 338 std::vector<int>* const locale_dependent_encoding_list =
321 canonical_encoding_name_map_singleton.locale_dependent_encoding_ids(); 339 canonical_encoding_name_map_singleton.locale_dependent_encoding_ids();
322 std::vector<int>* const encoding_list = 340 std::vector<CharacterEncoding::EncodingInfo>* const encoding_list =
323 canonical_encoding_name_map_singleton.current_display_encoding_ids(); 341 canonical_encoding_name_map_singleton.current_display_encodings();
324 342
325 // Initialize locale dependent static encoding list. 343 // Initialize locale dependent static encoding list.
326 if (locale_dependent_encoding_list->empty() && !locale_encodings.empty()) 344 if (locale_dependent_encoding_list->empty() && !locale_encodings.empty())
327 ParseEncodingListSeparatedWithComma(locale_encodings, 345 ParseEncodingListSeparatedWithComma(locale_encodings,
328 locale_dependent_encoding_list, 346 locale_dependent_encoding_list,
329 kUserSelectedEncodingsMaxLength); 347 kUserSelectedEncodingsMaxLength);
330 348
331 static std::wstring cached_user_selected_encodings; 349 static std::wstring cached_user_selected_encodings;
332 // Build current display encoding list. 350 // Build current display encoding list.
333 if (encoding_list->empty() || 351 if (encoding_list->empty() ||
334 cached_user_selected_encodings != recently_select_encodings) { 352 cached_user_selected_encodings != recently_select_encodings) {
335 // Update user recently selected encodings. 353 // Update user recently selected encodings.
336 cached_user_selected_encodings = recently_select_encodings; 354 cached_user_selected_encodings = recently_select_encodings;
337 // Clear old encoding list since user recently selected encodings changed. 355 // Clear old encoding list since user recently selected encodings changed.
338 encoding_list->clear(); 356 encoding_list->clear();
339 // Always add UTF-8 to first encoding position. 357 // Always add UTF-8 to first encoding position.
340 encoding_list->push_back(IDC_ENCODING_UTF8); 358 encoding_list->push_back(EncodingInfo(IDC_ENCODING_UTF8));
341 std::set<int> inserted_encoding; 359 std::set<int> inserted_encoding;
342 inserted_encoding.insert(IDC_ENCODING_UTF8); 360 inserted_encoding.insert(IDC_ENCODING_UTF8);
343 361
344 // Parse user recently selected encodings and get list 362 // Parse user recently selected encodings and get list
345 std::vector<int> recently_select_encoding_list; 363 std::vector<int> recently_select_encoding_list;
346 ParseEncodingListSeparatedWithComma(recently_select_encodings, 364 ParseEncodingListSeparatedWithComma(recently_select_encodings,
347 &recently_select_encoding_list, 365 &recently_select_encoding_list,
348 kUserSelectedEncodingsMaxLength); 366 kUserSelectedEncodingsMaxLength);
349 367
350 // Put 'cached encodings' (dynamic encoding list) after 'local dependent 368 // Put 'cached encodings' (dynamic encoding list) after 'local dependent
351 // encoding list'. 369 // encoding list'.
352 recently_select_encoding_list.insert(recently_select_encoding_list.begin(), 370 recently_select_encoding_list.insert(recently_select_encoding_list.begin(),
353 locale_dependent_encoding_list->begin(), 371 locale_dependent_encoding_list->begin(),
354 locale_dependent_encoding_list->end()); 372 locale_dependent_encoding_list->end());
355 std::vector<int>::const_iterator it; 373 for (std::vector<int>::iterator it = recently_select_encoding_list.begin();
356 for (it = recently_select_encoding_list.begin();
357 it != recently_select_encoding_list.end(); ++it) { 374 it != recently_select_encoding_list.end(); ++it) {
358 // Test whether we have met this encoding id. 375 // Test whether we have met this encoding id.
359 bool ok = inserted_encoding.insert(*it).second; 376 bool ok = inserted_encoding.insert(*it).second;
360 // Duplicated encoding, ignore it. Ideally, this situation should not 377 // Duplicated encoding, ignore it. Ideally, this situation should not
361 // happened, but just in case some one manually edit preference file. 378 // happened, but just in case some one manually edit preference file.
362 if (!ok) 379 if (!ok)
363 continue; 380 continue;
364 encoding_list->push_back(*it); 381 encoding_list->push_back(EncodingInfo(*it));
365 } 382 }
366 // Append a separator; 383 // Append a separator;
367 encoding_list->push_back(0); 384 encoding_list->push_back(EncodingInfo(0));
368 385
369 // Add those encodings which are in default_encoding_menus and does not 386 // We need to keep "Unicode (UTF-16LE)" always at the top (among the rest
370 // override with locale-dependent encodings list. 387 // of encodings) instead of being sorted along with other encodings. So if
371 bool previous_is_separator = true; 388 // "Unicode (UTF-16LE)" is already in previous encodings, sort the rest
389 // of encodings. Otherwise Put "Unicode (UTF-16LE)" on the first of the
390 // rest of encodings, skip "Unicode (UTF-16LE)" and sort all left encodings.
391 int start_sorted_index = encoding_list->size();
392 if (inserted_encoding.find(IDC_ENCODING_UTF16LE) ==
393 inserted_encoding.end()) {
394 encoding_list->push_back(EncodingInfo(IDC_ENCODING_UTF16LE));
395 inserted_encoding.insert(IDC_ENCODING_UTF16LE);
396 start_sorted_index++;
397 }
398
399 // Add the rest of encodings that are neither in the static encoding list
400 // nor in the list of recently selected encodings.
401 // Build the encoding list sorted in the current locale sorting order.
372 for (int i = 0; i < default_encoding_menus_length; ++i) { 402 for (int i = 0; i < default_encoding_menus_length; ++i) {
373 int id = default_encoding_menus[i]; 403 int id = default_encoding_menus[i];
374 if (id) { 404 // We have inserted this encoding, skip it.
375 // We have inserted this encoding, skip it. 405 if (inserted_encoding.find(id) != inserted_encoding.end())
376 if (inserted_encoding.find(id) != inserted_encoding.end()) 406 continue;
377 continue; 407 encoding_list->push_back(EncodingInfo(id));
378 encoding_list->push_back(id);
379 previous_is_separator = false;
380 } else if (!previous_is_separator) {
381 encoding_list->push_back(0);
382 previous_is_separator = true;
383 }
384 } 408 }
409 // Sort the encoding list.
410 l10n_util::SortVectorWithStringKey(locale,
411 encoding_list,
412 start_sorted_index,
413 encoding_list->size(),
414 true);
385 } 415 }
386 DCHECK(!encoding_list->empty()); 416 DCHECK(!encoding_list->empty());
387 return encoding_list; 417 return encoding_list;
388 } 418 }
389 419
390 // Static 420 // Static
391 bool CharacterEncoding::UpdateRecentlySelectdEncoding( 421 bool CharacterEncoding::UpdateRecentlySelectdEncoding(
392 const std::wstring& original_selected_encodings, 422 const std::wstring& original_selected_encodings,
393 int new_selected_encoding_id, 423 int new_selected_encoding_id,
394 std::wstring* selected_encodings) { 424 std::wstring* selected_encodings) {
(...skipping 29 matching lines...) Expand all
424 // Insert new encoding to head of selected encoding list. 454 // Insert new encoding to head of selected encoding list.
425 *selected_encodings = encoding_name; 455 *selected_encodings = encoding_name;
426 // Generate the string for rest selected encoding list. 456 // Generate the string for rest selected encoding list.
427 for (std::vector<int>::const_iterator it = selected_encoding_list.begin(); 457 for (std::vector<int>::const_iterator it = selected_encoding_list.begin();
428 it != selected_encoding_list.end(); ++it) { 458 it != selected_encoding_list.end(); ++it) {
429 selected_encodings->append(1, L','); 459 selected_encodings->append(1, L',');
430 selected_encodings->append(GetCanonicalEncodingNameByCommandId(*it)); 460 selected_encodings->append(GetCanonicalEncodingNameByCommandId(*it));
431 } 461 }
432 return true; 462 return true;
433 } 463 }
OLDNEW
« no previous file with comments | « chrome/browser/character_encoding.h ('k') | chrome/browser/encoding_menu_controller_delegate.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698