| // Copyright 2026 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #ifndef COMPONENTS_SPELLCHECK_RENDERER_DOCUMENT_CUSTOM_DICTIONARY_H_ | |
| #define COMPONENTS_SPELLCHECK_RENDERER_DOCUMENT_CUSTOM_DICTIONARY_H_ | |
| #include <stddef.h> | |
| #include <string> | |
| #include <string_view> | |
| #include <vector> | |
| #include "third_party/abseil-cpp/absl/container/flat_hash_set.h" | |
| // The words a document adds through the SpellCheckCustomDictionary web API. | |
| // Words in this dictionary are spelled correctly in that document. Each | |
| // SpellCheckProvider owns one for its frame's document and clears it when a new | |
| // document is created. | |
| // | |
| // Unlike CustomDictionaryEngine, which holds the profile's custom dictionary | |
| // and is shared by every frame in the renderer, this dictionary is scoped to a | |
| // single document and its size is capped (see Update()). | |
| class DocumentCustomDictionary { | |
| public: | |
| DocumentCustomDictionary(); | |
| DocumentCustomDictionary(const DocumentCustomDictionary&) = delete; | |
| DocumentCustomDictionary& operator=(const DocumentCustomDictionary&) = delete; | |
| ~DocumentCustomDictionary(); | |
| // Removes `words_removed`, then adds `words_added`. Words longer than | |
| // spellcheck::kMaxDocumentCustomDictionaryWordBytes are skipped, and once | |
| // the dictionary holds spellcheck::kMaxDocumentCustomDictionaryWords words | |
| // the remaining additions are dropped. Because removals are applied first | |
| // and shrink the dictionary, the cap bounds the words held at any one time | |
| // rather than the number of changes. | |
| // | |
| // Returns the words in `words_added` that this call inserted, after the | |
| // removals, in their original order. A word that was already in the | |
| // dictionary and is in both `words_removed` and `words_added` is included. | |
| [[nodiscard]] std::vector<std::string> Update( | |
| const std::vector<std::string>& words_added, | |
| const std::vector<std::string>& words_removed); | |
| // Returns true if `word` is in the dictionary. Matching is exact and | |
| // case-sensitive. | |
| bool Contains(std::u16string_view word) const; | |
| // Returns the words in the dictionary, sorted. | |
| std::vector<std::u16string> GetWords() const; | |
| // Removes every word. | |
| void Clear(); | |
| bool empty() const { return words_.empty(); } | |
| size_t size() const { return words_.size(); } | |
| private: | |
| absl::flat_hash_set<std::u16string> words_; | |
| }; | |
| #endif // COMPONENTS_SPELLCHECK_RENDERER_DOCUMENT_CUSTOM_DICTIONARY_H_ |