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 "content/public/browser/browser_thread.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "net/base/load_flags.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_request_context_getter.h"
22 #if !defined(OS_ANDROID)
23 #include "base/files/memory_mapped_file.h"
24 #include "third_party/hunspell/google/bdict.h"
27 using content::BrowserThread
;
32 void CloseDictionary(base::File file
) {
33 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
37 // Saves |data| to file at |path|. Returns true on successful save, otherwise
39 bool SaveDictionaryData(scoped_ptr
<std::string
> data
,
40 const base::FilePath
& path
) {
41 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
43 size_t bytes_written
=
44 base::WriteFile(path
, data
->data(), data
->length());
45 if (bytes_written
!= data
->length()) {
48 base::FilePath dict_dir
;
49 PathService::Get(chrome::DIR_USER_DATA
, &dict_dir
);
50 base::FilePath fallback_file_path
=
51 dict_dir
.Append(path
.BaseName());
53 base::WriteFile(fallback_file_path
, data
->data(), data
->length());
54 if (bytes_written
== data
->length())
59 base::DeleteFile(path
, false);
69 SpellcheckHunspellDictionary::DictionaryFile::DictionaryFile() {
72 SpellcheckHunspellDictionary::DictionaryFile::~DictionaryFile() {
74 BrowserThread::PostTask(
77 base::Bind(&CloseDictionary
, Passed(&file
)));
81 SpellcheckHunspellDictionary::DictionaryFile::DictionaryFile(RValue other
)
82 : path(other
.object
->path
),
83 file(other
.object
->file
.Pass()) {
86 SpellcheckHunspellDictionary::DictionaryFile
&
87 SpellcheckHunspellDictionary::DictionaryFile::operator=(RValue other
) {
88 if (this != other
.object
) {
89 path
= other
.object
->path
;
90 file
= other
.object
->file
.Pass();
95 SpellcheckHunspellDictionary::SpellcheckHunspellDictionary(
96 const std::string
& language
,
97 net::URLRequestContextGetter
* request_context_getter
,
98 SpellcheckService
* spellcheck_service
)
99 : language_(language
),
100 use_browser_spellchecker_(false),
101 request_context_getter_(request_context_getter
),
102 spellcheck_service_(spellcheck_service
),
103 download_status_(DOWNLOAD_NONE
),
104 weak_ptr_factory_(this) {
107 SpellcheckHunspellDictionary::~SpellcheckHunspellDictionary() {
110 void SpellcheckHunspellDictionary::Load() {
111 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
113 #if defined(USE_BROWSER_SPELLCHECKER)
114 if (spellcheck_platform::SpellCheckerAvailable() &&
115 spellcheck_platform::PlatformSupportsLanguage(language_
)) {
116 use_browser_spellchecker_
= true;
117 spellcheck_platform::SetLanguage(language_
);
118 base::MessageLoop::current()->PostTask(FROM_HERE
,
120 &SpellcheckHunspellDictionary::InformListenersOfInitialization
,
121 weak_ptr_factory_
.GetWeakPtr()));
124 #endif // USE_BROWSER_SPELLCHECKER
126 // Mac falls back on hunspell if its platform spellchecker isn't available.
127 // However, Android does not support hunspell.
128 #if !defined(OS_ANDROID)
129 BrowserThread::PostTaskAndReplyWithResult(
132 base::Bind(&InitializeDictionaryLocation
, language_
),
134 &SpellcheckHunspellDictionary::InitializeDictionaryLocationComplete
,
135 weak_ptr_factory_
.GetWeakPtr()));
136 #endif // !OS_ANDROID
139 void SpellcheckHunspellDictionary::RetryDownloadDictionary(
140 net::URLRequestContextGetter
* request_context_getter
) {
141 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
142 request_context_getter_
= request_context_getter
;
143 DownloadDictionary(GetDictionaryURL());
146 bool SpellcheckHunspellDictionary::IsReady() const {
147 return GetDictionaryFile().IsValid() || IsUsingPlatformChecker();
150 const base::File
& SpellcheckHunspellDictionary::GetDictionaryFile() const {
151 return dictionary_file_
.file
;
154 const std::string
& SpellcheckHunspellDictionary::GetLanguage() const {
158 bool SpellcheckHunspellDictionary::IsUsingPlatformChecker() const {
159 return use_browser_spellchecker_
;
162 void SpellcheckHunspellDictionary::AddObserver(Observer
* observer
) {
163 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
164 observers_
.AddObserver(observer
);
167 void SpellcheckHunspellDictionary::RemoveObserver(Observer
* observer
) {
168 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
169 observers_
.RemoveObserver(observer
);
172 bool SpellcheckHunspellDictionary::IsDownloadInProgress() {
173 return download_status_
== DOWNLOAD_IN_PROGRESS
;
176 bool SpellcheckHunspellDictionary::IsDownloadFailure() {
177 return download_status_
== DOWNLOAD_FAILED
;
180 void SpellcheckHunspellDictionary::OnURLFetchComplete(
181 const net::URLFetcher
* source
) {
183 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
184 scoped_ptr
<net::URLFetcher
> fetcher_destructor(fetcher_
.release());
186 if ((source
->GetResponseCode() / 100) != 2) {
187 // Initialize will not try to download the file a second time.
188 InformListenersOfDownloadFailure();
192 // Basic sanity check on the dictionary. There's a small chance of 200 status
193 // code for a body that represents some form of failure.
194 scoped_ptr
<std::string
> data(new std::string
);
195 source
->GetResponseAsString(data
.get());
196 if (data
->size() < 4 || data
->compare(0, 4, "BDic") != 0) {
197 InformListenersOfDownloadFailure();
201 #if !defined(OS_ANDROID)
202 // To prevent corrupted dictionary data from causing a renderer crash, scan
203 // the dictionary data and verify it is sane before save it to a file.
204 // TODO(rlp): Adding metrics to RecordDictionaryCorruptionStats
205 if (!hunspell::BDict::Verify(data
->data(), data
->size())) {
206 // Let PostTaskAndReply caller send to InformListenersOfInitialization
207 // through SaveDictionaryDataComplete().
208 SaveDictionaryDataComplete(false);
213 BrowserThread::PostTaskAndReplyWithResult
<bool>(
216 base::Bind(&SaveDictionaryData
,
218 dictionary_file_
.path
),
219 base::Bind(&SpellcheckHunspellDictionary::SaveDictionaryDataComplete
,
220 weak_ptr_factory_
.GetWeakPtr()));
223 GURL
SpellcheckHunspellDictionary::GetDictionaryURL() {
224 static const char kDownloadServerUrl
[] =
225 "https://redirector.gvt1.com/edgedl/chrome/dict/";
226 std::string bdict_file
= dictionary_file_
.path
.BaseName().MaybeAsASCII();
228 DCHECK(!bdict_file
.empty());
230 return GURL(std::string(kDownloadServerUrl
) +
231 base::ToLowerASCII(bdict_file
));
234 void SpellcheckHunspellDictionary::DownloadDictionary(GURL url
) {
235 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
236 DCHECK(request_context_getter_
);
238 download_status_
= DOWNLOAD_IN_PROGRESS
;
239 FOR_EACH_OBSERVER(Observer
, observers_
, OnHunspellDictionaryDownloadBegin());
241 fetcher_
= net::URLFetcher::Create(url
, net::URLFetcher::GET
, this);
242 fetcher_
->SetRequestContext(request_context_getter_
);
243 fetcher_
->SetLoadFlags(
244 net::LOAD_DO_NOT_SEND_COOKIES
| net::LOAD_DO_NOT_SAVE_COOKIES
);
246 // Attempt downloading the dictionary only once.
247 request_context_getter_
= NULL
;
250 // The default_dictionary_file can either come from the standard list of
251 // hunspell dictionaries (determined in InitializeDictionaryLocation), or it
252 // can be passed in via an extension. In either case, the file is checked for
253 // existence so that it's not re-downloaded.
254 // For systemwide installations on Windows, the default directory may not
255 // have permissions for download. In that case, the alternate directory for
256 // download is chrome::DIR_USER_DATA.
257 SpellcheckHunspellDictionary::DictionaryFile
258 SpellcheckHunspellDictionary::OpenDictionaryFile(const base::FilePath
& path
) {
259 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
260 DictionaryFile dictionary
;
263 // Check if the dictionary exists in the fallback location. If so, use it
264 // rather than downloading anew.
265 base::FilePath user_dir
;
266 PathService::Get(chrome::DIR_USER_DATA
, &user_dir
);
267 base::FilePath fallback
= user_dir
.Append(path
.BaseName());
268 if (!base::PathExists(path
) && base::PathExists(fallback
))
269 dictionary
.path
= fallback
;
271 dictionary
.path
= path
;
273 dictionary
.path
= path
;
276 // Read the dictionary file and scan its data to check for corruption. The
277 // scoping closes the memory-mapped file before it is opened or deleted.
278 bool bdict_is_valid
= false;
280 #if !defined(OS_ANDROID)
282 base::MemoryMappedFile map
;
284 base::PathExists(dictionary
.path
) &&
285 map
.Initialize(dictionary
.path
) &&
286 hunspell::BDict::Verify(reinterpret_cast<const char*>(map
.data()),
291 if (bdict_is_valid
) {
292 dictionary
.file
.Initialize(dictionary
.path
,
293 base::File::FLAG_READ
| base::File::FLAG_OPEN
);
295 base::DeleteFile(dictionary
.path
, false);
298 return dictionary
.Pass();
301 // The default place where the spellcheck dictionary resides is
302 // chrome::DIR_APP_DICTIONARIES.
303 SpellcheckHunspellDictionary::DictionaryFile
304 SpellcheckHunspellDictionary::InitializeDictionaryLocation(
305 const std::string
& language
) {
306 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
308 // Initialize the BDICT path. Initialization should be in the FILE thread
309 // because it checks if there is a "Dictionaries" directory and create it.
310 base::FilePath dict_dir
;
311 PathService::Get(chrome::DIR_APP_DICTIONARIES
, &dict_dir
);
312 base::FilePath dict_path
=
313 chrome::spellcheck_common::GetVersionedFileName(language
, dict_dir
);
315 return OpenDictionaryFile(dict_path
);
318 void SpellcheckHunspellDictionary::InitializeDictionaryLocationComplete(
319 DictionaryFile file
) {
320 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
321 dictionary_file_
= file
.Pass();
323 if (!dictionary_file_
.file
.IsValid()) {
324 // Notify browser tests that this dictionary is corrupted. Skip downloading
325 // the dictionary in browser tests.
326 // TODO(rouslan): Remove this test-only case.
327 if (spellcheck_service_
->SignalStatusEvent(
328 SpellcheckService::BDICT_CORRUPTED
)) {
329 request_context_getter_
= NULL
;
332 if (request_context_getter_
) {
333 // Download from the UI thread to check that |request_context_getter_| is
335 DownloadDictionary(GetDictionaryURL());
340 InformListenersOfInitialization();
343 void SpellcheckHunspellDictionary::SaveDictionaryDataComplete(
344 bool dictionary_saved
) {
345 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
347 if (dictionary_saved
) {
348 download_status_
= DOWNLOAD_NONE
;
349 FOR_EACH_OBSERVER(Observer
,
351 OnHunspellDictionaryDownloadSuccess());
354 InformListenersOfDownloadFailure();
355 InformListenersOfInitialization();
359 void SpellcheckHunspellDictionary::InformListenersOfInitialization() {
360 FOR_EACH_OBSERVER(Observer
, observers_
, OnHunspellDictionaryInitialized());
363 void SpellcheckHunspellDictionary::InformListenersOfDownloadFailure() {
364 download_status_
= DOWNLOAD_FAILED
;
365 FOR_EACH_OBSERVER(Observer
,
367 OnHunspellDictionaryDownloadFailure());