summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/spellchecker/custom_dictionary_engine.cc
blob: a6dca91e86f7a87d7b0b11dae9771f80bf656210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/spellchecker/custom_dictionary_engine.h"

#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"

CustomDictionaryEngine::CustomDictionaryEngine() {
}

CustomDictionaryEngine::~CustomDictionaryEngine() {
}

void CustomDictionaryEngine::Init(const std::set<std::string>& custom_words) {
  // SpellingMenuOberver calls UTF16ToUTF8(word) to convert words for storage,
  // synchronization, and use in the custom dictionary engine. Since
  // (UTF8ToUTF16(UTF16ToUTF8(word)) == word) holds, the engine does not need to
  // normalize the strings.
  for (std::set<std::string>::const_iterator it = custom_words.begin();
       it != custom_words.end();
       ++it) {
    dictionary_.insert(UTF8ToUTF16(*it));
  }
}

void CustomDictionaryEngine::OnCustomDictionaryChanged(
    const std::vector<std::string>& words_added,
    const std::vector<std::string>& words_removed) {
  for (std::vector<std::string>::const_iterator it = words_added.begin();
       it != words_added.end();
       ++it) {
    dictionary_.insert(UTF8ToUTF16(*it));
  }
  for (std::vector<std::string>::const_iterator it = words_removed.begin();
       it != words_removed.end();
       ++it) {
    dictionary_.erase(UTF8ToUTF16(*it));
  }
}

bool CustomDictionaryEngine::SpellCheckWord(
    const char16* text,
    int misspelling_start,
    int misspelling_len) {
  DCHECK(text);
  string16 text16(text);

  // The text to be checked is empty on OSX(async) right now.
  // TODO(groby): Fix as part of async hook-up. (http://crbug.com/178241)
  if (text16.empty())
    return false;
  DCHECK(text16.length() >= size_t(misspelling_start + misspelling_len));
  return misspelling_start >= 0 &&
      misspelling_len > 0 &&
      dictionary_.count(text16.substr(misspelling_start, misspelling_len)) > 0;
}