| 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/logging.h" | 7 #include "base/logging.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/supports_user_data.h" |
| 11 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 12 #include "chrome/browser/spellchecker/feedback_sender.h" | 13 #include "chrome/browser/spellchecker/feedback_sender.h" |
| 13 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 14 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 14 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 15 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
| 15 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" | 16 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" |
| 16 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" | 17 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" |
| 17 #include "chrome/browser/spellchecker/spelling_service_client.h" | 18 #include "chrome/browser/spellchecker/spelling_service_client.h" |
| 18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/common/spellcheck_common.h" | 20 #include "chrome/common/spellcheck_common.h" |
| 20 #include "chrome/common/spellcheck_messages.h" | 21 #include "chrome/common/spellcheck_messages.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 35 base::WaitableEvent* g_status_event = NULL; | 36 base::WaitableEvent* g_status_event = NULL; |
| 36 SpellcheckService::EventType g_status_type = | 37 SpellcheckService::EventType g_status_type = |
| 37 SpellcheckService::BDICT_NOTINITIALIZED; | 38 SpellcheckService::BDICT_NOTINITIALIZED; |
| 38 | 39 |
| 39 SpellcheckService::SpellcheckService(content::BrowserContext* context) | 40 SpellcheckService::SpellcheckService(content::BrowserContext* context) |
| 40 : context_(context), | 41 : context_(context), |
| 41 weak_ptr_factory_(this) { | 42 weak_ptr_factory_(this) { |
| 42 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 43 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 43 PrefService* prefs = user_prefs::UserPrefs::Get(context); | 44 PrefService* prefs = user_prefs::UserPrefs::Get(context); |
| 44 pref_change_registrar_.Init(prefs); | 45 pref_change_registrar_.Init(prefs); |
| 45 | |
| 46 StringListPrefMember dictionaries_pref; | 46 StringListPrefMember dictionaries_pref; |
| 47 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, prefs); | 47 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, prefs); |
| 48 std::string first_of_dictionaries; | 48 std::string first_of_dictionaries; |
| 49 if (!dictionaries_pref.GetValue().empty()) | 49 if (!dictionaries_pref.GetValue().empty()) |
| 50 first_of_dictionaries = dictionaries_pref.GetValue().front(); | 50 first_of_dictionaries = dictionaries_pref.GetValue().front(); |
| 51 | 51 |
| 52 // For preference migration, set the new preference kSpellCheckDictionaries | 52 // For preference migration, set the new preference kSpellCheckDictionaries |
| 53 // to be the same as the old kSpellCheckDictionary. | 53 // to be the same as the old kSpellCheckDictionary. |
| 54 StringPrefMember single_dictionary_pref; | 54 StringPrefMember single_dictionary_pref; |
| 55 single_dictionary_pref.Init(prefs::kSpellCheckDictionary, prefs); | 55 single_dictionary_pref.Init(prefs::kSpellCheckDictionary, prefs); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 71 &country_code); | 71 &country_code); |
| 72 feedback_sender_.reset(new spellcheck::FeedbackSender( | 72 feedback_sender_.reset(new spellcheck::FeedbackSender( |
| 73 context->GetRequestContext(), language_code, country_code)); | 73 context->GetRequestContext(), language_code, country_code)); |
| 74 | 74 |
| 75 pref_change_registrar_.Add( | 75 pref_change_registrar_.Add( |
| 76 prefs::kEnableAutoSpellCorrect, | 76 prefs::kEnableAutoSpellCorrect, |
| 77 base::Bind(&SpellcheckService::OnEnableAutoSpellCorrectChanged, | 77 base::Bind(&SpellcheckService::OnEnableAutoSpellCorrectChanged, |
| 78 base::Unretained(this))); | 78 base::Unretained(this))); |
| 79 pref_change_registrar_.Add( | 79 pref_change_registrar_.Add( |
| 80 prefs::kSpellCheckDictionaries, | 80 prefs::kSpellCheckDictionaries, |
| 81 base::Bind(&SpellcheckService::OnSpellCheckDictionaryChanged, | 81 base::Bind(&SpellcheckService::OnSpellCheckDictionariesChanged, |
| 82 base::Unretained(this))); | 82 base::Unretained(this))); |
| 83 if (!chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) { | 83 if (!chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) { |
| 84 pref_change_registrar_.Add( | 84 pref_change_registrar_.Add( |
| 85 prefs::kSpellCheckUseSpellingService, | 85 prefs::kSpellCheckUseSpellingService, |
| 86 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged, | 86 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged, |
| 87 base::Unretained(this))); | 87 base::Unretained(this))); |
| 88 } | 88 } |
| 89 |
| 89 pref_change_registrar_.Add( | 90 pref_change_registrar_.Add( |
| 90 prefs::kEnableContinuousSpellcheck, | 91 prefs::kEnableContinuousSpellcheck, |
| 91 base::Bind(&SpellcheckService::InitForAllRenderers, | 92 base::Bind(&SpellcheckService::InitForAllRenderers, |
| 92 base::Unretained(this))); | 93 base::Unretained(this))); |
| 93 | 94 |
| 94 OnSpellCheckDictionaryChanged(); | 95 OnSpellCheckDictionariesChanged(); |
| 95 | 96 |
| 96 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath())); | 97 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath())); |
| 97 custom_dictionary_->AddObserver(this); | 98 custom_dictionary_->AddObserver(this); |
| 98 custom_dictionary_->Load(); | 99 custom_dictionary_->Load(); |
| 99 | 100 |
| 100 registrar_.Add(this, | 101 registrar_.Add(this, |
| 101 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 102 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 102 content::NotificationService::AllSources()); | 103 content::NotificationService::AllSources()); |
| 103 } | 104 } |
| 104 | 105 |
| 105 SpellcheckService::~SpellcheckService() { | 106 SpellcheckService::~SpellcheckService() { |
| 106 // Remove pref observers | 107 // Remove pref observers |
| 107 pref_change_registrar_.RemoveAll(); | 108 pref_change_registrar_.RemoveAll(); |
| 108 } | 109 } |
| 109 | 110 |
| 110 base::WeakPtr<SpellcheckService> SpellcheckService::GetWeakPtr() { | 111 base::WeakPtr<SpellcheckService> SpellcheckService::GetWeakPtr() { |
| 111 return weak_ptr_factory_.GetWeakPtr(); | 112 return weak_ptr_factory_.GetWeakPtr(); |
| 112 } | 113 } |
| 113 | 114 |
| 115 #if !defined(OS_MACOSX) |
| 114 // static | 116 // static |
| 115 int SpellcheckService::GetSpellCheckLanguages( | 117 size_t SpellcheckService::GetSpellCheckLanguages( |
| 116 base::SupportsUserData* context, | 118 base::SupportsUserData* context, |
| 117 std::vector<std::string>* languages) { | 119 std::vector<std::string>* languages) { |
| 118 PrefService* prefs = user_prefs::UserPrefs::Get(context); | 120 PrefService* prefs = user_prefs::UserPrefs::Get(context); |
| 119 StringPrefMember accept_languages_pref; | 121 StringPrefMember accept_languages_pref; |
| 120 accept_languages_pref.Init(prefs::kAcceptLanguages, prefs); | 122 accept_languages_pref.Init(prefs::kAcceptLanguages, prefs); |
| 121 | 123 |
| 122 std::string dictionary_language; | 124 std::vector<std::string> accept_languages; |
| 123 prefs->GetList(prefs::kSpellCheckDictionaries) | 125 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); |
| 124 ->GetString(0, &dictionary_language); | |
| 125 | 126 |
| 126 // Now scan through the list of accept languages, and find possible mappings | 127 StringListPrefMember dictionaries_pref; |
| 127 // from this list to the existing list of spell check languages. | 128 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, prefs); |
| 128 std::vector<std::string> accept_languages; | 129 *languages = dictionaries_pref.GetValue(); |
| 129 | 130 size_t enabled_spellcheck_languages = languages->size(); |
| 130 #if defined(OS_MACOSX) | |
| 131 if (spellcheck_mac::SpellCheckerAvailable()) | |
| 132 spellcheck_mac::GetAvailableLanguages(&accept_languages); | |
| 133 else | |
| 134 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); | |
| 135 #else | |
| 136 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); | |
| 137 #endif // !OS_MACOSX | |
| 138 | |
| 139 languages->push_back(dictionary_language); | |
| 140 | 131 |
| 141 for (std::vector<std::string>::const_iterator i = accept_languages.begin(); | 132 for (std::vector<std::string>::const_iterator i = accept_languages.begin(); |
| 142 i != accept_languages.end(); ++i) { | 133 i != accept_languages.end(); ++i) { |
| 143 std::string language = | 134 std::string language = |
| 144 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i); | 135 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i); |
| 145 if (!language.empty() && | 136 if (!language.empty() && |
| 146 std::find(languages->begin(), languages->end(), language) == | 137 std::find(languages->begin(), languages->end(), language) == |
| 147 languages->end()) { | 138 languages->end()) { |
| 148 languages->push_back(language); | 139 languages->push_back(language); |
| 149 } | 140 } |
| 150 } | 141 } |
| 151 | 142 |
| 152 for (size_t i = 0; i < languages->size(); ++i) { | 143 return enabled_spellcheck_languages; |
| 153 if ((*languages)[i] == dictionary_language) | |
| 154 return i; | |
| 155 } | |
| 156 | |
| 157 return -1; | |
| 158 } | 144 } |
| 145 #endif // !OS_MACOSX |
| 159 | 146 |
| 160 // static | 147 // static |
| 161 bool SpellcheckService::SignalStatusEvent( | 148 bool SpellcheckService::SignalStatusEvent( |
| 162 SpellcheckService::EventType status_type) { | 149 SpellcheckService::EventType status_type) { |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 150 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 164 | 151 |
| 165 if (!g_status_event) | 152 if (!g_status_event) |
| 166 return false; | 153 return false; |
| 167 g_status_type = status_type; | 154 g_status_type = status_type; |
| 168 g_status_event->Signal(); | 155 g_status_event->Signal(); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 bool enabled = pref_change_registrar_.prefs()->GetBoolean( | 279 bool enabled = pref_change_registrar_.prefs()->GetBoolean( |
| 293 prefs::kEnableAutoSpellCorrect); | 280 prefs::kEnableAutoSpellCorrect); |
| 294 for (content::RenderProcessHost::iterator i( | 281 for (content::RenderProcessHost::iterator i( |
| 295 content::RenderProcessHost::AllHostsIterator()); | 282 content::RenderProcessHost::AllHostsIterator()); |
| 296 !i.IsAtEnd(); i.Advance()) { | 283 !i.IsAtEnd(); i.Advance()) { |
| 297 content::RenderProcessHost* process = i.GetCurrentValue(); | 284 content::RenderProcessHost* process = i.GetCurrentValue(); |
| 298 process->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled)); | 285 process->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled)); |
| 299 } | 286 } |
| 300 } | 287 } |
| 301 | 288 |
| 302 void SpellcheckService::OnSpellCheckDictionaryChanged() { | 289 void SpellcheckService::OnSpellCheckDictionariesChanged() { |
| 303 if (hunspell_dictionary_.get()) | 290 if (hunspell_dictionary_.get()) |
| 304 hunspell_dictionary_->RemoveObserver(this); | 291 hunspell_dictionary_->RemoveObserver(this); |
| 305 PrefService* prefs = user_prefs::UserPrefs::Get(context_); | 292 PrefService* prefs = user_prefs::UserPrefs::Get(context_); |
| 306 DCHECK(prefs); | 293 DCHECK(prefs); |
| 307 | 294 |
| 308 std::string dictionary; | 295 std::string dictionary; |
| 309 prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &dictionary); | 296 prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &dictionary); |
| 310 | 297 |
| 311 hunspell_dictionary_.reset(new SpellcheckHunspellDictionary( | 298 hunspell_dictionary_.reset(new SpellcheckHunspellDictionary( |
| 312 dictionary, context_->GetRequestContext(), this)); | 299 dictionary, context_->GetRequestContext(), this)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 329 } | 316 } |
| 330 | 317 |
| 331 void SpellcheckService::UpdateFeedbackSenderState() { | 318 void SpellcheckService::UpdateFeedbackSenderState() { |
| 332 if (SpellingServiceClient::IsAvailable( | 319 if (SpellingServiceClient::IsAvailable( |
| 333 context_, SpellingServiceClient::SPELLCHECK)) { | 320 context_, SpellingServiceClient::SPELLCHECK)) { |
| 334 feedback_sender_->StartFeedbackCollection(); | 321 feedback_sender_->StartFeedbackCollection(); |
| 335 } else { | 322 } else { |
| 336 feedback_sender_->StopFeedbackCollection(); | 323 feedback_sender_->StopFeedbackCollection(); |
| 337 } | 324 } |
| 338 } | 325 } |
| OLD | NEW |