OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/renderer_context_menu/spellchecker_submenu_observer.h" | 5 #include "chrome/browser/renderer_context_menu/spellchecker_submenu_observer.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/numerics/safe_conversions.h" |
9 #include "base/prefs/pref_member.h" | 12 #include "base/prefs/pref_member.h" |
10 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/strings/string_split.h" |
| 15 #include "base/strings/string_util.h" |
11 #include "chrome/app/chrome_command_ids.h" | 16 #include "chrome/app/chrome_command_ids.h" |
12 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 18 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
14 #include "chrome/browser/spellchecker/spellcheck_service.h" | 19 #include "chrome/browser/spellchecker/spellcheck_service.h" |
15 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 22 #include "chrome/common/spellcheck_common.h" |
17 #include "chrome/common/spellcheck_messages.h" | 23 #include "chrome/common/spellcheck_messages.h" |
18 #include "chrome/grit/generated_resources.h" | 24 #include "chrome/grit/generated_resources.h" |
19 #include "content/public/browser/render_view_host.h" | 25 #include "content/public/browser/render_view_host.h" |
20 #include "content/public/browser/render_widget_host_view.h" | 26 #include "content/public/browser/render_widget_host_view.h" |
21 #include "extensions/browser/view_type_utils.h" | 27 #include "extensions/browser/view_type_utils.h" |
22 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/base/models/simple_menu_model.h" | 29 #include "ui/base/models/simple_menu_model.h" |
24 | 30 |
25 using content::BrowserThread; | 31 using content::BrowserThread; |
26 | 32 |
27 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( | 33 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( |
28 RenderViewContextMenuProxy* proxy, | 34 RenderViewContextMenuProxy* proxy, |
29 ui::SimpleMenuModel::Delegate* delegate, | 35 ui::SimpleMenuModel::Delegate* delegate, |
30 int group) | 36 int group) |
31 : proxy_(proxy), | 37 : proxy_(proxy), |
32 submenu_model_(delegate), | 38 submenu_model_(delegate), |
33 language_group_(group), | 39 language_group_(group), |
34 language_selected_(0) { | 40 selected_languages_(0) { |
35 DCHECK(proxy_); | 41 DCHECK(proxy_); |
36 } | 42 } |
37 | 43 |
38 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { | 44 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
39 } | 45 } |
40 | 46 |
41 void SpellCheckerSubMenuObserver::InitMenu( | 47 void SpellCheckerSubMenuObserver::InitMenu( |
42 const content::ContextMenuParams& params) { | 48 const content::ContextMenuParams& params) { |
43 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
44 | 50 |
45 // Add available spell-checker languages to the sub menu. | 51 // Add available spell-checker languages to the sub menu. |
46 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 52 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
47 DCHECK(browser_context); | 53 DCHECK(browser_context); |
48 language_selected_ = | 54 selected_languages_ = |
49 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); | 55 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); |
50 DCHECK(languages_.size() < | 56 DCHECK(languages_.size() < |
51 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | 57 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
52 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 58 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
53 for (size_t i = 0; i < languages_.size(); ++i) { | 59 |
54 base::string16 display_name( | 60 if (chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) { |
55 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | 61 for (size_t i = 0; i < languages_.size(); ++i) { |
56 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, | 62 submenu_model_.AddCheckItem( |
57 display_name, | 63 IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
58 language_group_); | 64 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); |
| 65 } |
| 66 } else { |
| 67 for (size_t i = 0; i < languages_.size(); ++i) { |
| 68 submenu_model_.AddRadioItem( |
| 69 IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
| 70 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true), |
| 71 language_group_); |
| 72 } |
59 } | 73 } |
60 | 74 |
61 // Add an item that opens the 'fonts and languages options' page. | 75 // Add an item that opens the 'fonts and languages options' page. |
62 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 76 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
63 submenu_model_.AddItemWithStringId( | 77 submenu_model_.AddItemWithStringId( |
64 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, | 78 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, |
65 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); | 79 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); |
66 | 80 |
67 // Add a 'Check spelling while typing' item in the sub menu. | 81 // Add a 'Check spelling while typing' item in the sub menu. |
68 submenu_model_.AddCheckItem( | 82 submenu_model_.AddCheckItem( |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 } | 124 } |
111 | 125 |
112 return false; | 126 return false; |
113 } | 127 } |
114 | 128 |
115 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { | 129 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
116 DCHECK(IsCommandIdSupported(command_id)); | 130 DCHECK(IsCommandIdSupported(command_id)); |
117 | 131 |
118 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 132 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
119 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 133 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
120 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 134 return selected_languages_ > |
| 135 base::checked_cast<size_t>(command_id - |
| 136 IDC_SPELLCHECK_LANGUAGES_FIRST); |
121 } | 137 } |
122 | 138 |
123 // Check box for 'Check Spelling while typing'. | 139 // Check box for 'Check Spelling while typing'. |
124 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | 140 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
125 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 141 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
126 DCHECK(profile); | 142 DCHECK(profile); |
127 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | 143 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); |
128 } | 144 } |
129 | 145 |
130 return false; | 146 return false; |
(...skipping 19 matching lines...) Expand all Loading... |
150 | 166 |
151 return false; | 167 return false; |
152 } | 168 } |
153 | 169 |
154 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { | 170 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
155 DCHECK(IsCommandIdSupported(command_id)); | 171 DCHECK(IsCommandIdSupported(command_id)); |
156 | 172 |
157 // Check to see if one of the spell check language ids have been clicked. | 173 // Check to see if one of the spell check language ids have been clicked. |
158 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 174 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
159 DCHECK(profile); | 175 DCHECK(profile); |
| 176 |
160 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 177 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
161 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 178 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
162 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 179 PrefService* prefs = profile->GetPrefs(); |
163 if (profile && language < languages_.size()) { | 180 size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
164 StringPrefMember dictionary_language; | 181 DCHECK_LT(language, languages_.size()); |
165 dictionary_language.Init(prefs::kSpellCheckDictionary, | 182 |
166 profile->GetPrefs()); | 183 if (chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) { |
167 dictionary_language.SetValue(languages_[language]); | 184 std::vector<std::string> dictionary_languages = |
| 185 chrome::spellcheck_common::GetDictionaryLanguagesPref(prefs); |
| 186 |
| 187 auto found_language = |
| 188 std::find(dictionary_languages.begin(), dictionary_languages.end(), |
| 189 languages_[language]); |
| 190 |
| 191 if (found_language != dictionary_languages.end()) |
| 192 dictionary_languages.erase(found_language); |
| 193 else |
| 194 dictionary_languages.push_back(languages_[language]); |
| 195 |
| 196 prefs->SetString( |
| 197 prefs::kSpellCheckDictionaries, |
| 198 JoinString(dictionary_languages, |
| 199 chrome::spellcheck_common::kDictionaryLanguagesSeparator)); |
| 200 } else { |
| 201 prefs->SetString(prefs::kSpellCheckDictionary, languages_[language]); |
168 } | 202 } |
169 return; | 203 return; |
170 } | 204 } |
171 | 205 |
172 switch (command_id) { | 206 switch (command_id) { |
173 case IDC_CHECK_SPELLING_WHILE_TYPING: | 207 case IDC_CHECK_SPELLING_WHILE_TYPING: |
174 profile->GetPrefs()->SetBoolean( | 208 profile->GetPrefs()->SetBoolean( |
175 prefs::kEnableContinuousSpellcheck, | 209 prefs::kEnableContinuousSpellcheck, |
176 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | 210 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); |
177 break; | 211 break; |
178 } | 212 } |
179 } | 213 } |
OLD | NEW |