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 "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/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // text to the result text so we can replace its misspelled regions with | 83 // text to the result text so we can replace its misspelled regions with |
84 // suggestions. | 84 // suggestions. |
85 succeeded_ = false; | 85 succeeded_ = false; |
86 result_ = params.misspelled_word; | 86 result_ = params.misspelled_word; |
87 | 87 |
88 // Add a placeholder item. This item will be updated when we receive a | 88 // Add a placeholder item. This item will be updated when we receive a |
89 // response from the Spelling service. (We do not have to disable this | 89 // response from the Spelling service. (We do not have to disable this |
90 // item now since Chrome will call IsCommandIdEnabled() and disable it.) | 90 // item now since Chrome will call IsCommandIdEnabled() and disable it.) |
91 loading_message_ = | 91 loading_message_ = |
92 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_CHECKING); | 92 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_CHECKING); |
93 proxy_->AddMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, | 93 if (!useSpellingService) { |
94 loading_message_); | 94 proxy_->AddMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, |
95 | 95 loading_message_); |
| 96 } |
96 // Invoke a JSON-RPC call to the Spelling service in the background so we | 97 // Invoke a JSON-RPC call to the Spelling service in the background so we |
97 // can update the placeholder item when we receive its response. It also | 98 // can update the placeholder item when we receive its response. It also |
98 // starts the animation timer so we can show animation until we receive | 99 // starts the animation timer so we can show animation until we receive |
99 // it. | 100 // it. |
100 SpellingServiceClient::ServiceType type = SpellingServiceClient::SUGGEST; | 101 SpellingServiceClient::ServiceType type = SpellingServiceClient::SUGGEST; |
101 if (useSpellingService) | 102 if (useSpellingService) |
102 type = SpellingServiceClient::SPELLCHECK; | 103 type = SpellingServiceClient::SPELLCHECK; |
103 client_.reset(new SpellingServiceClient); | 104 client_.reset(new SpellingServiceClient); |
104 bool result = client_->RequestTextCheck( | 105 bool result = client_->RequestTextCheck( |
105 profile, 0, type, params.misspelled_word, | 106 profile, type, params.misspelled_word, |
106 base::Bind(&SpellingMenuObserver::OnTextCheckComplete, | 107 base::Bind(&SpellingMenuObserver::OnTextCheckComplete, |
107 base::Unretained(this))); | 108 base::Unretained(this), type)); |
108 if (result) { | 109 if (result) { |
109 loading_frame_ = 0; | 110 loading_frame_ = 0; |
110 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), | 111 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), |
111 this, &SpellingMenuObserver::OnAnimationTimerExpired); | 112 this, &SpellingMenuObserver::OnAnimationTimerExpired); |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 if (!params.dictionary_suggestions.empty()) { | 116 if (!params.dictionary_suggestions.empty()) { |
116 proxy_->AddSeparator(); | 117 proxy_->AddSeparator(); |
117 | 118 |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 if (profile) { | 297 if (profile) { |
297 bool current_value = autocorrect_spelling_.GetValue(); | 298 bool current_value = autocorrect_spelling_.GetValue(); |
298 profile->GetPrefs()->SetBoolean(prefs::kEnableAutoSpellCorrect, | 299 profile->GetPrefs()->SetBoolean(prefs::kEnableAutoSpellCorrect, |
299 !current_value); | 300 !current_value); |
300 } | 301 } |
301 } | 302 } |
302 } | 303 } |
303 } | 304 } |
304 | 305 |
305 void SpellingMenuObserver::OnTextCheckComplete( | 306 void SpellingMenuObserver::OnTextCheckComplete( |
306 int tag, | 307 SpellingServiceClient::ServiceType type, |
307 bool success, | 308 bool success, |
308 const string16& text, | 309 const string16& text, |
309 const std::vector<SpellCheckResult>& results) { | 310 const std::vector<SpellCheckResult>& results) { |
310 animation_timer_.Stop(); | 311 animation_timer_.Stop(); |
311 | 312 |
312 // Scan the text-check results and replace the misspelled regions with | 313 // Scan the text-check results and replace the misspelled regions with |
313 // suggested words. If the replaced text is included in the suggestion list | 314 // suggested words. If the replaced text is included in the suggestion list |
314 // provided by the local spellchecker, we show a "No suggestions from Google" | 315 // provided by the local spellchecker, we show a "No suggestions from Google" |
315 // message. | 316 // message. |
316 succeeded_ = success; | 317 succeeded_ = success; |
317 if (results.empty()) { | 318 if (results.empty()) { |
318 succeeded_ = false; | 319 succeeded_ = false; |
319 } else { | 320 } else { |
320 typedef std::vector<SpellCheckResult> SpellCheckResults; | 321 typedef std::vector<SpellCheckResult> SpellCheckResults; |
321 for (SpellCheckResults::const_iterator it = results.begin(); | 322 for (SpellCheckResults::const_iterator it = results.begin(); |
322 it != results.end(); ++it) { | 323 it != results.end(); ++it) { |
323 result_.replace(it->location, it->length, it->replacement); | 324 result_.replace(it->location, it->length, it->replacement); |
324 } | 325 } |
325 string16 result = base::i18n::ToLower(result_); | 326 string16 result = base::i18n::ToLower(result_); |
326 for (std::vector<string16>::const_iterator it = suggestions_.begin(); | 327 for (std::vector<string16>::const_iterator it = suggestions_.begin(); |
327 it != suggestions_.end(); ++it) { | 328 it != suggestions_.end(); ++it) { |
328 if (result == base::i18n::ToLower(*it)) { | 329 if (result == base::i18n::ToLower(*it)) { |
329 succeeded_ = false; | 330 succeeded_ = false; |
330 break; | 331 break; |
331 } | 332 } |
332 } | 333 } |
333 } | 334 } |
334 if (!succeeded_) { | 335 if (type != SpellingServiceClient::SPELLCHECK) { |
335 result_ = l10n_util::GetStringUTF16( | 336 if (!succeeded_) { |
336 IDS_CONTENT_CONTEXT_SPELLING_NO_SUGGESTIONS_FROM_GOOGLE); | 337 result_ = l10n_util::GetStringUTF16( |
| 338 IDS_CONTENT_CONTEXT_SPELLING_NO_SUGGESTIONS_FROM_GOOGLE); |
| 339 } |
| 340 |
| 341 // Update the menu item with the result text. We disable this item and hide |
| 342 // it when the spelling service does not provide valid suggestions. |
| 343 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, succeeded_, |
| 344 false, result_); |
337 } | 345 } |
338 | |
339 // Update the menu item with the result text. We disable this item and hide it | |
340 // when the spelling service does not provide valid suggestions. | |
341 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, succeeded_, | |
342 false, result_); | |
343 } | 346 } |
344 | 347 |
345 void SpellingMenuObserver::OnAnimationTimerExpired() { | 348 void SpellingMenuObserver::OnAnimationTimerExpired() { |
346 // Append '.' characters to the end of "Checking". | 349 // Append '.' characters to the end of "Checking". |
347 loading_frame_ = (loading_frame_ + 1) & 3; | 350 loading_frame_ = (loading_frame_ + 1) & 3; |
348 string16 loading_message = loading_message_ + string16(loading_frame_,'.'); | 351 string16 loading_message = loading_message_ + string16(loading_frame_,'.'); |
349 | 352 |
350 // Update the menu item with the text. We disable this item to prevent users | 353 // Update the menu item with the text. We disable this item to prevent users |
351 // from selecting it. | 354 // from selecting it. |
352 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, false, false, | 355 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, false, false, |
353 loading_message); | 356 loading_message); |
354 } | 357 } |
OLD | NEW |