| 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/spelling_menu_observer.h" | 5 #include "chrome/browser/renderer_context_menu/spelling_menu_observer.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/i18n/case_conversion.h" | 11 #include "base/i18n/case_conversion.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/app/chrome_command_ids.h" | 13 #include "chrome/app/chrome_command_ids.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 15 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
| 16 #include "chrome/browser/renderer_context_menu/spelling_bubble_model.h" | 16 #include "chrome/browser/renderer_context_menu/spelling_bubble_model.h" |
| 17 #include "chrome/browser/spellchecker/feedback_sender.h" | |
| 18 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 17 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 19 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 18 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
| 20 #include "chrome/browser/spellchecker/spellcheck_platform.h" | 19 #include "chrome/browser/spellchecker/spellcheck_platform.h" |
| 21 #include "chrome/browser/spellchecker/spellcheck_service.h" | 20 #include "chrome/browser/spellchecker/spellcheck_service.h" |
| 22 #include "chrome/browser/spellchecker/spelling_service_client.h" | 21 #include "chrome/browser/spellchecker/spelling_service_client.h" |
| 23 #include "chrome/browser/ui/confirm_bubble.h" | 22 #include "chrome/browser/ui/confirm_bubble.h" |
| 24 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 25 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/grit/generated_resources.h" | 25 #include "chrome/grit/generated_resources.h" |
| 27 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
| 28 #include "components/spellcheck/common/spellcheck_common.h" | 27 #include "components/spellcheck/common/spellcheck_common.h" |
| 29 #include "components/spellcheck/common/spellcheck_result.h" | 28 #include "components/spellcheck/common/spellcheck_result.h" |
| 30 #include "content/public/browser/render_view_host.h" | 29 #include "content/public/browser/render_view_host.h" |
| 31 #include "content/public/browser/render_widget_host.h" | 30 #include "content/public/browser/render_widget_host.h" |
| 32 #include "content/public/browser/render_widget_host_view.h" | 31 #include "content/public/browser/render_widget_host_view.h" |
| 33 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 34 #include "content/public/common/context_menu_params.h" | 33 #include "content/public/common/context_menu_params.h" |
| 35 #include "extensions/browser/view_type_utils.h" | 34 #include "extensions/browser/view_type_utils.h" |
| 36 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
| 37 #include "ui/gfx/geometry/rect.h" | 36 #include "ui/gfx/geometry/rect.h" |
| 38 | 37 |
| 39 using content::BrowserThread; | 38 using content::BrowserThread; |
| 40 | 39 |
| 41 const int kMaxSpellingSuggestions = 3; | 40 const int kMaxSpellingSuggestions = 3; |
| 42 | 41 |
| 43 SpellingMenuObserver::SpellingMenuObserver(RenderViewContextMenuProxy* proxy) | 42 SpellingMenuObserver::SpellingMenuObserver(RenderViewContextMenuProxy* proxy) |
| 44 : proxy_(proxy), | 43 : proxy_(proxy), |
| 45 loading_frame_(0), | 44 loading_frame_(0), |
| 46 succeeded_(false), | 45 succeeded_(false), |
| 47 misspelling_hash_(0), | |
| 48 client_(new SpellingServiceClient) { | 46 client_(new SpellingServiceClient) { |
| 49 if (proxy_ && proxy_->GetBrowserContext()) { | 47 if (proxy_ && proxy_->GetBrowserContext()) { |
| 50 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 48 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 51 integrate_spelling_service_.Init(prefs::kSpellCheckUseSpellingService, | 49 integrate_spelling_service_.Init(prefs::kSpellCheckUseSpellingService, |
| 52 profile->GetPrefs()); | 50 profile->GetPrefs()); |
| 53 } | 51 } |
| 54 } | 52 } |
| 55 | 53 |
| 56 SpellingMenuObserver::~SpellingMenuObserver() { | 54 SpellingMenuObserver::~SpellingMenuObserver() { |
| 57 } | 55 } |
| 58 | 56 |
| 59 void SpellingMenuObserver::InitMenu(const content::ContextMenuParams& params) { | 57 void SpellingMenuObserver::InitMenu(const content::ContextMenuParams& params) { |
| 60 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 61 DCHECK(!params.misspelled_word.empty() || | 59 DCHECK(!params.misspelled_word.empty() || |
| 62 params.dictionary_suggestions.empty()); | 60 params.dictionary_suggestions.empty()); |
| 63 | 61 |
| 64 // Exit if we are not in an editable element because we add a menu item only | 62 // Exit if we are not in an editable element because we add a menu item only |
| 65 // for editable elements. | 63 // for editable elements. |
| 66 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 64 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| 67 if (!params.is_editable || !browser_context) | 65 if (!params.is_editable || !browser_context) |
| 68 return; | 66 return; |
| 69 | 67 |
| 70 // Exit if there is no misspelled word. | 68 // Exit if there is no misspelled word. |
| 71 if (params.misspelled_word.empty()) | 69 if (params.misspelled_word.empty()) |
| 72 return; | 70 return; |
| 73 | 71 |
| 74 suggestions_ = params.dictionary_suggestions; | 72 suggestions_ = params.dictionary_suggestions; |
| 75 misspelled_word_ = params.misspelled_word; | 73 misspelled_word_ = params.misspelled_word; |
| 76 misspelling_hash_ = params.misspelling_hash; | |
| 77 | 74 |
| 78 bool use_suggestions = SpellingServiceClient::IsAvailable( | 75 bool use_suggestions = SpellingServiceClient::IsAvailable( |
| 79 browser_context, SpellingServiceClient::SUGGEST); | 76 browser_context, SpellingServiceClient::SUGGEST); |
| 80 | 77 |
| 81 if (!suggestions_.empty() || use_suggestions) | 78 if (!suggestions_.empty() || use_suggestions) |
| 82 proxy_->AddSeparator(); | 79 proxy_->AddSeparator(); |
| 83 | 80 |
| 84 // Append Dictionary spell check suggestions. | 81 // Append Dictionary spell check suggestions. |
| 85 int length = std::min(kMaxSpellingSuggestions, | 82 int length = std::min(kMaxSpellingSuggestions, |
| 86 static_cast<int>(params.dictionary_suggestions.size())); | 83 static_cast<int>(params.dictionary_suggestions.size())); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 suggestions_[suggestion_index]); | 230 suggestions_[suggestion_index]); |
| 234 // GetSpellCheckHost() can return null when the suggested word is provided | 231 // GetSpellCheckHost() can return null when the suggested word is provided |
| 235 // by Web SpellCheck API. | 232 // by Web SpellCheck API. |
| 236 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 233 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| 237 if (browser_context) { | 234 if (browser_context) { |
| 238 SpellcheckService* spellcheck = | 235 SpellcheckService* spellcheck = |
| 239 SpellcheckServiceFactory::GetForContext(browser_context); | 236 SpellcheckServiceFactory::GetForContext(browser_context); |
| 240 if (spellcheck) { | 237 if (spellcheck) { |
| 241 if (spellcheck->GetMetrics()) | 238 if (spellcheck->GetMetrics()) |
| 242 spellcheck->GetMetrics()->RecordReplacedWordStats(1); | 239 spellcheck->GetMetrics()->RecordReplacedWordStats(1); |
| 243 spellcheck->GetFeedbackSender()->SelectedSuggestion( | |
| 244 misspelling_hash_, suggestion_index); | |
| 245 } | 240 } |
| 246 } | 241 } |
| 247 return; | 242 return; |
| 248 } | 243 } |
| 249 | 244 |
| 250 // When we choose the suggestion sent from the Spelling service, we replace | 245 // When we choose the suggestion sent from the Spelling service, we replace |
| 251 // the misspelled word with the suggestion and add it to our custom-word | 246 // the misspelled word with the suggestion and add it to our custom-word |
| 252 // dictionary so this word is not marked as misspelled any longer. | 247 // dictionary so this word is not marked as misspelled any longer. |
| 253 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION) { | 248 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION) { |
| 254 proxy_->GetWebContents()->ReplaceMisspelling(result_); | 249 proxy_->GetWebContents()->ReplaceMisspelling(result_); |
| 255 misspelled_word_ = result_; | 250 misspelled_word_ = result_; |
| 256 } | 251 } |
| 257 | 252 |
| 258 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION || | 253 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION || |
| 259 command_id == IDC_SPELLCHECK_ADD_TO_DICTIONARY) { | 254 command_id == IDC_SPELLCHECK_ADD_TO_DICTIONARY) { |
| 260 // GetHostForProfile() can return null when the suggested word is provided | 255 // GetHostForProfile() can return null when the suggested word is provided |
| 261 // by Web SpellCheck API. | 256 // by Web SpellCheck API. |
| 262 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 257 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| 263 if (browser_context) { | 258 if (browser_context) { |
| 264 SpellcheckService* spellcheck = | 259 SpellcheckService* spellcheck = |
| 265 SpellcheckServiceFactory::GetForContext(browser_context); | 260 SpellcheckServiceFactory::GetForContext(browser_context); |
| 266 if (spellcheck) { | 261 if (spellcheck) { |
| 267 spellcheck->GetCustomDictionary()->AddWord(base::UTF16ToUTF8( | 262 spellcheck->GetCustomDictionary()->AddWord(base::UTF16ToUTF8( |
| 268 misspelled_word_)); | 263 misspelled_word_)); |
| 269 spellcheck->GetFeedbackSender()->AddedToDictionary(misspelling_hash_); | |
| 270 } | 264 } |
| 271 } | 265 } |
| 272 #if defined(USE_BROWSER_SPELLCHECKER) | 266 #if defined(USE_BROWSER_SPELLCHECKER) |
| 273 spellcheck_platform::AddWord(misspelled_word_); | 267 spellcheck_platform::AddWord(misspelled_word_); |
| 274 #endif | 268 #endif |
| 275 } | 269 } |
| 276 | 270 |
| 277 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 271 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 278 | 272 |
| 279 // The spelling service can be toggled by the user only if it is not managed. | 273 // The spelling service can be toggled by the user only if it is not managed. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 294 gfx::Point(rect.CenterPoint().x(), rect.y()), std::move(model)); | 288 gfx::Point(rect.CenterPoint().x(), rect.y()), std::move(model)); |
| 295 } else { | 289 } else { |
| 296 if (profile) { | 290 if (profile) { |
| 297 profile->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, | 291 profile->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, |
| 298 false); | 292 false); |
| 299 } | 293 } |
| 300 } | 294 } |
| 301 } | 295 } |
| 302 } | 296 } |
| 303 | 297 |
| 304 void SpellingMenuObserver::OnMenuCancel() { | |
| 305 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | |
| 306 if (!browser_context) | |
| 307 return; | |
| 308 SpellcheckService* spellcheck = | |
| 309 SpellcheckServiceFactory::GetForContext(browser_context); | |
| 310 if (!spellcheck) | |
| 311 return; | |
| 312 spellcheck->GetFeedbackSender()->IgnoredSuggestions(misspelling_hash_); | |
| 313 } | |
| 314 | |
| 315 void SpellingMenuObserver::OnTextCheckComplete( | 298 void SpellingMenuObserver::OnTextCheckComplete( |
| 316 SpellingServiceClient::ServiceType type, | 299 SpellingServiceClient::ServiceType type, |
| 317 bool success, | 300 bool success, |
| 318 const base::string16& text, | 301 const base::string16& text, |
| 319 const std::vector<SpellCheckResult>& results) { | 302 const std::vector<SpellCheckResult>& results) { |
| 320 animation_timer_.Stop(); | 303 animation_timer_.Stop(); |
| 321 | 304 |
| 322 // Scan the text-check results and replace the misspelled regions with | 305 // Scan the text-check results and replace the misspelled regions with |
| 323 // suggested words. If the replaced text is included in the suggestion list | 306 // suggested words. If the replaced text is included in the suggestion list |
| 324 // provided by the local spellchecker, we show a "No suggestions from Google" | 307 // provided by the local spellchecker, we show a "No suggestions from Google" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 // Append '.' characters to the end of "Checking". | 341 // Append '.' characters to the end of "Checking". |
| 359 loading_frame_ = (loading_frame_ + 1) & 3; | 342 loading_frame_ = (loading_frame_ + 1) & 3; |
| 360 base::string16 loading_message = | 343 base::string16 loading_message = |
| 361 loading_message_ + base::string16(loading_frame_,'.'); | 344 loading_message_ + base::string16(loading_frame_,'.'); |
| 362 | 345 |
| 363 // Update the menu item with the text. We disable this item to prevent users | 346 // Update the menu item with the text. We disable this item to prevent users |
| 364 // from selecting it. | 347 // from selecting it. |
| 365 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, false, false, | 348 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, false, false, |
| 366 loading_message); | 349 loading_message); |
| 367 } | 350 } |
| OLD | NEW |