Chromium Code Reviews| 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 class TextCheckingTask : public Task { | |
|
Hironori Bono
2011/02/10 11:02:56
nit: I would be great if this class has a class co
pink (ping after 24hrs)
2011/02/11 17:33:40
100% agreed. not a nit at all.
gmorrita
2011/02/17 11:39:44
Added an description.
| |
| 29 public: | |
| 30 TextCheckingTask(BrowserMessageFilter* destination, | |
| 31 int route_id, | |
| 32 int identifier, | |
| 33 const string16& text, | |
| 34 int document_tag) | |
| 35 : destination_(destination_), | |
| 36 route_id_(route_id), | |
| 37 identifier_(identifier), | |
| 38 text_(text), | |
| 39 document_tag_(document_tag) { | |
| 40 } | |
| 41 | |
| 42 virtual void Run() { | |
| 43 // TODO(morrita): Use [NSSpellChecker requestCheckingOfString] | |
| 44 // when the build target goes upto 10.6 | |
|
pink (ping after 24hrs)
2011/02/11 17:33:40
s/upto/up to.
Can you do a respondsToSelector: ch
gmorrita
2011/02/17 11:39:44
Fixed.
| |
| 45 std::vector<WebKit::WebTextCheckingResult> check_results; | |
| 46 NSString* text_to_check = base::SysUTF16ToNSString(text_); | |
| 47 size_t starting_at = 0; | |
| 48 while (starting_at < text_.size()) { | |
| 49 NSRange range = [[NSSpellChecker sharedSpellChecker] | |
| 50 checkSpellingOfString:text_to_check | |
| 51 startingAt:starting_at | |
|
pink (ping after 24hrs)
2011/02/11 17:33:40
line up colons
gmorrita
2011/02/17 11:39:44
Done.
| |
| 52 language:nil wrap:NO | |
| 53 inSpellDocumentWithTag:document_tag_ | |
| 54 wordCount:NULL]; | |
| 55 if (0 == range.length) | |
|
pink (ping after 24hrs)
2011/02/11 17:33:40
if (range.length == 0). The constant before is muc
gmorrita
2011/02/17 11:39:44
Done.
| |
| 56 break; | |
| 57 check_results.push_back(WebKit::WebTextCheckingResult( | |
| 58 WebKit::WebTextCheckingResult::ErrorSpelling, | |
| 59 range.location, | |
| 60 range.length)); | |
| 61 starting_at = range.location + range.length; | |
| 62 } | |
| 63 | |
| 64 BrowserThread::PostTask( | |
| 65 BrowserThread::UI, FROM_HERE, NewRunnableMethod( | |
| 66 destination_, &BrowserMessageFilter::Send, | |
| 67 new ViewMsg_SpellChecker_RespondTextCheck(route_id_, | |
| 68 identifier_, | |
| 69 document_tag_, | |
| 70 check_results))); | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 BrowserMessageFilter* destination_; | |
|
pink (ping after 24hrs)
2011/02/11 17:33:40
comment this is a weak pointer.
gmorrita
2011/02/17 11:39:44
Noticed we have to own it to avoiding the dangling
| |
| 75 int route_id_; | |
| 76 int identifier_; | |
| 77 string16 text_; | |
| 78 int document_tag_; | |
| 79 }; | |
| 80 | |
| 23 // A private utility function to convert hunspell language codes to os x | 81 // A private utility function to convert hunspell language codes to os x |
| 24 // language codes. | 82 // language codes. |
| 25 NSString* ConvertLanguageCodeToMac(const std::string& hunspell_lang_code) { | 83 NSString* ConvertLanguageCodeToMac(const std::string& hunspell_lang_code) { |
| 26 NSString* whole_code = base::SysUTF8ToNSString(hunspell_lang_code); | 84 NSString* whole_code = base::SysUTF8ToNSString(hunspell_lang_code); |
| 27 | 85 |
| 28 if ([whole_code length] > kShortLanguageCodeSize) { | 86 if ([whole_code length] > kShortLanguageCodeSize) { |
| 29 NSString* lang_code = [whole_code | 87 NSString* lang_code = [whole_code |
| 30 substringToIndex:kShortLanguageCodeSize]; | 88 substringToIndex:kShortLanguageCodeSize]; |
| 31 // Add 1 here to skip the underscore. | 89 // Add 1 here to skip the underscore. |
| 32 NSString* region_code = [whole_code | 90 NSString* region_code = [whole_code |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 return static_cast<int>(doc_tag); | 259 return static_cast<int>(doc_tag); |
| 202 } | 260 } |
| 203 | 261 |
| 204 void IgnoreWord(const string16& word) { | 262 void IgnoreWord(const string16& word) { |
| 205 [[NSSpellChecker sharedSpellChecker] ignoreWord:base::SysUTF16ToNSString(word) | 263 [[NSSpellChecker sharedSpellChecker] ignoreWord:base::SysUTF16ToNSString(word) |
| 206 inSpellDocumentWithTag:last_seen_tag_]; | 264 inSpellDocumentWithTag:last_seen_tag_]; |
| 207 } | 265 } |
| 208 | 266 |
| 209 void CloseDocumentWithTag(int tag) { | 267 void CloseDocumentWithTag(int tag) { |
| 210 [[NSSpellChecker sharedSpellChecker] | 268 [[NSSpellChecker sharedSpellChecker] |
| 211 closeSpellDocumentWithTag:static_cast<NSInteger>(tag)]; | 269 closeSpellDocumentWithTag:static_cast<NSInteger>(tag)]; |
| 270 } | |
| 271 | |
| 272 void RequestTextCheck(int route_id, | |
| 273 int identifier, | |
| 274 int document_tag, | |
| 275 const string16& text, BrowserMessageFilter* destination) { | |
| 276 BrowserThread::PostTask( | |
| 277 BrowserThread::IO, FROM_HERE, | |
| 278 new TextCheckingTask( | |
| 279 destination, route_id, identifier, text, document_tag)); | |
| 212 } | 280 } |
| 213 | 281 |
| 214 } // namespace SpellCheckerPlatform | 282 } // namespace SpellCheckerPlatform |
| OLD | NEW |