| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "extensions/browser/view_type_utils.h" | 30 #include "extensions/browser/view_type_utils.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 32 #include "ui/gfx/geometry/rect.h" | 32 #include "ui/gfx/geometry/rect.h" |
| 33 | 33 |
| 34 using content::BrowserThread; | 34 using content::BrowserThread; |
| 35 | 35 |
| 36 SpellingMenuObserver::SpellingMenuObserver(RenderViewContextMenuProxy* proxy) | 36 SpellingMenuObserver::SpellingMenuObserver(RenderViewContextMenuProxy* proxy) |
| 37 : proxy_(proxy), | 37 : proxy_(proxy), |
| 38 loading_frame_(0), | 38 loading_frame_(0), |
| 39 succeeded_(false), | 39 succeeded_(false), |
| 40 misspelling_hash_(0), |
| 40 client_(new SpellingServiceClient) { | 41 client_(new SpellingServiceClient) { |
| 41 if (proxy_ && proxy_->GetBrowserContext()) { | 42 if (proxy_ && proxy_->GetBrowserContext()) { |
| 42 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 43 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 43 integrate_spelling_service_.Init(prefs::kSpellCheckUseSpellingService, | 44 integrate_spelling_service_.Init(prefs::kSpellCheckUseSpellingService, |
| 44 profile->GetPrefs()); | 45 profile->GetPrefs()); |
| 45 autocorrect_spelling_.Init(prefs::kEnableAutoSpellCorrect, | 46 autocorrect_spelling_.Init(prefs::kEnableAutoSpellCorrect, |
| 46 profile->GetPrefs()); | 47 profile->GetPrefs()); |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 60 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 61 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| 61 if (!params.is_editable || !browser_context) | 62 if (!params.is_editable || !browser_context) |
| 62 return; | 63 return; |
| 63 | 64 |
| 64 // Exit if there is no misspelled word. | 65 // Exit if there is no misspelled word. |
| 65 if (params.misspelled_word.empty()) | 66 if (params.misspelled_word.empty()) |
| 66 return; | 67 return; |
| 67 | 68 |
| 68 suggestions_ = params.dictionary_suggestions; | 69 suggestions_ = params.dictionary_suggestions; |
| 69 misspelled_word_ = params.misspelled_word; | 70 misspelled_word_ = params.misspelled_word; |
| 71 misspelling_hash_ = params.misspelling_hash; |
| 70 | 72 |
| 71 bool use_suggestions = SpellingServiceClient::IsAvailable( | 73 bool use_suggestions = SpellingServiceClient::IsAvailable( |
| 72 browser_context, SpellingServiceClient::SUGGEST); | 74 browser_context, SpellingServiceClient::SUGGEST); |
| 73 | 75 |
| 74 if (!suggestions_.empty() || use_suggestions) | 76 if (!suggestions_.empty() || use_suggestions) |
| 75 proxy_->AddSeparator(); | 77 proxy_->AddSeparator(); |
| 76 | 78 |
| 77 // Append Dictionary spell check suggestions. | 79 // Append Dictionary spell check suggestions. |
| 78 for (size_t i = 0; i < params.dictionary_suggestions.size() && | 80 for (size_t i = 0; i < params.dictionary_suggestions.size() && |
| 79 IDC_SPELLCHECK_SUGGESTION_0 + i <= IDC_SPELLCHECK_SUGGESTION_LAST; | 81 IDC_SPELLCHECK_SUGGESTION_0 + i <= IDC_SPELLCHECK_SUGGESTION_LAST; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 suggestions_[suggestion_index]); | 251 suggestions_[suggestion_index]); |
| 250 // GetSpellCheckHost() can return null when the suggested word is provided | 252 // GetSpellCheckHost() can return null when the suggested word is provided |
| 251 // by Web SpellCheck API. | 253 // by Web SpellCheck API. |
| 252 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 254 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| 253 if (browser_context) { | 255 if (browser_context) { |
| 254 SpellcheckService* spellcheck = | 256 SpellcheckService* spellcheck = |
| 255 SpellcheckServiceFactory::GetForContext(browser_context); | 257 SpellcheckServiceFactory::GetForContext(browser_context); |
| 256 if (spellcheck) { | 258 if (spellcheck) { |
| 257 if (spellcheck->GetMetrics()) | 259 if (spellcheck->GetMetrics()) |
| 258 spellcheck->GetMetrics()->RecordReplacedWordStats(1); | 260 spellcheck->GetMetrics()->RecordReplacedWordStats(1); |
| 261 spellcheck->GetFeedbackSender()->SelectedSuggestion( |
| 262 misspelling_hash_, suggestion_index); |
| 259 } | 263 } |
| 260 } | 264 } |
| 261 return; | 265 return; |
| 262 } | 266 } |
| 263 | 267 |
| 264 // When we choose the suggestion sent from the Spelling service, we replace | 268 // When we choose the suggestion sent from the Spelling service, we replace |
| 265 // the misspelled word with the suggestion and add it to our custom-word | 269 // the misspelled word with the suggestion and add it to our custom-word |
| 266 // dictionary so this word is not marked as misspelled any longer. | 270 // dictionary so this word is not marked as misspelled any longer. |
| 267 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION) { | 271 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION) { |
| 268 proxy_->GetWebContents()->ReplaceMisspelling(result_); | 272 proxy_->GetWebContents()->ReplaceMisspelling(result_); |
| 269 misspelled_word_ = result_; | 273 misspelled_word_ = result_; |
| 270 } | 274 } |
| 271 | 275 |
| 272 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION || | 276 if (command_id == IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION || |
| 273 command_id == IDC_SPELLCHECK_ADD_TO_DICTIONARY) { | 277 command_id == IDC_SPELLCHECK_ADD_TO_DICTIONARY) { |
| 274 // GetHostForProfile() can return null when the suggested word is provided | 278 // GetHostForProfile() can return null when the suggested word is provided |
| 275 // by Web SpellCheck API. | 279 // by Web SpellCheck API. |
| 276 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 280 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| 277 if (browser_context) { | 281 if (browser_context) { |
| 278 SpellcheckService* spellcheck = | 282 SpellcheckService* spellcheck = |
| 279 SpellcheckServiceFactory::GetForContext(browser_context); | 283 SpellcheckServiceFactory::GetForContext(browser_context); |
| 280 if (spellcheck) { | 284 if (spellcheck) { |
| 281 spellcheck->GetCustomDictionary()->AddWord(base::UTF16ToUTF8( | 285 spellcheck->GetCustomDictionary()->AddWord(base::UTF16ToUTF8( |
| 282 misspelled_word_)); | 286 misspelled_word_)); |
| 287 spellcheck->GetFeedbackSender()->AddedToDictionary(misspelling_hash_); |
| 283 } | 288 } |
| 284 } | 289 } |
| 285 #if defined(OS_MACOSX) | 290 #if defined(OS_MACOSX) |
| 286 spellcheck_mac::AddWord(misspelled_word_); | 291 spellcheck_mac::AddWord(misspelled_word_); |
| 287 #endif | 292 #endif |
| 288 } | 293 } |
| 289 | 294 |
| 290 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 295 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 291 | 296 |
| 292 // The spelling service can be toggled by the user only if it is not managed. | 297 // The spelling service can be toggled by the user only if it is not managed. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 } else { | 340 } else { |
| 336 if (profile) { | 341 if (profile) { |
| 337 bool current_value = autocorrect_spelling_.GetValue(); | 342 bool current_value = autocorrect_spelling_.GetValue(); |
| 338 profile->GetPrefs()->SetBoolean(prefs::kEnableAutoSpellCorrect, | 343 profile->GetPrefs()->SetBoolean(prefs::kEnableAutoSpellCorrect, |
| 339 !current_value); | 344 !current_value); |
| 340 } | 345 } |
| 341 } | 346 } |
| 342 } | 347 } |
| 343 } | 348 } |
| 344 | 349 |
| 350 void SpellingMenuObserver::OnMenuCancel() { |
| 351 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| 352 if (!browser_context) |
| 353 return; |
| 354 SpellcheckService* spellcheck = |
| 355 SpellcheckServiceFactory::GetForContext(browser_context); |
| 356 if (!spellcheck) |
| 357 return; |
| 358 spellcheck->GetFeedbackSender()->IgnoredSuggestions(misspelling_hash_); |
| 359 } |
| 360 |
| 345 void SpellingMenuObserver::OnTextCheckComplete( | 361 void SpellingMenuObserver::OnTextCheckComplete( |
| 346 SpellingServiceClient::ServiceType type, | 362 SpellingServiceClient::ServiceType type, |
| 347 bool success, | 363 bool success, |
| 348 const base::string16& text, | 364 const base::string16& text, |
| 349 const std::vector<SpellCheckResult>& results) { | 365 const std::vector<SpellCheckResult>& results) { |
| 350 animation_timer_.Stop(); | 366 animation_timer_.Stop(); |
| 351 | 367 |
| 352 // Scan the text-check results and replace the misspelled regions with | 368 // Scan the text-check results and replace the misspelled regions with |
| 353 // suggested words. If the replaced text is included in the suggestion list | 369 // suggested words. If the replaced text is included in the suggestion list |
| 354 // provided by the local spellchecker, we show a "No suggestions from Google" | 370 // 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... |
| 388 // Append '.' characters to the end of "Checking". | 404 // Append '.' characters to the end of "Checking". |
| 389 loading_frame_ = (loading_frame_ + 1) & 3; | 405 loading_frame_ = (loading_frame_ + 1) & 3; |
| 390 base::string16 loading_message = | 406 base::string16 loading_message = |
| 391 loading_message_ + base::string16(loading_frame_,'.'); | 407 loading_message_ + base::string16(loading_frame_,'.'); |
| 392 | 408 |
| 393 // Update the menu item with the text. We disable this item to prevent users | 409 // Update the menu item with the text. We disable this item to prevent users |
| 394 // from selecting it. | 410 // from selecting it. |
| 395 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, false, false, | 411 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, false, false, |
| 396 loading_message); | 412 loading_message); |
| 397 } | 413 } |
| OLD | NEW |