Chromium Code Reviews| 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); |
| 51 | |
| 52 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 52 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
| 53 for (size_t i = 0; i < languages_.size(); ++i) { | 53 |
| 54 base::string16 display_name( | 54 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 55 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | 55 if (command_line->HasSwitch(switches::kEnableMultilingualSpellChecker)) { |
|
please use gerrit instead
2014/11/07 16:20:08
It's best to put this line into a separate functio
Klemen Forstnerič
2014/11/11 21:52:01
Good idea, I'll do it this way.
| |
| 56 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, | 56 for (size_t i = 0; i < languages_.size(); ++i) { |
| 57 display_name, | 57 base::string16 display_name( |
| 58 language_group_); | 58 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); |
| 59 submenu_model_.AddCheckItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, | |
| 60 display_name); | |
| 61 } | |
| 62 } else { | |
| 63 for (size_t i = 0; i < languages_.size(); ++i) { | |
| 64 base::string16 display_name( | |
| 65 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | |
| 66 submenu_model_.AddRadioItem( | |
| 67 IDC_SPELLCHECK_LANGUAGES_FIRST + i, display_name, language_group_); | |
| 68 } | |
| 59 } | 69 } |
| 60 | 70 |
| 61 // Add an item that opens the 'fonts and languages options' page. | 71 // Add an item that opens the 'fonts and languages options' page. |
| 62 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 72 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
| 63 submenu_model_.AddItemWithStringId( | 73 submenu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, |
| 64 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, | 74 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); |
| 65 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); | |
| 66 | 75 |
| 67 // Add a 'Check spelling while typing' item in the sub menu. | 76 // Add a 'Check spelling while typing' item in the sub menu. |
| 68 submenu_model_.AddCheckItem( | 77 submenu_model_.AddCheckItem( |
| 69 IDC_CHECK_SPELLING_WHILE_TYPING, | 78 IDC_CHECK_SPELLING_WHILE_TYPING, |
| 70 l10n_util::GetStringUTF16( | 79 l10n_util::GetStringUTF16( |
| 71 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); | 80 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); |
| 72 | 81 |
| 73 // Add a check item "Ask Google for spelling suggestions" item. (This class | 82 // Add a check item "Ask Google for spelling suggestions" item. (This class |
| 74 // does not handle this item because the SpellingMenuObserver class handles it | 83 // does not handle this item because the SpellingMenuObserver class handles it |
| 75 // on behalf of this class.) | 84 // on behalf of this class.) |
| 76 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | 85 submenu_model_.AddCheckItem( |
| 86 IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | |
| 77 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); | 87 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); |
| 78 | 88 |
| 79 // Add a check item "Automatically correct spelling". | 89 // Add a check item "Automatically correct spelling". |
| 80 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 81 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { | 90 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { |
| 82 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, | 91 submenu_model_.AddCheckItem( |
| 92 IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, | |
| 83 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); | 93 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); |
| 84 } | 94 } |
| 85 | 95 |
| 86 proxy_->AddSubMenu( | 96 proxy_->AddSubMenu( |
| 87 IDC_SPELLCHECK_MENU, | 97 IDC_SPELLCHECK_MENU, |
| 88 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), | 98 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), |
| 89 &submenu_model_); | 99 &submenu_model_); |
| 90 } | 100 } |
| 91 | 101 |
| 92 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) { | 102 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 109 } | 119 } |
| 110 | 120 |
| 111 return false; | 121 return false; |
| 112 } | 122 } |
| 113 | 123 |
| 114 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { | 124 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
| 115 DCHECK(IsCommandIdSupported(command_id)); | 125 DCHECK(IsCommandIdSupported(command_id)); |
| 116 | 126 |
| 117 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 127 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 118 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 128 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 119 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 129 return languages_selected_.count(command_id - |
| 130 IDC_SPELLCHECK_LANGUAGES_FIRST) == 1; | |
| 120 } | 131 } |
| 121 | 132 |
| 122 // Check box for 'Check Spelling while typing'. | 133 // Check box for 'Check Spelling while typing'. |
| 123 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | 134 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
| 124 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 135 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 125 DCHECK(profile); | 136 DCHECK(profile); |
| 126 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | 137 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); |
| 127 } | 138 } |
| 128 | 139 |
| 129 return false; | 140 return false; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 149 | 160 |
| 150 return false; | 161 return false; |
| 151 } | 162 } |
| 152 | 163 |
| 153 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { | 164 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
| 154 DCHECK(IsCommandIdSupported(command_id)); | 165 DCHECK(IsCommandIdSupported(command_id)); |
| 155 | 166 |
| 156 // Check to see if one of the spell check language ids have been clicked. | 167 // Check to see if one of the spell check language ids have been clicked. |
| 157 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 168 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 158 DCHECK(profile); | 169 DCHECK(profile); |
| 159 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 170 |
| 160 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 171 PrefService* prefs = profile->GetPrefs(); |
| 172 | |
| 173 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | |
| 174 prefs->SetBoolean(prefs::kEnableContinuousSpellcheck, | |
| 175 !prefs->GetBoolean(prefs::kEnableContinuousSpellcheck)); | |
| 176 } else if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
| 177 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
| 161 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 178 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
| 162 if (profile && language < languages_.size()) { | 179 if (!profile || language >= languages_.size()) |
| 163 StringPrefMember dictionary_language; | 180 return; |
| 164 dictionary_language.Init(prefs::kSpellCheckDictionary, | 181 |
| 165 profile->GetPrefs()); | 182 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 166 dictionary_language.SetValue(languages_[language]); | 183 if (command_line->HasSwitch(switches::kEnableMultilingualSpellChecker)) { |
| 184 std::vector<std::string> languages_separated; | |
| 185 base::SplitString(prefs->GetString(prefs::kSpellCheckDictionaries), | |
| 186 ',', | |
| 187 &languages_separated); | |
| 188 | |
| 189 std::vector<std::string>::iterator language_iter = | |
| 190 std::find(languages_separated.begin(), | |
| 191 languages_separated.end(), | |
| 192 languages_[language]); | |
| 193 | |
| 194 if (language_iter != languages_separated.end()) | |
| 195 languages_separated.erase(language_iter); | |
| 196 else | |
| 197 languages_separated.push_back(languages_[language]); | |
| 198 | |
| 199 prefs->SetString(prefs::kSpellCheckDictionaries, | |
| 200 JoinString(languages_separated, ',')); | |
| 201 } else { | |
| 202 prefs->SetString(prefs::kSpellCheckDictionary, languages_[language]); | |
| 167 } | 203 } |
| 168 return; | |
| 169 } | |
| 170 | |
| 171 switch (command_id) { | |
| 172 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
| 173 profile->GetPrefs()->SetBoolean( | |
| 174 prefs::kEnableContinuousSpellcheck, | |
| 175 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | |
| 176 break; | |
| 177 } | 204 } |
| 178 } | 205 } |
| OLD | NEW |