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/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 // text to the result text so we can replace its misspelled regions with | 79 // text to the result text so we can replace its misspelled regions with |
80 // suggestions. | 80 // suggestions. |
81 succeeded_ = false; | 81 succeeded_ = false; |
82 result_ = params.misspelled_word; | 82 result_ = params.misspelled_word; |
83 | 83 |
84 // Add a placeholder item. This item will be updated when we receive a | 84 // Add a placeholder item. This item will be updated when we receive a |
85 // response from the Spelling service. (We do not have to disable this | 85 // response from the Spelling service. (We do not have to disable this |
86 // item now since Chrome will call IsCommandIdEnabled() and disable it.) | 86 // item now since Chrome will call IsCommandIdEnabled() and disable it.) |
87 loading_message_ = | 87 loading_message_ = |
88 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_CHECKING); | 88 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_CHECKING); |
89 proxy_->AddMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, | 89 if (!useSpellingService) { |
groby-ooo-7-16
2012/12/21 22:18:13
Can you add a browsertest?
rpetterson
2013/01/04 00:05:34
Done.
| |
90 loading_message_); | 90 proxy_->AddMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, |
91 | 91 loading_message_); |
92 } | |
92 // Invoke a JSON-RPC call to the Spelling service in the background so we | 93 // Invoke a JSON-RPC call to the Spelling service in the background so we |
93 // can update the placeholder item when we receive its response. It also | 94 // can update the placeholder item when we receive its response. It also |
94 // starts the animation timer so we can show animation until we receive | 95 // starts the animation timer so we can show animation until we receive |
95 // it. | 96 // it. |
96 SpellingServiceClient::ServiceType type = SpellingServiceClient::SUGGEST; | 97 SpellingServiceClient::ServiceType type = SpellingServiceClient::SUGGEST; |
97 if (useSpellingService) | 98 if (useSpellingService) |
98 type = SpellingServiceClient::SPELLCHECK; | 99 type = SpellingServiceClient::SPELLCHECK; |
99 client_.reset(new SpellingServiceClient); | 100 client_.reset(new SpellingServiceClient); |
100 bool result = client_->RequestTextCheck( | 101 bool result = client_->RequestTextCheck( |
101 profile, 0, type, params.misspelled_word, | 102 profile, 0, type, params.misspelled_word, |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 if (profile) | 255 if (profile) |
255 profile->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, | 256 profile->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, |
256 false); | 257 false); |
257 } | 258 } |
258 } | 259 } |
259 } | 260 } |
260 | 261 |
261 void SpellingMenuObserver::OnTextCheckComplete( | 262 void SpellingMenuObserver::OnTextCheckComplete( |
262 int tag, | 263 int tag, |
263 bool success, | 264 bool success, |
265 SpellingServiceClient::ServiceType type, | |
264 const string16& text, | 266 const string16& text, |
265 const std::vector<SpellCheckResult>& results) { | 267 const std::vector<SpellCheckResult>& results) { |
266 animation_timer_.Stop(); | 268 animation_timer_.Stop(); |
267 | 269 |
268 // Scan the text-check results and replace the misspelled regions with | 270 // Scan the text-check results and replace the misspelled regions with |
269 // suggested words. If the replaced text is included in the suggestion list | 271 // suggested words. If the replaced text is included in the suggestion list |
270 // provided by the local spellchecker, we show a "No suggestions from Google" | 272 // provided by the local spellchecker, we show a "No suggestions from Google" |
271 // message. | 273 // message. |
272 succeeded_ = success; | 274 succeeded_ = success; |
273 if (results.empty()) { | 275 if (results.empty()) { |
274 succeeded_ = false; | 276 succeeded_ = false; |
275 } else { | 277 } else { |
276 typedef std::vector<SpellCheckResult> SpellCheckResults; | 278 typedef std::vector<SpellCheckResult> SpellCheckResults; |
277 for (SpellCheckResults::const_iterator it = results.begin(); | 279 for (SpellCheckResults::const_iterator it = results.begin(); |
278 it != results.end(); ++it) { | 280 it != results.end(); ++it) { |
279 result_.replace(it->location, it->length, it->replacement); | 281 result_.replace(it->location, it->length, it->replacement); |
280 } | 282 } |
281 string16 result = base::i18n::ToLower(result_); | 283 string16 result = base::i18n::ToLower(result_); |
282 for (std::vector<string16>::const_iterator it = suggestions_.begin(); | 284 for (std::vector<string16>::const_iterator it = suggestions_.begin(); |
283 it != suggestions_.end(); ++it) { | 285 it != suggestions_.end(); ++it) { |
284 if (result == base::i18n::ToLower(*it)) { | 286 if (result == base::i18n::ToLower(*it)) { |
285 succeeded_ = false; | 287 succeeded_ = false; |
286 break; | 288 break; |
287 } | 289 } |
288 } | 290 } |
289 } | 291 } |
290 if (!succeeded_) { | 292 if (type != SpellingServiceClient::SPELLCHECK) { |
groby-ooo-7-16
2012/12/21 22:18:13
Can you add a browsertest?
rpetterson
2013/01/04 00:05:34
I think this and the one above both cover the same
| |
291 result_ = l10n_util::GetStringUTF16( | 293 if (!succeeded_) { |
292 IDS_CONTENT_CONTEXT_SPELLING_NO_SUGGESTIONS_FROM_GOOGLE); | 294 result_ = l10n_util::GetStringUTF16( |
295 IDS_CONTENT_CONTEXT_SPELLING_NO_SUGGESTIONS_FROM_GOOGLE); | |
296 } | |
297 | |
298 // Update the menu item with the result text. We disable this item and hide | |
299 // it when the spelling service does not provide valid suggestions. | |
300 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, succeeded_, | |
301 false, result_); | |
293 } | 302 } |
294 | |
295 // Update the menu item with the result text. We disable this item and hide it | |
296 // when the spelling service does not provide valid suggestions. | |
297 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, succeeded_, | |
298 false, result_); | |
299 } | 303 } |
300 | 304 |
301 void SpellingMenuObserver::OnAnimationTimerExpired() { | 305 void SpellingMenuObserver::OnAnimationTimerExpired() { |
302 // Append '.' characters to the end of "Checking". | 306 // Append '.' characters to the end of "Checking". |
303 loading_frame_ = (loading_frame_ + 1) & 3; | 307 loading_frame_ = (loading_frame_ + 1) & 3; |
304 string16 loading_message = loading_message_ + string16(loading_frame_,'.'); | 308 string16 loading_message = loading_message_ + string16(loading_frame_,'.'); |
305 | 309 |
306 // Update the menu item with the text. We disable this item to prevent users | 310 // Update the menu item with the text. We disable this item to prevent users |
307 // from selecting it. | 311 // from selecting it. |
308 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, false, false, | 312 proxy_->UpdateMenuItem(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, false, false, |
309 loading_message); | 313 loading_message); |
310 } | 314 } |
OLD | NEW |