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" |
9 #include "base/prefs/pref_member.h" | 11 #include "base/prefs/pref_member.h" |
10 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
13 #include "base/strings/string_split.h" | |
14 #include "base/strings/string_util.h" | |
11 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
12 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 17 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
14 #include "chrome/browser/spellchecker/spellcheck_service.h" | 18 #include "chrome/browser/spellchecker/spellcheck_service.h" |
15 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
17 #include "chrome/common/spellcheck_messages.h" | 21 #include "chrome/common/spellcheck_messages.h" |
18 #include "chrome/grit/generated_resources.h" | 22 #include "chrome/grit/generated_resources.h" |
19 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
20 #include "content/public/browser/render_widget_host_view.h" | 24 #include "content/public/browser/render_widget_host_view.h" |
21 #include "extensions/browser/view_type_utils.h" | 25 #include "extensions/browser/view_type_utils.h" |
22 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/base/models/simple_menu_model.h" | 27 #include "ui/base/models/simple_menu_model.h" |
24 | 28 |
25 using content::BrowserThread; | 29 using content::BrowserThread; |
26 | 30 |
31 namespace { | |
32 static const char kLanguagesSeparator = ','; | |
please use gerrit instead
2015/02/24 23:38:38
Let's use the constant from spellcheck_common here
Klemen Forstnerič
2015/02/25 09:48:22
Done.
| |
33 } // namespace | |
34 | |
27 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( | 35 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( |
28 RenderViewContextMenuProxy* proxy, | 36 RenderViewContextMenuProxy* proxy, |
29 ui::SimpleMenuModel::Delegate* delegate, | 37 ui::SimpleMenuModel::Delegate* delegate, |
30 int group) | 38 int group) |
31 : proxy_(proxy), | 39 : proxy_(proxy), |
32 submenu_model_(delegate), | 40 submenu_model_(delegate), |
33 language_group_(group), | 41 language_group_(group) { |
34 language_selected_(0) { | |
35 DCHECK(proxy_); | 42 DCHECK(proxy_); |
36 } | 43 } |
37 | 44 |
38 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { | 45 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
39 } | 46 } |
40 | 47 |
41 void SpellCheckerSubMenuObserver::InitMenu( | 48 void SpellCheckerSubMenuObserver::InitMenu( |
42 const content::ContextMenuParams& params) { | 49 const content::ContextMenuParams& params) { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
44 | 51 |
45 // Add available spell-checker languages to the sub menu. | 52 // Add available spell-checker languages to the sub menu. |
46 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 53 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
47 DCHECK(browser_context); | 54 DCHECK(browser_context); |
48 language_selected_ = | 55 spellcheck_languages_ = |
49 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); | 56 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); |
50 DCHECK(languages_.size() < | 57 DCHECK(languages_.size() < |
51 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | 58 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
52 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 59 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
60 const base::CommandLine* command_line = | |
61 base::CommandLine::ForCurrentProcess(); | |
62 | |
53 for (size_t i = 0; i < languages_.size(); ++i) { | 63 for (size_t i = 0; i < languages_.size(); ++i) { |
54 base::string16 display_name( | 64 base::string16 display_name( |
55 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | 65 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); |
56 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, | 66 if (command_line->HasSwitch(switches::kEnableMultilingualSpellChecker)) { |
57 display_name, | 67 submenu_model_.AddCheckItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
58 language_group_); | 68 display_name); |
69 } else { | |
70 submenu_model_.AddRadioItem( | |
please use gerrit instead
2015/02/24 23:38:38
Please format submenu_model_.AddCheckItem() and su
Klemen Forstnerič
2015/02/25 09:48:23
Done.
| |
71 IDC_SPELLCHECK_LANGUAGES_FIRST + i, display_name, 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( |
69 IDC_CHECK_SPELLING_WHILE_TYPING, | 83 IDC_CHECK_SPELLING_WHILE_TYPING, |
70 l10n_util::GetStringUTF16( | 84 l10n_util::GetStringUTF16( |
71 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); | 85 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); |
72 | 86 |
73 // Add a check item "Ask Google for spelling suggestions" item. (This class | 87 // Add a check item "Ask Google for spelling suggestions" item. (This class |
74 // does not handle this item because the SpellingMenuObserver class handles it | 88 // does not handle this item because the SpellingMenuObserver class handles it |
75 // on behalf of this class.) | 89 // on behalf of this class.) |
76 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | 90 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, |
77 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); | 91 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); |
78 | 92 |
79 // Add a check item "Automatically correct spelling". | 93 // Add a check item "Automatically correct spelling". |
80 const base::CommandLine* command_line = | |
81 base::CommandLine::ForCurrentProcess(); | |
82 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { | 94 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { |
83 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, | 95 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, |
84 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); | 96 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); |
85 } | 97 } |
86 | 98 |
87 proxy_->AddSubMenu( | 99 proxy_->AddSubMenu( |
88 IDC_SPELLCHECK_MENU, | 100 IDC_SPELLCHECK_MENU, |
89 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), | 101 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), |
90 &submenu_model_); | 102 &submenu_model_); |
91 } | 103 } |
(...skipping 18 matching lines...) Expand all Loading... | |
110 } | 122 } |
111 | 123 |
112 return false; | 124 return false; |
113 } | 125 } |
114 | 126 |
115 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { | 127 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
116 DCHECK(IsCommandIdSupported(command_id)); | 128 DCHECK(IsCommandIdSupported(command_id)); |
117 | 129 |
118 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 130 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
119 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 131 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
120 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 132 return spellcheck_languages_.find(command_id - |
133 IDC_SPELLCHECK_LANGUAGES_FIRST) != spellcheck_languages_.end(); | |
121 } | 134 } |
122 | 135 |
123 // Check box for 'Check Spelling while typing'. | 136 // Check box for 'Check Spelling while typing'. |
124 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | 137 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
125 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 138 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
126 DCHECK(profile); | 139 DCHECK(profile); |
127 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | 140 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); |
128 } | 141 } |
129 | 142 |
130 return false; | 143 return false; |
(...skipping 19 matching lines...) Expand all Loading... | |
150 | 163 |
151 return false; | 164 return false; |
152 } | 165 } |
153 | 166 |
154 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { | 167 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
155 DCHECK(IsCommandIdSupported(command_id)); | 168 DCHECK(IsCommandIdSupported(command_id)); |
156 | 169 |
157 // Check to see if one of the spell check language ids have been clicked. | 170 // Check to see if one of the spell check language ids have been clicked. |
158 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 171 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
159 DCHECK(profile); | 172 DCHECK(profile); |
173 | |
174 PrefService* prefs = profile->GetPrefs(); | |
160 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 175 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
161 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 176 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
162 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 177 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
please use gerrit instead
2015/02/24 23:38:38
"const" is not necessary. We try to not go overboa
Klemen Forstnerič
2015/02/25 09:48:22
Done.
| |
163 if (profile && language < languages_.size()) { | 178 |
164 StringPrefMember dictionary_language; | 179 DCHECK_LT(language, languages_.size()); |
165 dictionary_language.Init(prefs::kSpellCheckDictionary, | 180 |
166 profile->GetPrefs()); | 181 const base::CommandLine* command_line = |
167 dictionary_language.SetValue(languages_[language]); | 182 base::CommandLine::ForCurrentProcess(); |
183 if (command_line->HasSwitch(switches::kEnableMultilingualSpellChecker)) { | |
please use gerrit instead
2015/02/24 23:38:38
Please inline "command_line", because it's used on
Klemen Forstnerič
2015/02/25 09:48:23
Done.
| |
184 std::vector<std::string> languages_split; | |
185 base::SplitString(prefs->GetString(prefs::kSpellCheckDictionaries), | |
186 kLanguagesSeparator, | |
187 &languages_split); | |
188 | |
189 auto found_language = std::find(languages_split.begin(), | |
190 languages_split.end(), | |
191 languages_[language]); | |
192 | |
193 if (found_language != languages_split.end()) | |
194 languages_split.erase(found_language); | |
195 else | |
196 languages_split.push_back(languages_[language]); | |
197 | |
198 prefs->SetString(prefs::kSpellCheckDictionaries, | |
199 JoinString(languages_split, kLanguagesSeparator)); | |
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 |