Patch 3.3: Refactored cocoa_spelling_engine_mac to be generic.
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellcheck_hunspell_dictionary.cc
blob39a087ee7da7c7f9c60d27905018527c0ce0a7db
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
7 #include "base/files/file_util.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "chrome/browser/spellchecker/spellcheck_platform.h"
12 #include "chrome/browser/spellchecker/spellcheck_service.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/common/spellcheck_common.h"
15 #include "chrome/common/spellcheck_messages.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "net/base/load_flags.h"
19 #include "net/url_request/url_fetcher.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "url/gurl.h"
23 #if !defined(OS_ANDROID)
24 #include "base/files/memory_mapped_file.h"
25 #include "third_party/hunspell/google/bdict.h"
26 #endif
28 using content::BrowserThread;
30 namespace {
32 // Close the file.
33 void CloseDictionary(base::File file) {
34 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
35 file.Close();
38 // Saves |data| to file at |path|. Returns true on successful save, otherwise
39 // returns false.
40 bool SaveDictionaryData(scoped_ptr<std::string> data,
41 const base::FilePath& path) {
42 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
44 size_t bytes_written =
45 base::WriteFile(path, data->data(), data->length());
46 if (bytes_written != data->length()) {
47 bool success = false;
48 #if defined(OS_WIN)
49 base::FilePath dict_dir;
50 PathService::Get(chrome::DIR_USER_DATA, &dict_dir);
51 base::FilePath fallback_file_path =
52 dict_dir.Append(path.BaseName());
53 bytes_written =
54 base::WriteFile(fallback_file_path, data->data(), data->length());
55 if (bytes_written == data->length())
56 success = true;
57 #endif
59 if (!success) {
60 base::DeleteFile(path, false);
61 return false;
65 return true;
68 } // namespace
70 SpellcheckHunspellDictionary::DictionaryFile::DictionaryFile() {
73 SpellcheckHunspellDictionary::DictionaryFile::~DictionaryFile() {
74 if (file.IsValid()) {
75 BrowserThread::PostTask(
76 BrowserThread::FILE,
77 FROM_HERE,
78 base::Bind(&CloseDictionary, Passed(&file)));
82 SpellcheckHunspellDictionary::DictionaryFile::DictionaryFile(RValue other)
83 : path(other.object->path),
84 file(other.object->file.Pass()) {
87 SpellcheckHunspellDictionary::DictionaryFile&
88 SpellcheckHunspellDictionary::DictionaryFile::operator=(RValue other) {
89 if (this != other.object) {
90 path = other.object->path;
91 file = other.object->file.Pass();
93 return *this;
96 SpellcheckHunspellDictionary::SpellcheckHunspellDictionary(
97 const std::string& language,
98 net::URLRequestContextGetter* request_context_getter,
99 SpellcheckService* spellcheck_service)
100 : language_(language),
101 use_platform_spellchecker_(false),
102 request_context_getter_(request_context_getter),
103 spellcheck_service_(spellcheck_service),
104 download_status_(DOWNLOAD_NONE),
105 weak_ptr_factory_(this) {
108 SpellcheckHunspellDictionary::~SpellcheckHunspellDictionary() {
111 void SpellcheckHunspellDictionary::Load() {
112 DCHECK_CURRENTLY_ON(BrowserThread::UI);
114 #if defined(USE_PLATFORM_SPELLCHECKER)
115 if (spellcheck_platform::SpellCheckerAvailable() &&
116 spellcheck_platform::PlatformSupportsLanguage(language_)) {
117 use_platform_spellchecker_ = true;
118 spellcheck_platform::SetLanguage(language_);
119 base::MessageLoop::current()->PostTask(FROM_HERE,
120 base::Bind(
121 &SpellcheckHunspellDictionary::InformListenersOfInitialization,
122 weak_ptr_factory_.GetWeakPtr()));
123 return;
125 #endif // USE_PLATFORM_SPELLCHECKER
127 // Mac falls back on hunspell if its platform spellchecker isn't available.
128 // However, Android does not support hunspell.
129 #if !defined(OS_ANDROID)
130 BrowserThread::PostTaskAndReplyWithResult(
131 BrowserThread::FILE,
132 FROM_HERE,
133 base::Bind(&InitializeDictionaryLocation, language_),
134 base::Bind(
135 &SpellcheckHunspellDictionary::InitializeDictionaryLocationComplete,
136 weak_ptr_factory_.GetWeakPtr()));
137 #endif // !OS_ANDROID
140 void SpellcheckHunspellDictionary::RetryDownloadDictionary(
141 net::URLRequestContextGetter* request_context_getter) {
142 DCHECK_CURRENTLY_ON(BrowserThread::UI);
143 request_context_getter_ = request_context_getter;
144 DownloadDictionary(GetDictionaryURL());
147 bool SpellcheckHunspellDictionary::IsReady() const {
148 return GetDictionaryFile().IsValid() || IsUsingPlatformChecker();
151 const base::File& SpellcheckHunspellDictionary::GetDictionaryFile() const {
152 return dictionary_file_.file;
155 const std::string& SpellcheckHunspellDictionary::GetLanguage() const {
156 return language_;
159 bool SpellcheckHunspellDictionary::IsUsingPlatformChecker() const {
160 return use_platform_spellchecker_;
163 void SpellcheckHunspellDictionary::AddObserver(Observer* observer) {
164 DCHECK_CURRENTLY_ON(BrowserThread::UI);
165 observers_.AddObserver(observer);
168 void SpellcheckHunspellDictionary::RemoveObserver(Observer* observer) {
169 DCHECK_CURRENTLY_ON(BrowserThread::UI);
170 observers_.RemoveObserver(observer);
173 bool SpellcheckHunspellDictionary::IsDownloadInProgress() {
174 return download_status_ == DOWNLOAD_IN_PROGRESS;
177 bool SpellcheckHunspellDictionary::IsDownloadFailure() {
178 return download_status_ == DOWNLOAD_FAILED;
181 void SpellcheckHunspellDictionary::OnURLFetchComplete(
182 const net::URLFetcher* source) {
183 DCHECK(source);
184 DCHECK_CURRENTLY_ON(BrowserThread::UI);
185 scoped_ptr<net::URLFetcher> fetcher_destructor(fetcher_.release());
187 if ((source->GetResponseCode() / 100) != 2) {
188 // Initialize will not try to download the file a second time.
189 InformListenersOfDownloadFailure();
190 return;
193 // Basic sanity check on the dictionary. There's a small chance of 200 status
194 // code for a body that represents some form of failure.
195 scoped_ptr<std::string> data(new std::string);
196 source->GetResponseAsString(data.get());
197 if (data->size() < 4 || data->compare(0, 4, "BDic") != 0) {
198 InformListenersOfDownloadFailure();
199 return;
202 #if !defined(OS_ANDROID)
203 // To prevent corrupted dictionary data from causing a renderer crash, scan
204 // the dictionary data and verify it is sane before save it to a file.
205 // TODO(rlp): Adding metrics to RecordDictionaryCorruptionStats
206 if (!hunspell::BDict::Verify(data->data(), data->size())) {
207 // Let PostTaskAndReply caller send to InformListenersOfInitialization
208 // through SaveDictionaryDataComplete().
209 SaveDictionaryDataComplete(false);
210 return;
212 #endif
214 BrowserThread::PostTaskAndReplyWithResult<bool>(
215 BrowserThread::FILE,
216 FROM_HERE,
217 base::Bind(&SaveDictionaryData,
218 base::Passed(&data),
219 dictionary_file_.path),
220 base::Bind(&SpellcheckHunspellDictionary::SaveDictionaryDataComplete,
221 weak_ptr_factory_.GetWeakPtr()));
224 GURL SpellcheckHunspellDictionary::GetDictionaryURL() {
225 static const char kDownloadServerUrl[] =
226 "https://redirector.gvt1.com/edgedl/chrome/dict/";
227 std::string bdict_file = dictionary_file_.path.BaseName().MaybeAsASCII();
229 DCHECK(!bdict_file.empty());
231 return GURL(std::string(kDownloadServerUrl) +
232 base::StringToLowerASCII(bdict_file));
235 void SpellcheckHunspellDictionary::DownloadDictionary(GURL url) {
236 DCHECK_CURRENTLY_ON(BrowserThread::UI);
237 DCHECK(request_context_getter_);
239 download_status_ = DOWNLOAD_IN_PROGRESS;
240 FOR_EACH_OBSERVER(Observer, observers_, OnHunspellDictionaryDownloadBegin());
242 fetcher_ = net::URLFetcher::Create(url, net::URLFetcher::GET, this);
243 fetcher_->SetRequestContext(request_context_getter_);
244 fetcher_->SetLoadFlags(
245 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES);
246 fetcher_->Start();
247 // Attempt downloading the dictionary only once.
248 request_context_getter_ = NULL;
251 // The default_dictionary_file can either come from the standard list of
252 // hunspell dictionaries (determined in InitializeDictionaryLocation), or it
253 // can be passed in via an extension. In either case, the file is checked for
254 // existence so that it's not re-downloaded.
255 // For systemwide installations on Windows, the default directory may not
256 // have permissions for download. In that case, the alternate directory for
257 // download is chrome::DIR_USER_DATA.
258 SpellcheckHunspellDictionary::DictionaryFile
259 SpellcheckHunspellDictionary::OpenDictionaryFile(const base::FilePath& path) {
260 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
261 DictionaryFile dictionary;
263 #if defined(OS_WIN)
264 // Check if the dictionary exists in the fallback location. If so, use it
265 // rather than downloading anew.
266 base::FilePath user_dir;
267 PathService::Get(chrome::DIR_USER_DATA, &user_dir);
268 base::FilePath fallback = user_dir.Append(path.BaseName());
269 if (!base::PathExists(path) && base::PathExists(fallback))
270 dictionary.path = fallback;
271 else
272 dictionary.path = path;
273 #else
274 dictionary.path = path;
275 #endif
277 // Read the dictionary file and scan its data to check for corruption. The
278 // scoping closes the memory-mapped file before it is opened or deleted.
279 bool bdict_is_valid = false;
281 #if !defined(OS_ANDROID)
283 base::MemoryMappedFile map;
284 bdict_is_valid =
285 base::PathExists(dictionary.path) &&
286 map.Initialize(dictionary.path) &&
287 hunspell::BDict::Verify(reinterpret_cast<const char*>(map.data()),
288 map.length());
290 #endif
292 if (bdict_is_valid) {
293 dictionary.file.Initialize(dictionary.path,
294 base::File::FLAG_READ | base::File::FLAG_OPEN);
295 } else {
296 base::DeleteFile(dictionary.path, false);
299 return dictionary.Pass();
302 // The default place where the spellcheck dictionary resides is
303 // chrome::DIR_APP_DICTIONARIES.
304 SpellcheckHunspellDictionary::DictionaryFile
305 SpellcheckHunspellDictionary::InitializeDictionaryLocation(
306 const std::string& language) {
307 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
309 // Initialize the BDICT path. Initialization should be in the FILE thread
310 // because it checks if there is a "Dictionaries" directory and create it.
311 base::FilePath dict_dir;
312 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
313 base::FilePath dict_path =
314 chrome::spellcheck_common::GetVersionedFileName(language, dict_dir);
316 return OpenDictionaryFile(dict_path);
319 void SpellcheckHunspellDictionary::InitializeDictionaryLocationComplete(
320 DictionaryFile file) {
321 DCHECK_CURRENTLY_ON(BrowserThread::UI);
322 dictionary_file_ = file.Pass();
324 if (!dictionary_file_.file.IsValid()) {
326 // Notify browser tests that this dictionary is corrupted. Skip downloading
327 // the dictionary in browser tests.
328 // TODO(rouslan): Remove this test-only case.
329 if (spellcheck_service_->SignalStatusEvent(
330 SpellcheckService::BDICT_CORRUPTED)) {
331 request_context_getter_ = NULL;
334 if (request_context_getter_) {
335 // Download from the UI thread to check that |request_context_getter_| is
336 // still valid.
337 DownloadDictionary(GetDictionaryURL());
338 return;
342 InformListenersOfInitialization();
345 void SpellcheckHunspellDictionary::SaveDictionaryDataComplete(
346 bool dictionary_saved) {
347 DCHECK_CURRENTLY_ON(BrowserThread::UI);
349 if (dictionary_saved) {
350 download_status_ = DOWNLOAD_NONE;
351 FOR_EACH_OBSERVER(Observer,
352 observers_,
353 OnHunspellDictionaryDownloadSuccess());
354 Load();
355 } else {
356 InformListenersOfDownloadFailure();
357 InformListenersOfInitialization();
361 void SpellcheckHunspellDictionary::InformListenersOfInitialization() {
362 FOR_EACH_OBSERVER(Observer, observers_, OnHunspellDictionaryInitialized());
365 void SpellcheckHunspellDictionary::InformListenersOfDownloadFailure() {
366 download_status_ = DOWNLOAD_FAILED;
367 FOR_EACH_OBSERVER(Observer,
368 observers_,
369 OnHunspellDictionaryDownloadFailure());