| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 // This file implements the interface defined in spellchecker_platform_engine.h | 5 // This file implements the interface defined in spellchecker_platform_engine.h |
| 6 // for the OS X platform. | 6 // for the OS X platform. |
| 7 | 7 |
| 8 #include "chrome/browser/spellchecker_platform_engine.h" | 8 #include "chrome/browser/spellchecker_platform_engine.h" |
| 9 | 9 |
| 10 #import <Cocoa/Cocoa.h> | 10 #import <Cocoa/Cocoa.h> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/task.h" |
| 14 #include "base/time.h" | 15 #include "base/time.h" |
| 15 #include "base/sys_string_conversions.h" | 16 #include "base/sys_string_conversions.h" |
| 17 #include "chrome/browser/browser_thread.h" |
| 18 #include "chrome/browser/browser_message_filter.h" |
| 19 #include "chrome/common/render_messages.h" |
| 16 #include "chrome/common/spellcheck_common.h" | 20 #include "chrome/common/spellcheck_common.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult
.h" |
| 17 | 22 |
| 18 using base::TimeTicks; | 23 using base::TimeTicks; |
| 19 namespace { | 24 namespace { |
| 20 // The number of characters in the first part of the language code. | 25 // The number of characters in the first part of the language code. |
| 21 const unsigned int kShortLanguageCodeSize = 2; | 26 const unsigned int kShortLanguageCodeSize = 2; |
| 22 | 27 |
| 28 // TextCheckingTask is reserved for spell checking against large size |
| 29 // of text, which possible contains multiple paragrpahs. Checking |
| 30 // that size of text might take time, and should be done as a task on |
| 31 // the FILE thread. |
| 32 // |
| 33 // The result of the check is returned back as a |
| 34 // ViewMsg_SpellChecker_RespondTextCheck message. |
| 35 class TextCheckingTask : public Task { |
| 36 public: |
| 37 TextCheckingTask(BrowserMessageFilter* destination, |
| 38 int route_id, |
| 39 int identifier, |
| 40 const string16& text, |
| 41 int document_tag) |
| 42 : destination_(destination), |
| 43 route_id_(route_id), |
| 44 identifier_(identifier), |
| 45 text_(text), |
| 46 document_tag_(document_tag) { |
| 47 } |
| 48 |
| 49 virtual void Run() { |
| 50 // TODO(morrita): Use [NSSpellChecker requestCheckingOfString] |
| 51 // when the build target goes up to 10.6 |
| 52 std::vector<WebKit::WebTextCheckingResult> check_results; |
| 53 NSString* text_to_check = base::SysUTF16ToNSString(text_); |
| 54 size_t starting_at = 0; |
| 55 while (starting_at < text_.size()) { |
| 56 NSRange range = [[NSSpellChecker sharedSpellChecker] |
| 57 checkSpellingOfString:text_to_check |
| 58 startingAt:starting_at |
| 59 language:nil |
| 60 wrap:NO |
| 61 inSpellDocumentWithTag:document_tag_ |
| 62 wordCount:NULL]; |
| 63 if (range.length == 0) |
| 64 break; |
| 65 check_results.push_back(WebKit::WebTextCheckingResult( |
| 66 WebKit::WebTextCheckingResult::ErrorSpelling, |
| 67 range.location, |
| 68 range.length)); |
| 69 starting_at = range.location + range.length; |
| 70 } |
| 71 |
| 72 BrowserThread::PostTask( |
| 73 BrowserThread::IO, |
| 74 FROM_HERE, |
| 75 NewRunnableMethod( |
| 76 destination_.get(), |
| 77 &BrowserMessageFilter::Send, |
| 78 new ViewMsg_SpellChecker_RespondTextCheck(route_id_, |
| 79 identifier_, |
| 80 document_tag_, |
| 81 check_results))); |
| 82 } |
| 83 |
| 84 private: |
| 85 scoped_refptr<BrowserMessageFilter> destination_; |
| 86 int route_id_; |
| 87 int identifier_; |
| 88 string16 text_; |
| 89 int document_tag_; |
| 90 }; |
| 91 |
| 23 // A private utility function to convert hunspell language codes to OS X | 92 // A private utility function to convert hunspell language codes to OS X |
| 24 // language codes. | 93 // language codes. |
| 25 NSString* ConvertLanguageCodeToMac(const std::string& hunspell_lang_code) { | 94 NSString* ConvertLanguageCodeToMac(const std::string& hunspell_lang_code) { |
| 26 NSString* whole_code = base::SysUTF8ToNSString(hunspell_lang_code); | 95 NSString* whole_code = base::SysUTF8ToNSString(hunspell_lang_code); |
| 27 | 96 |
| 28 if ([whole_code length] > kShortLanguageCodeSize) { | 97 if ([whole_code length] > kShortLanguageCodeSize) { |
| 29 NSString* lang_code = [whole_code | 98 NSString* lang_code = [whole_code |
| 30 substringToIndex:kShortLanguageCodeSize]; | 99 substringToIndex:kShortLanguageCodeSize]; |
| 31 // Add 1 here to skip the underscore. | 100 // Add 1 here to skip the underscore. |
| 32 NSString* region_code = [whole_code | 101 NSString* region_code = [whole_code |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 return static_cast<int>(doc_tag); | 275 return static_cast<int>(doc_tag); |
| 207 } | 276 } |
| 208 | 277 |
| 209 void IgnoreWord(const string16& word) { | 278 void IgnoreWord(const string16& word) { |
| 210 [[NSSpellChecker sharedSpellChecker] ignoreWord:base::SysUTF16ToNSString(word) | 279 [[NSSpellChecker sharedSpellChecker] ignoreWord:base::SysUTF16ToNSString(word) |
| 211 inSpellDocumentWithTag:last_seen_tag_]; | 280 inSpellDocumentWithTag:last_seen_tag_]; |
| 212 } | 281 } |
| 213 | 282 |
| 214 void CloseDocumentWithTag(int tag) { | 283 void CloseDocumentWithTag(int tag) { |
| 215 [[NSSpellChecker sharedSpellChecker] | 284 [[NSSpellChecker sharedSpellChecker] |
| 216 closeSpellDocumentWithTag:static_cast<NSInteger>(tag)]; | 285 closeSpellDocumentWithTag:static_cast<NSInteger>(tag)]; |
| 286 } |
| 287 |
| 288 void RequestTextCheck(int route_id, |
| 289 int identifier, |
| 290 int document_tag, |
| 291 const string16& text, BrowserMessageFilter* destination) { |
| 292 BrowserThread::PostTask( |
| 293 BrowserThread::FILE, FROM_HERE, |
| 294 new TextCheckingTask( |
| 295 destination, route_id, identifier, text, document_tag)); |
| 217 } | 296 } |
| 218 | 297 |
| 219 } // namespace SpellCheckerPlatform | 298 } // namespace SpellCheckerPlatform |
| OLD | NEW |