Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/spellchecker/spellcheck_host_impl.h" | 5 #include "chrome/browser/spellchecker/spellcheck_host_impl.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 : profile_(profile), | 87 : profile_(profile), |
| 88 language_(language), | 88 language_(language), |
| 89 file_(base::kInvalidPlatformFileValue), | 89 file_(base::kInvalidPlatformFileValue), |
| 90 tried_to_download_(false), | 90 tried_to_download_(false), |
| 91 use_platform_spellchecker_(false), | 91 use_platform_spellchecker_(false), |
| 92 request_context_getter_(request_context_getter), | 92 request_context_getter_(request_context_getter), |
| 93 metrics_(metrics) { | 93 metrics_(metrics) { |
| 94 DCHECK(profile_); | 94 DCHECK(profile_); |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 96 | 96 |
| 97 FilePath personal_file_directory; | |
| 98 PathService::Get(chrome::DIR_USER_DATA, &personal_file_directory); | |
| 99 custom_dictionary_file_ = | |
| 100 personal_file_directory.Append(chrome::kCustomDictionaryFileName); | |
| 101 | |
| 102 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 97 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 103 NotificationService::AllSources()); | 98 NotificationService::AllSources()); |
| 104 } | 99 } |
| 105 | 100 |
| 106 SpellCheckHostImpl::~SpellCheckHostImpl() { | 101 SpellCheckHostImpl::~SpellCheckHostImpl() { |
| 107 if (file_ != base::kInvalidPlatformFileValue) | 102 if (file_ != base::kInvalidPlatformFileValue) |
| 108 base::ClosePlatformFile(file_); | 103 base::ClosePlatformFile(file_); |
| 109 } | 104 } |
| 110 | 105 |
| 111 void SpellCheckHostImpl::Initialize() { | 106 void SpellCheckHostImpl::Initialize() { |
| 112 if (SpellCheckerPlatform::SpellCheckerAvailable() && | 107 if (SpellCheckerPlatform::SpellCheckerAvailable() && |
| 113 SpellCheckerPlatform::PlatformSupportsLanguage(language_)) { | 108 SpellCheckerPlatform::PlatformSupportsLanguage(language_)) { |
| 114 #if defined(OS_MACOSX) | 109 #if defined(OS_MACOSX) |
| 115 RecordSpellCheckStats(true, language_); | 110 RecordSpellCheckStats(true, language_); |
| 116 #endif | 111 #endif |
| 117 use_platform_spellchecker_ = true; | 112 use_platform_spellchecker_ = true; |
| 118 SpellCheckerPlatform::SetLanguage(language_); | 113 SpellCheckerPlatform::SetLanguage(language_); |
| 119 MessageLoop::current()->PostTask(FROM_HERE, | 114 MessageLoop::current()->PostTask(FROM_HERE, |
| 120 NewRunnableMethod(this, | 115 NewRunnableMethod(this, |
| 121 &SpellCheckHostImpl::InformProfileOfInitialization)); | 116 &SpellCheckHostImpl::InformProfileOfInitialization)); |
| 122 return; | 117 return; |
| 123 } | 118 } |
| 124 | 119 |
| 125 #if defined(OS_MACOSX) | 120 #if defined(OS_MACOSX) |
| 126 RecordSpellCheckStats(false, language_); | 121 RecordSpellCheckStats(false, language_); |
| 127 #endif | 122 #endif |
| 128 | 123 |
| 124 if (profile_) | |
| 125 profile_->LoadCustomDictionary(); | |
|
Hironori Bono
2011/10/13 09:29:59
It seems this code start loading a custom-dictiona
| |
| 129 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 126 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 130 NewRunnableMethod(this, | 127 NewRunnableMethod(this, |
| 131 &SpellCheckHostImpl::InitializeDictionaryLocation)); | 128 &SpellCheckHostImpl::InitializeDictionaryLocation)); |
| 132 } | 129 } |
| 133 | 130 |
| 134 void SpellCheckHostImpl::UnsetProfile() { | 131 void SpellCheckHostImpl::UnsetProfile() { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 136 | 133 |
| 137 profile_ = NULL; | 134 profile_ = NULL; |
| 138 request_context_getter_ = NULL; | 135 request_context_getter_ = NULL; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 166 profile_ ? profile_->GetCustomWords() : CustomWordList(), | 163 profile_ ? profile_->GetCustomWords() : CustomWordList(), |
| 167 GetLanguage(), | 164 GetLanguage(), |
| 168 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect))); | 165 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect))); |
| 169 } | 166 } |
| 170 | 167 |
| 171 void SpellCheckHostImpl::AddWord(const std::string& word) { | 168 void SpellCheckHostImpl::AddWord(const std::string& word) { |
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 173 | 170 |
| 174 if (profile_) | 171 if (profile_) |
| 175 profile_->CustomWordAddedLocally(word); | 172 profile_->CustomWordAddedLocally(word); |
| 176 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 173 |
| 177 NewRunnableMethod(this, | |
| 178 &SpellCheckHostImpl::WriteWordToCustomDictionary, word)); | |
| 179 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 174 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 180 !i.IsAtEnd(); i.Advance()) { | 175 !i.IsAtEnd(); i.Advance()) { |
| 181 i.GetCurrentValue()->Send(new SpellCheckMsg_WordAdded(word)); | 176 i.GetCurrentValue()->Send(new SpellCheckMsg_WordAdded(word)); |
| 182 } | 177 } |
| 183 } | 178 } |
| 184 | 179 |
| 185 void SpellCheckHostImpl::InitializeDictionaryLocation() { | 180 void SpellCheckHostImpl::InitializeDictionaryLocation() { |
| 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 187 | 182 |
| 188 // Initialize the BDICT path. This initialization should be in the FILE thread | 183 // Initialize the BDICT path. This initialization should be in the FILE thread |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 request_context_getter_) { | 224 request_context_getter_) { |
| 230 // We download from the ui thread because we need to know that | 225 // We download from the ui thread because we need to know that |
| 231 // |request_context_getter_| is still valid before initiating the download. | 226 // |request_context_getter_| is still valid before initiating the download. |
| 232 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 227 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 233 NewRunnableMethod(this, &SpellCheckHostImpl::DownloadDictionary)); | 228 NewRunnableMethod(this, &SpellCheckHostImpl::DownloadDictionary)); |
| 234 return; | 229 return; |
| 235 } | 230 } |
| 236 | 231 |
| 237 request_context_getter_ = NULL; | 232 request_context_getter_ = NULL; |
| 238 | 233 |
| 239 scoped_ptr<CustomWordList> custom_words(new CustomWordList()); | |
| 240 if (file_ != base::kInvalidPlatformFileValue) { | |
| 241 // Load custom dictionary. | |
| 242 std::string contents; | |
| 243 file_util::ReadFileToString(custom_dictionary_file_, &contents); | |
| 244 CustomWordList list_of_words; | |
| 245 base::SplitString(contents, '\n', &list_of_words); | |
| 246 for (size_t i = 0; i < list_of_words.size(); ++i) | |
| 247 custom_words->push_back(list_of_words[i]); | |
| 248 } | |
| 249 | |
| 250 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 234 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 251 NewRunnableMethod( | 235 NewRunnableMethod( |
| 252 this, | 236 this, |
| 253 &SpellCheckHostImpl::InformProfileOfInitializationWithCustomWords, | 237 &SpellCheckHostImpl::InformProfileOfInitialization)); |
| 254 custom_words.release())); | |
| 255 } | 238 } |
| 256 | 239 |
| 257 void SpellCheckHostImpl::InitializeOnFileThread() { | 240 void SpellCheckHostImpl::InitializeOnFileThread() { |
| 258 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 241 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 259 | 242 |
| 260 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 243 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 261 NewRunnableMethod(this, &SpellCheckHostImpl::Initialize)); | 244 NewRunnableMethod(this, &SpellCheckHostImpl::Initialize)); |
| 262 } | 245 } |
| 263 | 246 |
| 264 void SpellCheckHostImpl::InformProfileOfInitialization() { | 247 void SpellCheckHostImpl::InformProfileOfInitialization() { |
| 265 InformProfileOfInitializationWithCustomWords(NULL); | |
| 266 } | |
| 267 | |
| 268 void SpellCheckHostImpl::InformProfileOfInitializationWithCustomWords( | |
| 269 CustomWordList* custom_words) { | |
| 270 // Non-null |custom_words| should be given only if the profile is available | |
| 271 // for simplifying the life-cycle management of the word list. | |
| 272 DCHECK(profile_ || !custom_words); | |
| 273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 274 | 249 |
| 275 if (profile_) | 250 if (profile_) |
| 276 profile_->SpellCheckHostInitialized(custom_words); | 251 profile_->SpellCheckHostInitialized(); |
| 277 | 252 |
| 278 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 253 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 279 !i.IsAtEnd(); i.Advance()) { | 254 !i.IsAtEnd(); i.Advance()) { |
| 280 RenderProcessHost* process = i.GetCurrentValue(); | 255 RenderProcessHost* process = i.GetCurrentValue(); |
| 281 if (process) | 256 if (process) |
| 282 InitForRenderer(process); | 257 InitForRenderer(process); |
| 283 } | 258 } |
| 284 } | 259 } |
| 285 | 260 |
| 286 void SpellCheckHostImpl::DownloadDictionary() { | 261 void SpellCheckHostImpl::DownloadDictionary() { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 301 } | 276 } |
| 302 GURL url = GURL(std::string(kDownloadServerUrl) + | 277 GURL url = GURL(std::string(kDownloadServerUrl) + |
| 303 StringToLowerASCII(bdict_file)); | 278 StringToLowerASCII(bdict_file)); |
| 304 fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this)); | 279 fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this)); |
| 305 fetcher_->set_request_context(request_context_getter_); | 280 fetcher_->set_request_context(request_context_getter_); |
| 306 tried_to_download_ = true; | 281 tried_to_download_ = true; |
| 307 fetcher_->Start(); | 282 fetcher_->Start(); |
| 308 request_context_getter_ = NULL; | 283 request_context_getter_ = NULL; |
| 309 } | 284 } |
| 310 | 285 |
| 311 void SpellCheckHostImpl::WriteWordToCustomDictionary(const std::string& word) { | |
| 312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 313 | |
| 314 // Stored in UTF-8. | |
| 315 std::string word_to_add(word + "\n"); | |
| 316 FILE* f = file_util::OpenFile(custom_dictionary_file_, "a+"); | |
| 317 if (f) | |
| 318 fputs(word_to_add.c_str(), f); | |
| 319 file_util::CloseFile(f); | |
| 320 } | |
| 321 | |
| 322 void SpellCheckHostImpl::OnURLFetchComplete(const URLFetcher* source, | 286 void SpellCheckHostImpl::OnURLFetchComplete(const URLFetcher* source, |
| 323 const GURL& url, | 287 const GURL& url, |
| 324 const net::URLRequestStatus& status, | 288 const net::URLRequestStatus& status, |
| 325 int response_code, | 289 int response_code, |
| 326 const net::ResponseCookies& cookies, | 290 const net::ResponseCookies& cookies, |
| 327 const std::string& data) { | 291 const std::string& data) { |
| 328 DCHECK(source); | 292 DCHECK(source); |
| 329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 293 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 330 fetcher_.reset(); | 294 fetcher_.reset(); |
| 331 | 295 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 432 return file_; | 396 return file_; |
| 433 } | 397 } |
| 434 | 398 |
| 435 const std::string& SpellCheckHostImpl::GetLanguage() const { | 399 const std::string& SpellCheckHostImpl::GetLanguage() const { |
| 436 return language_; | 400 return language_; |
| 437 } | 401 } |
| 438 | 402 |
| 439 bool SpellCheckHostImpl::IsUsingPlatformChecker() const { | 403 bool SpellCheckHostImpl::IsUsingPlatformChecker() const { |
| 440 return use_platform_spellchecker_; | 404 return use_platform_spellchecker_; |
| 441 } | 405 } |
| OLD | NEW |