summaryrefslogtreecommitdiffstats
path: root/chrome/browser/spellchecker/feedback.cc
blob: 4cbdd5e008fb12051d854c0dea278e6cd1992638 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2013 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/browser/spellchecker/feedback.h"

#include <algorithm>
#include <iterator>

namespace spellcheck {

Feedback::Feedback() {
}

Feedback::~Feedback() {
}

Misspelling* Feedback::GetMisspelling(uint32 hash) {
  HashMisspellingMap::iterator it = misspellings_.find(hash);
  if (it == misspellings_.end())
    return NULL;
  return &it->second;
}

void Feedback::FinalizeRemovedMisspellings(
    int renderer_process_id,
    const std::vector<uint32>& remaining_markers) {
  RendererHashesMap::iterator i = renderers_.find(renderer_process_id);
  if (i == renderers_.end() || i->second.empty())
    return;
  HashCollection remaining_set(remaining_markers.begin(),
                               remaining_markers.end());
  std::vector<uint32> removed_markers;
  std::set_difference(i->second.begin(),
                      i->second.end(),
                      remaining_set.begin(),
                      remaining_set.end(),
                      std::back_inserter(removed_markers));
  for (std::vector<uint32>::const_iterator j = removed_markers.begin();
       j != removed_markers.end();
       ++j) {
    HashMisspellingMap::iterator k = misspellings_.find(*j);
    if (k != misspellings_.end() && !k->second.action.IsFinal())
      k->second.action.Finalize();
  }
}

bool Feedback::RendererHasMisspellings(int renderer_process_id) const {
  RendererHashesMap::const_iterator it = renderers_.find(renderer_process_id);
  return it != renderers_.end() && !it->second.empty();
}

std::vector<Misspelling> Feedback::GetMisspellingsInRenderer(
    int renderer_process_id) const {
  std::vector<Misspelling> result;
  RendererHashesMap::const_iterator i = renderers_.find(renderer_process_id);
  if (i == renderers_.end() || i->second.empty())
    return result;
  for (HashCollection::const_iterator j = i->second.begin();
       j != i->second.end();
       ++j) {
    HashMisspellingMap::const_iterator k = misspellings_.find(*j);
    if (k != misspellings_.end())
      result.push_back(k->second);
  }
  return result;
}

void Feedback::EraseFinalizedMisspellings(int renderer_process_id) {
  RendererHashesMap::iterator i = renderers_.find(renderer_process_id);
  if (i == renderers_.end() || i->second.empty())
    return;
  HashCollection pending;
  for (HashCollection::const_iterator j = i->second.begin();
       j != i->second.end();
       ++j) {
    HashMisspellingMap::iterator k = misspellings_.find(*j);
    if (k != misspellings_.end()) {
      if (k->second.action.IsFinal()) {
        text_[k->second.context.substr(k->second.location, k->second.length)]
            .erase(k->first);
        misspellings_.erase(k);
      } else {
        pending.insert(*j);
      }
    }
  }
  i->second.swap(pending);
}

bool Feedback::HasMisspelling(uint32 hash) const {
  return !!misspellings_.count(hash);
}

void Feedback::AddMisspelling(int renderer_process_id,
                              const Misspelling& misspelling) {
  misspellings_[misspelling.hash] = misspelling;
  renderers_[renderer_process_id].insert(misspelling.hash);
  text_[misspelling.context.substr(
      misspelling.location, misspelling.length)].insert(misspelling.hash);
}

bool Feedback::Empty() const {
  return misspellings_.empty();
}

std::vector<int> Feedback::GetRendersWithMisspellings() const {
  std::vector<int> result;
  for (RendererHashesMap::const_iterator it = renderers_.begin();
       it != renderers_.end();
       ++it) {
    if (!it->second.empty())
      result.push_back(it->first);
  }
  return result;
}

void Feedback::FinalizeAllMisspellings() {
  for (HashMisspellingMap::iterator it = misspellings_.begin();
       it != misspellings_.end();
       ++it) {
    if (!it->second.action.IsFinal())
      it->second.action.Finalize();
  }
}

std::vector<Misspelling> Feedback::GetAllMisspellings() const {
  std::vector<Misspelling> result;
  for (HashMisspellingMap::const_iterator it = misspellings_.begin();
       it != misspellings_.end();
       ++it) {
    result.push_back(it->second);
  }
  return result;
}

void Feedback::Clear() {
  misspellings_.clear();
  renderers_.clear();
  text_.clear();
}

const std::set<uint32>& Feedback::FindMisspellings(
    const string16& misspelled_text) {
  return text_[misspelled_text];
}

}  // namespace spellcheck