Chromium Code Reviews| Index: chrome/browser/tab_contents/spelling_menu_observer.cc |
| =================================================================== |
| --- chrome/browser/tab_contents/spelling_menu_observer.cc (revision 107913) |
| +++ chrome/browser/tab_contents/spelling_menu_observer.cc (working copy) |
| @@ -14,6 +14,8 @@ |
| #include "chrome/app/chrome_command_ids.h" |
| #include "chrome/browser/prefs/pref_service.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/spellchecker/spellcheck_host.h" |
| +#include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
| #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| #include "chrome/common/pref_names.h" |
| #include "content/browser/renderer_host/render_view_host.h" |
| @@ -55,45 +57,89 @@ |
| if (!profile || !profile->GetRequestContext()) |
| return; |
| - // Retrieve the misspelled word to be sent to the Spelling service. |
| - string16 text = params.misspelled_word; |
| - if (text.empty()) |
| - return; |
| + PrefService* pref = profile->GetPrefs(); |
| + if (pref->GetBoolean(prefs::kEnableSpellCheck) && |
| + pref->GetBoolean(prefs::kSpellCheckUseSpellingService)) { |
| + // Retrieve the misspelled word to be sent to the Spelling service. |
| + string16 text = params.misspelled_word; |
| + if (text.empty()) |
| + return; |
| - // Initialize variables used in OnURLFetchComplete(). We copy the input text |
| - // to the result text so we can replace its misspelled regions with |
| - // suggestions. |
| - loading_frame_ = 0; |
| - succeeded_ = false; |
| - result_ = text; |
| + // Initialize variables used in OnURLFetchComplete(). We copy the input text |
| + // to the result text so we can replace its misspelled regions with |
| + // suggestions. |
| + loading_frame_ = 0; |
| + succeeded_ = false; |
| + result_ = text; |
| - // Add a placeholder item. This item will be updated when we receive a |
| - // response from the Spelling service. (We do not have to disable this item |
| - // now since Chrome will call IsCommandIdEnabled() and disable it.) |
| - loading_message_ = |
| - l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_CHECKING); |
| - proxy_->AddMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, |
| - loading_message_); |
| + // Add a placeholder item. This item will be updated when we receive a |
| + // response from the Spelling service. (We do not have to disable this item |
| + // now since Chrome will call IsCommandIdEnabled() and disable it.) |
| + loading_message_ = |
| + l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_CHECKING); |
| + proxy_->AddMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, |
| + loading_message_); |
| - // Invoke a JSON-RPC call to the Spelling service in the background so we can |
| - // update the placeholder item when we receive its response. It also starts |
| - // the animation timer so we can show animation until we receive it. |
| - const PrefService* pref = profile->GetPrefs(); |
| - std::string language = |
| - pref ? pref->GetString(prefs::kSpellCheckDictionary) : "en-US"; |
| - Invoke(text, language, profile->GetRequestContext()); |
| + // Invoke a JSON-RPC call to the Spelling service in the background so we |
| + // can update the placeholder item when we receive its response. It also |
| + // starts the animation timer so we can show animation until we receive it. |
| + const PrefService* pref = profile->GetPrefs(); |
| + std::string language = |
| + pref ? pref->GetString(prefs::kSpellCheckDictionary) : "en-US"; |
| + Invoke(text, language, profile->GetRequestContext()); |
| + } |
| + |
| + // Append Dictionary spell check suggestions. |
| + suggestions_ = params.dictionary_suggestions; |
| + for (size_t i = 0; i < params.dictionary_suggestions.size() && |
| + IDC_SPELLCHECK_SUGGESTION_0 + i <= IDC_SPELLCHECK_SUGGESTION_LAST; |
| + ++i) { |
| + proxy_->AddMenuItem(IDC_SPELLCHECK_SUGGESTION_0 + static_cast<int>(i), |
| + params.dictionary_suggestions[i]); |
| + } |
| } |
| bool SpellingMenuObserver::IsCommandIdSupported(int command_id) { |
| - return command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION; |
| + if (command_id >= IDC_SPELLCHECK_SUGGESTION_0 && |
| + command_id <= IDC_SPELLCHECK_SUGGESTION_4) |
| + return true; |
| + |
| + if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION) |
|
Miranda Callahan
2011/10/31 13:33:46
Maybe change the last three lines to:
return (com
|
| + return true; |
| + return false; |
| } |
| bool SpellingMenuObserver::IsCommandIdEnabled(int command_id) { |
| - return command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION && succeeded_; |
| + DCHECK(IsCommandIdSupported(command_id)); |
| + |
| + if (command_id >= IDC_SPELLCHECK_SUGGESTION_0 && |
| + command_id <= IDC_SPELLCHECK_SUGGESTION_4) |
| + return true; |
| + |
| + if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION) |
| + return succeeded_; |
| + return false; |
| } |
| void SpellingMenuObserver::ExecuteCommand(int command_id) { |
| - if (IsCommandIdEnabled(command_id)) |
| + DCHECK(IsCommandIdSupported(command_id)); |
| + |
| + if (command_id >= IDC_SPELLCHECK_SUGGESTION_0 && |
| + command_id <= IDC_SPELLCHECK_SUGGESTION_4) { |
| + proxy_->GetRenderViewHost()->Replace( |
| + suggestions_[command_id - IDC_SPELLCHECK_SUGGESTION_0]); |
| + // GetSpellCheckHost() can return null when the suggested word is |
| + // provided by Web SpellCheck API. |
| + Profile* profile = proxy_->GetProfile(); |
| + if (profile) { |
| + SpellCheckHost* spellcheck_host = profile->GetSpellCheckHost(); |
| + if (spellcheck_host && spellcheck_host->GetMetrics()) |
| + spellcheck_host->GetMetrics()->RecordReplacedWordStats(1); |
| + } |
| + return; |
| + } |
| + |
| + if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION) |
| proxy_->GetRenderViewHost()->Replace(result_); |
| } |
| @@ -163,16 +209,11 @@ |
| std::string data; |
| source->GetResponseAsString(&data); |
| succeeded_ = ParseResponse(source->GetResponseCode(), data); |
| - if (!succeeded_) |
| - result_ = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_CORRECT); |
| - // Update the menu item with the result text. We enable this item only when |
| - // the request text has misspelled words. (We disable this item not only when |
| - // we receive a server error but also when the input text consists only of |
| - // well-spelled words. For either case, we do not need to replace the input |
| - // text.) |
| + // Update the menu item with the result text. We disable this item and hide it |
| + // when the spelling service does not provide valid suggestions. |
| proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, succeeded_, |
| - false, result_); |
| + !succeeded_, result_); |
| } |
| bool SpellingMenuObserver::ParseResponse(int response, |
| @@ -255,6 +296,14 @@ |
| result_.replace(start, length, text); |
| } |
| + // If the above result text is included in the suggestion list provided by the |
| + // local spellchecker, we return false to hide this item. |
| + for (std::vector<string16>::const_iterator it = suggestions_.begin(); |
| + it != suggestions_.end(); ++it) { |
| + if (result_ == *it) |
| + return false; |
| + } |
| + |
| return true; |
| } |