| OLD | NEW |
| (Empty) |
| 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. | |
| 4 // | |
| 5 // The |Feedback| object keeps track of each instance of user feedback in a map | |
| 6 // |misspellings_|. This is a map from uint32_t hashes to |Misspelling| objects. | |
| 7 // | |
| 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. | |
| 11 // | |
| 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. | |
| 16 | |
| 17 #include "chrome/browser/spellchecker/feedback.h" | |
| 18 | |
| 19 #include <algorithm> | |
| 20 #include <iterator> | |
| 21 #include <limits> | |
| 22 | |
| 23 #include "base/logging.h" | |
| 24 #include "base/stl_util.h" | |
| 25 | |
| 26 namespace spellcheck { | |
| 27 | |
| 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); | |
| 31 } | |
| 32 | |
| 33 Feedback::~Feedback() {} | |
| 34 | |
| 35 Misspelling* Feedback::GetMisspelling(uint32_t hash) { | |
| 36 HashMisspellingMap::iterator misspelling_it = misspellings_.find(hash); | |
| 37 if (misspelling_it == misspellings_.end()) | |
| 38 return nullptr; | |
| 39 return &misspelling_it->second; | |
| 40 } | |
| 41 | |
| 42 void Feedback::FinalizeRemovedMisspellings( | |
| 43 int renderer_process_id, | |
| 44 const std::vector<uint32_t>& remaining_markers) { | |
| 45 RendererHashesMap::iterator renderer_it = | |
| 46 renderers_.find(renderer_process_id); | |
| 47 if (renderer_it == renderers_.end() || renderer_it->second.empty()) | |
| 48 return; | |
| 49 HashCollection& renderer_hashes = renderer_it->second; | |
| 50 HashCollection remaining_hashes(remaining_markers.begin(), | |
| 51 remaining_markers.end()); | |
| 52 std::vector<HashCollection::value_type> removed_hashes = | |
| 53 base::STLSetDifference<std::vector<HashCollection::value_type>>( | |
| 54 renderer_hashes, remaining_hashes); | |
| 55 for (auto hash : removed_hashes) { | |
| 56 HashMisspellingMap::iterator misspelling_it = misspellings_.find(hash); | |
| 57 if (misspelling_it != misspellings_.end() && | |
| 58 !misspelling_it->second.action.IsFinal()) { | |
| 59 misspelling_it->second.action.Finalize(); | |
| 60 } | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 bool Feedback::RendererHasMisspellings(int renderer_process_id) const { | |
| 65 RendererHashesMap::const_iterator renderer_it = | |
| 66 renderers_.find(renderer_process_id); | |
| 67 return renderer_it != renderers_.end() && !renderer_it->second.empty(); | |
| 68 } | |
| 69 | |
| 70 std::vector<Misspelling> Feedback::GetMisspellingsInRenderer( | |
| 71 int renderer_process_id) const { | |
| 72 std::vector<Misspelling> misspellings_in_renderer; | |
| 73 RendererHashesMap::const_iterator renderer_it = | |
| 74 renderers_.find(renderer_process_id); | |
| 75 if (renderer_it == renderers_.end() || renderer_it->second.empty()) | |
| 76 return misspellings_in_renderer; | |
| 77 const HashCollection& renderer_hashes = renderer_it->second; | |
| 78 for (HashCollection::const_iterator hash_it = renderer_hashes.begin(); | |
| 79 hash_it != renderer_hashes.end(); ++hash_it) { | |
| 80 HashMisspellingMap::const_iterator misspelling_it = | |
| 81 misspellings_.find(*hash_it); | |
| 82 if (misspelling_it != misspellings_.end()) | |
| 83 misspellings_in_renderer.push_back(misspelling_it->second); | |
| 84 } | |
| 85 return misspellings_in_renderer; | |
| 86 } | |
| 87 | |
| 88 void Feedback::EraseFinalizedMisspellings(int renderer_process_id) { | |
| 89 RendererHashesMap::iterator renderer_it = | |
| 90 renderers_.find(renderer_process_id); | |
| 91 if (renderer_it == renderers_.end()) | |
| 92 return; | |
| 93 HashCollection& renderer_hashes = renderer_it->second; | |
| 94 for (HashCollection::const_iterator hash_it = renderer_hashes.begin(); | |
| 95 hash_it != renderer_hashes.end();) { | |
| 96 HashMisspellingMap::iterator misspelling_it = misspellings_.find(*hash_it); | |
| 97 HashCollection::iterator erasable_hash_it = hash_it; | |
| 98 ++hash_it; | |
| 99 if (misspelling_it == misspellings_.end()) | |
| 100 continue; | |
| 101 const Misspelling& misspelling = misspelling_it->second; | |
| 102 if (!misspelling.action.IsFinal()) | |
| 103 continue; | |
| 104 renderer_hashes.erase(erasable_hash_it); | |
| 105 text_[GetMisspelledString(misspelling)].erase(misspelling.hash); | |
| 106 size_t approximate_size = ApproximateSerializedSize(misspelling_it->second); | |
| 107 // Prevent underlfow. | |
| 108 if (total_text_size_ >= approximate_size) | |
| 109 total_text_size_ -= approximate_size; | |
| 110 else | |
| 111 total_text_size_ = 0; | |
| 112 misspellings_.erase(misspelling_it); | |
| 113 } | |
| 114 if (renderer_hashes.empty()) | |
| 115 renderers_.erase(renderer_it); | |
| 116 } | |
| 117 | |
| 118 bool Feedback::HasMisspelling(uint32_t hash) const { | |
| 119 return !!misspellings_.count(hash); | |
| 120 } | |
| 121 | |
| 122 void Feedback::AddMisspelling(int renderer_process_id, | |
| 123 const Misspelling& misspelling) { | |
| 124 HashMisspellingMap::iterator misspelling_it = | |
| 125 misspellings_.find(misspelling.hash); | |
| 126 if (misspelling_it != misspellings_.end()) { | |
| 127 const Misspelling& existing_misspelling = misspelling_it->second; | |
| 128 text_[GetMisspelledString(existing_misspelling)].erase(misspelling.hash); | |
| 129 for (RendererHashesMap::iterator renderer_it = renderers_.begin(); | |
| 130 renderer_it != renderers_.end();) { | |
| 131 HashCollection& renderer_hashes = renderer_it->second; | |
| 132 RendererHashesMap::iterator erasable_renderer_it = renderer_it; | |
| 133 ++renderer_it; | |
| 134 renderer_hashes.erase(misspelling.hash); | |
| 135 if (renderer_hashes.empty()) | |
| 136 renderers_.erase(erasable_renderer_it); | |
| 137 } | |
| 138 } else { | |
| 139 size_t approximate_size = ApproximateSerializedSize(misspelling); | |
| 140 // Prevent overflow. | |
| 141 if (total_text_size_ <= | |
| 142 std::numeric_limits<size_t>::max() - approximate_size) { | |
| 143 total_text_size_ += approximate_size; | |
| 144 } | |
| 145 if (total_text_size_ >= max_total_text_size_) | |
| 146 return; | |
| 147 } | |
| 148 misspellings_[misspelling.hash] = misspelling; | |
| 149 text_[GetMisspelledString(misspelling)].insert(misspelling.hash); | |
| 150 renderers_[renderer_process_id].insert(misspelling.hash); | |
| 151 } | |
| 152 | |
| 153 bool Feedback::Empty() const { | |
| 154 return misspellings_.empty(); | |
| 155 } | |
| 156 | |
| 157 std::vector<int> Feedback::GetRendersWithMisspellings() const { | |
| 158 std::vector<int> renderers_with_misspellings; | |
| 159 for (const auto& renderer : renderers_) { | |
| 160 if (!renderer.second.empty()) | |
| 161 renderers_with_misspellings.push_back(renderer.first); | |
| 162 } | |
| 163 return renderers_with_misspellings; | |
| 164 } | |
| 165 | |
| 166 void Feedback::FinalizeAllMisspellings() { | |
| 167 for (auto& misspelling : misspellings_) { | |
| 168 if (!misspelling.second.action.IsFinal()) | |
| 169 misspelling.second.action.Finalize(); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 std::vector<Misspelling> Feedback::GetAllMisspellings() const { | |
| 174 std::vector<Misspelling> all_misspellings; | |
| 175 for (const auto& misspelling : misspellings_) | |
| 176 all_misspellings.push_back(misspelling.second); | |
| 177 return all_misspellings; | |
| 178 } | |
| 179 | |
| 180 void Feedback::Clear() { | |
| 181 total_text_size_ = 0; | |
| 182 misspellings_.clear(); | |
| 183 text_.clear(); | |
| 184 renderers_.clear(); | |
| 185 } | |
| 186 | |
| 187 const std::set<uint32_t>& Feedback::FindMisspellings( | |
| 188 const base::string16& misspelled_text) const { | |
| 189 const TextHashesMap::const_iterator text_it = text_.find(misspelled_text); | |
| 190 return text_it == text_.end() ? empty_hash_collection_ : text_it->second; | |
| 191 } | |
| 192 | |
| 193 } // namespace spellcheck | |
| OLD | NEW |