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