Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: chrome/browser/spellchecker/spellcheck_host_impl.cc

Issue 8345034: SpellCheck: the custom dictionary should be per profile. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Update. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 // We download from the ui thread because we need to know that 232 // We download from the ui thread because we need to know that
233 // |request_context_getter_| is still valid before initiating the download. 233 // |request_context_getter_| is still valid before initiating the download.
234 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 234 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
235 NewRunnableMethod(this, &SpellCheckHostImpl::DownloadDictionary)); 235 NewRunnableMethod(this, &SpellCheckHostImpl::DownloadDictionary));
236 return; 236 return;
237 } 237 }
238 238
239 request_context_getter_ = NULL; 239 request_context_getter_ = NULL;
240 240
241 scoped_ptr<CustomWordList> custom_words(new CustomWordList()); 241 scoped_ptr<CustomWordList> custom_words(new CustomWordList());
242 if (file_ != base::kInvalidPlatformFileValue) { 242 if (file_ != base::kInvalidPlatformFileValue)
243 // Load custom dictionary. 243 LoadCustomDictionary(custom_words.get());
244 std::string contents;
245 file_util::ReadFileToString(custom_dictionary_file_, &contents);
246 CustomWordList list_of_words;
247 base::SplitString(contents, '\n', &list_of_words);
248 for (size_t i = 0; i < list_of_words.size(); ++i)
249 custom_words->push_back(list_of_words[i]);
250 }
251 244
252 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 245 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
253 NewRunnableMethod( 246 NewRunnableMethod(
254 this, 247 this,
255 &SpellCheckHostImpl::InformProfileOfInitializationWithCustomWords, 248 &SpellCheckHostImpl::InformProfileOfInitializationWithCustomWords,
256 custom_words.release())); 249 custom_words.release()));
257 } 250 }
258 251
259 void SpellCheckHostImpl::InitializeOnFileThread() { 252 void SpellCheckHostImpl::InitializeOnFileThread() {
260 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); 253 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 } 296 }
304 GURL url = GURL(std::string(kDownloadServerUrl) + 297 GURL url = GURL(std::string(kDownloadServerUrl) +
305 StringToLowerASCII(bdict_file)); 298 StringToLowerASCII(bdict_file));
306 fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this)); 299 fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this));
307 fetcher_->set_request_context(request_context_getter_); 300 fetcher_->set_request_context(request_context_getter_);
308 tried_to_download_ = true; 301 tried_to_download_ = true;
309 fetcher_->Start(); 302 fetcher_->Start();
310 request_context_getter_ = NULL; 303 request_context_getter_ = NULL;
311 } 304 }
312 305
306 void SpellCheckHostImpl::LoadCustomDictionary(CustomWordList* custom_words) {
307 if (!custom_words)
308 return;
309
310 // Load custom dictionary for profile.
311 if (profile_)
312 profile_->LoadCustomDictionary(custom_words);
313
314 // Load custom dictionary.
315 std::string contents;
316 file_util::ReadFileToString(custom_dictionary_file_, &contents);
317 if (contents.empty())
318 return;
319
320 CustomWordList list_of_words;
321 base::SplitString(contents, '\n', &list_of_words);
322 for (size_t i = 0; i < list_of_words.size(); ++i) {
323 if (list_of_words[i] != "")
324 custom_words->push_back(list_of_words[i]);
325 }
326 }
327
313 void SpellCheckHostImpl::WriteWordToCustomDictionary(const std::string& word) { 328 void SpellCheckHostImpl::WriteWordToCustomDictionary(const std::string& word) {
314 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
315 330
316 // Stored in UTF-8. 331 if (profile_)
317 std::string word_to_add(word + "\n"); 332 profile_->WriteWordToCustomDictionary(word);
318 FILE* f = file_util::OpenFile(custom_dictionary_file_, "a+");
319 if (f)
320 fputs(word_to_add.c_str(), f);
321 file_util::CloseFile(f);
322 } 333 }
323 334
324 void SpellCheckHostImpl::OnURLFetchComplete(const URLFetcher* source) { 335 void SpellCheckHostImpl::OnURLFetchComplete(const URLFetcher* source) {
325 DCHECK(source); 336 DCHECK(source);
326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
327 scoped_ptr<URLFetcher> fetcher_destructor(fetcher_.release()); 338 scoped_ptr<URLFetcher> fetcher_destructor(fetcher_.release());
328 339
329 if ((source->response_code() / 100) != 2) { 340 if ((source->response_code() / 100) != 2) {
330 // Initialize will not try to download the file a second time. 341 // Initialize will not try to download the file a second time.
331 LOG(ERROR) << "Failure to download dictionary."; 342 LOG(ERROR) << "Failure to download dictionary.";
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 return file_; 442 return file_;
432 } 443 }
433 444
434 const std::string& SpellCheckHostImpl::GetLanguage() const { 445 const std::string& SpellCheckHostImpl::GetLanguage() const {
435 return language_; 446 return language_;
436 } 447 }
437 448
438 bool SpellCheckHostImpl::IsUsingPlatformChecker() const { 449 bool SpellCheckHostImpl::IsUsingPlatformChecker() const {
439 return use_platform_spellchecker_; 450 return use_platform_spellchecker_;
440 } 451 }
OLDNEW
« no previous file with comments | « chrome/browser/spellchecker/spellcheck_host_impl.h ('k') | chrome/browser/spellchecker/spellcheck_profile.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698