1 // Copyright (c) 2013 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 // The |Feedback| object keeps track of each instance of user feedback in a map
6 // |misspellings_|. This is a map from uint32 hashes to |Misspelling| objects.
8 // Each misspelling should be present in only one renderer process. The
9 // |Feedback| objects keeps track of misspelling-renderer relationship in the
10 // |renderers_| map of renderer process identifiers to a set of hashes.
12 // When the user adds a misspelling to their custom dictionary, all of the
13 // |Misspelling| objects with the same misspelled string are updated. The
14 // |Feedback| object facilitates efficient access to these misspellings through
15 // a |text_| map of misspelled strings to a set of hashes.
17 #include "chrome/browser/spellchecker/feedback.h"
23 #include "base/logging.h"
24 #include "base/stl_util.h"
26 namespace spellcheck
{
28 Feedback::Feedback(size_t max_total_text_size
)
29 : max_total_text_size_(max_total_text_size
), total_text_size_(0) {
30 DCHECK_GE(max_total_text_size
, 1024U);
33 Feedback::~Feedback() {}
35 Misspelling
* Feedback::GetMisspelling(uint32 hash
) {
36 HashMisspellingMap::iterator misspelling_it
= misspellings_
.find(hash
);
37 if (misspelling_it
== misspellings_
.end())
39 return &misspelling_it
->second
;
42 void Feedback::FinalizeRemovedMisspellings(
43 int renderer_process_id
,
44 const std::vector
<uint32
>& remaining_markers
) {
45 RendererHashesMap::iterator renderer_it
=
46 renderers_
.find(renderer_process_id
);
47 if (renderer_it
== renderers_
.end() || renderer_it
->second
.empty())
49 HashCollection
& renderer_hashes
= renderer_it
->second
;
50 HashCollection
remaining_hashes(remaining_markers
.begin(),
51 remaining_markers
.end());
52 std::vector
<uint32
> removed_hashes
=
53 base::STLSetDifference
<std::vector
<uint32
>>(renderer_hashes
,
55 for (std::vector
<uint32
>::const_iterator hash_it
= removed_hashes
.begin();
56 hash_it
!= removed_hashes
.end(); ++hash_it
) {
57 HashMisspellingMap::iterator misspelling_it
= misspellings_
.find(*hash_it
);
58 if (misspelling_it
!= misspellings_
.end() &&
59 !misspelling_it
->second
.action
.IsFinal()) {
60 misspelling_it
->second
.action
.Finalize();
65 bool Feedback::RendererHasMisspellings(int renderer_process_id
) const {
66 RendererHashesMap::const_iterator renderer_it
=
67 renderers_
.find(renderer_process_id
);
68 return renderer_it
!= renderers_
.end() && !renderer_it
->second
.empty();
71 std::vector
<Misspelling
> Feedback::GetMisspellingsInRenderer(
72 int renderer_process_id
) const {
73 std::vector
<Misspelling
> misspellings_in_renderer
;
74 RendererHashesMap::const_iterator renderer_it
=
75 renderers_
.find(renderer_process_id
);
76 if (renderer_it
== renderers_
.end() || renderer_it
->second
.empty())
77 return misspellings_in_renderer
;
78 const HashCollection
& renderer_hashes
= renderer_it
->second
;
79 for (HashCollection::const_iterator hash_it
= renderer_hashes
.begin();
80 hash_it
!= renderer_hashes
.end(); ++hash_it
) {
81 HashMisspellingMap::const_iterator misspelling_it
=
82 misspellings_
.find(*hash_it
);
83 if (misspelling_it
!= misspellings_
.end())
84 misspellings_in_renderer
.push_back(misspelling_it
->second
);
86 return misspellings_in_renderer
;
89 void Feedback::EraseFinalizedMisspellings(int renderer_process_id
) {
90 RendererHashesMap::iterator renderer_it
=
91 renderers_
.find(renderer_process_id
);
92 if (renderer_it
== renderers_
.end())
94 HashCollection
& renderer_hashes
= renderer_it
->second
;
95 for (HashCollection::const_iterator hash_it
= renderer_hashes
.begin();
96 hash_it
!= renderer_hashes
.end();) {
97 HashMisspellingMap::iterator misspelling_it
= misspellings_
.find(*hash_it
);
98 HashCollection::iterator erasable_hash_it
= hash_it
;
100 if (misspelling_it
== misspellings_
.end())
102 const Misspelling
& misspelling
= misspelling_it
->second
;
103 if (!misspelling
.action
.IsFinal())
105 renderer_hashes
.erase(erasable_hash_it
);
106 text_
[GetMisspelledString(misspelling
)].erase(misspelling
.hash
);
107 size_t approximate_size
= ApproximateSerializedSize(misspelling_it
->second
);
108 // Prevent underlfow.
109 if (total_text_size_
>= approximate_size
)
110 total_text_size_
-= approximate_size
;
112 total_text_size_
= 0;
113 misspellings_
.erase(misspelling_it
);
115 if (renderer_hashes
.empty())
116 renderers_
.erase(renderer_it
);
119 bool Feedback::HasMisspelling(uint32 hash
) const {
120 return !!misspellings_
.count(hash
);
123 void Feedback::AddMisspelling(int renderer_process_id
,
124 const Misspelling
& misspelling
) {
125 HashMisspellingMap::iterator misspelling_it
=
126 misspellings_
.find(misspelling
.hash
);
127 if (misspelling_it
!= misspellings_
.end()) {
128 const Misspelling
& existing_misspelling
= misspelling_it
->second
;
129 text_
[GetMisspelledString(existing_misspelling
)].erase(misspelling
.hash
);
130 for (RendererHashesMap::iterator renderer_it
= renderers_
.begin();
131 renderer_it
!= renderers_
.end();) {
132 HashCollection
& renderer_hashes
= renderer_it
->second
;
133 RendererHashesMap::iterator erasable_renderer_it
= renderer_it
;
135 renderer_hashes
.erase(misspelling
.hash
);
136 if (renderer_hashes
.empty())
137 renderers_
.erase(erasable_renderer_it
);
140 size_t approximate_size
= ApproximateSerializedSize(misspelling
);
142 if (total_text_size_
<=
143 std::numeric_limits
<size_t>::max() - approximate_size
) {
144 total_text_size_
+= approximate_size
;
146 if (total_text_size_
>= max_total_text_size_
)
149 misspellings_
[misspelling
.hash
] = misspelling
;
150 text_
[GetMisspelledString(misspelling
)].insert(misspelling
.hash
);
151 renderers_
[renderer_process_id
].insert(misspelling
.hash
);
154 bool Feedback::Empty() const {
155 return misspellings_
.empty();
158 std::vector
<int> Feedback::GetRendersWithMisspellings() const {
159 std::vector
<int> renderers_with_misspellings
;
160 for (RendererHashesMap::const_iterator renderer_it
= renderers_
.begin();
161 renderer_it
!= renderers_
.end(); ++renderer_it
) {
162 if (!renderer_it
->second
.empty())
163 renderers_with_misspellings
.push_back(renderer_it
->first
);
165 return renderers_with_misspellings
;
168 void Feedback::FinalizeAllMisspellings() {
169 for (HashMisspellingMap::iterator misspelling_it
= misspellings_
.begin();
170 misspelling_it
!= misspellings_
.end(); ++misspelling_it
) {
171 if (!misspelling_it
->second
.action
.IsFinal())
172 misspelling_it
->second
.action
.Finalize();
176 std::vector
<Misspelling
> Feedback::GetAllMisspellings() const {
177 std::vector
<Misspelling
> all_misspellings
;
178 for (HashMisspellingMap::const_iterator misspelling_it
=
179 misspellings_
.begin();
180 misspelling_it
!= misspellings_
.end(); ++misspelling_it
) {
181 all_misspellings
.push_back(misspelling_it
->second
);
183 return all_misspellings
;
186 void Feedback::Clear() {
187 total_text_size_
= 0;
188 misspellings_
.clear();
193 const std::set
<uint32
>& Feedback::FindMisspellings(
194 const base::string16
& misspelled_text
) const {
195 const TextHashesMap::const_iterator text_it
= text_
.find(misspelled_text
);
196 return text_it
== text_
.end() ? empty_hash_collection_
: text_it
->second
;
199 } // namespace spellcheck