| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // The |Feedback| object keeps track of each instance of user feedback in a map | 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. | 6 // |misspellings_|. This is a map from uint32_t hashes to |Misspelling| objects. |
| 7 // | 7 // |
| 8 // Each misspelling should be present in only one renderer process. The | 8 // Each misspelling should be present in only one renderer process. The |
| 9 // |Feedback| objects keeps track of misspelling-renderer relationship in the | 9 // |Feedback| objects keeps track of misspelling-renderer relationship in the |
| 10 // |renderers_| map of renderer process identifiers to a set of hashes. | 10 // |renderers_| map of renderer process identifiers to a set of hashes. |
| 11 // | 11 // |
| 12 // When the user adds a misspelling to their custom dictionary, all of the | 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 | 13 // |Misspelling| objects with the same misspelled string are updated. The |
| 14 // |Feedback| object facilitates efficient access to these misspellings through | 14 // |Feedback| object facilitates efficient access to these misspellings through |
| 15 // a |text_| map of misspelled strings to a set of hashes. | 15 // a |text_| map of misspelled strings to a set of hashes. |
| 16 | 16 |
| 17 #include "chrome/browser/spellchecker/feedback.h" | 17 #include "chrome/browser/spellchecker/feedback.h" |
| 18 | 18 |
| 19 #include <algorithm> | 19 #include <algorithm> |
| 20 #include <iterator> | 20 #include <iterator> |
| 21 #include <limits> | 21 #include <limits> |
| 22 | 22 |
| 23 #include "base/logging.h" | 23 #include "base/logging.h" |
| 24 #include "base/stl_util.h" | 24 #include "base/stl_util.h" |
| 25 | 25 |
| 26 namespace spellcheck { | 26 namespace spellcheck { |
| 27 | 27 |
| 28 Feedback::Feedback(size_t max_total_text_size) | 28 Feedback::Feedback(size_t max_total_text_size) |
| 29 : max_total_text_size_(max_total_text_size), total_text_size_(0) { | 29 : max_total_text_size_(max_total_text_size), total_text_size_(0) { |
| 30 DCHECK_GE(max_total_text_size, 1024U); | 30 DCHECK_GE(max_total_text_size, 1024U); |
| 31 } | 31 } |
| 32 | 32 |
| 33 Feedback::~Feedback() {} | 33 Feedback::~Feedback() {} |
| 34 | 34 |
| 35 Misspelling* Feedback::GetMisspelling(uint32 hash) { | 35 Misspelling* Feedback::GetMisspelling(uint32_t hash) { |
| 36 HashMisspellingMap::iterator misspelling_it = misspellings_.find(hash); | 36 HashMisspellingMap::iterator misspelling_it = misspellings_.find(hash); |
| 37 if (misspelling_it == misspellings_.end()) | 37 if (misspelling_it == misspellings_.end()) |
| 38 return NULL; | 38 return NULL; |
| 39 return &misspelling_it->second; | 39 return &misspelling_it->second; |
| 40 } | 40 } |
| 41 | 41 |
| 42 void Feedback::FinalizeRemovedMisspellings( | 42 void Feedback::FinalizeRemovedMisspellings( |
| 43 int renderer_process_id, | 43 int renderer_process_id, |
| 44 const std::vector<uint32>& remaining_markers) { | 44 const std::vector<uint32_t>& remaining_markers) { |
| 45 RendererHashesMap::iterator renderer_it = | 45 RendererHashesMap::iterator renderer_it = |
| 46 renderers_.find(renderer_process_id); | 46 renderers_.find(renderer_process_id); |
| 47 if (renderer_it == renderers_.end() || renderer_it->second.empty()) | 47 if (renderer_it == renderers_.end() || renderer_it->second.empty()) |
| 48 return; | 48 return; |
| 49 HashCollection& renderer_hashes = renderer_it->second; | 49 HashCollection& renderer_hashes = renderer_it->second; |
| 50 HashCollection remaining_hashes(remaining_markers.begin(), | 50 HashCollection remaining_hashes(remaining_markers.begin(), |
| 51 remaining_markers.end()); | 51 remaining_markers.end()); |
| 52 std::vector<uint32> removed_hashes = | 52 std::vector<uint32_t> removed_hashes = |
| 53 base::STLSetDifference<std::vector<uint32>>(renderer_hashes, | 53 base::STLSetDifference<std::vector<uint32_t>>(renderer_hashes, |
| 54 remaining_hashes); | 54 remaining_hashes); |
| 55 for (std::vector<uint32>::const_iterator hash_it = removed_hashes.begin(); | 55 for (std::vector<uint32_t>::const_iterator hash_it = removed_hashes.begin(); |
| 56 hash_it != removed_hashes.end(); ++hash_it) { | 56 hash_it != removed_hashes.end(); ++hash_it) { |
| 57 HashMisspellingMap::iterator misspelling_it = misspellings_.find(*hash_it); | 57 HashMisspellingMap::iterator misspelling_it = misspellings_.find(*hash_it); |
| 58 if (misspelling_it != misspellings_.end() && | 58 if (misspelling_it != misspellings_.end() && |
| 59 !misspelling_it->second.action.IsFinal()) { | 59 !misspelling_it->second.action.IsFinal()) { |
| 60 misspelling_it->second.action.Finalize(); | 60 misspelling_it->second.action.Finalize(); |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 bool Feedback::RendererHasMisspellings(int renderer_process_id) const { | 65 bool Feedback::RendererHasMisspellings(int renderer_process_id) const { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 if (total_text_size_ >= approximate_size) | 109 if (total_text_size_ >= approximate_size) |
| 110 total_text_size_ -= approximate_size; | 110 total_text_size_ -= approximate_size; |
| 111 else | 111 else |
| 112 total_text_size_ = 0; | 112 total_text_size_ = 0; |
| 113 misspellings_.erase(misspelling_it); | 113 misspellings_.erase(misspelling_it); |
| 114 } | 114 } |
| 115 if (renderer_hashes.empty()) | 115 if (renderer_hashes.empty()) |
| 116 renderers_.erase(renderer_it); | 116 renderers_.erase(renderer_it); |
| 117 } | 117 } |
| 118 | 118 |
| 119 bool Feedback::HasMisspelling(uint32 hash) const { | 119 bool Feedback::HasMisspelling(uint32_t hash) const { |
| 120 return !!misspellings_.count(hash); | 120 return !!misspellings_.count(hash); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void Feedback::AddMisspelling(int renderer_process_id, | 123 void Feedback::AddMisspelling(int renderer_process_id, |
| 124 const Misspelling& misspelling) { | 124 const Misspelling& misspelling) { |
| 125 HashMisspellingMap::iterator misspelling_it = | 125 HashMisspellingMap::iterator misspelling_it = |
| 126 misspellings_.find(misspelling.hash); | 126 misspellings_.find(misspelling.hash); |
| 127 if (misspelling_it != misspellings_.end()) { | 127 if (misspelling_it != misspellings_.end()) { |
| 128 const Misspelling& existing_misspelling = misspelling_it->second; | 128 const Misspelling& existing_misspelling = misspelling_it->second; |
| 129 text_[GetMisspelledString(existing_misspelling)].erase(misspelling.hash); | 129 text_[GetMisspelledString(existing_misspelling)].erase(misspelling.hash); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 return all_misspellings; | 183 return all_misspellings; |
| 184 } | 184 } |
| 185 | 185 |
| 186 void Feedback::Clear() { | 186 void Feedback::Clear() { |
| 187 total_text_size_ = 0; | 187 total_text_size_ = 0; |
| 188 misspellings_.clear(); | 188 misspellings_.clear(); |
| 189 text_.clear(); | 189 text_.clear(); |
| 190 renderers_.clear(); | 190 renderers_.clear(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 const std::set<uint32>& Feedback::FindMisspellings( | 193 const std::set<uint32_t>& Feedback::FindMisspellings( |
| 194 const base::string16& misspelled_text) const { | 194 const base::string16& misspelled_text) const { |
| 195 const TextHashesMap::const_iterator text_it = text_.find(misspelled_text); | 195 const TextHashesMap::const_iterator text_it = text_.find(misspelled_text); |
| 196 return text_it == text_.end() ? empty_hash_collection_ : text_it->second; | 196 return text_it == text_.end() ? empty_hash_collection_ : text_it->second; |
| 197 } | 197 } |
| 198 | 198 |
| 199 } // namespace spellcheck | 199 } // namespace spellcheck |
| OLD | NEW |