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/files/memory_mapped_file.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "chrome/browser/spellchecker/spellcheck_platform.h"
13 #include "chrome/browser/spellchecker/spellcheck_service.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/spellcheck_common.h"
16 #include "chrome/common/spellcheck_messages.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "net/base/load_flags.h"
20 #include "net/url_request/url_fetcher.h"
21 #include "net/url_request/url_request_context_getter.h"
22 #include "third_party/hunspell/google/bdict.h"
25 using content::BrowserThread
;
30 void CloseDictionary(base::File file
) {
31 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
35 // Saves |data| to file at |path|. Returns true on successful save, otherwise
37 bool SaveDictionaryData(scoped_ptr
<std::string
> data
,
38 const base::FilePath
& path
) {
39 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
41 size_t bytes_written
=
42 base::WriteFile(path
, data
->data(), data
->length());
43 if (bytes_written
!= data
->length()) {
46 base::FilePath dict_dir
;
47 PathService::Get(chrome::DIR_USER_DATA
, &dict_dir
);
48 base::FilePath fallback_file_path
=
49 dict_dir
.Append(path
.BaseName());
51 base::WriteFile(fallback_file_path
, data
->data(), data
->length());
52 if (bytes_written
== data
->length())
57 base::DeleteFile(path
, false);
67 SpellcheckHunspellDictionary::DictionaryFile::DictionaryFile() {
70 SpellcheckHunspellDictionary::DictionaryFile::~DictionaryFile() {
72 BrowserThread::PostTask(
75 base::Bind(&CloseDictionary
, Passed(&file
)));
79 SpellcheckHunspellDictionary::DictionaryFile::DictionaryFile(RValue other
)
80 : path(other
.object
->path
),
81 file(other
.object
->file
.Pass()) {
84 SpellcheckHunspellDictionary::DictionaryFile
&
85 SpellcheckHunspellDictionary::DictionaryFile::operator=(RValue other
) {
86 if (this != other
.object
) {
87 path
= other
.object
->path
;
88 file
= other
.object
->file
.Pass();
93 SpellcheckHunspellDictionary::SpellcheckHunspellDictionary(
94 const std::string
& language
,
95 net::URLRequestContextGetter
* request_context_getter
,
96 SpellcheckService
* spellcheck_service
)
97 : language_(language
),
98 use_platform_spellchecker_(false),
99 request_context_getter_(request_context_getter
),
100 spellcheck_service_(spellcheck_service
),
101 download_status_(DOWNLOAD_NONE
),
102 weak_ptr_factory_(this) {
105 SpellcheckHunspellDictionary::~SpellcheckHunspellDictionary() {
108 void SpellcheckHunspellDictionary::Load() {
109 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
111 #if defined(USE_PLATFORM_SPELLCHECKER)
112 if (spellcheck_platform::SpellCheckerAvailable() &&
113 spellcheck_platform::PlatformSupportsLanguage(language_
)) {
114 use_platform_spellchecker_
= true;
115 spellcheck_platform::SetLanguage(language_
);
116 base::MessageLoop::current()->PostTask(FROM_HERE
,
118 &SpellcheckHunspellDictionary::InformListenersOfInitialization
,
119 weak_ptr_factory_
.GetWeakPtr()));
122 #endif // USE_PLATFORM_SPELLCHECKER
124 // Mac falls back on hunspell if its platform spellchecker isn't available.
125 // However, Android does not support hunspell.
126 #if !defined(OS_ANDROID)
127 BrowserThread::PostTaskAndReplyWithResult(
130 base::Bind(&InitializeDictionaryLocation
, language_
),
132 &SpellcheckHunspellDictionary::InitializeDictionaryLocationComplete
,
133 weak_ptr_factory_
.GetWeakPtr()));
134 #endif // !OS_ANDROID
137 void SpellcheckHunspellDictionary::RetryDownloadDictionary(
138 net::URLRequestContextGetter
* request_context_getter
) {
139 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
140 request_context_getter_
= request_context_getter
;
141 DownloadDictionary(GetDictionaryURL());
144 bool SpellcheckHunspellDictionary::IsReady() const {
145 return GetDictionaryFile().IsValid() || IsUsingPlatformChecker();
148 const base::File
& SpellcheckHunspellDictionary::GetDictionaryFile() const {
149 return dictionary_file_
.file
;
152 const std::string
& SpellcheckHunspellDictionary::GetLanguage() const {
156 bool SpellcheckHunspellDictionary::IsUsingPlatformChecker() const {
157 return use_platform_spellchecker_
;
160 void SpellcheckHunspellDictionary::AddObserver(Observer
* observer
) {
161 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
162 observers_
.AddObserver(observer
);
165 void SpellcheckHunspellDictionary::RemoveObserver(Observer
* observer
) {
166 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
167 observers_
.RemoveObserver(observer
);
170 bool SpellcheckHunspellDictionary::IsDownloadInProgress() {
171 return download_status_
== DOWNLOAD_IN_PROGRESS
;
174 bool SpellcheckHunspellDictionary::IsDownloadFailure() {
175 return download_status_
== DOWNLOAD_FAILED
;
178 void SpellcheckHunspellDictionary::OnURLFetchComplete(
179 const net::URLFetcher
* source
) {
181 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
182 scoped_ptr
<net::URLFetcher
> fetcher_destructor(fetcher_
.release());
184 if ((source
->GetResponseCode() / 100) != 2) {
185 // Initialize will not try to download the file a second time.
186 InformListenersOfDownloadFailure();
190 // Basic sanity check on the dictionary. There's a small chance of 200 status
191 // code for a body that represents some form of failure.
192 scoped_ptr
<std::string
> data(new std::string
);
193 source
->GetResponseAsString(data
.get());
194 if (data
->size() < 4 || data
->compare(0, 4, "BDic") != 0) {
195 InformListenersOfDownloadFailure();
199 // To prevent corrupted dictionary data from causing a renderer crash, scan
200 // the dictionary data and verify it is sane before save it to a file.
201 // TODO(rlp): Adding metrics to RecordDictionaryCorruptionStats
202 if (!hunspell::BDict::Verify(data
->data(), data
->size())) {
203 // Let PostTaskAndReply caller send to InformListenersOfInitialization
204 // through SaveDictionaryDataComplete().
205 SaveDictionaryDataComplete(false);
209 BrowserThread::PostTaskAndReplyWithResult
<bool>(
212 base::Bind(&SaveDictionaryData
,
214 dictionary_file_
.path
),
215 base::Bind(&SpellcheckHunspellDictionary::SaveDictionaryDataComplete
,
216 weak_ptr_factory_
.GetWeakPtr()));
219 GURL
SpellcheckHunspellDictionary::GetDictionaryURL() {
220 static const char kDownloadServerUrl
[] =
221 "https://redirector.gvt1.com/edgedl/chrome/dict/";
222 std::string bdict_file
= dictionary_file_
.path
.BaseName().MaybeAsASCII();
224 DCHECK(!bdict_file
.empty());
226 return GURL(std::string(kDownloadServerUrl
) +
227 base::StringToLowerASCII(bdict_file
));
230 void SpellcheckHunspellDictionary::DownloadDictionary(GURL url
) {
231 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
232 DCHECK(request_context_getter_
);
234 download_status_
= DOWNLOAD_IN_PROGRESS
;
235 FOR_EACH_OBSERVER(Observer
, observers_
, OnHunspellDictionaryDownloadBegin());
237 fetcher_
= net::URLFetcher::Create(url
, net::URLFetcher::GET
, this);
238 fetcher_
->SetRequestContext(request_context_getter_
);
239 fetcher_
->SetLoadFlags(
240 net::LOAD_DO_NOT_SEND_COOKIES
| net::LOAD_DO_NOT_SAVE_COOKIES
);
242 // Attempt downloading the dictionary only once.
243 request_context_getter_
= NULL
;
246 // The default_dictionary_file can either come from the standard list of
247 // hunspell dictionaries (determined in InitializeDictionaryLocation), or it
248 // can be passed in via an extension. In either case, the file is checked for
249 // existence so that it's not re-downloaded.
250 // For systemwide installations on Windows, the default directory may not
251 // have permissions for download. In that case, the alternate directory for
252 // download is chrome::DIR_USER_DATA.
253 SpellcheckHunspellDictionary::DictionaryFile
254 SpellcheckHunspellDictionary::OpenDictionaryFile(const base::FilePath
& path
) {
255 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
256 DictionaryFile dictionary
;
259 // Check if the dictionary exists in the fallback location. If so, use it
260 // rather than downloading anew.
261 base::FilePath user_dir
;
262 PathService::Get(chrome::DIR_USER_DATA
, &user_dir
);
263 base::FilePath fallback
= user_dir
.Append(path
.BaseName());
264 if (!base::PathExists(path
) && base::PathExists(fallback
))
265 dictionary
.path
= fallback
;
267 dictionary
.path
= path
;
269 dictionary
.path
= path
;
272 // Read the dictionary file and scan its data to check for corruption. The
273 // scoping closes the memory-mapped file before it is opened or deleted.
276 base::MemoryMappedFile map
;
278 base::PathExists(dictionary
.path
) &&
279 map
.Initialize(dictionary
.path
) &&
280 hunspell::BDict::Verify(reinterpret_cast<const char*>(map
.data()),
283 if (bdict_is_valid
) {
284 dictionary
.file
.Initialize(dictionary
.path
,
285 base::File::FLAG_READ
| base::File::FLAG_OPEN
);
287 base::DeleteFile(dictionary
.path
, false);
290 return dictionary
.Pass();
293 // The default place where the spellcheck dictionary resides is
294 // chrome::DIR_APP_DICTIONARIES.
295 SpellcheckHunspellDictionary::DictionaryFile
296 SpellcheckHunspellDictionary::InitializeDictionaryLocation(
297 const std::string
& language
) {
298 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
300 // Initialize the BDICT path. Initialization should be in the FILE thread
301 // because it checks if there is a "Dictionaries" directory and create it.
302 base::FilePath dict_dir
;
303 PathService::Get(chrome::DIR_APP_DICTIONARIES
, &dict_dir
);
304 base::FilePath dict_path
=
305 chrome::spellcheck_common::GetVersionedFileName(language
, dict_dir
);
307 return OpenDictionaryFile(dict_path
);
310 void SpellcheckHunspellDictionary::InitializeDictionaryLocationComplete(
311 DictionaryFile file
) {
312 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
313 dictionary_file_
= file
.Pass();
315 if (!dictionary_file_
.file
.IsValid()) {
317 // Notify browser tests that this dictionary is corrupted. Skip downloading
318 // the dictionary in browser tests.
319 // TODO(rouslan): Remove this test-only case.
320 if (spellcheck_service_
->SignalStatusEvent(
321 SpellcheckService::BDICT_CORRUPTED
)) {
322 request_context_getter_
= NULL
;
325 if (request_context_getter_
) {
326 // Download from the UI thread to check that |request_context_getter_| is
328 DownloadDictionary(GetDictionaryURL());
333 InformListenersOfInitialization();
336 void SpellcheckHunspellDictionary::SaveDictionaryDataComplete(
337 bool dictionary_saved
) {
338 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
340 if (dictionary_saved
) {
341 download_status_
= DOWNLOAD_NONE
;
342 FOR_EACH_OBSERVER(Observer
,
344 OnHunspellDictionaryDownloadSuccess());
347 InformListenersOfDownloadFailure();
348 InformListenersOfInitialization();
352 void SpellcheckHunspellDictionary::InformListenersOfInitialization() {
353 FOR_EACH_OBSERVER(Observer
, observers_
, OnHunspellDictionaryInitialized());
356 void SpellcheckHunspellDictionary::InformListenersOfDownloadFailure() {
357 download_status_
= DOWNLOAD_FAILED
;
358 FOR_EACH_OBSERVER(Observer
,
360 OnHunspellDictionaryDownloadFailure());