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/bind.h" | |
| 9 #include "base/file_util.h" | 10 #include "base/file_util.h" | 
| 10 #include "base/logging.h" | 11 #include "base/logging.h" | 
| 11 #include "base/path_service.h" | 12 #include "base/path_service.h" | 
| 12 #include "base/string_split.h" | 13 #include "base/string_split.h" | 
| 13 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" | 
| 14 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" | 
| 15 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" | 
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" | 
| 17 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 18 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 
| 18 #include "chrome/browser/spellchecker/spellcheck_profile_provider.h" | 19 #include "chrome/browser/spellchecker/spellcheck_profile_provider.h" | 
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 #endif | 72 #endif | 
| 72 | 73 | 
| 73 #if defined(OS_WIN) | 74 #if defined(OS_WIN) | 
| 74 FilePath GetFallbackFilePath(const FilePath& first_choice) { | 75 FilePath GetFallbackFilePath(const FilePath& first_choice) { | 
| 75 FilePath dict_dir; | 76 FilePath dict_dir; | 
| 76 PathService::Get(chrome::DIR_USER_DATA, &dict_dir); | 77 PathService::Get(chrome::DIR_USER_DATA, &dict_dir); | 
| 77 return dict_dir.Append(first_choice.BaseName()); | 78 return dict_dir.Append(first_choice.BaseName()); | 
| 78 } | 79 } | 
| 79 #endif | 80 #endif | 
| 80 | 81 | 
| 82 void CloseDictionary(base::PlatformFile file) { | |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 84 base::ClosePlatformFile(file); | |
| 85 } | |
| 86 | |
| 81 } // namespace | 87 } // namespace | 
| 82 | 88 | 
| 83 // Constructed on UI thread. | 89 // Constructed on UI thread. | 
| 84 SpellCheckHostImpl::SpellCheckHostImpl( | 90 SpellCheckHostImpl::SpellCheckHostImpl( | 
| 85 SpellCheckProfileProvider* profile, | 91 SpellCheckProfileProvider* profile, | 
| 86 const std::string& language, | 92 const std::string& language, | 
| 87 net::URLRequestContextGetter* request_context_getter, | 93 net::URLRequestContextGetter* request_context_getter, | 
| 88 SpellCheckHostMetrics* metrics) | 94 SpellCheckHostMetrics* metrics) | 
| 89 : profile_(profile), | 95 : profile_(profile), | 
| 90 language_(language), | 96 language_(language), | 
| 91 file_(base::kInvalidPlatformFileValue), | 97 file_(base::kInvalidPlatformFileValue), | 
| 92 tried_to_download_(false), | 98 tried_to_download_(false), | 
| 93 use_platform_spellchecker_(false), | 99 use_platform_spellchecker_(false), | 
| 94 request_context_getter_(request_context_getter), | 100 request_context_getter_(request_context_getter), | 
| 95 metrics_(metrics) { | 101 metrics_(metrics), | 
| 102 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 96 DCHECK(profile_); | 103 DCHECK(profile_); | 
| 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 98 | 105 | 
| 99 FilePath personal_file_directory; | 106 FilePath personal_file_directory; | 
| 100 PathService::Get(chrome::DIR_USER_DATA, &personal_file_directory); | 107 PathService::Get(chrome::DIR_USER_DATA, &personal_file_directory); | 
| 101 custom_dictionary_file_ = | 108 custom_dictionary_file_ = | 
| 102 personal_file_directory.Append(chrome::kCustomDictionaryFileName); | 109 personal_file_directory.Append(chrome::kCustomDictionaryFileName); | 
| 103 | 110 | 
| 104 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 111 registrar_.Add(weak_ptr_factory_.GetWeakPtr(), | 
| 112 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | |
| 105 content::NotificationService::AllSources()); | 113 content::NotificationService::AllSources()); | 
| 106 } | 114 } | 
| 107 | 115 | 
| 108 SpellCheckHostImpl::~SpellCheckHostImpl() { | 116 SpellCheckHostImpl::~SpellCheckHostImpl() { | 
| 109 if (file_ != base::kInvalidPlatformFileValue) | 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 110 base::ClosePlatformFile(file_); | 118 | 
| 119 if (file_ != base::kInvalidPlatformFileValue) { | |
| 120 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 121 base::Bind(&CloseDictionary, file_)); | |
| 122 file_ = base::kInvalidPlatformFileValue; | |
| 123 } | |
| 111 } | 124 } | 
| 112 | 125 | 
| 113 void SpellCheckHostImpl::Initialize() { | 126 void SpellCheckHostImpl::Initialize() { | 
| 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 128 | |
| 114 if (SpellCheckerPlatform::SpellCheckerAvailable() && | 129 if (SpellCheckerPlatform::SpellCheckerAvailable() && | 
| 115 SpellCheckerPlatform::PlatformSupportsLanguage(language_)) { | 130 SpellCheckerPlatform::PlatformSupportsLanguage(language_)) { | 
| 116 #if defined(OS_MACOSX) | 131 #if defined(OS_MACOSX) | 
| 117 RecordSpellCheckStats(true, language_); | 132 RecordSpellCheckStats(true, language_); | 
| 118 #endif | 133 #endif | 
| 119 use_platform_spellchecker_ = true; | 134 use_platform_spellchecker_ = true; | 
| 120 SpellCheckerPlatform::SetLanguage(language_); | 135 SpellCheckerPlatform::SetLanguage(language_); | 
| 121 MessageLoop::current()->PostTask(FROM_HERE, | 136 MessageLoop::current()->PostTask(FROM_HERE, | 
| 122 NewRunnableMethod(this, | 137 base::Bind(&SpellCheckHostImpl::InformProfileOfInitialization, | 
| 123 &SpellCheckHostImpl::InformProfileOfInitialization)); | 138 weak_ptr_factory_.GetWeakPtr())); | 
| 124 return; | 139 return; | 
| 125 } | 140 } | 
| 126 | 141 | 
| 127 #if defined(OS_MACOSX) | 142 #if defined(OS_MACOSX) | 
| 128 RecordSpellCheckStats(false, language_); | 143 RecordSpellCheckStats(false, language_); | 
| 129 #endif | 144 #endif | 
| 130 | 145 | 
| 131 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 146 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | 
| 132 NewRunnableMethod(this, | 147 base::Bind(&SpellCheckHostImpl::InitializeDictionaryLocation, | 
| 133 &SpellCheckHostImpl::InitializeDictionaryLocation)); | 148 base::Unretained(this)), | 
| 149 base::Bind(&SpellCheckHostImpl::InitializeDictionaryLocationComplete, | |
| 150 weak_ptr_factory_.GetWeakPtr())); | |
| 134 } | 151 } | 
| 135 | 152 | 
| 136 void SpellCheckHostImpl::UnsetProfile() { | 153 void SpellCheckHostImpl::UnsetProfile() { | 
| 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 138 | 155 | 
| 139 profile_ = NULL; | 156 profile_ = NULL; | 
| 140 request_context_getter_ = NULL; | 157 request_context_getter_ = NULL; | 
| 141 fetcher_.reset(); | 158 fetcher_.reset(); | 
| 142 registrar_.RemoveAll(); | 159 registrar_.RemoveAll(); | 
| 143 } | 160 } | 
| (...skipping 24 matching lines...) Expand all Loading... | |
| 168 profile_ ? profile_->GetCustomWords() : CustomWordList(), | 185 profile_ ? profile_->GetCustomWords() : CustomWordList(), | 
| 169 GetLanguage(), | 186 GetLanguage(), | 
| 170 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect))); | 187 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect))); | 
| 171 } | 188 } | 
| 172 | 189 | 
| 173 void SpellCheckHostImpl::AddWord(const std::string& word) { | 190 void SpellCheckHostImpl::AddWord(const std::string& word) { | 
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 175 | 192 | 
| 176 if (profile_) | 193 if (profile_) | 
| 177 profile_->CustomWordAddedLocally(word); | 194 profile_->CustomWordAddedLocally(word); | 
| 178 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 195 | 
| 179 NewRunnableMethod(this, | 196 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | 
| 180 &SpellCheckHostImpl::WriteWordToCustomDictionary, word)); | 197 base::Bind(&SpellCheckHostImpl::WriteWordToCustomDictionary, | 
| 181 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 198 base::Unretained(this), word), | 
| 182 !i.IsAtEnd(); i.Advance()) { | 199 base::Bind(&SpellCheckHostImpl::AddWordComplete, | 
| 183 i.GetCurrentValue()->Send(new SpellCheckMsg_WordAdded(word)); | 200 weak_ptr_factory_.GetWeakPtr(), word)); | 
| 184 } | |
| 185 } | 201 } | 
| 186 | 202 | 
| 187 void SpellCheckHostImpl::InitializeDictionaryLocation() { | 203 void SpellCheckHostImpl::InitializeDictionaryLocation() { | 
| 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
| 189 | 205 | 
| 190 // Initialize the BDICT path. This initialization should be in the FILE thread | 206 // Initialize the BDICT path. This initialization should be in the FILE thread | 
| 191 // because it checks if there is a "Dictionaries" directory and create it. | 207 // because it checks if there is a "Dictionaries" directory and create it. | 
| 192 if (bdict_file_path_.empty()) | 208 if (bdict_file_path_.empty()) | 
| 193 bdict_file_path_ = GetFirstChoiceFilePath(language_); | 209 bdict_file_path_ = GetFirstChoiceFilePath(language_); | 
| 194 | 210 | 
| 195 #if defined(OS_WIN) | 211 #if defined(OS_WIN) | 
| 196 // Check if the dictionary exists in the fallback location. If so, use it | 212 // Check if the dictionary exists in the fallback location. If so, use it | 
| 197 // rather than downloading anew. | 213 // rather than downloading anew. | 
| 198 FilePath fallback = GetFallbackFilePath(bdict_file_path_); | 214 FilePath fallback = GetFallbackFilePath(bdict_file_path_); | 
| 199 if (!file_util::PathExists(bdict_file_path_) && | 215 if (!file_util::PathExists(bdict_file_path_) && | 
| 200 file_util::PathExists(fallback)) { | 216 file_util::PathExists(fallback)) { | 
| 201 bdict_file_path_ = fallback; | 217 bdict_file_path_ = fallback; | 
| 202 } | 218 } | 
| 203 #endif | 219 #endif | 
| 204 | 220 | 
| 205 InitializeInternal(); | |
| 206 } | |
| 207 | |
| 208 void SpellCheckHostImpl::InitializeInternal() { | |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 210 | |
| 211 if (!profile_) | 221 if (!profile_) | 
| 212 return; | 222 return; | 
| 213 | 223 | 
| 214 if (!VerifyBDict(bdict_file_path_)) { | 224 if (!VerifyBDict(bdict_file_path_)) { | 
| 215 DCHECK_EQ(file_, base::kInvalidPlatformFileValue); | 225 DCHECK_EQ(file_, base::kInvalidPlatformFileValue); | 
| 216 file_util::Delete(bdict_file_path_, false); | 226 file_util::Delete(bdict_file_path_, false); | 
| 217 | 227 | 
| 218 // Notify browser tests that this dictionary is corrupted. We also skip | 228 // Notify browser tests that this dictionary is corrupted. We also skip | 
| 219 // downloading the dictionary when we run this function on browser tests. | 229 // downloading the dictionary when we run this function on browser tests. | 
| 220 if (SignalStatusEvent(BDICT_CORRUPTED)) | 230 if (SignalStatusEvent(BDICT_CORRUPTED)) | 
| 221 tried_to_download_ = true; | 231 tried_to_download_ = true; | 
| 222 } else { | 232 } else { | 
| 223 file_ = base::CreatePlatformFile( | 233 file_ = base::CreatePlatformFile( | 
| 224 bdict_file_path_, | 234 bdict_file_path_, | 
| 225 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_OPEN, | 235 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_OPEN, | 
| 226 NULL, NULL); | 236 NULL, NULL); | 
| 227 } | 237 } | 
| 228 | 238 | 
| 229 // File didn't exist. Download it. | 239 // File didn't exist. Download it. | 
| 230 if (file_ == base::kInvalidPlatformFileValue && !tried_to_download_ && | 240 if (file_ == base::kInvalidPlatformFileValue && !tried_to_download_ && | 
| 231 request_context_getter_) { | 241 request_context_getter_) { | 
| 242 // Return this function so InitializeDictionaryLocationComplete() can start | |
| 243 // downloading the dictionary. | |
| 244 return; | |
| 245 } | |
| 246 | |
| 247 request_context_getter_ = NULL; | |
| 248 | |
| 249 custom_words_.reset(new CustomWordList()); | |
| 250 if (file_ != base::kInvalidPlatformFileValue) | |
| 251 LoadCustomDictionary(custom_words_.get()); | |
| 252 } | |
| 253 | |
| 254 void SpellCheckHostImpl::InitializeDictionaryLocationComplete() { | |
| 255 if (file_ == base::kInvalidPlatformFileValue && !tried_to_download_ && | |
| 256 request_context_getter_) { | |
| 232 // We download from the ui thread because we need to know that | 257 // We download from the ui thread because we need to know that | 
| 233 // |request_context_getter_| is still valid before initiating the download. | 258 // |request_context_getter_| is still valid before initiating the download. | 
| 234 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 259 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 
| 235 NewRunnableMethod(this, &SpellCheckHostImpl::DownloadDictionary)); | 260 base::Bind(&SpellCheckHostImpl::DownloadDictionary, | 
| 236 return; | 261 weak_ptr_factory_.GetWeakPtr())); | 
| 262 } else { | |
| 263 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 264 base::Bind( | |
| 265 &SpellCheckHostImpl::InformProfileOfInitializationWithCustomWords, | |
| 266 weak_ptr_factory_.GetWeakPtr(), | |
| 267 custom_words_.release())); | |
| 237 } | 268 } | 
| 238 | |
| 239 request_context_getter_ = NULL; | |
| 240 | |
| 241 scoped_ptr<CustomWordList> custom_words(new CustomWordList()); | |
| 242 if (file_ != base::kInvalidPlatformFileValue) | |
| 243 LoadCustomDictionary(custom_words.get()); | |
| 244 | |
| 245 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 246 NewRunnableMethod( | |
| 247 this, | |
| 248 &SpellCheckHostImpl::InformProfileOfInitializationWithCustomWords, | |
| 249 custom_words.release())); | |
| 250 } | |
| 251 | |
| 252 void SpellCheckHostImpl::InitializeOnFileThread() { | |
| 253 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 254 | |
| 255 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 256 NewRunnableMethod(this, &SpellCheckHostImpl::Initialize)); | |
| 257 } | 269 } | 
| 258 | 270 | 
| 259 void SpellCheckHostImpl::InformProfileOfInitialization() { | 271 void SpellCheckHostImpl::InformProfileOfInitialization() { | 
| 260 InformProfileOfInitializationWithCustomWords(NULL); | 272 InformProfileOfInitializationWithCustomWords(NULL); | 
| 261 } | 273 } | 
| 262 | 274 | 
| 263 void SpellCheckHostImpl::InformProfileOfInitializationWithCustomWords( | 275 void SpellCheckHostImpl::InformProfileOfInitializationWithCustomWords( | 
| 264 CustomWordList* custom_words) { | 276 CustomWordList* custom_words) { | 
| 265 // Non-null |custom_words| should be given only if the profile is available | 277 // Non-null |custom_words| should be given only if the profile is available | 
| 266 // for simplifying the life-cycle management of the word list. | 278 // for simplifying the life-cycle management of the word list. | 
| 267 DCHECK(profile_ || !custom_words); | 279 DCHECK(profile_ || !custom_words); | 
| 268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 269 | 281 | 
| 270 if (profile_) | 282 if (profile_) | 
| 271 profile_->SpellCheckHostInitialized(custom_words); | 283 profile_->SpellCheckHostInitialized(custom_words); | 
| 272 | 284 | 
| 273 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 285 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 
| 274 !i.IsAtEnd(); i.Advance()) { | 286 !i.IsAtEnd(); i.Advance()) { | 
| 275 RenderProcessHost* process = i.GetCurrentValue(); | 287 RenderProcessHost* process = i.GetCurrentValue(); | 
| 276 if (process) | 288 if (process) | 
| 277 InitForRenderer(process); | 289 InitForRenderer(process); | 
| 278 } | 290 } | 
| 279 } | 291 } | 
| 280 | 292 | 
| 281 void SpellCheckHostImpl::DownloadDictionary() { | 293 void SpellCheckHostImpl::DownloadDictionary() { | 
| 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 283 | 295 DCHECK(request_context_getter_); | 
| 284 if (!request_context_getter_) { | |
| 285 InitializeOnFileThread(); | |
| 286 return; | |
| 287 } | |
| 288 | 296 | 
| 289 // Determine URL of file to download. | 297 // Determine URL of file to download. | 
| 290 static const char kDownloadServerUrl[] = | 298 static const char kDownloadServerUrl[] = | 
| 291 "http://cache.pack.google.com/edgedl/chrome/dict/"; | 299 "http://cache.pack.google.com/edgedl/chrome/dict/"; | 
| 292 std::string bdict_file = bdict_file_path_.BaseName().MaybeAsASCII(); | 300 std::string bdict_file = bdict_file_path_.BaseName().MaybeAsASCII(); | 
| 293 if (bdict_file.empty()) { | 301 if (bdict_file.empty()) { | 
| 294 NOTREACHED(); | 302 NOTREACHED(); | 
| 295 return; | 303 return; | 
| 296 } | 304 } | 
| 297 GURL url = GURL(std::string(kDownloadServerUrl) + | 305 GURL url = GURL(std::string(kDownloadServerUrl) + | 
| 298 StringToLowerASCII(bdict_file)); | 306 StringToLowerASCII(bdict_file)); | 
| 299 fetcher_.reset(content::URLFetcher::Create( | 307 fetcher_.reset(content::URLFetcher::Create(url, content::URLFetcher::GET, | 
| 300 url, content::URLFetcher::GET, this)); | 308 weak_ptr_factory_.GetWeakPtr())); | 
| 301 fetcher_->SetRequestContext(request_context_getter_); | 309 fetcher_->SetRequestContext(request_context_getter_); | 
| 302 tried_to_download_ = true; | 310 tried_to_download_ = true; | 
| 303 fetcher_->Start(); | 311 fetcher_->Start(); | 
| 304 request_context_getter_ = NULL; | 312 request_context_getter_ = NULL; | 
| 305 } | 313 } | 
| 306 | 314 | 
| 307 void SpellCheckHostImpl::LoadCustomDictionary(CustomWordList* custom_words) { | 315 void SpellCheckHostImpl::LoadCustomDictionary(CustomWordList* custom_words) { | 
| 308 if (!custom_words) | 316 if (!custom_words) | 
| 309 return; | 317 return; | 
| 310 | 318 | 
| (...skipping 23 matching lines...) Expand all Loading... | |
| 334 } | 342 } | 
| 335 | 343 | 
| 336 void SpellCheckHostImpl::OnURLFetchComplete(const content::URLFetcher* source) { | 344 void SpellCheckHostImpl::OnURLFetchComplete(const content::URLFetcher* source) { | 
| 337 DCHECK(source); | 345 DCHECK(source); | 
| 338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 339 scoped_ptr<content::URLFetcher> fetcher_destructor(fetcher_.release()); | 347 scoped_ptr<content::URLFetcher> fetcher_destructor(fetcher_.release()); | 
| 340 | 348 | 
| 341 if ((source->GetResponseCode() / 100) != 2) { | 349 if ((source->GetResponseCode() / 100) != 2) { | 
| 342 // Initialize will not try to download the file a second time. | 350 // Initialize will not try to download the file a second time. | 
| 343 LOG(ERROR) << "Failure to download dictionary."; | 351 LOG(ERROR) << "Failure to download dictionary."; | 
| 344 InitializeOnFileThread(); | |
| 345 return; | 352 return; | 
| 346 } | 353 } | 
| 347 | 354 | 
| 348 // Basic sanity check on the dictionary. | 355 // Basic sanity check on the dictionary. | 
| 349 // There's the small chance that we might see a 200 status code for a body | 356 // There's the small chance that we might see a 200 status code for a body | 
| 350 // that represents some form of failure. | 357 // that represents some form of failure. | 
| 351 std::string data; | 358 std::string data; | 
| 352 source->GetResponseAsString(&data); | 359 source->GetResponseAsString(&data); | 
| 353 if (data.size() < 4 || data[0] != 'B' || data[1] != 'D' || data[2] != 'i' || | 360 if (data.size() < 4 || data[0] != 'B' || data[1] != 'D' || data[2] != 'i' || | 
| 354 data[3] != 'c') { | 361 data[3] != 'c') { | 
| 355 LOG(ERROR) << "Failure to download dictionary."; | 362 LOG(ERROR) << "Failure to download dictionary."; | 
| 356 InitializeOnFileThread(); | |
| 357 return; | 363 return; | 
| 358 } | 364 } | 
| 359 | 365 | 
| 360 data_ = data; | 366 data_ = data; | 
| 361 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 367 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | 
| 362 NewRunnableMethod(this, &SpellCheckHostImpl::SaveDictionaryData)); | 368 base::Bind(&SpellCheckHostImpl::SaveDictionaryData, | 
| 369 base::Unretained(this)), | |
| 370 base::Bind(&SpellCheckHostImpl::InformProfileOfInitialization, | |
| 371 weak_ptr_factory_.GetWeakPtr())); | |
| 363 } | 372 } | 
| 364 | 373 | 
| 365 void SpellCheckHostImpl::Observe(int type, | 374 void SpellCheckHostImpl::Observe(int type, | 
| 366 const content::NotificationSource& source, | 375 const content::NotificationSource& source, | 
| 367 const content::NotificationDetails& details) { | 376 const content::NotificationDetails& details) { | 
| 368 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CREATED); | 377 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CREATED); | 
| 369 RenderProcessHost* process = content::Source<RenderProcessHost>(source).ptr(); | 378 RenderProcessHost* process = content::Source<RenderProcessHost>(source).ptr(); | 
| 370 InitForRenderer(process); | 379 InitForRenderer(process); | 
| 371 } | 380 } | 
| 372 | 381 | 
| 373 void SpellCheckHostImpl::SaveDictionaryData() { | 382 void SpellCheckHostImpl::SaveDictionaryData() { | 
| 374 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 383 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
| 375 | 384 | 
| 376 // To prevent corrupted dictionary data from causing a renderer crash, scan | 385 // To prevent corrupted dictionary data from causing a renderer crash, scan | 
| 377 // the dictionary data and verify it is sane before save it to a file. | 386 // the dictionary data and verify it is sane before save it to a file. | 
| 378 bool verified = hunspell::BDict::Verify(data_.data(), data_.size()); | 387 bool verified = hunspell::BDict::Verify(data_.data(), data_.size()); | 
| 379 if (metrics_) | 388 if (metrics_) | 
| 380 metrics_->RecordDictionaryCorruptionStats(!verified); | 389 metrics_->RecordDictionaryCorruptionStats(!verified); | 
| 381 if (!verified) { | 390 if (!verified) { | 
| 382 LOG(ERROR) << "Failure to verify the downloaded dictionary."; | 391 LOG(ERROR) << "Failure to verify the downloaded dictionary."; | 
| 383 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 392 // Let PostTaskAndReply caller send to InformProfileOfInitialization | 
| 384 NewRunnableMethod(this, | |
| 385 &SpellCheckHostImpl::InformProfileOfInitialization)); | |
| 386 return; | 393 return; | 
| 387 } | 394 } | 
| 388 | 395 | 
| 389 size_t bytes_written = | 396 size_t bytes_written = | 
| 390 file_util::WriteFile(bdict_file_path_, data_.data(), data_.length()); | 397 file_util::WriteFile(bdict_file_path_, data_.data(), data_.length()); | 
| 391 if (bytes_written != data_.length()) { | 398 if (bytes_written != data_.length()) { | 
| 392 bool success = false; | 399 bool success = false; | 
| 393 #if defined(OS_WIN) | 400 #if defined(OS_WIN) | 
| 394 bdict_file_path_ = GetFallbackFilePath(bdict_file_path_); | 401 bdict_file_path_ = GetFallbackFilePath(bdict_file_path_); | 
| 395 bytes_written = | 402 bytes_written = | 
| 396 file_util::WriteFile(GetFallbackFilePath(bdict_file_path_), | 403 file_util::WriteFile(GetFallbackFilePath(bdict_file_path_), | 
| 397 data_.data(), data_.length()); | 404 data_.data(), data_.length()); | 
| 398 if (bytes_written == data_.length()) | 405 if (bytes_written == data_.length()) | 
| 399 success = true; | 406 success = true; | 
| 400 #endif | 407 #endif | 
| 401 data_.clear(); | 408 data_.clear(); | 
| 402 | 409 | 
| 403 if (!success) { | 410 if (!success) { | 
| 404 LOG(ERROR) << "Failure to save dictionary."; | 411 LOG(ERROR) << "Failure to save dictionary."; | 
| 405 file_util::Delete(bdict_file_path_, false); | 412 file_util::Delete(bdict_file_path_, false); | 
| 406 // To avoid trying to load a partially saved dictionary, shortcut the | 413 // To avoid trying to load a partially saved dictionary, shortcut the | 
| 407 // Initialize() call. | 414 // Initialize() call. Let PostTaskAndReply caller send to | 
| 408 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 415 // InformProfileOfInitialization. | 
| 409 NewRunnableMethod(this, | |
| 410 &SpellCheckHostImpl::InformProfileOfInitialization)); | |
| 411 return; | 416 return; | 
| 412 } | 417 } | 
| 413 } | 418 } | 
| 414 | 419 | 
| 415 data_.clear(); | 420 data_.clear(); | 
| 421 | |
| 
 
Hironori Bono
2011/11/04 03:46:59
nit: sorry, I forgot answering your question why w
 
 | |
| 416 Initialize(); | 422 Initialize(); | 
| 417 } | 423 } | 
| 418 | 424 | 
| 419 bool SpellCheckHostImpl::VerifyBDict(const FilePath& path) const { | 425 bool SpellCheckHostImpl::VerifyBDict(const FilePath& path) const { | 
| 420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
| 421 | 427 | 
| 422 // Read the dictionary file and scan its data. We need to close this file | 428 // Read the dictionary file and scan its data. We need to close this file | 
| 423 // after scanning its data so we can delete it and download a new dictionary | 429 // after scanning its data so we can delete it and download a new dictionary | 
| 424 // from our dictionary-download server if it is corrupted. | 430 // from our dictionary-download server if it is corrupted. | 
| 425 file_util::MemoryMappedFile map; | 431 file_util::MemoryMappedFile map; | 
| (...skipping 17 matching lines...) Expand all Loading... | |
| 443 return file_; | 449 return file_; | 
| 444 } | 450 } | 
| 445 | 451 | 
| 446 const std::string& SpellCheckHostImpl::GetLanguage() const { | 452 const std::string& SpellCheckHostImpl::GetLanguage() const { | 
| 447 return language_; | 453 return language_; | 
| 448 } | 454 } | 
| 449 | 455 | 
| 450 bool SpellCheckHostImpl::IsUsingPlatformChecker() const { | 456 bool SpellCheckHostImpl::IsUsingPlatformChecker() const { | 
| 451 return use_platform_spellchecker_; | 457 return use_platform_spellchecker_; | 
| 452 } | 458 } | 
| 459 | |
| 460 void SpellCheckHostImpl::AddWordComplete(const std::string& word) { | |
| 461 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 462 | |
| 463 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | |
| 464 !i.IsAtEnd(); i.Advance()) { | |
| 465 i.GetCurrentValue()->Send(new SpellCheckMsg_WordAdded(word)); | |
| 466 } | |
| 467 } | |
| OLD | NEW |