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/spelling_menu_observer.h"
8 #include "base/command_line.h"
9 #include "base/i18n/case_conversion.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
15 #include "chrome/browser/renderer_context_menu/spelling_bubble_model.h"
16 #include "chrome/browser/spellchecker/spellcheck_factory.h"
17 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
18 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h"
19 #include "chrome/browser/spellchecker/spellcheck_service.h"
20 #include "chrome/browser/spellchecker/spelling_service_client.h"
21 #include "chrome/browser/ui/confirm_bubble.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/common/spellcheck_result.h"
25 #include "chrome/grit/generated_resources.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/browser/render_widget_host_view.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/common/context_menu_params.h"
30 #include "extensions/browser/view_type_utils.h"
31 #include "ui/base/l10n/l10n_util.h"
32 #include "ui/gfx/geometry/rect.h"
34 using content::BrowserThread
;
36 SpellingMenuObserver::SpellingMenuObserver(RenderViewContextMenuProxy
* proxy
)
41 client_(new SpellingServiceClient
) {
42 if (proxy_
&& proxy_
->GetBrowserContext()) {
43 Profile
* profile
= Profile::FromBrowserContext(proxy_
->GetBrowserContext());
44 integrate_spelling_service_
.Init(prefs::kSpellCheckUseSpellingService
,
46 autocorrect_spelling_
.Init(prefs::kEnableAutoSpellCorrect
,
51 SpellingMenuObserver::~SpellingMenuObserver() {
54 void SpellingMenuObserver::InitMenu(const content::ContextMenuParams
& params
) {
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
56 DCHECK(!params
.misspelled_word
.empty() ||
57 params
.dictionary_suggestions
.empty());
59 // Exit if we are not in an editable element because we add a menu item only
60 // for editable elements.
61 content::BrowserContext
* browser_context
= proxy_
->GetBrowserContext();
62 if (!params
.is_editable
|| !browser_context
)
65 // Exit if there is no misspelled word.
66 if (params
.misspelled_word
.empty())
69 suggestions_
= params
.dictionary_suggestions
;
70 misspelled_word_
= params
.misspelled_word
;
71 misspelling_hash_
= params
.misspelling_hash
;
73 bool use_suggestions
= SpellingServiceClient::IsAvailable(
74 browser_context
, SpellingServiceClient::SUGGEST
);
76 if (!suggestions_
.empty() || use_suggestions
)
77 proxy_
->AddSeparator();
79 // Append Dictionary spell check suggestions.
80 for (size_t i
= 0; i
< params
.dictionary_suggestions
.size() &&
81 IDC_SPELLCHECK_SUGGESTION_0
+ i
<= IDC_SPELLCHECK_SUGGESTION_LAST
;
83 proxy_
->AddMenuItem(IDC_SPELLCHECK_SUGGESTION_0
+ static_cast<int>(i
),
84 params
.dictionary_suggestions
[i
]);
87 // The service types |SpellingServiceClient::SPELLCHECK| and
88 // |SpellingServiceClient::SUGGEST| are mutually exclusive. Only one is
89 // available at at time.
91 // When |SpellingServiceClient::SPELLCHECK| is available, the contextual
92 // suggestions from |SpellingServiceClient| are already stored in
93 // |params.dictionary_suggestions|. |SpellingMenuObserver| places these
94 // suggestions in the slots |IDC_SPELLCHECK_SUGGESTION_[0-LAST]|. If
95 // |SpellingMenuObserver| queried |SpellingServiceClient| again, then quality
96 // of suggestions would be reduced by lack of context around the misspelled
99 // When |SpellingServiceClient::SUGGEST| is available,
100 // |params.dictionary_suggestions| contains suggestions only from Hunspell
101 // dictionary. |SpellingMenuObserver| queries |SpellingServiceClient| with the
102 // misspelled word without the surrounding context. Spellcheck suggestions
103 // from |SpellingServiceClient::SUGGEST| are not available until
104 // |SpellingServiceClient| responds to the query. While |SpellingMenuObserver|
105 // waits for |SpellingServiceClient|, it shows a placeholder text "Loading
106 // suggestion..." in the |IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION| slot. After
107 // |SpellingServiceClient| responds to the query, |SpellingMenuObserver|
108 // replaces the placeholder text with either the spelling suggestion or the
109 // message "No more suggestions from Google." The "No more suggestions"
110 // message is there when |SpellingServiceClient| returned the same suggestion
112 if (use_suggestions
) {
113 // Append a placeholder item for the suggestion from the Spelling service
114 // and send a request to the service if we can retrieve suggestions from it.
115 // Also, see if we can use the spelling service to get an ideal suggestion.
116 // Otherwise, we'll fall back to the set of suggestions. Initialize
117 // variables used in OnTextCheckComplete(). We copy the input text to the
118 // result text so we can replace its misspelled regions with suggestions.
120 result_
= params
.misspelled_word
;
122 // Add a placeholder item. This item will be updated when we receive a
123 // response from the Spelling service. (We do not have to disable this
124 // item now since Chrome will call IsCommandIdEnabled() and disable it.)
126 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_CHECKING
);
127 proxy_
->AddMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION
,
129 // Invoke a JSON-RPC call to the Spelling service in the background so we
130 // can update the placeholder item when we receive its response. It also
131 // starts the animation timer so we can show animation until we receive
133 bool result
= client_
->RequestTextCheck(
135 SpellingServiceClient::SUGGEST
,
136 params
.misspelled_word
,
137 base::Bind(&SpellingMenuObserver::OnTextCheckComplete
,
138 base::Unretained(this),
139 SpellingServiceClient::SUGGEST
));
142 animation_timer_
.Start(FROM_HERE
, base::TimeDelta::FromSeconds(1),
143 this, &SpellingMenuObserver::OnAnimationTimerExpired
);
147 if (params
.dictionary_suggestions
.empty()) {
149 IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS
,
150 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS
));
151 bool use_spelling_service
= SpellingServiceClient::IsAvailable(
152 browser_context
, SpellingServiceClient::SPELLCHECK
);
153 if (use_suggestions
|| use_spelling_service
)
154 proxy_
->AddSeparator();
156 proxy_
->AddSeparator();
158 // |spellcheck_service| can be null when the suggested word is
159 // provided by Web SpellCheck API.
160 SpellcheckService
* spellcheck_service
=
161 SpellcheckServiceFactory::GetForContext(browser_context
);
162 if (spellcheck_service
&& spellcheck_service
->GetMetrics())
163 spellcheck_service
->GetMetrics()->RecordSuggestionStats(1);
166 // If word is misspelled, give option for "Add to dictionary" and a check item
167 // "Ask Google for suggestions".
168 proxy_
->AddMenuItem(IDC_SPELLCHECK_ADD_TO_DICTIONARY
,
169 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY
));
171 proxy_
->AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
,
172 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE
));
174 const base::CommandLine
* command_line
=
175 base::CommandLine::ForCurrentProcess();
176 if (command_line
->HasSwitch(switches::kEnableSpellingAutoCorrect
)) {
177 proxy_
->AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE
,
178 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT
));
181 proxy_
->AddSeparator();
184 bool SpellingMenuObserver::IsCommandIdSupported(int command_id
) {
185 if (command_id
>= IDC_SPELLCHECK_SUGGESTION_0
&&
186 command_id
<= IDC_SPELLCHECK_SUGGESTION_LAST
)
189 switch (command_id
) {
190 case IDC_SPELLCHECK_ADD_TO_DICTIONARY
:
191 case IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS
:
192 case IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION
:
193 case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
:
194 case IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE
:
202 bool SpellingMenuObserver::IsCommandIdChecked(int command_id
) {
203 DCHECK(IsCommandIdSupported(command_id
));
204 Profile
* profile
= Profile::FromBrowserContext(proxy_
->GetBrowserContext());
206 if (command_id
== IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
)
207 return integrate_spelling_service_
.GetValue() && !profile
->IsOffTheRecord();
208 if (command_id
== IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE
)
209 return autocorrect_spelling_
.GetValue() && !profile
->IsOffTheRecord();
213 bool SpellingMenuObserver::IsCommandIdEnabled(int command_id
) {
214 DCHECK(IsCommandIdSupported(command_id
));
216 if (command_id
>= IDC_SPELLCHECK_SUGGESTION_0
&&
217 command_id
<= IDC_SPELLCHECK_SUGGESTION_LAST
)
220 Profile
* profile
= Profile::FromBrowserContext(proxy_
->GetBrowserContext());
221 switch (command_id
) {
222 case IDC_SPELLCHECK_ADD_TO_DICTIONARY
:
223 return !misspelled_word_
.empty();
225 case IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS
:
228 case IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION
:
231 case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
:
232 return integrate_spelling_service_
.IsUserModifiable() &&
233 !profile
->IsOffTheRecord();
235 case IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE
:
236 return integrate_spelling_service_
.IsUserModifiable() &&
237 !profile
->IsOffTheRecord();
244 void SpellingMenuObserver::ExecuteCommand(int command_id
) {
245 DCHECK(IsCommandIdSupported(command_id
));
247 if (command_id
>= IDC_SPELLCHECK_SUGGESTION_0
&&
248 command_id
<= IDC_SPELLCHECK_SUGGESTION_LAST
) {
249 int suggestion_index
= command_id
- IDC_SPELLCHECK_SUGGESTION_0
;
250 proxy_
->GetWebContents()->ReplaceMisspelling(
251 suggestions_
[suggestion_index
]);
252 // GetSpellCheckHost() can return null when the suggested word is provided
253 // by Web SpellCheck API.
254 content::BrowserContext
* browser_context
= proxy_
->GetBrowserContext();
255 if (browser_context
) {
256 SpellcheckService
* spellcheck
=
257 SpellcheckServiceFactory::GetForContext(browser_context
);
259 if (spellcheck
->GetMetrics())
260 spellcheck
->GetMetrics()->RecordReplacedWordStats(1);
261 spellcheck
->GetFeedbackSender()->SelectedSuggestion(
262 misspelling_hash_
, suggestion_index
);
268 // When we choose the suggestion sent from the Spelling service, we replace
269 // the misspelled word with the suggestion and add it to our custom-word
270 // dictionary so this word is not marked as misspelled any longer.
271 if (command_id
== IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION
) {
272 proxy_
->GetWebContents()->ReplaceMisspelling(result_
);
273 misspelled_word_
= result_
;
276 if (command_id
== IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION
||
277 command_id
== IDC_SPELLCHECK_ADD_TO_DICTIONARY
) {
278 // GetHostForProfile() can return null when the suggested word is provided
279 // by Web SpellCheck API.
280 content::BrowserContext
* browser_context
= proxy_
->GetBrowserContext();
281 if (browser_context
) {
282 SpellcheckService
* spellcheck
=
283 SpellcheckServiceFactory::GetForContext(browser_context
);
285 spellcheck
->GetCustomDictionary()->AddWord(base::UTF16ToUTF8(
287 spellcheck
->GetFeedbackSender()->AddedToDictionary(misspelling_hash_
);
290 #if defined(OS_MACOSX)
291 spellcheck_mac::AddWord(misspelled_word_
);
295 Profile
* profile
= Profile::FromBrowserContext(proxy_
->GetBrowserContext());
297 // The spelling service can be toggled by the user only if it is not managed.
298 if (command_id
== IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
&&
299 integrate_spelling_service_
.IsUserModifiable()) {
300 // When a user enables the "Ask Google for spelling suggestions" item, we
301 // show a bubble to confirm it. On the other hand, when a user disables this
302 // item, we directly update/ the profile and stop integrating the spelling
303 // service immediately.
304 if (!integrate_spelling_service_
.GetValue()) {
305 content::RenderViewHost
* rvh
= proxy_
->GetRenderViewHost();
306 gfx::Rect rect
= rvh
->GetView()->GetViewBounds();
307 scoped_ptr
<SpellingBubbleModel
> model(
308 new SpellingBubbleModel(profile
, proxy_
->GetWebContents(), false));
309 chrome::ShowConfirmBubble(
310 proxy_
->GetWebContents()->GetTopLevelNativeWindow(),
311 rvh
->GetView()->GetNativeView(),
312 gfx::Point(rect
.CenterPoint().x(), rect
.y()),
316 profile
->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService
,
318 profile
->GetPrefs()->SetBoolean(prefs::kEnableAutoSpellCorrect
,
323 // Autocorrect requires use of the spelling service and the spelling service
324 // can be toggled by the user only if it is not managed.
325 if (command_id
== IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE
&&
326 integrate_spelling_service_
.IsUserModifiable()) {
327 // When the user enables autocorrect, we'll need to make sure that we can
328 // ask Google for suggestions since that service is required. So we show
329 // the bubble and just make sure to enable autocorrect as well.
330 if (!integrate_spelling_service_
.GetValue()) {
331 content::RenderViewHost
* rvh
= proxy_
->GetRenderViewHost();
332 gfx::Rect rect
= rvh
->GetView()->GetViewBounds();
333 scoped_ptr
<SpellingBubbleModel
> model(
334 new SpellingBubbleModel(profile
, proxy_
->GetWebContents(), true));
335 chrome::ShowConfirmBubble(
336 proxy_
->GetWebContents()->GetTopLevelNativeWindow(),
337 rvh
->GetView()->GetNativeView(),
338 gfx::Point(rect
.CenterPoint().x(), rect
.y()),
342 bool current_value
= autocorrect_spelling_
.GetValue();
343 profile
->GetPrefs()->SetBoolean(prefs::kEnableAutoSpellCorrect
,
350 void SpellingMenuObserver::OnMenuCancel() {
351 content::BrowserContext
* browser_context
= proxy_
->GetBrowserContext();
352 if (!browser_context
)
354 SpellcheckService
* spellcheck
=
355 SpellcheckServiceFactory::GetForContext(browser_context
);
358 spellcheck
->GetFeedbackSender()->IgnoredSuggestions(misspelling_hash_
);
361 void SpellingMenuObserver::OnTextCheckComplete(
362 SpellingServiceClient::ServiceType type
,
364 const base::string16
& text
,
365 const std::vector
<SpellCheckResult
>& results
) {
366 animation_timer_
.Stop();
368 // Scan the text-check results and replace the misspelled regions with
369 // suggested words. If the replaced text is included in the suggestion list
370 // provided by the local spellchecker, we show a "No suggestions from Google"
372 succeeded_
= success
;
373 if (results
.empty()) {
376 typedef std::vector
<SpellCheckResult
> SpellCheckResults
;
377 for (SpellCheckResults::const_iterator it
= results
.begin();
378 it
!= results
.end(); ++it
) {
379 result_
.replace(it
->location
, it
->length
, it
->replacement
);
381 base::string16 result
= base::i18n::ToLower(result_
);
382 for (std::vector
<base::string16
>::const_iterator it
= suggestions_
.begin();
383 it
!= suggestions_
.end(); ++it
) {
384 if (result
== base::i18n::ToLower(*it
)) {
390 if (type
!= SpellingServiceClient::SPELLCHECK
) {
392 result_
= l10n_util::GetStringUTF16(
393 IDS_CONTENT_CONTEXT_SPELLING_NO_SUGGESTIONS_FROM_GOOGLE
);
396 // Update the menu item with the result text. We disable this item and hide
397 // it when the spelling service does not provide valid suggestions.
398 proxy_
->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION
, succeeded_
,
403 void SpellingMenuObserver::OnAnimationTimerExpired() {
404 // Append '.' characters to the end of "Checking".
405 loading_frame_
= (loading_frame_
+ 1) & 3;
406 base::string16 loading_message
=
407 loading_message_
+ base::string16(loading_frame_
,'.');
409 // Update the menu item with the text. We disable this item to prevent users
410 // from selecting it.
411 proxy_
->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION
, false, false,