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), |
| 32 submenu_model_(delegate), | 34 submenu_model_(delegate), |
| 33 language_group_(group), | 35 language_group_(group) { |
| 34 language_selected_(0) { | |
| 35 DCHECK(proxy_); | 36 DCHECK(proxy_); |
| 36 } | 37 } |
| 37 | 38 |
| 38 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { | 39 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
| 39 } | 40 } |
| 40 | 41 |
| 41 void SpellCheckerSubMenuObserver::InitMenu( | 42 void SpellCheckerSubMenuObserver::InitMenu( |
| 42 const content::ContextMenuParams& params) { | 43 const content::ContextMenuParams& params) { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 44 | 45 |
| 45 // Add available spell-checker languages to the sub menu. | 46 // Add available spell-checker languages to the sub menu. |
| 46 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 47 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| 47 DCHECK(browser_context); | 48 DCHECK(browser_context); |
| 48 language_selected_ = | 49 spellcheck_languages_ = |
| 49 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); | 50 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); |
| 50 DCHECK(languages_.size() < | 51 DCHECK(languages_.size() < |
| 51 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | 52 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
| 52 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 53 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
| 53 for (size_t i = 0; i < languages_.size(); ++i) { | 54 const base::CommandLine* command_line = |
| 55 base::CommandLine::ForCurrentProcess(); | |
| 56 | |
| 57 for (std::size_t i = 0; i < languages_.size(); ++i) { | |
| 54 base::string16 display_name( | 58 base::string16 display_name( |
| 55 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | 59 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); |
| 56 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, | 60 if (command_line->HasSwitch(switches::kEnableMultilingualSpellChecker)) { |
| 57 display_name, | 61 submenu_model_.AddCheckItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
| 58 language_group_); | 62 display_name); |
| 63 } else { | |
| 64 submenu_model_.AddRadioItem( | |
| 65 IDC_SPELLCHECK_LANGUAGES_FIRST + i, display_name, language_group_); | |
| 66 } | |
| 59 } | 67 } |
| 60 | 68 |
| 61 // Add an item that opens the 'fonts and languages options' page. | 69 // Add an item that opens the 'fonts and languages options' page. |
| 62 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 70 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
| 63 submenu_model_.AddItemWithStringId( | 71 submenu_model_.AddItemWithStringId( |
| 64 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, | 72 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, |
| 65 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); | 73 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); |
| 66 | 74 |
| 67 // Add a 'Check spelling while typing' item in the sub menu. | 75 // Add a 'Check spelling while typing' item in the sub menu. |
| 68 submenu_model_.AddCheckItem( | 76 submenu_model_.AddCheckItem( |
| 69 IDC_CHECK_SPELLING_WHILE_TYPING, | 77 IDC_CHECK_SPELLING_WHILE_TYPING, |
| 70 l10n_util::GetStringUTF16( | 78 l10n_util::GetStringUTF16( |
| 71 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); | 79 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); |
| 72 | 80 |
| 73 // Add a check item "Ask Google for spelling suggestions" item. (This class | 81 // Add a check item "Ask Google for spelling suggestions" item. (This class |
| 74 // does not handle this item because the SpellingMenuObserver class handles it | 82 // does not handle this item because the SpellingMenuObserver class handles it |
| 75 // on behalf of this class.) | 83 // on behalf of this class.) |
| 76 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | 84 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, |
| 77 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); | 85 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); |
| 78 | 86 |
| 79 // Add a check item "Automatically correct spelling". | 87 // Add a check item "Automatically correct spelling". |
| 80 const base::CommandLine* command_line = | |
| 81 base::CommandLine::ForCurrentProcess(); | |
| 82 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { | 88 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { |
| 83 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, | 89 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, |
| 84 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); | 90 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); |
| 85 } | 91 } |
| 86 | 92 |
| 87 proxy_->AddSubMenu( | 93 proxy_->AddSubMenu( |
| 88 IDC_SPELLCHECK_MENU, | 94 IDC_SPELLCHECK_MENU, |
| 89 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), | 95 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), |
| 90 &submenu_model_); | 96 &submenu_model_); |
| 91 } | 97 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 110 } | 116 } |
| 111 | 117 |
| 112 return false; | 118 return false; |
| 113 } | 119 } |
| 114 | 120 |
| 115 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { | 121 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
| 116 DCHECK(IsCommandIdSupported(command_id)); | 122 DCHECK(IsCommandIdSupported(command_id)); |
| 117 | 123 |
| 118 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 124 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 119 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 125 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 120 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 126 return spellcheck_languages_.count(command_id - |
|
please use gerrit instead
2015/02/23 18:51:47
Please use "spellcheck_languages_.find(command_id
Klemen Forstnerič
2015/02/24 21:57:45
Done.
| |
| 127 IDC_SPELLCHECK_LANGUAGES_FIRST) == 1; | |
| 121 } | 128 } |
| 122 | 129 |
| 123 // Check box for 'Check Spelling while typing'. | 130 // Check box for 'Check Spelling while typing'. |
| 124 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | 131 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
| 125 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 132 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 126 DCHECK(profile); | 133 DCHECK(profile); |
| 127 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | 134 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); |
| 128 } | 135 } |
| 129 | 136 |
| 130 return false; | 137 return false; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 150 | 157 |
| 151 return false; | 158 return false; |
| 152 } | 159 } |
| 153 | 160 |
| 154 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { | 161 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
| 155 DCHECK(IsCommandIdSupported(command_id)); | 162 DCHECK(IsCommandIdSupported(command_id)); |
| 156 | 163 |
| 157 // Check to see if one of the spell check language ids have been clicked. | 164 // Check to see if one of the spell check language ids have been clicked. |
| 158 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 165 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 159 DCHECK(profile); | 166 DCHECK(profile); |
| 160 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 167 |
| 161 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 168 PrefService* prefs = profile->GetPrefs(); |
| 162 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 169 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
| 163 if (profile && language < languages_.size()) { | 170 prefs->SetBoolean(prefs::kEnableContinuousSpellcheck, |
| 164 StringPrefMember dictionary_language; | 171 !prefs->GetBoolean(prefs::kEnableContinuousSpellcheck)); |
| 165 dictionary_language.Init(prefs::kSpellCheckDictionary, | 172 } else if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 166 profile->GetPrefs()); | 173 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 167 dictionary_language.SetValue(languages_[language]); | 174 const std::size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
|
please use gerrit instead
2015/02/23 18:51:47
size_t should be OK. No need to change it to std::
Klemen Forstnerič
2015/02/24 21:57:45
Done.
| |
| 175 | |
| 176 if (language >= languages_.size()) | |
|
please use gerrit instead
2015/02/23 18:51:47
This should never happen. Put a DCHECK here:
DCHE
Klemen Forstnerič
2015/02/24 21:57:45
Done.
| |
| 177 return; | |
| 178 | |
| 179 const base::CommandLine* command_line = | |
| 180 base::CommandLine::ForCurrentProcess(); | |
| 181 if (command_line->HasSwitch(switches::kEnableMultilingualSpellChecker)) { | |
| 182 std::vector<std::string> languages_split; | |
| 183 base::SplitString(prefs->GetString(prefs::kSpellCheckDictionaries), | |
| 184 ',', | |
|
please use gerrit instead
2015/02/23 18:51:47
Put a "static const char kLanguagesSeparator = ','
Klemen Forstnerič
2015/02/24 21:57:45
Done.
| |
| 185 &languages_split); | |
| 186 | |
| 187 auto found_language = std::find(languages_split.begin(), | |
|
please use gerrit instead
2015/02/23 18:51:47
#include <algorithm>
Klemen Forstnerič
2015/02/24 21:57:45
Done.
| |
| 188 languages_split.end(), | |
| 189 languages_[language]); | |
| 190 | |
| 191 if (found_language != languages_split.end()) | |
| 192 languages_split.erase(found_language); | |
| 193 else | |
| 194 languages_split.push_back(languages_[language]); | |
| 195 | |
| 196 prefs->SetString(prefs::kSpellCheckDictionaries, | |
| 197 JoinString(languages_split, ',')); | |
|
please use gerrit instead
2015/02/23 18:51:47
base::JoinString
Klemen Forstnerič
2015/02/24 21:57:45
JoinString is in the global namespace.
| |
| 198 } else { | |
| 199 prefs->SetString(prefs::kSpellCheckDictionary, languages_[language]); | |
|
please use gerrit instead
2015/02/23 18:51:47
Is this equivalent to the original code?
StringPr
Klemen Forstnerič
2015/02/24 21:57:45
Yes, |StringPrefMember::SetValue()| calls |StringP
| |
| 168 } | 200 } |
| 169 return; | |
| 170 } | |
| 171 | |
| 172 switch (command_id) { | |
|
please use gerrit instead
2015/02/23 18:51:47
No need to change this code. Please change it back
Klemen Forstnerič
2015/02/24 21:57:45
Done.
| |
| 173 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
| 174 profile->GetPrefs()->SetBoolean( | |
| 175 prefs::kEnableContinuousSpellcheck, | |
| 176 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | |
| 177 break; | |
| 178 } | 201 } |
| 179 } | 202 } |
| OLD | NEW |