Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / encoding_menu_controller.cc
blob0f4a8217123270248c3d50cb0a877e28be158d8f
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.
5 #include "chrome/browser/ui/toolbar/encoding_menu_controller.h"
7 #include "base/i18n/rtl.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/character_encoding.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
18 const int EncodingMenuController::kValidEncodingIds[] = {
19 IDC_ENCODING_UTF8,
20 IDC_ENCODING_UTF16LE,
21 IDC_ENCODING_WINDOWS1252,
22 IDC_ENCODING_GBK,
23 IDC_ENCODING_GB18030,
24 IDC_ENCODING_BIG5,
25 IDC_ENCODING_KOREAN,
26 IDC_ENCODING_SHIFTJIS,
27 IDC_ENCODING_ISO2022JP,
28 IDC_ENCODING_EUCJP,
29 IDC_ENCODING_THAI,
30 IDC_ENCODING_ISO885915,
31 IDC_ENCODING_MACINTOSH,
32 IDC_ENCODING_ISO88592,
33 IDC_ENCODING_WINDOWS1250,
34 IDC_ENCODING_ISO88595,
35 IDC_ENCODING_WINDOWS1251,
36 IDC_ENCODING_KOI8R,
37 IDC_ENCODING_KOI8U,
38 IDC_ENCODING_ISO88597,
39 IDC_ENCODING_WINDOWS1253,
40 IDC_ENCODING_ISO88594,
41 IDC_ENCODING_ISO885913,
42 IDC_ENCODING_WINDOWS1257,
43 IDC_ENCODING_ISO88593,
44 IDC_ENCODING_ISO885910,
45 IDC_ENCODING_ISO885914,
46 IDC_ENCODING_ISO885916,
47 IDC_ENCODING_WINDOWS1254,
48 IDC_ENCODING_ISO88596,
49 IDC_ENCODING_WINDOWS1256,
50 IDC_ENCODING_ISO88598,
51 IDC_ENCODING_WINDOWS1255,
52 IDC_ENCODING_WINDOWS1258,
53 IDC_ENCODING_ISO88598I,
54 IDC_ENCODING_IBM866,
57 bool EncodingMenuController::DoesCommandBelongToEncodingMenu(int id) {
58 if (id == IDC_ENCODING_AUTO_DETECT) {
59 return true;
62 for (size_t i = 0; i < arraysize(kValidEncodingIds); ++i) {
63 if (id == kValidEncodingIds[i]) {
64 return true;
68 return false;
71 const int* EncodingMenuController::ValidGUIEncodingIDs() {
72 return kValidEncodingIds;
75 int EncodingMenuController::NumValidGUIEncodingIDs() {
76 return arraysize(kValidEncodingIds);
79 bool EncodingMenuController::IsItemChecked(
80 Profile* browser_profile,
81 const std::string& current_tab_encoding,
82 int item_id) {
83 if (!DoesCommandBelongToEncodingMenu(item_id))
84 return false;
86 std::string encoding = current_tab_encoding;
87 if (encoding.empty())
88 encoding = browser_profile->GetPrefs()->GetString(prefs::kDefaultCharset);
90 if (item_id == IDC_ENCODING_AUTO_DETECT) {
91 return browser_profile->GetPrefs()->GetBoolean(
92 prefs::kWebKitUsesUniversalDetector);
95 if (!encoding.empty()) {
96 return encoding ==
97 CharacterEncoding::GetCanonicalEncodingNameByCommandId(item_id);
100 return false;
103 void EncodingMenuController::GetEncodingMenuItems(Profile* profile,
104 EncodingMenuItemList* menu_items) {
106 DCHECK(menu_items);
107 EncodingMenuItem separator(0, base::string16());
109 menu_items->clear();
110 menu_items->push_back(
111 EncodingMenuItem(IDC_ENCODING_AUTO_DETECT,
112 l10n_util::GetStringUTF16(IDS_ENCODING_AUTO_DETECT)));
113 menu_items->push_back(separator);
115 // Create current display encoding list.
116 const std::vector<CharacterEncoding::EncodingInfo>* encodings;
118 // Build the list of encoding ids : It is made of the
119 // locale-dependent short list, the cache of recently selected
120 // encodings and other encodings.
121 encodings = CharacterEncoding::GetCurrentDisplayEncodings(
122 g_browser_process->GetApplicationLocale(),
123 profile->GetPrefs()->GetString(prefs::kStaticEncodings),
124 profile->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding));
125 DCHECK(encodings);
126 DCHECK(!encodings->empty());
128 // Build up output list for menu.
129 std::vector<CharacterEncoding::EncodingInfo>::const_iterator it;
130 for (it = encodings->begin(); it != encodings->end(); ++it) {
131 if (it->encoding_id) {
132 base::string16 encoding = it->encoding_display_name;
133 base::i18n::AdjustStringForLocaleDirection(&encoding);
134 menu_items->push_back(EncodingMenuItem(it->encoding_id, encoding));
135 } else {
136 menu_items->push_back(separator);