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
3 // found in the LICENSE file.
5 #include "chrome/renderer/spellchecker/custom_dictionary_engine.h"
7 #include "base/strings/utf_string_conversions.h"
9 CustomDictionaryEngine::CustomDictionaryEngine() {
12 CustomDictionaryEngine::~CustomDictionaryEngine() {
15 void CustomDictionaryEngine::Init(const std::set
<std::string
>& custom_words
) {
16 // SpellingMenuOberver calls UTF16ToUTF8(word) to convert words for storage,
17 // synchronization, and use in the custom dictionary engine. Since
18 // (UTF8ToUTF16(UTF16ToUTF8(word)) == word) holds, the engine does not need to
19 // normalize the strings.
20 for (const std::string
& word
: custom_words
)
21 dictionary_
.insert(base::UTF8ToUTF16(word
));
24 void CustomDictionaryEngine::OnCustomDictionaryChanged(
25 const std::set
<std::string
>& words_added
,
26 const std::set
<std::string
>& words_removed
) {
27 for (const std::string
& word
: words_added
)
28 dictionary_
.insert(base::UTF8ToUTF16(word
));
30 for (const std::string
& word
: words_removed
)
31 dictionary_
.erase(base::UTF8ToUTF16(word
));
34 bool CustomDictionaryEngine::SpellCheckWord(
35 const base::string16
& text
,
36 int misspelling_start
,
37 int misspelling_len
) {
38 // The text to be checked is empty on OSX(async) right now.
39 // TODO(groby): Fix as part of async hook-up. (http://crbug.com/178241)
41 misspelling_start
>= 0 &&
42 misspelling_len
> 0 &&
43 size_t(misspelling_start
+ misspelling_len
) <= text
.length() &&
44 dictionary_
.count(text
.substr(misspelling_start
, misspelling_len
)) > 0;