| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/tab_contents/spelling_menu_observer.h" | 5 #include "chrome/browser/tab_contents/spelling_menu_observer.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/string_escape.h" | 11 #include "base/json/string_escape.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
| 16 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 18 #include "chrome/browser/spellchecker/spellcheck_host.h" | 19 #include "chrome/browser/spellchecker/spellcheck_host.h" |
| 19 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 20 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
| 20 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" | 21 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" |
| 21 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 22 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 22 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 23 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 24 #include "content/browser/renderer_host/render_view_host.h" | 25 #include "content/browser/renderer_host/render_view_host.h" |
| 25 #include "content/public/common/url_fetcher.h" | 26 #include "content/public/common/url_fetcher.h" |
| 26 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
| 27 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 pref ? pref->GetString(prefs::kSpellCheckDictionary) : "en-US"; | 107 pref ? pref->GetString(prefs::kSpellCheckDictionary) : "en-US"; |
| 107 Invoke(text, language, profile->GetRequestContext()); | 108 Invoke(text, language, profile->GetRequestContext()); |
| 108 } | 109 } |
| 109 } | 110 } |
| 110 | 111 |
| 111 if (!params.dictionary_suggestions.empty()) { | 112 if (!params.dictionary_suggestions.empty()) { |
| 112 proxy_->AddSeparator(); | 113 proxy_->AddSeparator(); |
| 113 | 114 |
| 114 // |spellcheck_host| can be null when the suggested word is | 115 // |spellcheck_host| can be null when the suggested word is |
| 115 // provided by Web SpellCheck API. | 116 // provided by Web SpellCheck API. |
| 116 SpellCheckHost* spellcheck_host = profile->GetSpellCheckHost(); | 117 SpellCheckHost* spellcheck_host = |
| 118 SpellCheckFactory::GetHostForProfile(profile); |
| 117 if (spellcheck_host && spellcheck_host->GetMetrics()) | 119 if (spellcheck_host && spellcheck_host->GetMetrics()) |
| 118 spellcheck_host->GetMetrics()->RecordSuggestionStats(1); | 120 spellcheck_host->GetMetrics()->RecordSuggestionStats(1); |
| 119 } | 121 } |
| 120 | 122 |
| 121 // If word is misspelled, give option for "Add to dictionary" and "Ask Google | 123 // If word is misspelled, give option for "Add to dictionary" and "Ask Google |
| 122 // for suggestions". (The SpellCheckerSubMenuObserver class handles the "Ask | 124 // for suggestions". (The SpellCheckerSubMenuObserver class handles the "Ask |
| 123 // Goole for suggestions" item so this class does not have to handle it.) | 125 // Goole for suggestions" item so this class does not have to handle it.) |
| 124 if (!params.misspelled_word.empty()) { | 126 if (!params.misspelled_word.empty()) { |
| 125 if (params.dictionary_suggestions.empty()) { | 127 if (params.dictionary_suggestions.empty()) { |
| 126 proxy_->AddMenuItem(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, | 128 proxy_->AddMenuItem(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 DCHECK(IsCommandIdSupported(command_id)); | 196 DCHECK(IsCommandIdSupported(command_id)); |
| 195 | 197 |
| 196 if (command_id >= IDC_SPELLCHECK_SUGGESTION_0 && | 198 if (command_id >= IDC_SPELLCHECK_SUGGESTION_0 && |
| 197 command_id <= IDC_SPELLCHECK_SUGGESTION_4) { | 199 command_id <= IDC_SPELLCHECK_SUGGESTION_4) { |
| 198 proxy_->GetRenderViewHost()->Replace( | 200 proxy_->GetRenderViewHost()->Replace( |
| 199 suggestions_[command_id - IDC_SPELLCHECK_SUGGESTION_0]); | 201 suggestions_[command_id - IDC_SPELLCHECK_SUGGESTION_0]); |
| 200 // GetSpellCheckHost() can return null when the suggested word is | 202 // GetSpellCheckHost() can return null when the suggested word is |
| 201 // provided by Web SpellCheck API. | 203 // provided by Web SpellCheck API. |
| 202 Profile* profile = proxy_->GetProfile(); | 204 Profile* profile = proxy_->GetProfile(); |
| 203 if (profile) { | 205 if (profile) { |
| 204 SpellCheckHost* spellcheck_host = profile->GetSpellCheckHost(); | 206 SpellCheckHost* spellcheck_host = |
| 207 SpellCheckFactory::GetHostForProfile(profile); |
| 205 if (spellcheck_host && spellcheck_host->GetMetrics()) | 208 if (spellcheck_host && spellcheck_host->GetMetrics()) |
| 206 spellcheck_host->GetMetrics()->RecordReplacedWordStats(1); | 209 spellcheck_host->GetMetrics()->RecordReplacedWordStats(1); |
| 207 } | 210 } |
| 208 return; | 211 return; |
| 209 } | 212 } |
| 210 | 213 |
| 211 // When we choose the suggestion sent from the Spelling service, we replace | 214 // When we choose the suggestion sent from the Spelling service, we replace |
| 212 // the misspelled word with the suggestion and add it to our custom-word | 215 // the misspelled word with the suggestion and add it to our custom-word |
| 213 // dictionary so this word is not marked as misspelled any longer. | 216 // dictionary so this word is not marked as misspelled any longer. |
| 214 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION) { | 217 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION) { |
| 215 proxy_->GetRenderViewHost()->Replace(result_); | 218 proxy_->GetRenderViewHost()->Replace(result_); |
| 216 misspelled_word_ = result_; | 219 misspelled_word_ = result_; |
| 217 } | 220 } |
| 218 | 221 |
| 219 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION || | 222 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION || |
| 220 command_id == IDC_SPELLCHECK_ADD_TO_DICTIONARY) { | 223 command_id == IDC_SPELLCHECK_ADD_TO_DICTIONARY) { |
| 221 // GetSpellCheckHost() can return null when the suggested word is | 224 // GetHostForProfile() can return null when the suggested word is |
| 222 // provided by Web SpellCheck API. | 225 // provided by Web SpellCheck API. |
| 223 Profile* profile = proxy_->GetProfile(); | 226 Profile* profile = proxy_->GetProfile(); |
| 224 if (profile && profile->GetSpellCheckHost()) | 227 if (profile) { |
| 225 profile->GetSpellCheckHost()->AddWord(UTF16ToUTF8(misspelled_word_)); | 228 SpellCheckHost* host = SpellCheckFactory::GetHostForProfile(profile); |
| 229 if (host) |
| 230 host->AddWord(UTF16ToUTF8(misspelled_word_)); |
| 231 } |
| 226 #if defined(OS_MACOSX) | 232 #if defined(OS_MACOSX) |
| 227 spellcheck_mac::AddWord(misspelled_word_); | 233 spellcheck_mac::AddWord(misspelled_word_); |
| 228 #endif | 234 #endif |
| 229 } | 235 } |
| 230 } | 236 } |
| 231 | 237 |
| 232 bool SpellingMenuObserver::Invoke(const string16& text, | 238 bool SpellingMenuObserver::Invoke(const string16& text, |
| 233 const std::string& locale, | 239 const std::string& locale, |
| 234 net::URLRequestContextGetter* context) { | 240 net::URLRequestContextGetter* context) { |
| 235 // Create the parameters needed by Spelling API. Spelling API needs three | 241 // Create the parameters needed by Spelling API. Spelling API needs three |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 loading_frame_ = (loading_frame_ + 1) & 3; | 411 loading_frame_ = (loading_frame_ + 1) & 3; |
| 406 string16 loading_message = loading_message_; | 412 string16 loading_message = loading_message_; |
| 407 for (int i = 0; i < loading_frame_; ++i) | 413 for (int i = 0; i < loading_frame_; ++i) |
| 408 loading_message.push_back('.'); | 414 loading_message.push_back('.'); |
| 409 | 415 |
| 410 // Update the menu item with the text. We disable this item to prevent users | 416 // Update the menu item with the text. We disable this item to prevent users |
| 411 // from selecting it. | 417 // from selecting it. |
| 412 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, false, false, | 418 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, false, false, |
| 413 loading_message); | 419 loading_message); |
| 414 } | 420 } |
| OLD | NEW |