| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 if (render_view) { // NULL in unit tests. | 44 if (render_view) { // NULL in unit tests. |
| 45 render_view->GetWebView()->setSpellCheckClient(this); | 45 render_view->GetWebView()->setSpellCheckClient(this); |
| 46 EnableSpellcheck(spellcheck_->is_spellcheck_enabled()); | 46 EnableSpellcheck(spellcheck_->is_spellcheck_enabled()); |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 | 49 |
| 50 SpellCheckProvider::~SpellCheckProvider() { | 50 SpellCheckProvider::~SpellCheckProvider() { |
| 51 } | 51 } |
| 52 | 52 |
| 53 void SpellCheckProvider::RequestTextChecking( | 53 void SpellCheckProvider::RequestTextChecking( |
| 54 const string16& text, | 54 const base::string16& text, |
| 55 WebTextCheckingCompletion* completion, | 55 WebTextCheckingCompletion* completion, |
| 56 const std::vector<SpellCheckMarker>& markers) { | 56 const std::vector<SpellCheckMarker>& markers) { |
| 57 // Ignore invalid requests. | 57 // Ignore invalid requests. |
| 58 if (text.empty() || !HasWordCharacters(text, 0)) { | 58 if (text.empty() || !HasWordCharacters(text, 0)) { |
| 59 completion->didCancelCheckingText(); | 59 completion->didCancelCheckingText(); |
| 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)) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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, |
| 81 markers)); | 81 markers)); |
| 82 #else | 82 #else |
| 83 Send(new SpellCheckHostMsg_CallSpellingService( | 83 Send(new SpellCheckHostMsg_CallSpellingService( |
| 84 routing_id(), | 84 routing_id(), |
| 85 text_check_completions_.Add(completion), | 85 text_check_completions_.Add(completion), |
| 86 string16(text), | 86 base::string16(text), |
| 87 markers)); | 87 markers)); |
| 88 #endif // !OS_MACOSX | 88 #endif // !OS_MACOSX |
| 89 } | 89 } |
| 90 | 90 |
| 91 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { | 91 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { |
| 92 bool handled = true; | 92 bool handled = true; |
| 93 IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) | 93 IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) |
| 94 #if !defined(OS_MACOSX) | 94 #if !defined(OS_MACOSX) |
| 95 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondSpellingService, | 95 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondSpellingService, |
| 96 OnRespondSpellingService) | 96 OnRespondSpellingService) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 122 | 122 |
| 123 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); | 123 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); |
| 124 #endif // OS_MACOSX | 124 #endif // OS_MACOSX |
| 125 } | 125 } |
| 126 | 126 |
| 127 void SpellCheckProvider::spellCheck( | 127 void SpellCheckProvider::spellCheck( |
| 128 const WebString& text, | 128 const WebString& text, |
| 129 int& offset, | 129 int& offset, |
| 130 int& length, | 130 int& length, |
| 131 WebVector<WebString>* optional_suggestions) { | 131 WebVector<WebString>* optional_suggestions) { |
| 132 string16 word(text); | 132 base::string16 word(text); |
| 133 std::vector<string16> suggestions; | 133 std::vector<base::string16> suggestions; |
| 134 spellcheck_->SpellCheckWord( | 134 spellcheck_->SpellCheckWord( |
| 135 word.c_str(), word.size(), routing_id(), | 135 word.c_str(), word.size(), routing_id(), |
| 136 &offset, &length, optional_suggestions ? & suggestions : NULL); | 136 &offset, &length, optional_suggestions ? & suggestions : NULL); |
| 137 if (optional_suggestions) { | 137 if (optional_suggestions) { |
| 138 *optional_suggestions = suggestions; | 138 *optional_suggestions = suggestions; |
| 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. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 RequestTextChecking(text, completion, spellcheck_markers); | 175 RequestTextChecking(text, completion, spellcheck_markers); |
| 176 UMA_HISTOGRAM_COUNTS("SpellCheck.api.async", text.length()); | 176 UMA_HISTOGRAM_COUNTS("SpellCheck.api.async", text.length()); |
| 177 } | 177 } |
| 178 | 178 |
| 179 WebString SpellCheckProvider::autoCorrectWord(const WebString& word) { | 179 WebString SpellCheckProvider::autoCorrectWord(const WebString& word) { |
| 180 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 180 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 181 if (command_line.HasSwitch(switches::kEnableSpellingAutoCorrect)) { | 181 if (command_line.HasSwitch(switches::kEnableSpellingAutoCorrect)) { |
| 182 UMA_HISTOGRAM_COUNTS("SpellCheck.api.autocorrect", word.length()); | 182 UMA_HISTOGRAM_COUNTS("SpellCheck.api.autocorrect", word.length()); |
| 183 return spellcheck_->GetAutoCorrectionWord(word, routing_id()); | 183 return spellcheck_->GetAutoCorrectionWord(word, routing_id()); |
| 184 } | 184 } |
| 185 return string16(); | 185 return base::string16(); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void SpellCheckProvider::showSpellingUI(bool show) { | 188 void SpellCheckProvider::showSpellingUI(bool show) { |
| 189 #if defined(OS_MACOSX) | 189 #if defined(OS_MACOSX) |
| 190 UMA_HISTOGRAM_BOOLEAN("SpellCheck.api.showUI", show); | 190 UMA_HISTOGRAM_BOOLEAN("SpellCheck.api.showUI", show); |
| 191 Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); | 191 Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); |
| 192 #endif | 192 #endif |
| 193 } | 193 } |
| 194 | 194 |
| 195 bool SpellCheckProvider::isShowingSpellingUI() { | 195 bool SpellCheckProvider::isShowingSpellingUI() { |
| 196 return spelling_panel_visible_; | 196 return spelling_panel_visible_; |
| 197 } | 197 } |
| 198 | 198 |
| 199 void SpellCheckProvider::updateSpellingUIWithMisspelledWord( | 199 void SpellCheckProvider::updateSpellingUIWithMisspelledWord( |
| 200 const WebString& word) { | 200 const WebString& word) { |
| 201 #if defined(OS_MACOSX) | 201 #if defined(OS_MACOSX) |
| 202 Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord(routing_id(), | 202 Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord(routing_id(), |
| 203 word)); | 203 word)); |
| 204 #endif | 204 #endif |
| 205 } | 205 } |
| 206 | 206 |
| 207 #if !defined(OS_MACOSX) | 207 #if !defined(OS_MACOSX) |
| 208 void SpellCheckProvider::OnRespondSpellingService( | 208 void SpellCheckProvider::OnRespondSpellingService( |
| 209 int identifier, | 209 int identifier, |
| 210 bool succeeded, | 210 bool succeeded, |
| 211 const string16& line, | 211 const base::string16& line, |
| 212 const std::vector<SpellCheckResult>& results) { | 212 const std::vector<SpellCheckResult>& results) { |
| 213 WebTextCheckingCompletion* completion = | 213 WebTextCheckingCompletion* completion = |
| 214 text_check_completions_.Lookup(identifier); | 214 text_check_completions_.Lookup(identifier); |
| 215 if (!completion) | 215 if (!completion) |
| 216 return; | 216 return; |
| 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); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 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); |
| 238 } | 238 } |
| 239 #endif | 239 #endif |
| 240 | 240 |
| 241 bool SpellCheckProvider::HasWordCharacters( | 241 bool SpellCheckProvider::HasWordCharacters( |
| 242 const string16& text, | 242 const base::string16& text, |
| 243 int index) const { | 243 int index) const { |
| 244 const char16* data = text.data(); | 244 const char16* data = text.data(); |
| 245 int length = text.length(); | 245 int length = text.length(); |
| 246 while (index < length) { | 246 while (index < length) { |
| 247 uint32 code = 0; | 247 uint32 code = 0; |
| 248 U16_NEXT(data, index, length, code); | 248 U16_NEXT(data, index, length, code); |
| 249 UErrorCode error = U_ZERO_ERROR; | 249 UErrorCode error = U_ZERO_ERROR; |
| 250 if (uscript_getScript(code, &error) != USCRIPT_COMMON) | 250 if (uscript_getScript(code, &error) != USCRIPT_COMMON) |
| 251 return true; | 251 return true; |
| 252 } | 252 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 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 blink::WebVector<blink::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 base::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. |
| 285 } | 285 } |
| 286 | 286 |
| 287 void SpellCheckProvider::OnToggleSpellPanel(bool is_currently_visible) { | 287 void SpellCheckProvider::OnToggleSpellPanel(bool is_currently_visible) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 299 if (!render_view()->GetWebView()) | 299 if (!render_view()->GetWebView()) |
| 300 return; | 300 return; |
| 301 | 301 |
| 302 WebFrame* frame = render_view()->GetWebView()->focusedFrame(); | 302 WebFrame* frame = render_view()->GetWebView()->focusedFrame(); |
| 303 frame->enableContinuousSpellChecking(enable); | 303 frame->enableContinuousSpellChecking(enable); |
| 304 if (!enable) | 304 if (!enable) |
| 305 frame->removeSpellingMarkers(); | 305 frame->removeSpellingMarkers(); |
| 306 } | 306 } |
| 307 | 307 |
| 308 bool SpellCheckProvider::SatisfyRequestFromCache( | 308 bool SpellCheckProvider::SatisfyRequestFromCache( |
| 309 const string16& text, | 309 const base::string16& text, |
| 310 WebTextCheckingCompletion* completion) { | 310 WebTextCheckingCompletion* completion) { |
| 311 size_t last_length = last_request_.length(); | 311 size_t last_length = last_request_.length(); |
| 312 | 312 |
| 313 // Send back the |last_results_| if the |last_request_| is a substring of | 313 // Send back the |last_results_| if the |last_request_| is a substring of |
| 314 // |text| and |text| does not have more words to check. Provider cannot cancel | 314 // |text| and |text| does not have more words to check. Provider cannot cancel |
| 315 // the spellcheck request here, because WebKit might have discarded the | 315 // the spellcheck request here, because WebKit might have discarded the |
| 316 // previous spellcheck results and erased the spelling markers in response to | 316 // previous spellcheck results and erased the spelling markers in response to |
| 317 // the user editing the text. | 317 // the user editing the text. |
| 318 string16 request(text); | 318 base::string16 request(text); |
| 319 size_t text_length = request.length(); | 319 size_t text_length = request.length(); |
| 320 if (text_length >= last_length && | 320 if (text_length >= last_length && |
| 321 !request.compare(0, last_length, last_request_)) { | 321 !request.compare(0, last_length, last_request_)) { |
| 322 if (text_length == last_length || !HasWordCharacters(text, last_length)) { | 322 if (text_length == last_length || !HasWordCharacters(text, last_length)) { |
| 323 completion->didFinishCheckingText(last_results_); | 323 completion->didFinishCheckingText(last_results_); |
| 324 return true; | 324 return true; |
| 325 } | 325 } |
| 326 int code = 0; | 326 int code = 0; |
| 327 int length = static_cast<int>(text_length); | 327 int length = static_cast<int>(text_length); |
| 328 U16_PREV(text.data(), 0, length, code); | 328 U16_PREV(text.data(), 0, length, code); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 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 |