| 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" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 65 |
| 66 // Try to satisfy check from cache. | 66 // Try to satisfy check from cache. |
| 67 if (SatisfyRequestFromCache(text, completion)) | 67 if (SatisfyRequestFromCache(text, completion)) |
| 68 return; | 68 return; |
| 69 | 69 |
| 70 // Send this text to a browser. A browser checks the user profile and send | 70 // Send this text to a browser. A browser checks the user profile and send |
| 71 // this text to the Spelling service only if a user enables this feature. | 71 // this text to the Spelling service only if a user enables this feature. |
| 72 last_request_.clear(); | 72 last_request_.clear(); |
| 73 last_results_.assign(blink::WebVector<blink::WebTextCheckingResult>()); | 73 last_results_.assign(blink::WebVector<blink::WebTextCheckingResult>()); |
| 74 | 74 |
| 75 #if defined(OS_MACOSX) | 75 #if defined(USE_PLATFORM_SPELLCHECKER) |
| 76 // Text check (unified request for grammar and spell check) is only | 76 // Text check (unified request for grammar and spell check) is only |
| 77 // available for browser process, so we ask the system spellchecker | 77 // available for browser process, so we ask the system spellchecker |
| 78 // over IPC or return an empty result if the checker is not | 78 // over IPC or return an empty result if the checker is not |
| 79 // available. | 79 // available. |
| 80 Send(new SpellCheckHostMsg_RequestTextCheck( | 80 Send(new SpellCheckHostMsg_RequestTextCheck( |
| 81 routing_id(), | 81 routing_id(), |
| 82 text_check_completions_.Add(completion), | 82 text_check_completions_.Add(completion), |
| 83 text, | 83 text, |
| 84 markers)); | 84 markers)); |
| 85 #else | 85 #else |
| 86 Send(new SpellCheckHostMsg_CallSpellingService( | 86 Send(new SpellCheckHostMsg_CallSpellingService( |
| 87 routing_id(), | 87 routing_id(), |
| 88 text_check_completions_.Add(completion), | 88 text_check_completions_.Add(completion), |
| 89 base::string16(text), | 89 base::string16(text), |
| 90 markers)); | 90 markers)); |
| 91 #endif // !OS_MACOSX | 91 #endif // !USE_PLATFORM_SPELLCHECKER |
| 92 } | 92 } |
| 93 | 93 |
| 94 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { | 94 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { |
| 95 bool handled = true; | 95 bool handled = true; |
| 96 IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) | 96 IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) |
| 97 #if !defined(OS_MACOSX) | 97 #if !defined(USE_PLATFORM_SPELLCHECKER) |
| 98 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondSpellingService, | 98 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondSpellingService, |
| 99 OnRespondSpellingService) | 99 OnRespondSpellingService) |
| 100 #endif | 100 #else |
| 101 #if defined(OS_MACOSX) | |
| 102 IPC_MESSAGE_HANDLER(SpellCheckMsg_AdvanceToNextMisspelling, | 101 IPC_MESSAGE_HANDLER(SpellCheckMsg_AdvanceToNextMisspelling, |
| 103 OnAdvanceToNextMisspelling) | 102 OnAdvanceToNextMisspelling) |
| 104 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) | 103 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) |
| 105 IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellPanel, OnToggleSpellPanel) | 104 IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellPanel, OnToggleSpellPanel) |
| 106 #endif | 105 #endif // USE_PLATFORM_SPELLCHECKER |
| 107 IPC_MESSAGE_UNHANDLED(handled = false) | 106 IPC_MESSAGE_UNHANDLED(handled = false) |
| 108 IPC_END_MESSAGE_MAP() | 107 IPC_END_MESSAGE_MAP() |
| 109 return handled; | 108 return handled; |
| 110 } | 109 } |
| 111 | 110 |
| 112 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { | 111 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { |
| 113 #if defined(OS_MACOSX) | 112 #if defined(USE_PLATFORM_SPELLCHECKER) |
| 114 WebFrame* frame = render_view()->GetWebView()->focusedFrame(); | 113 WebFrame* frame = render_view()->GetWebView()->focusedFrame(); |
| 115 WebElement element = frame->document().isNull() ? WebElement() : | 114 WebElement element = frame->document().isNull() ? WebElement() : |
| 116 frame->document().focusedElement(); | 115 frame->document().focusedElement(); |
| 117 bool enabled = !element.isNull() && render_view()->IsEditableNode(element); | 116 bool enabled = !element.isNull() && render_view()->IsEditableNode(element); |
| 118 | 117 |
| 119 bool checked = enabled && render_view()->GetWebView() && | 118 bool checked = enabled && render_view()->GetWebView() && |
| 120 frame->isContinuousSpellCheckingEnabled(); | 119 frame->isContinuousSpellCheckingEnabled(); |
| 121 | 120 |
| 122 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); | 121 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); |
| 123 #endif // OS_MACOSX | 122 #endif // USE_PLATFORM_SPELLCHECKER |
| 124 } | 123 } |
| 125 | 124 |
| 126 void SpellCheckProvider::spellCheck( | 125 void SpellCheckProvider::spellCheck( |
| 127 const WebString& text, | 126 const WebString& text, |
| 128 int& offset, | 127 int& offset, |
| 129 int& length, | 128 int& length, |
| 130 WebVector<WebString>* optional_suggestions) { | 129 WebVector<WebString>* optional_suggestions) { |
| 131 base::string16 word(text); | 130 base::string16 word(text); |
| 132 std::vector<base::string16> suggestions; | 131 std::vector<base::string16> suggestions; |
| 133 spellcheck_->SpellCheckWord( | 132 spellcheck_->SpellCheckWord( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 const base::CommandLine& command_line = | 178 const base::CommandLine& command_line = |
| 180 *base::CommandLine::ForCurrentProcess(); | 179 *base::CommandLine::ForCurrentProcess(); |
| 181 if (command_line.HasSwitch(switches::kEnableSpellingAutoCorrect)) { | 180 if (command_line.HasSwitch(switches::kEnableSpellingAutoCorrect)) { |
| 182 UMA_HISTOGRAM_COUNTS("SpellCheck.api.autocorrect", word.length()); | 181 UMA_HISTOGRAM_COUNTS("SpellCheck.api.autocorrect", word.length()); |
| 183 return spellcheck_->GetAutoCorrectionWord(word, routing_id()); | 182 return spellcheck_->GetAutoCorrectionWord(word, routing_id()); |
| 184 } | 183 } |
| 185 return base::string16(); | 184 return base::string16(); |
| 186 } | 185 } |
| 187 | 186 |
| 188 void SpellCheckProvider::showSpellingUI(bool show) { | 187 void SpellCheckProvider::showSpellingUI(bool show) { |
| 189 #if defined(OS_MACOSX) | 188 #if defined(USE_PLATFORM_SPELLCHECKER) |
| 190 UMA_HISTOGRAM_BOOLEAN("SpellCheck.api.showUI", show); | 189 UMA_HISTOGRAM_BOOLEAN("SpellCheck.api.showUI", show); |
| 191 Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); | 190 Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); |
| 192 #endif | 191 #endif |
| 193 } | 192 } |
| 194 | 193 |
| 195 bool SpellCheckProvider::isShowingSpellingUI() { | 194 bool SpellCheckProvider::isShowingSpellingUI() { |
| 196 return spelling_panel_visible_; | 195 return spelling_panel_visible_; |
| 197 } | 196 } |
| 198 | 197 |
| 199 void SpellCheckProvider::updateSpellingUIWithMisspelledWord( | 198 void SpellCheckProvider::updateSpellingUIWithMisspelledWord( |
| 200 const WebString& word) { | 199 const WebString& word) { |
| 201 #if defined(OS_MACOSX) | 200 #if defined(USE_PLATFORM_SPELLCHECKER) |
| 202 Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord(routing_id(), | 201 Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord(routing_id(), |
| 203 word)); | 202 word)); |
| 204 #endif | 203 #endif |
| 205 } | 204 } |
| 206 | 205 |
| 207 #if !defined(OS_MACOSX) | 206 #if !defined(USE_PLATFORM_SPELLCHECKER) |
| 208 void SpellCheckProvider::OnRespondSpellingService( | 207 void SpellCheckProvider::OnRespondSpellingService( |
| 209 int identifier, | 208 int identifier, |
| 210 bool succeeded, | 209 bool succeeded, |
| 211 const base::string16& line, | 210 const base::string16& line, |
| 212 const std::vector<SpellCheckResult>& results) { | 211 const std::vector<SpellCheckResult>& results) { |
| 213 WebTextCheckingCompletion* completion = | 212 WebTextCheckingCompletion* completion = |
| 214 text_check_completions_.Lookup(identifier); | 213 text_check_completions_.Lookup(identifier); |
| 215 if (!completion) | 214 if (!completion) |
| 216 return; | 215 return; |
| 217 text_check_completions_.Remove(identifier); | 216 text_check_completions_.Remove(identifier); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 229 0, | 228 0, |
| 230 line, | 229 line, |
| 231 results, | 230 results, |
| 232 &textcheck_results); | 231 &textcheck_results); |
| 233 completion->didFinishCheckingText(textcheck_results); | 232 completion->didFinishCheckingText(textcheck_results); |
| 234 | 233 |
| 235 // Cache the request and the converted results. | 234 // Cache the request and the converted results. |
| 236 last_request_ = line; | 235 last_request_ = line; |
| 237 last_results_.swap(textcheck_results); | 236 last_results_.swap(textcheck_results); |
| 238 } | 237 } |
| 239 #endif | 238 #endif // !USE_PLATFORM_SPELLCHECKER |
| 240 | 239 |
| 241 bool SpellCheckProvider::HasWordCharacters( | 240 bool SpellCheckProvider::HasWordCharacters( |
| 242 const base::string16& text, | 241 const base::string16& text, |
| 243 int index) const { | 242 int index) const { |
| 244 const base::char16* data = text.data(); | 243 const base::char16* data = text.data(); |
| 245 int length = text.length(); | 244 int length = text.length(); |
| 246 while (index < length) { | 245 while (index < length) { |
| 247 uint32 code = 0; | 246 uint32 code = 0; |
| 248 U16_NEXT(data, index, length, code); | 247 U16_NEXT(data, index, length, code); |
| 249 UErrorCode error = U_ZERO_ERROR; | 248 UErrorCode error = U_ZERO_ERROR; |
| 250 if (uscript_getScript(code, &error) != USCRIPT_COMMON) | 249 if (uscript_getScript(code, &error) != USCRIPT_COMMON) |
| 251 return true; | 250 return true; |
| 252 } | 251 } |
| 253 return false; | 252 return false; |
| 254 } | 253 } |
| 255 | 254 |
| 256 #if defined(OS_MACOSX) | 255 #if defined(USE_PLATFORM_SPELLCHECKER) |
| 257 void SpellCheckProvider::OnAdvanceToNextMisspelling() { | 256 void SpellCheckProvider::OnAdvanceToNextMisspelling() { |
| 258 if (!render_view()->GetWebView()) | 257 if (!render_view()->GetWebView()) |
| 259 return; | 258 return; |
| 260 render_view()->GetWebView()->focusedFrame()->executeCommand( | 259 render_view()->GetWebView()->focusedFrame()->executeCommand( |
| 261 WebString::fromUTF8("AdvanceToNextMisspelling")); | 260 WebString::fromUTF8("AdvanceToNextMisspelling")); |
| 262 } | 261 } |
| 263 | 262 |
| 264 void SpellCheckProvider::OnRespondTextCheck( | 263 void SpellCheckProvider::OnRespondTextCheck( |
| 265 int identifier, | 264 int identifier, |
| 266 const std::vector<SpellCheckResult>& results) { | 265 const std::vector<SpellCheckResult>& results) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 286 | 285 |
| 287 void SpellCheckProvider::OnToggleSpellPanel(bool is_currently_visible) { | 286 void SpellCheckProvider::OnToggleSpellPanel(bool is_currently_visible) { |
| 288 if (!render_view()->GetWebView()) | 287 if (!render_view()->GetWebView()) |
| 289 return; | 288 return; |
| 290 // We need to tell the webView whether the spelling panel is visible or not so | 289 // We need to tell the webView whether the spelling panel is visible or not so |
| 291 // that it won't need to make ipc calls later. | 290 // that it won't need to make ipc calls later. |
| 292 spelling_panel_visible_ = is_currently_visible; | 291 spelling_panel_visible_ = is_currently_visible; |
| 293 render_view()->GetWebView()->focusedFrame()->executeCommand( | 292 render_view()->GetWebView()->focusedFrame()->executeCommand( |
| 294 WebString::fromUTF8("ToggleSpellPanel")); | 293 WebString::fromUTF8("ToggleSpellPanel")); |
| 295 } | 294 } |
| 296 #endif | 295 #endif // USE_PLATFORM_SPELLCHECKER |
| 297 | 296 |
| 298 void SpellCheckProvider::EnableSpellcheck(bool enable) { | 297 void SpellCheckProvider::EnableSpellcheck(bool enable) { |
| 299 if (!render_view()->GetWebView()) | 298 if (!render_view()->GetWebView()) |
| 300 return; | 299 return; |
| 301 | 300 |
| 302 WebFrame* frame = render_view()->GetWebView()->focusedFrame(); | 301 WebFrame* frame = render_view()->GetWebView()->focusedFrame(); |
| 303 frame->enableContinuousSpellChecking(enable); | 302 frame->enableContinuousSpellChecking(enable); |
| 304 if (!enable) | 303 if (!enable) |
| 305 frame->removeSpellingMarkers(); | 304 frame->removeSpellingMarkers(); |
| 306 } | 305 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 results[i].length = last_results_[i].length; | 350 results[i].length = last_results_[i].length; |
| 352 results[i].replacement = last_results_[i].replacement; | 351 results[i].replacement = last_results_[i].replacement; |
| 353 } | 352 } |
| 354 completion->didFinishCheckingText(results); | 353 completion->didFinishCheckingText(results); |
| 355 return true; | 354 return true; |
| 356 } | 355 } |
| 357 } | 356 } |
| 358 | 357 |
| 359 return false; | 358 return false; |
| 360 } | 359 } |
| OLD | NEW |