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/browser/spellcheck_host.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/browser/spellcheck_host.h')
-rw-r--r-- | chrome/browser/spellcheck_host.h | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/chrome/browser/spellcheck_host.h b/chrome/browser/spellcheck_host.h new file mode 100644 index 0000000..6b0f316 --- /dev/null +++ b/chrome/browser/spellcheck_host.h @@ -0,0 +1,103 @@ +// 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_BROWSER_SPELLCHECK_HOST_H_ +#define CHROME_BROWSER_SPELLCHECK_HOST_H_ + +#include <string> +#include <vector> + +#include "base/file_descriptor_posix.h" +#include "base/file_path.h" +#include "base/ref_counted.h" +#include "chrome/browser/chrome_thread.h" +#include "chrome/browser/net/url_fetcher.h" + +class SpellCheckHost : public base::RefCountedThreadSafe<SpellCheckHost, + ChromeThread::DeleteOnFileThread>, + public URLFetcher::Delegate { + public: + class Observer { + public: + virtual void SpellCheckHostInitialized() = 0; + }; + + SpellCheckHost(Observer* observer, + const std::string& language, + URLRequestContextGetter* request_context_getter); + + // Clear |observer_|. Used to prevent calling back to a deleted object. + void UnsetObserver(); + + // Add the given word to the custom words list and inform renderer of the + // update. + void AddWord(const std::string& word); + + const base::FileDescriptor& bdict_fd() const { return fd_; }; + + const std::vector<std::string>& custom_words() const { return custom_words_; } + + const std::string& last_added_word() const { return custom_words_.back(); } + + const std::string& language() const { return language_; } + + private: + // These two classes can destruct us. + friend class ChromeThread; + friend class DeleteTask<SpellCheckHost>; + + virtual ~SpellCheckHost(); + + // Load and parse the custom words dictionary and open the bdic file. + // Executed on the file thread. + void Initialize(); + + // Inform |observer_| that initialization has finished. + void InformObserverOfInitialization(); + + // If |bdict_file_| is missing, we attempt to download it. + void DownloadDictionary(); + + // Write a custom dictionary addition to disk. + void WriteWordToCustomDictionary(const std::string& word); + + // URLFetcher::Delegate implementation. Called when we finish downloading the + // spellcheck dictionary; saves the dictionary to disk. + virtual void OnURLFetchComplete(const URLFetcher* source, + const GURL& url, + const URLRequestStatus& status, + int response_code, + const ResponseCookies& cookies, + const std::string& data); + + // May be NULL. + Observer* observer_; + + // The desired location of the dictionary file (whether or not it exists yet). + FilePath bdict_file_; + + // The location of the custom words file. + FilePath custom_dictionary_file_; + + // The language of the dictionary file. + std::string language_; + + // On POSIX, the file descriptor for the dictionary file. + base::FileDescriptor fd_; + + // In-memory cache of the custom words file. + std::vector<std::string> custom_words_; + + // We don't want to attempt to download a missing dictionary file more than + // once. + bool tried_to_download_; + + // Used for downloading the dictionary file. + URLRequestContextGetter* request_context_getter_; + + // Used for downloading the dictionary file. + scoped_ptr<URLFetcher> fetcher_; +}; + +#endif // CHROME_BROWSER_SPELLCHECK_HOST_H_ |