| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_service.h" | 5 #include "chrome/browser/spellchecker/spellcheck_service.h" |
| 6 | 6 |
| 7 #include "base/platform_file.h" | 7 #include "base/platform_file.h" |
| 8 #include "base/prefs/pref_member.h" | 8 #include "base/prefs/pref_member.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| 11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 12 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 14 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 13 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
| 15 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" | 14 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" |
| 16 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" | 15 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" |
| 17 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/common/spellcheck_messages.h" | 17 #include "chrome/common/spellcheck_messages.h" |
| 18 #include "components/user_prefs/user_prefs.h" |
| 19 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
| 22 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
| 23 #include "ipc/ipc_platform_file.h" | 24 #include "ipc/ipc_platform_file.h" |
| 24 | 25 |
| 25 using content::BrowserThread; | 26 using content::BrowserThread; |
| 26 using chrome::spellcheck_common::WordList; | 27 using chrome::spellcheck_common::WordList; |
| 27 | 28 |
| 28 // TODO(rlp): I do not like globals, but keeping these for now during | 29 // TODO(rlp): I do not like globals, but keeping these for now during |
| 29 // transition. | 30 // transition. |
| 30 // An event used by browser tests to receive status events from this class and | 31 // An event used by browser tests to receive status events from this class and |
| 31 // its derived classes. | 32 // its derived classes. |
| 32 base::WaitableEvent* g_status_event = NULL; | 33 base::WaitableEvent* g_status_event = NULL; |
| 33 SpellcheckService::EventType g_status_type = | 34 SpellcheckService::EventType g_status_type = |
| 34 SpellcheckService::BDICT_NOTINITIALIZED; | 35 SpellcheckService::BDICT_NOTINITIALIZED; |
| 35 | 36 |
| 36 SpellcheckService::SpellcheckService(Profile* profile) | 37 SpellcheckService::SpellcheckService(content::BrowserContext* context) |
| 37 : profile_(profile), | 38 : context_(context), |
| 38 weak_ptr_factory_(this) { | 39 weak_ptr_factory_(this) { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 40 PrefService* prefs = profile_->GetPrefs(); | 41 PrefService* prefs = user_prefs::UserPrefs::Get(context); |
| 41 pref_change_registrar_.Init(prefs); | 42 pref_change_registrar_.Init(prefs); |
| 42 | 43 |
| 43 std::string language_code; | 44 std::string language_code; |
| 44 std::string country_code; | 45 std::string country_code; |
| 45 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( | 46 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( |
| 46 prefs->GetString(prefs::kSpellCheckDictionary), | 47 prefs->GetString(prefs::kSpellCheckDictionary), |
| 47 &language_code, | 48 &language_code, |
| 48 &country_code); | 49 &country_code); |
| 49 feedback_sender_.reset(new spellcheck::FeedbackSender( | 50 feedback_sender_.reset(new spellcheck::FeedbackSender( |
| 50 profile->GetRequestContext(), language_code, country_code)); | 51 context->GetRequestContext(), language_code, country_code)); |
| 51 | 52 |
| 52 pref_change_registrar_.Add( | 53 pref_change_registrar_.Add( |
| 53 prefs::kEnableAutoSpellCorrect, | 54 prefs::kEnableAutoSpellCorrect, |
| 54 base::Bind(&SpellcheckService::OnEnableAutoSpellCorrectChanged, | 55 base::Bind(&SpellcheckService::OnEnableAutoSpellCorrectChanged, |
| 55 base::Unretained(this))); | 56 base::Unretained(this))); |
| 56 pref_change_registrar_.Add( | 57 pref_change_registrar_.Add( |
| 57 prefs::kSpellCheckDictionary, | 58 prefs::kSpellCheckDictionary, |
| 58 base::Bind(&SpellcheckService::OnSpellCheckDictionaryChanged, | 59 base::Bind(&SpellcheckService::OnSpellCheckDictionaryChanged, |
| 59 base::Unretained(this))); | 60 base::Unretained(this))); |
| 60 pref_change_registrar_.Add( | 61 pref_change_registrar_.Add( |
| 61 prefs::kSpellCheckUseSpellingService, | 62 prefs::kSpellCheckUseSpellingService, |
| 62 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged, | 63 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged, |
| 63 base::Unretained(this))); | 64 base::Unretained(this))); |
| 64 pref_change_registrar_.Add( | 65 pref_change_registrar_.Add( |
| 65 prefs::kEnableContinuousSpellcheck, | 66 prefs::kEnableContinuousSpellcheck, |
| 66 base::Bind(&SpellcheckService::InitForAllRenderers, | 67 base::Bind(&SpellcheckService::InitForAllRenderers, |
| 67 base::Unretained(this))); | 68 base::Unretained(this))); |
| 68 | 69 |
| 69 OnSpellCheckDictionaryChanged(); | 70 OnSpellCheckDictionaryChanged(); |
| 70 | 71 |
| 71 custom_dictionary_.reset(new SpellcheckCustomDictionary(profile_->GetPath())); | 72 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath())); |
| 72 custom_dictionary_->AddObserver(this); | 73 custom_dictionary_->AddObserver(this); |
| 73 custom_dictionary_->Load(); | 74 custom_dictionary_->Load(); |
| 74 | 75 |
| 75 registrar_.Add(this, | 76 registrar_.Add(this, |
| 76 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 77 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 77 content::NotificationService::AllSources()); | 78 content::NotificationService::AllSources()); |
| 78 } | 79 } |
| 79 | 80 |
| 80 SpellcheckService::~SpellcheckService() { | 81 SpellcheckService::~SpellcheckService() { |
| 81 // Remove pref observers | 82 // Remove pref observers |
| 82 pref_change_registrar_.RemoveAll(); | 83 pref_change_registrar_.RemoveAll(); |
| 83 } | 84 } |
| 84 | 85 |
| 85 // static | 86 // static |
| 86 int SpellcheckService::GetSpellCheckLanguages( | 87 int SpellcheckService::GetSpellCheckLanguages( |
| 87 Profile* profile, | 88 content::BrowserContext* context, |
| 88 std::vector<std::string>* languages) { | 89 std::vector<std::string>* languages) { |
| 90 PrefService* prefs = user_prefs::UserPrefs::Get(context); |
| 89 StringPrefMember accept_languages_pref; | 91 StringPrefMember accept_languages_pref; |
| 90 StringPrefMember dictionary_language_pref; | 92 StringPrefMember dictionary_language_pref; |
| 91 accept_languages_pref.Init(prefs::kAcceptLanguages, profile->GetPrefs()); | 93 accept_languages_pref.Init(prefs::kAcceptLanguages, prefs); |
| 92 dictionary_language_pref.Init(prefs::kSpellCheckDictionary, | 94 dictionary_language_pref.Init(prefs::kSpellCheckDictionary, prefs); |
| 93 profile->GetPrefs()); | |
| 94 std::string dictionary_language = dictionary_language_pref.GetValue(); | 95 std::string dictionary_language = dictionary_language_pref.GetValue(); |
| 95 | 96 |
| 96 // Now scan through the list of accept languages, and find possible mappings | 97 // Now scan through the list of accept languages, and find possible mappings |
| 97 // from this list to the existing list of spell check languages. | 98 // from this list to the existing list of spell check languages. |
| 98 std::vector<std::string> accept_languages; | 99 std::vector<std::string> accept_languages; |
| 99 | 100 |
| 100 #if defined(OS_MACOSX) | 101 #if defined(OS_MACOSX) |
| 101 if (spellcheck_mac::SpellCheckerAvailable()) | 102 if (spellcheck_mac::SpellCheckerAvailable()) |
| 102 spellcheck_mac::GetAvailableLanguages(&accept_languages); | 103 spellcheck_mac::GetAvailableLanguages(&accept_languages); |
| 103 else | 104 else |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 151 |
| 151 void SpellcheckService::StartRecordingMetrics(bool spellcheck_enabled) { | 152 void SpellcheckService::StartRecordingMetrics(bool spellcheck_enabled) { |
| 152 metrics_.reset(new SpellCheckHostMetrics()); | 153 metrics_.reset(new SpellCheckHostMetrics()); |
| 153 metrics_->RecordEnabledStats(spellcheck_enabled); | 154 metrics_->RecordEnabledStats(spellcheck_enabled); |
| 154 OnUseSpellingServiceChanged(); | 155 OnUseSpellingServiceChanged(); |
| 155 } | 156 } |
| 156 | 157 |
| 157 void SpellcheckService::InitForRenderer(content::RenderProcessHost* process) { | 158 void SpellcheckService::InitForRenderer(content::RenderProcessHost* process) { |
| 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 159 | 160 |
| 160 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext()); | 161 content::BrowserContext* context = process->GetBrowserContext(); |
| 161 if (SpellcheckServiceFactory::GetForProfile(profile) != this) | 162 if (SpellcheckServiceFactory::GetForContext(context) != this) |
| 162 return; | 163 return; |
| 163 | 164 |
| 164 PrefService* prefs = profile->GetPrefs(); | 165 PrefService* prefs = user_prefs::UserPrefs::Get(context); |
| 165 IPC::PlatformFileForTransit file = IPC::InvalidPlatformFileForTransit(); | 166 IPC::PlatformFileForTransit file = IPC::InvalidPlatformFileForTransit(); |
| 166 | 167 |
| 167 if (hunspell_dictionary_->GetDictionaryFile() != | 168 if (hunspell_dictionary_->GetDictionaryFile() != |
| 168 base::kInvalidPlatformFileValue) { | 169 base::kInvalidPlatformFileValue) { |
| 169 #if defined(OS_POSIX) | 170 #if defined(OS_POSIX) |
| 170 file = base::FileDescriptor(hunspell_dictionary_->GetDictionaryFile(), | 171 file = base::FileDescriptor(hunspell_dictionary_->GetDictionaryFile(), |
| 171 false); | 172 false); |
| 172 #elif defined(OS_WIN) | 173 #elif defined(OS_WIN) |
| 173 BOOL ok = ::DuplicateHandle(::GetCurrentProcess(), | 174 BOOL ok = ::DuplicateHandle(::GetCurrentProcess(), |
| 174 hunspell_dictionary_->GetDictionaryFile(), | 175 hunspell_dictionary_->GetDictionaryFile(), |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 content::RenderProcessHost::AllHostsIterator()); | 286 content::RenderProcessHost::AllHostsIterator()); |
| 286 !i.IsAtEnd(); i.Advance()) { | 287 !i.IsAtEnd(); i.Advance()) { |
| 287 content::RenderProcessHost* process = i.GetCurrentValue(); | 288 content::RenderProcessHost* process = i.GetCurrentValue(); |
| 288 process->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled)); | 289 process->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled)); |
| 289 } | 290 } |
| 290 } | 291 } |
| 291 | 292 |
| 292 void SpellcheckService::OnSpellCheckDictionaryChanged() { | 293 void SpellcheckService::OnSpellCheckDictionaryChanged() { |
| 293 if (hunspell_dictionary_.get()) | 294 if (hunspell_dictionary_.get()) |
| 294 hunspell_dictionary_->RemoveObserver(this); | 295 hunspell_dictionary_->RemoveObserver(this); |
| 296 PrefService* prefs = user_prefs::UserPrefs::Get(context_); |
| 297 DCHECK(prefs); |
| 298 |
| 295 std::string dictionary = | 299 std::string dictionary = |
| 296 profile_->GetPrefs()->GetString(prefs::kSpellCheckDictionary); | 300 prefs->GetString(prefs::kSpellCheckDictionary); |
| 297 hunspell_dictionary_.reset(new SpellcheckHunspellDictionary( | 301 hunspell_dictionary_.reset(new SpellcheckHunspellDictionary( |
| 298 dictionary, profile_->GetRequestContext(), this)); | 302 dictionary, context_->GetRequestContext(), this)); |
| 299 hunspell_dictionary_->AddObserver(this); | 303 hunspell_dictionary_->AddObserver(this); |
| 300 hunspell_dictionary_->Load(); | 304 hunspell_dictionary_->Load(); |
| 301 std::string language_code; | 305 std::string language_code; |
| 302 std::string country_code; | 306 std::string country_code; |
| 303 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( | 307 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( |
| 304 dictionary, &language_code, &country_code); | 308 dictionary, &language_code, &country_code); |
| 305 feedback_sender_->OnLanguageCountryChange(language_code, country_code); | 309 feedback_sender_->OnLanguageCountryChange(language_code, country_code); |
| 306 } | 310 } |
| 307 | 311 |
| 308 void SpellcheckService::OnUseSpellingServiceChanged() { | 312 void SpellcheckService::OnUseSpellingServiceChanged() { |
| 309 bool enabled = pref_change_registrar_.prefs()->GetBoolean( | 313 bool enabled = pref_change_registrar_.prefs()->GetBoolean( |
| 310 prefs::kSpellCheckUseSpellingService); | 314 prefs::kSpellCheckUseSpellingService); |
| 311 if (metrics_) | 315 if (metrics_) |
| 312 metrics_->RecordSpellingServiceStats(enabled); | 316 metrics_->RecordSpellingServiceStats(enabled); |
| 313 } | 317 } |
| OLD | NEW |