spellcheck_host.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2009 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#ifndef CHROME_BROWSER_SPELLCHECK_HOST_H_
6#define CHROME_BROWSER_SPELLCHECK_HOST_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/file_path.h"
13#include "base/platform_file.h"
14#include "base/ref_counted.h"
15#include "base/scoped_ptr.h"
16#include "chrome/browser/chrome_thread.h"
17#include "chrome/common/net/url_fetcher.h"
18
19class Profile;
20class SpellCheckHostObserver;
21class URLRequestContextGetter;
22
23class SpellCheckHost : public base::RefCountedThreadSafe<SpellCheckHost,
24                           ChromeThread::DeleteOnFileThread>,
25                       public URLFetcher::Delegate {
26 public:
27  SpellCheckHost(SpellCheckHostObserver* observer,
28                 const std::string& language,
29                 URLRequestContextGetter* request_context_getter);
30
31  void Initialize();
32
33  // Clear |observer_|. Used to prevent calling back to a deleted object.
34  void UnsetObserver();
35
36  // Add the given word to the custom words list and inform renderer of the
37  // update.
38  void AddWord(const std::string& word);
39
40  // This function computes a vector of strings which are to be displayed in
41  // the context menu over a text area for changing spell check languages. It
42  // returns the index of the current spell check language in the vector.
43  // TODO(port): this should take a vector of string16, but the implementation
44  // has some dependencies in l10n util that need porting first.
45  static int GetSpellCheckLanguages(Profile* profile,
46                                    std::vector<std::string>* languages);
47
48  const base::PlatformFile& bdict_file() const { return file_; }
49
50  const std::vector<std::string>& custom_words() const { return custom_words_; }
51
52  const std::string& last_added_word() const { return custom_words_.back(); }
53
54  const std::string& language() const { return language_; }
55
56  bool use_platform_spellchecker() const { return use_platform_spellchecker_; }
57
58 private:
59  // These two classes can destruct us.
60  friend class ChromeThread;
61  friend class DeleteTask<SpellCheckHost>;
62
63  virtual ~SpellCheckHost();
64
65  // Figure out the location for the dictionary. This is only non-trivial for
66  // Windows:
67  // The default place whether the spellcheck dictionary can reside is
68  // chrome::DIR_APP_DICTIONARIES. However, for systemwide installations,
69  // this directory may not have permissions for download. In that case, the
70  // alternate directory for download is chrome::DIR_USER_DATA.
71  void InitializeDictionaryLocation();
72
73  // Load and parse the custom words dictionary and open the bdic file.
74  // Executed on the file thread.
75  void InitializeInternal();
76
77  void InitializeOnFileThread();
78
79  // Inform |observer_| that initialization has finished.
80  void InformObserverOfInitialization();
81
82  // If |bdict_file_| is missing, we attempt to download it.
83  void DownloadDictionary();
84
85  // Write a custom dictionary addition to disk.
86  void WriteWordToCustomDictionary(const std::string& word);
87
88  // URLFetcher::Delegate implementation.  Called when we finish downloading the
89  // spellcheck dictionary; saves the dictionary to |data_|.
90  virtual void OnURLFetchComplete(const URLFetcher* source,
91                                  const GURL& url,
92                                  const URLRequestStatus& status,
93                                  int response_code,
94                                  const ResponseCookies& cookies,
95                                  const std::string& data);
96
97  // Saves |data_| to disk. Run on the file thread.
98  void SaveDictionaryData();
99
100  // May be NULL.
101  SpellCheckHostObserver* observer_;
102
103  // The desired location of the dictionary file (whether or not t exists yet).
104  FilePath bdict_file_path_;
105
106  // The location of the custom words file.
107  FilePath custom_dictionary_file_;
108
109  // The language of the dictionary file.
110  std::string language_;
111
112  // The file descriptor/handle for the dictionary file.
113  base::PlatformFile file_;
114
115  // In-memory cache of the custom words file.
116  std::vector<std::string> custom_words_;
117
118  // We don't want to attempt to download a missing dictionary file more than
119  // once.
120  bool tried_to_download_;
121
122  // Whether we should use the platform spellchecker instead of Hunspell.
123  bool use_platform_spellchecker_;
124
125  // Data received from the dictionary download.
126  std::string data_;
127
128  // Used for downloading the dictionary file. We don't hold a reference, and
129  // it is only valid to use it on the UI thread.
130  URLRequestContextGetter* request_context_getter_;
131
132  // Used for downloading the dictionary file.
133  scoped_ptr<URLFetcher> fetcher_;
134};
135
136#endif  // CHROME_BROWSER_SPELLCHECK_HOST_H_
137