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.
4
5#include "chrome/renderer/spellchecker/custom_dictionary_engine.h"
6
7#include "base/logging.h"
8#include "base/strings/utf_string_conversions.h"
9
10CustomDictionaryEngine::CustomDictionaryEngine() {
11}
12
13CustomDictionaryEngine::~CustomDictionaryEngine() {
14}
15
16void CustomDictionaryEngine::Init(const std::set<std::string>& custom_words) {
17  // SpellingMenuOberver calls UTF16ToUTF8(word) to convert words for storage,
18  // synchronization, and use in the custom dictionary engine. Since
19  // (UTF8ToUTF16(UTF16ToUTF8(word)) == word) holds, the engine does not need to
20  // normalize the strings.
21  for (std::set<std::string>::const_iterator it = custom_words.begin();
22       it != custom_words.end();
23       ++it) {
24    dictionary_.insert(base::UTF8ToUTF16(*it));
25  }
26}
27
28void CustomDictionaryEngine::OnCustomDictionaryChanged(
29    const std::vector<std::string>& words_added,
30    const std::vector<std::string>& words_removed) {
31  for (std::vector<std::string>::const_iterator it = words_added.begin();
32       it != words_added.end();
33       ++it) {
34    dictionary_.insert(base::UTF8ToUTF16(*it));
35  }
36  for (std::vector<std::string>::const_iterator it = words_removed.begin();
37       it != words_removed.end();
38       ++it) {
39    dictionary_.erase(base::UTF8ToUTF16(*it));
40  }
41}
42
43bool CustomDictionaryEngine::SpellCheckWord(
44    const base::string16& text,
45    int misspelling_start,
46    int misspelling_len) {
47  // The text to be checked is empty on OSX(async) right now.
48  // TODO(groby): Fix as part of async hook-up. (http://crbug.com/178241)
49  return
50      misspelling_start >= 0 &&
51      misspelling_len > 0 &&
52      size_t(misspelling_start + misspelling_len) <= text.length() &&
53      dictionary_.count(text.substr(misspelling_start, misspelling_len)) > 0;
54}
55