Update V8 to version 4.6.22.
[chromium-blink-merge.git] / chrome / browser / renderer_context_menu / spellchecker_submenu_observer_hunspell.cc
blobd0e40cf09601d891379897df3e707133df866482
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
3 // found in the LICENSE file.
5 #include "chrome/browser/renderer_context_menu/spellchecker_submenu_observer.h"
7 #include <algorithm>
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/prefs/pref_member.h"
12 #include "base/prefs/pref_service.h"
13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
16 #include "chrome/browser/spellchecker/spellcheck_service.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/common/spellcheck_common.h"
20 #include "chrome/common/spellcheck_messages.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/render_widget_host_view.h"
24 #include "extensions/browser/view_type_utils.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/models/simple_menu_model.h"
28 using content::BrowserThread;
30 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver(
31 RenderViewContextMenuProxy* proxy,
32 ui::SimpleMenuModel::Delegate* delegate,
33 int group)
34 : proxy_(proxy),
35 submenu_model_(delegate),
36 language_group_(group),
37 num_selected_languages_(0) {
38 DCHECK(proxy_);
41 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() {
44 void SpellCheckerSubMenuObserver::InitMenu(
45 const content::ContextMenuParams& params) {
46 DCHECK_CURRENTLY_ON(BrowserThread::UI);
48 // Add available spell-checker languages to the sub menu.
49 content::BrowserContext* browser_context = proxy_->GetBrowserContext();
50 DCHECK(browser_context);
51 num_selected_languages_ =
52 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_);
53 DCHECK(languages_.size() <
54 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST);
55 const std::string app_locale = g_browser_process->GetApplicationLocale();
57 if (chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) {
58 for (size_t i = 0; i < languages_.size(); ++i) {
59 submenu_model_.AddCheckItem(
60 IDC_SPELLCHECK_LANGUAGES_FIRST + i,
61 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true));
63 } else {
64 for (size_t i = 0; i < languages_.size(); ++i) {
65 submenu_model_.AddRadioItem(
66 IDC_SPELLCHECK_LANGUAGES_FIRST + i,
67 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true),
68 language_group_);
72 // Add an item that opens the 'fonts and languages options' page.
73 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
74 submenu_model_.AddItemWithStringId(
75 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS,
76 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS);
78 if (num_selected_languages_ > 0) {
79 // Add a 'Check spelling while typing' item in the sub menu.
80 submenu_model_.AddCheckItem(
81 IDC_CHECK_SPELLING_WHILE_TYPING,
82 l10n_util::GetStringUTF16(
83 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING));
86 // Add a check item "Ask Google for spelling suggestions" item. (This class
87 // does not handle this item because the SpellingMenuObserver class handles it
88 // on behalf of this class.)
89 if (!chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) {
90 submenu_model_.AddCheckItem(
91 IDC_CONTENT_CONTEXT_SPELLING_TOGGLE,
92 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE));
95 // Add a check item "Automatically correct spelling".
96 const base::CommandLine* command_line =
97 base::CommandLine::ForCurrentProcess();
98 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) {
99 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE,
100 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT));
103 proxy_->AddSubMenu(
104 IDC_SPELLCHECK_MENU,
105 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU),
106 &submenu_model_);
109 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) {
110 // Allow Spell Check language items on sub menu for text area context menu.
111 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
112 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) {
113 return true;
116 switch (command_id) {
117 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS:
118 // Return false so RenderViewContextMenu can handle this item because it
119 // is hard for this class to handle it.
120 return false;
122 case IDC_CHECK_SPELLING_WHILE_TYPING:
123 case IDC_SPELLPANEL_TOGGLE:
124 case IDC_SPELLCHECK_MENU:
125 return true;
128 return false;
131 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) {
132 DCHECK(IsCommandIdSupported(command_id));
134 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
135 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) {
136 return num_selected_languages_ >
137 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST);
140 // Check box for 'Check Spelling while typing'.
141 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) {
142 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext());
143 DCHECK(profile);
144 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck);
147 return false;
150 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) {
151 DCHECK(IsCommandIdSupported(command_id));
153 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext());
154 DCHECK(profile);
155 const PrefService* pref = profile->GetPrefs();
156 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
157 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) {
158 return pref->GetBoolean(prefs::kEnableContinuousSpellcheck);
161 switch (command_id) {
162 case IDC_CHECK_SPELLING_WHILE_TYPING:
163 case IDC_SPELLPANEL_TOGGLE:
164 case IDC_SPELLCHECK_MENU:
165 return true;
168 return false;
171 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) {
172 DCHECK(IsCommandIdSupported(command_id));
174 // Check to see if one of the spell check language ids have been clicked.
175 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext());
176 DCHECK(profile);
178 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
179 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) {
180 size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST;
181 DCHECK_LT(language, languages_.size());
182 StringListPrefMember dictionaries_pref;
183 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs());
185 if (chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) {
186 std::vector<std::string> dictionary_languages =
187 dictionaries_pref.GetValue();
189 auto found_language =
190 std::find(dictionary_languages.begin(), dictionary_languages.end(),
191 languages_[language]);
193 if (found_language != dictionary_languages.end())
194 dictionary_languages.erase(found_language);
195 else
196 dictionary_languages.push_back(languages_[language]);
198 dictionaries_pref.SetValue(dictionary_languages);
199 } else {
200 dictionaries_pref.SetValue(
201 std::vector<std::string>(1, languages_[language]));
203 return;
206 switch (command_id) {
207 case IDC_CHECK_SPELLING_WHILE_TYPING:
208 profile->GetPrefs()->SetBoolean(
209 prefs::kEnableContinuousSpellcheck,
210 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
211 break;