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 "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/prefs/pref_member.h" | 9 #include "base/prefs/pref_member.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/strings/string_split.h" | |
12 #include "base/strings/string_util.h" | |
11 #include "chrome/app/chrome_command_ids.h" | 13 #include "chrome/app/chrome_command_ids.h" |
12 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 15 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
14 #include "chrome/browser/spellchecker/spellcheck_service.h" | 16 #include "chrome/browser/spellchecker/spellcheck_service.h" |
15 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
17 #include "chrome/common/spellcheck_messages.h" | 19 #include "chrome/common/spellcheck_messages.h" |
18 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
19 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
20 #include "content/public/browser/render_widget_host_view.h" | 22 #include "content/public/browser/render_widget_host_view.h" |
21 #include "extensions/browser/view_type_utils.h" | 23 #include "extensions/browser/view_type_utils.h" |
22 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/base/models/simple_menu_model.h" | 25 #include "ui/base/models/simple_menu_model.h" |
24 | 26 |
25 using content::BrowserThread; | 27 using content::BrowserThread; |
26 | 28 |
27 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( | 29 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( |
28 RenderViewContextMenuProxy* proxy, | 30 RenderViewContextMenuProxy* proxy, |
29 ui::SimpleMenuModel::Delegate* delegate, | 31 ui::SimpleMenuModel::Delegate* delegate, |
30 int group) | 32 int group) |
31 : proxy_(proxy), | 33 : proxy_(proxy), submenu_model_(delegate), language_group_(group) { |
32 submenu_model_(delegate), | |
33 language_group_(group), | |
34 language_selected_(0) { | |
35 DCHECK(proxy_); | 34 DCHECK(proxy_); |
36 } | 35 } |
37 | 36 |
38 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { | 37 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
39 } | 38 } |
40 | 39 |
41 void SpellCheckerSubMenuObserver::InitMenu( | 40 void SpellCheckerSubMenuObserver::InitMenu( |
42 const content::ContextMenuParams& params) { | 41 const content::ContextMenuParams& params) { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
44 | 43 |
45 // Add available spell-checker languages to the sub menu. | 44 // Add available spell-checker languages to the sub menu. |
46 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 45 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
47 DCHECK(browser_context); | 46 DCHECK(browser_context); |
48 language_selected_ = | 47 languages_selected_ = |
49 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); | 48 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); |
50 DCHECK(languages_.size() < | 49 DCHECK(languages_.size() < |
51 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | 50 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
52 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 51 |
53 for (size_t i = 0; i < languages_.size(); ++i) { | 52 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
54 base::string16 display_name( | 53 if (command_line->HasSwitch(switches::kEnableMultilingualSpellChecker)) |
55 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | 54 AddLanguagesAsCheckItems(); |
56 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, | 55 else |
57 display_name, | 56 AddLanguagesAsRadioItems(); |
58 language_group_); | |
59 } | |
60 | 57 |
61 // Add an item that opens the 'fonts and languages options' page. | 58 // Add an item that opens the 'fonts and languages options' page. |
62 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 59 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
63 submenu_model_.AddItemWithStringId( | 60 submenu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, |
64 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, | 61 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); |
please use gerrit instead
2014/10/24 19:28:56
Whitespace changes should go into a whitespace-onl
| |
65 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); | |
66 | 62 |
67 // Add a 'Check spelling while typing' item in the sub menu. | 63 // Add a 'Check spelling while typing' item in the sub menu. |
68 submenu_model_.AddCheckItem( | 64 submenu_model_.AddCheckItem( |
69 IDC_CHECK_SPELLING_WHILE_TYPING, | 65 IDC_CHECK_SPELLING_WHILE_TYPING, |
70 l10n_util::GetStringUTF16( | 66 l10n_util::GetStringUTF16( |
71 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); | 67 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); |
72 | 68 |
73 // Add a check item "Ask Google for spelling suggestions" item. (This class | 69 // Add a check item "Ask Google for spelling suggestions" item. (This class |
74 // does not handle this item because the SpellingMenuObserver class handles it | 70 // does not handle this item because the SpellingMenuObserver class handles it |
75 // on behalf of this class.) | 71 // on behalf of this class.) |
76 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | 72 submenu_model_.AddCheckItem( |
73 IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | |
77 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); | 74 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); |
78 | 75 |
79 // Add a check item "Automatically correct spelling". | 76 // Add a check item "Automatically correct spelling". |
80 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
81 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { | 77 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { |
82 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, | 78 submenu_model_.AddCheckItem( |
79 IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, | |
83 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); | 80 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); |
84 } | 81 } |
85 | 82 |
86 proxy_->AddSubMenu( | 83 proxy_->AddSubMenu( |
87 IDC_SPELLCHECK_MENU, | 84 IDC_SPELLCHECK_MENU, |
88 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), | 85 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), |
89 &submenu_model_); | 86 &submenu_model_); |
90 } | 87 } |
91 | 88 |
92 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) { | 89 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) { |
(...skipping 16 matching lines...) Expand all Loading... | |
109 } | 106 } |
110 | 107 |
111 return false; | 108 return false; |
112 } | 109 } |
113 | 110 |
114 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { | 111 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
115 DCHECK(IsCommandIdSupported(command_id)); | 112 DCHECK(IsCommandIdSupported(command_id)); |
116 | 113 |
117 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 114 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
118 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 115 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
119 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 116 return languages_selected_.count(command_id - |
117 IDC_SPELLCHECK_LANGUAGES_FIRST) == 1; | |
120 } | 118 } |
121 | 119 |
122 // Check box for 'Check Spelling while typing'. | 120 // Check box for 'Check Spelling while typing'. |
123 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | 121 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
124 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 122 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
125 DCHECK(profile); | 123 DCHECK(profile); |
126 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | 124 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); |
127 } | 125 } |
128 | 126 |
129 return false; | 127 return false; |
(...skipping 19 matching lines...) Expand all Loading... | |
149 | 147 |
150 return false; | 148 return false; |
151 } | 149 } |
152 | 150 |
153 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { | 151 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
154 DCHECK(IsCommandIdSupported(command_id)); | 152 DCHECK(IsCommandIdSupported(command_id)); |
155 | 153 |
156 // Check to see if one of the spell check language ids have been clicked. | 154 // Check to see if one of the spell check language ids have been clicked. |
157 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 155 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
158 DCHECK(profile); | 156 DCHECK(profile); |
159 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 157 |
160 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 158 PrefService* prefs = profile->GetPrefs(); |
159 | |
160 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | |
161 prefs->SetBoolean(prefs::kEnableContinuousSpellcheck, | |
162 !prefs->GetBoolean(prefs::kEnableContinuousSpellcheck)); | |
163 } else if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
164 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
161 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 165 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
162 if (profile && language < languages_.size()) { | 166 if (!profile || language >= languages_.size()) |
163 StringPrefMember dictionary_language; | 167 return; |
164 dictionary_language.Init(prefs::kSpellCheckDictionary, | |
165 profile->GetPrefs()); | |
166 dictionary_language.SetValue(languages_[language]); | |
167 } | |
168 return; | |
169 } | |
170 | 168 |
171 switch (command_id) { | 169 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
172 case IDC_CHECK_SPELLING_WHILE_TYPING: | 170 if (command_line->HasSwitch(switches::kEnableMultilingualSpellChecker)) |
173 profile->GetPrefs()->SetBoolean( | 171 UpdateSpellcheckDictionariesWithLanguage(language, prefs); |
174 prefs::kEnableContinuousSpellcheck, | 172 else |
175 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | 173 SetLanguageAsSpellcheckDictionary(language, prefs); |
176 break; | |
177 } | 174 } |
178 } | 175 } |
176 | |
177 void SpellCheckerSubMenuObserver::AddLanguagesAsRadioItems() { | |
please use gerrit instead
2014/10/24 19:27:22
Ditto.
| |
178 const std::string app_locale = g_browser_process->GetApplicationLocale(); | |
179 for (size_t i = 0; i < languages_.size(); ++i) { | |
180 base::string16 display_name( | |
181 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | |
182 submenu_model_.AddRadioItem( | |
183 IDC_SPELLCHECK_LANGUAGES_FIRST + i, display_name, language_group_); | |
184 } | |
185 } | |
186 | |
187 void SpellCheckerSubMenuObserver::AddLanguagesAsCheckItems() { | |
please use gerrit instead
2014/10/24 19:27:22
Ditto.
| |
188 const std::string app_locale = g_browser_process->GetApplicationLocale(); | |
189 for (size_t i = 0; i < languages_.size(); ++i) { | |
190 base::string16 display_name( | |
191 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | |
192 submenu_model_.AddCheckItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, | |
193 display_name); | |
194 } | |
195 } | |
196 | |
197 void SpellCheckerSubMenuObserver::UpdateSpellcheckDictionariesWithLanguage( | |
please use gerrit instead
2014/10/24 19:30:01
Please inline.
| |
198 size_t language, | |
199 PrefService* prefs) { | |
200 std::vector<std::string> languages_separated; | |
201 base::SplitString(prefs->GetString(prefs::kSpellCheckDictionaries), | |
202 ',', | |
203 &languages_separated); | |
204 | |
205 std::vector<std::string>::iterator language_iter = | |
206 std::find(languages_separated.begin(), | |
207 languages_separated.end(), | |
208 languages_[language]); | |
209 | |
210 if (language_iter != languages_separated.end()) | |
211 languages_separated.erase(language_iter); | |
212 else | |
213 languages_separated.push_back(languages_[language]); | |
214 | |
215 prefs->SetString(prefs::kSpellCheckDictionaries, | |
216 JoinString(languages_separated, ',')); | |
217 } | |
218 | |
219 void SpellCheckerSubMenuObserver::SetLanguageAsSpellcheckDictionary( | |
please use gerrit instead
2014/10/24 19:30:02
Please inline.
| |
220 size_t language, | |
221 PrefService* prefs) { | |
222 | |
223 prefs->SetString(prefs::kSpellCheckDictionary, languages_[language]); | |
224 } | |
OLD | NEW |