diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-06 03:05:46 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-06 03:05:46 +0000 |
commit | 85c55dcd717445cd3763b5c94f9902b4cdd194b0 (patch) | |
tree | 2deea721cfac202e3eb8556f66a4cf317a331288 /chrome/renderer/spellchecker/spellcheck.h | |
parent | f1a8b962f0a6f1deb6c8c05a3f86d541e2ba61dd (diff) | |
download | chromium_src-85c55dcd717445cd3763b5c94f9902b4cdd194b0.zip chromium_src-85c55dcd717445cd3763b5c94f9902b4cdd194b0.tar.gz chromium_src-85c55dcd717445cd3763b5c94f9902b4cdd194b0.tar.bz2 |
Move the spellchecker to the renderer.
The motivation is that this removes the sync IPC on every call to the spellchecker. Also, currently we spellcheck in the IO thread, which frequently needs to go to disk (in particular, the entire spellcheck dictionary starts paged out), so this will block just the single renderer when that happens, rather than the whole IO thread.
This breaks the SpellChecker class into two new classes.
1) On the browser side, we have SpellCheckHost. This class handles browser-wide tasks, such as keeping the custom words list in sync with the on-disk custom words dictionary, downloading missing dictionaries, etc. On Posix, it also opens the bdic file since the renderer isn't allowed to open files. SpellCheckHost is created and destroyed on the UI thread. It is initialized on the file thread.
2) On the renderer side, SpellChecker2. This class will one day be renamed SpellChecker. It handles actual checking of the words, memory maps the dictionary file, loads hunspell, etc. There is one SpellChecker2 per RenderThread (hence one per render process).
My intention is for this patch to move Linux to this new approach, and follow up with ports for Windows (which will involve passing a dictionary file name rather than a file descriptor through to the renderer) and Mac (which will involve adding sync ViewHost IPC callsfor when the platform spellchecker is enabled). Note that anyone using the platform spellchecker rather than Hunspell will get no benefit out of this refactor.
There should be no loss of functionality for Linux (or any other platform) in this patch. The following should all still work:
- dictionary is loaded lazily
- hunspell is initialized lazily, per renderer
- language changes work.
- Dynamic downloading of new dictionaries
- auto spell correct works (as well as toggling it).
- disabling spellcheck works.
- custom words work (including adding in one renderer and immediately having it take effect in other renderers, for certain values of "immediate")
TODO:
- move spellchecker unit tests to test SpellCheck2
- add sync IPC for platform spellchecker; port to Mac
- add dictionary location fallback; port to Windows
- remove SpellChecker classes from browser/
BUG=25677
Review URL: http://codereview.chromium.org/357003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31199 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/spellchecker/spellcheck.h')
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck.h | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/chrome/renderer/spellchecker/spellcheck.h b/chrome/renderer/spellchecker/spellcheck.h new file mode 100644 index 0000000..3b2e19d --- /dev/null +++ b/chrome/renderer/spellchecker/spellcheck.h @@ -0,0 +1,127 @@ +// Copyright (c) 2009 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. + +#ifndef CHROME_RENDERER_SPELLCHECKER_SPELLCHECKER_H_ +#define CHROME_RENDERER_SPELLCHECKER_SPELLCHECKER_H_ + +#include <queue> +#include <string> +#include <vector> + +#include "app/l10n_util.h" +#include "base/file_descriptor_posix.h" +#include "base/string16.h" +#include "base/time.h" +#include "chrome/renderer/spellchecker/spellcheck_worditerator.h" +#include "unicode/uscript.h" + +class Hunspell; + +namespace base { +class FileDescriptor; +} + +namespace file_util { +class MemoryMappedFile; +} + +class SpellCheck { + public: + SpellCheck(); + + ~SpellCheck(); + + void Init(const base::FileDescriptor& bdict_fd, + const std::vector<std::string>& custom_words, + const std::string language); + + // SpellCheck a word. + // Returns true if spelled correctly, false otherwise. + // If the spellchecker failed to initialize, always returns true. + // The |tag| parameter should either be a unique identifier for the document + // that the word came from (if the current platform requires it), or 0. + // In addition, finds the suggested words for a given word + // and puts them into |*optional_suggestions|. + // If the word is spelled correctly, the vector is empty. + // If optional_suggestions is NULL, suggested words will not be looked up. + // Note that Doing suggest lookups can be slow. + bool SpellCheckWord(const char16* in_word, + int in_word_len, + int tag, + int* misspelling_start, + int* misspelling_len, + std::vector<string16>* optional_suggestions); + + // Find a possible correctly spelled word for a misspelled word. Computes an + // empty string if input misspelled word is too long, there is ambiguity, or + // the correct spelling cannot be determined. + string16 GetAutoCorrectionWord(const string16& word, int tag); + + // Turn auto spell correct support ON or OFF. + // |turn_on| = true means turn ON; false means turn OFF. + void EnableAutoSpellCorrect(bool turn_on); + + // Add a word to the custom list. This may be called before or after + // |hunspell_| has been initialized. + void WordAdded(const std::string& word); + + private: + // Initializes the Hunspell dictionary, or does nothing if |hunspell_| is + // non-null. This blocks. + void InitializeHunspell(); + + // If there is no dictionary file, then this requests one from the browser + // and does not block. In this case it returns true. + // If there is a dictionary file, but Hunspell has not been loaded, then + // this loads Hunspell. + // If Hunspell is already loaded, this does nothing. In both the latter cases + // it returns false, meaning that it is OK to continue spellchecking. + bool InitializeIfNeeded(); + + // When called, relays the request to check the spelling to the proper + // backend, either hunspell or a platform-specific backend. + bool CheckSpelling(const string16& word_to_check, int tag); + + // When called, relays the request to fill the list with suggestions to + // the proper backend, either hunspell or a platform-specific backend. + void FillSuggestionList(const string16& wrong_word, + std::vector<string16>* optional_suggestions); + + // Returns whether or not the given word is a contraction of valid words + // (e.g. "word:word"). + bool IsValidContraction(const string16& word, int tag); + + // Add the given custom word to |hunspell_|. + void AddWordToHunspell(const std::string& word); + + // We memory-map the BDict file. + scoped_ptr<file_util::MemoryMappedFile> bdict_file_; + + // The hunspell dictionary in use. + scoped_ptr<Hunspell> hunspell_; + + base::FileDescriptor fd_; + std::vector<std::string> custom_words_; + + // Represents character attributes used for filtering out characters which + // are not supported by this SpellCheck object. + SpellcheckCharAttribute character_attributes_; + + // Remember state for auto spell correct. + bool auto_spell_correct_turned_on_; + + // True if a platform-specific spellchecking engine is being used, + // and False if hunspell is being used. + bool is_using_platform_spelling_engine_; + + // This flags whether we have ever been initialized, or have asked the browser + // for a dictionary. The value indicates whether we should request a + // dictionary from the browser when the render view asks us to check the + // spelling of a word. + bool initialized_; + + DISALLOW_COPY_AND_ASSIGN(SpellCheck); +}; + +#endif // CHROME_RENDERER_SPELLCHECKER_SPELLCHECKER_H_ |