Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: chrome/browser/spellchecker_mac.mm

Issue 6392045: Integrating Mac OS Grammar checker into Chromium. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adopted Carnitas approach, added tests. Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 IO thread.
jam 2011/02/17 18:36:59 nit: you mean FILE?
gmorrita 2011/02/18 01:08:01 Oops, yes. Fixed.
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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 return static_cast<int>(doc_tag); 270 return static_cast<int>(doc_tag);
202 } 271 }
203 272
204 void IgnoreWord(const string16& word) { 273 void IgnoreWord(const string16& word) {
205 [[NSSpellChecker sharedSpellChecker] ignoreWord:base::SysUTF16ToNSString(word) 274 [[NSSpellChecker sharedSpellChecker] ignoreWord:base::SysUTF16ToNSString(word)
206 inSpellDocumentWithTag:last_seen_tag_]; 275 inSpellDocumentWithTag:last_seen_tag_];
207 } 276 }
208 277
209 void CloseDocumentWithTag(int tag) { 278 void CloseDocumentWithTag(int tag) {
210 [[NSSpellChecker sharedSpellChecker] 279 [[NSSpellChecker sharedSpellChecker]
211 closeSpellDocumentWithTag:static_cast<NSInteger>(tag)]; 280 closeSpellDocumentWithTag:static_cast<NSInteger>(tag)];
281 }
282
283 void RequestTextCheck(int route_id,
284 int identifier,
285 int document_tag,
286 const string16& text, BrowserMessageFilter* destination) {
287 BrowserThread::PostTask(
288 BrowserThread::FILE, FROM_HERE,
289 new TextCheckingTask(
290 destination, route_id, identifier, text, document_tag));
212 } 291 }
213 292
214 } // namespace SpellCheckerPlatform 293 } // namespace SpellCheckerPlatform
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698