| 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/renderer/spellchecker/spellcheck_provider.h" | 5 #include "chrome/renderer/spellchecker/spellcheck_provider.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
| 10 #include "chrome/common/spellcheck_marker.h" | 10 #include "chrome/common/spellcheck_marker.h" |
| 11 #include "chrome/common/spellcheck_messages.h" | 11 #include "chrome/common/spellcheck_messages.h" |
| 12 #include "chrome/common/spellcheck_result.h" | 12 #include "chrome/common/spellcheck_result.h" |
| 13 #include "chrome/renderer/spellchecker/spellcheck.h" | 13 #include "chrome/renderer/spellchecker/spellcheck.h" |
| 14 #include "content/public/renderer/render_view.h" | 14 #include "content/public/renderer/render_view.h" |
| 15 #include "third_party/WebKit/public/platform/WebVector.h" | 15 #include "third_party/WebKit/public/platform/WebVector.h" |
| 16 #include "third_party/WebKit/public/web/WebFrame.h" | 16 #include "third_party/WebKit/public/web/WebFrame.h" |
| 17 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" | 17 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" |
| 18 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" | 18 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" |
| 19 #include "third_party/WebKit/public/web/WebTextDecorationType.h" | 19 #include "third_party/WebKit/public/web/WebTextDecorationType.h" |
| 20 #include "third_party/WebKit/public/web/WebView.h" | 20 #include "third_party/WebKit/public/web/WebView.h" |
| 21 | 21 |
| 22 using WebKit::WebFrame; | 22 using blink::WebFrame; |
| 23 using WebKit::WebString; | 23 using blink::WebString; |
| 24 using WebKit::WebTextCheckingCompletion; | 24 using blink::WebTextCheckingCompletion; |
| 25 using WebKit::WebTextCheckingResult; | 25 using blink::WebTextCheckingResult; |
| 26 using WebKit::WebTextDecorationType; | 26 using blink::WebTextDecorationType; |
| 27 using WebKit::WebVector; | 27 using blink::WebVector; |
| 28 | 28 |
| 29 COMPILE_ASSERT(int(WebKit::WebTextDecorationTypeSpelling) == | 29 COMPILE_ASSERT(int(blink::WebTextDecorationTypeSpelling) == |
| 30 int(SpellCheckResult::SPELLING), mismatching_enums); | 30 int(SpellCheckResult::SPELLING), mismatching_enums); |
| 31 COMPILE_ASSERT(int(WebKit::WebTextDecorationTypeGrammar) == | 31 COMPILE_ASSERT(int(blink::WebTextDecorationTypeGrammar) == |
| 32 int(SpellCheckResult::GRAMMAR), mismatching_enums); | 32 int(SpellCheckResult::GRAMMAR), mismatching_enums); |
| 33 COMPILE_ASSERT(int(WebKit::WebTextDecorationTypeInvisibleSpellcheck) == | 33 COMPILE_ASSERT(int(blink::WebTextDecorationTypeInvisibleSpellcheck) == |
| 34 int(SpellCheckResult::INVISIBLE), mismatching_enums); | 34 int(SpellCheckResult::INVISIBLE), mismatching_enums); |
| 35 | 35 |
| 36 SpellCheckProvider::SpellCheckProvider( | 36 SpellCheckProvider::SpellCheckProvider( |
| 37 content::RenderView* render_view, | 37 content::RenderView* render_view, |
| 38 SpellCheck* spellcheck) | 38 SpellCheck* spellcheck) |
| 39 : content::RenderViewObserver(render_view), | 39 : content::RenderViewObserver(render_view), |
| 40 content::RenderViewObserverTracker<SpellCheckProvider>(render_view), | 40 content::RenderViewObserverTracker<SpellCheckProvider>(render_view), |
| 41 spelling_panel_visible_(false), | 41 spelling_panel_visible_(false), |
| 42 spellcheck_(spellcheck) { | 42 spellcheck_(spellcheck) { |
| 43 DCHECK(spellcheck_); | 43 DCHECK(spellcheck_); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 60 return; | 60 return; |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Try to satisfy check from cache. | 63 // Try to satisfy check from cache. |
| 64 if (SatisfyRequestFromCache(text, completion)) | 64 if (SatisfyRequestFromCache(text, completion)) |
| 65 return; | 65 return; |
| 66 | 66 |
| 67 // Send this text to a browser. A browser checks the user profile and send | 67 // Send this text to a browser. A browser checks the user profile and send |
| 68 // this text to the Spelling service only if a user enables this feature. | 68 // this text to the Spelling service only if a user enables this feature. |
| 69 last_request_.clear(); | 69 last_request_.clear(); |
| 70 last_results_.assign(WebKit::WebVector<WebKit::WebTextCheckingResult>()); | 70 last_results_.assign(blink::WebVector<blink::WebTextCheckingResult>()); |
| 71 | 71 |
| 72 #if defined(OS_MACOSX) | 72 #if defined(OS_MACOSX) |
| 73 // Text check (unified request for grammar and spell check) is only | 73 // Text check (unified request for grammar and spell check) is only |
| 74 // available for browser process, so we ask the system spellchecker | 74 // available for browser process, so we ask the system spellchecker |
| 75 // over IPC or return an empty result if the checker is not | 75 // over IPC or return an empty result if the checker is not |
| 76 // available. | 76 // available. |
| 77 Send(new SpellCheckHostMsg_RequestTextCheck( | 77 Send(new SpellCheckHostMsg_RequestTextCheck( |
| 78 routing_id(), | 78 routing_id(), |
| 79 text_check_completions_.Add(completion), | 79 text_check_completions_.Add(completion), |
| 80 text, | 80 text, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 99 IPC_MESSAGE_HANDLER(SpellCheckMsg_AdvanceToNextMisspelling, | 99 IPC_MESSAGE_HANDLER(SpellCheckMsg_AdvanceToNextMisspelling, |
| 100 OnAdvanceToNextMisspelling) | 100 OnAdvanceToNextMisspelling) |
| 101 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) | 101 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) |
| 102 IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellPanel, OnToggleSpellPanel) | 102 IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellPanel, OnToggleSpellPanel) |
| 103 #endif | 103 #endif |
| 104 IPC_MESSAGE_UNHANDLED(handled = false) | 104 IPC_MESSAGE_UNHANDLED(handled = false) |
| 105 IPC_END_MESSAGE_MAP() | 105 IPC_END_MESSAGE_MAP() |
| 106 return handled; | 106 return handled; |
| 107 } | 107 } |
| 108 | 108 |
| 109 void SpellCheckProvider::FocusedNodeChanged(const WebKit::WebNode& unused) { | 109 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { |
| 110 #if defined(OS_MACOSX) | 110 #if defined(OS_MACOSX) |
| 111 bool enabled = false; | 111 bool enabled = false; |
| 112 WebKit::WebNode node = render_view()->GetFocusedNode(); | 112 blink::WebNode node = render_view()->GetFocusedNode(); |
| 113 if (!node.isNull()) | 113 if (!node.isNull()) |
| 114 enabled = render_view()->IsEditableNode(node); | 114 enabled = render_view()->IsEditableNode(node); |
| 115 | 115 |
| 116 bool checked = false; | 116 bool checked = false; |
| 117 if (enabled && render_view()->GetWebView()) { | 117 if (enabled && render_view()->GetWebView()) { |
| 118 WebFrame* frame = render_view()->GetWebView()->focusedFrame(); | 118 WebFrame* frame = render_view()->GetWebView()->focusedFrame(); |
| 119 if (frame->isContinuousSpellCheckingEnabled()) | 119 if (frame->isContinuousSpellCheckingEnabled()) |
| 120 checked = true; | 120 checked = true; |
| 121 } | 121 } |
| 122 | 122 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 139 UMA_HISTOGRAM_COUNTS("SpellCheck.api.check.suggestions", word.size()); | 139 UMA_HISTOGRAM_COUNTS("SpellCheck.api.check.suggestions", word.size()); |
| 140 } else { | 140 } else { |
| 141 UMA_HISTOGRAM_COUNTS("SpellCheck.api.check", word.size()); | 141 UMA_HISTOGRAM_COUNTS("SpellCheck.api.check", word.size()); |
| 142 // If optional_suggestions is not requested, the API is called | 142 // If optional_suggestions is not requested, the API is called |
| 143 // for marking. So we use this for counting markable words. | 143 // for marking. So we use this for counting markable words. |
| 144 Send(new SpellCheckHostMsg_NotifyChecked(routing_id(), word, 0 < length)); | 144 Send(new SpellCheckHostMsg_NotifyChecked(routing_id(), word, 0 < length)); |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 void SpellCheckProvider::checkTextOfParagraph( | 148 void SpellCheckProvider::checkTextOfParagraph( |
| 149 const WebKit::WebString& text, | 149 const blink::WebString& text, |
| 150 WebKit::WebTextCheckingTypeMask mask, | 150 blink::WebTextCheckingTypeMask mask, |
| 151 WebKit::WebVector<WebKit::WebTextCheckingResult>* results) { | 151 blink::WebVector<blink::WebTextCheckingResult>* results) { |
| 152 if (!results) | 152 if (!results) |
| 153 return; | 153 return; |
| 154 | 154 |
| 155 if (!(mask & WebKit::WebTextCheckingTypeSpelling)) | 155 if (!(mask & blink::WebTextCheckingTypeSpelling)) |
| 156 return; | 156 return; |
| 157 | 157 |
| 158 // TODO(groby): As far as I can tell, this method is never invoked. | 158 // TODO(groby): As far as I can tell, this method is never invoked. |
| 159 // UMA results seem to support that. Investigate, clean up if true. | 159 // UMA results seem to support that. Investigate, clean up if true. |
| 160 NOTREACHED(); | 160 NOTREACHED(); |
| 161 spellcheck_->SpellCheckParagraph(text, results); | 161 spellcheck_->SpellCheckParagraph(text, results); |
| 162 UMA_HISTOGRAM_COUNTS("SpellCheck.api.paragraph", text.length()); | 162 UMA_HISTOGRAM_COUNTS("SpellCheck.api.paragraph", text.length()); |
| 163 } | 163 } |
| 164 | 164 |
| 165 void SpellCheckProvider::requestCheckingOfText( | 165 void SpellCheckProvider::requestCheckingOfText( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 text_check_completions_.Remove(identifier); | 217 text_check_completions_.Remove(identifier); |
| 218 | 218 |
| 219 // If |succeeded| is false, we use local spellcheck as a fallback. | 219 // If |succeeded| is false, we use local spellcheck as a fallback. |
| 220 if (!succeeded) { | 220 if (!succeeded) { |
| 221 spellcheck_->RequestTextChecking(line, completion); | 221 spellcheck_->RequestTextChecking(line, completion); |
| 222 return; | 222 return; |
| 223 } | 223 } |
| 224 | 224 |
| 225 // Double-check the returned spellchecking results with our spellchecker to | 225 // Double-check the returned spellchecking results with our spellchecker to |
| 226 // visualize the differences between ours and the on-line spellchecker. | 226 // visualize the differences between ours and the on-line spellchecker. |
| 227 WebKit::WebVector<WebKit::WebTextCheckingResult> textcheck_results; | 227 blink::WebVector<blink::WebTextCheckingResult> textcheck_results; |
| 228 spellcheck_->CreateTextCheckingResults(SpellCheck::USE_NATIVE_CHECKER, | 228 spellcheck_->CreateTextCheckingResults(SpellCheck::USE_NATIVE_CHECKER, |
| 229 0, | 229 0, |
| 230 line, | 230 line, |
| 231 results, | 231 results, |
| 232 &textcheck_results); | 232 &textcheck_results); |
| 233 completion->didFinishCheckingText(textcheck_results); | 233 completion->didFinishCheckingText(textcheck_results); |
| 234 | 234 |
| 235 // Cache the request and the converted results. | 235 // Cache the request and the converted results. |
| 236 last_request_ = line; | 236 last_request_ = line; |
| 237 last_results_.swap(textcheck_results); | 237 last_results_.swap(textcheck_results); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 264 void SpellCheckProvider::OnRespondTextCheck( | 264 void SpellCheckProvider::OnRespondTextCheck( |
| 265 int identifier, | 265 int identifier, |
| 266 const std::vector<SpellCheckResult>& results) { | 266 const std::vector<SpellCheckResult>& results) { |
| 267 // TODO(groby): Unify with SpellCheckProvider::OnRespondSpellingService | 267 // TODO(groby): Unify with SpellCheckProvider::OnRespondSpellingService |
| 268 DCHECK(spellcheck_); | 268 DCHECK(spellcheck_); |
| 269 WebTextCheckingCompletion* completion = | 269 WebTextCheckingCompletion* completion = |
| 270 text_check_completions_.Lookup(identifier); | 270 text_check_completions_.Lookup(identifier); |
| 271 if (!completion) | 271 if (!completion) |
| 272 return; | 272 return; |
| 273 text_check_completions_.Remove(identifier); | 273 text_check_completions_.Remove(identifier); |
| 274 WebKit::WebVector<WebKit::WebTextCheckingResult> textcheck_results; | 274 blink::WebVector<blink::WebTextCheckingResult> textcheck_results; |
| 275 spellcheck_->CreateTextCheckingResults(SpellCheck::DO_NOT_MODIFY, | 275 spellcheck_->CreateTextCheckingResults(SpellCheck::DO_NOT_MODIFY, |
| 276 0, | 276 0, |
| 277 string16(), | 277 string16(), |
| 278 results, | 278 results, |
| 279 &textcheck_results); | 279 &textcheck_results); |
| 280 completion->didFinishCheckingText(textcheck_results); | 280 completion->didFinishCheckingText(textcheck_results); |
| 281 | 281 |
| 282 // TODO(groby): Add request caching once OSX reports back original request. | 282 // TODO(groby): Add request caching once OSX reports back original request. |
| 283 // (cf. SpellCheckProvider::OnRespondSpellingService) | 283 // (cf. SpellCheckProvider::OnRespondSpellingService) |
| 284 // Cache the request and the converted results. | 284 // Cache the request and the converted results. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 if (text_length < last_length && | 337 if (text_length < last_length && |
| 338 !last_request_.compare(0, text_length, request)) { | 338 !last_request_.compare(0, text_length, request)) { |
| 339 size_t result_size = 0; | 339 size_t result_size = 0; |
| 340 for (size_t i = 0; i < last_results_.size(); ++i) { | 340 for (size_t i = 0; i < last_results_.size(); ++i) { |
| 341 size_t start = last_results_[i].location; | 341 size_t start = last_results_[i].location; |
| 342 size_t end = start + last_results_[i].length; | 342 size_t end = start + last_results_[i].length; |
| 343 if (start <= text_length && end <= text_length) | 343 if (start <= text_length && end <= text_length) |
| 344 ++result_size; | 344 ++result_size; |
| 345 } | 345 } |
| 346 if (result_size > 0) { | 346 if (result_size > 0) { |
| 347 WebKit::WebVector<WebKit::WebTextCheckingResult> results(result_size); | 347 blink::WebVector<blink::WebTextCheckingResult> results(result_size); |
| 348 for (size_t i = 0; i < result_size; ++i) { | 348 for (size_t i = 0; i < result_size; ++i) { |
| 349 results[i].decoration = last_results_[i].decoration; | 349 results[i].decoration = last_results_[i].decoration; |
| 350 results[i].location = last_results_[i].location; | 350 results[i].location = last_results_[i].location; |
| 351 results[i].length = last_results_[i].length; | 351 results[i].length = last_results_[i].length; |
| 352 results[i].replacement = last_results_[i].replacement; | 352 results[i].replacement = last_results_[i].replacement; |
| 353 } | 353 } |
| 354 completion->didFinishCheckingText(results); | 354 completion->didFinishCheckingText(results); |
| 355 return true; | 355 return true; |
| 356 } | 356 } |
| 357 } | 357 } |
| 358 | 358 |
| 359 return false; | 359 return false; |
| 360 } | 360 } |
| OLD | NEW |