| 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 <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.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/supports_user_data.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "chrome/browser/spellchecker/feedback_sender.h" | |
| 15 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 14 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 16 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 15 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
| 17 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" | 16 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" |
| 18 #include "chrome/browser/spellchecker/spellcheck_platform.h" | 17 #include "chrome/browser/spellchecker/spellcheck_platform.h" |
| 19 #include "chrome/browser/spellchecker/spelling_service_client.h" | 18 #include "chrome/browser/spellchecker/spelling_service_client.h" |
| 20 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 21 #include "components/prefs/pref_member.h" | 20 #include "components/prefs/pref_member.h" |
| 22 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
| 23 #include "components/spellcheck/common/spellcheck_bdict_language.h" | 22 #include "components/spellcheck/common/spellcheck_bdict_language.h" |
| 24 #include "components/spellcheck/common/spellcheck_common.h" | 23 #include "components/spellcheck/common/spellcheck_common.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 73 |
| 75 if (first_of_dictionaries.empty() && !single_dictionary.empty()) { | 74 if (first_of_dictionaries.empty() && !single_dictionary.empty()) { |
| 76 first_of_dictionaries = single_dictionary; | 75 first_of_dictionaries = single_dictionary; |
| 77 dictionaries_pref.SetValue( | 76 dictionaries_pref.SetValue( |
| 78 std::vector<std::string>(1, first_of_dictionaries)); | 77 std::vector<std::string>(1, first_of_dictionaries)); |
| 79 } | 78 } |
| 80 | 79 |
| 81 single_dictionary_pref.SetValue(""); | 80 single_dictionary_pref.SetValue(""); |
| 82 #endif // defined(USE_BROWSER_SPELLCHECKER) | 81 #endif // defined(USE_BROWSER_SPELLCHECKER) |
| 83 | 82 |
| 84 std::string language_code; | |
| 85 std::string country_code; | |
| 86 spellcheck::GetISOLanguageCountryCodeFromLocale( | |
| 87 first_of_dictionaries, &language_code, &country_code); | |
| 88 feedback_sender_.reset(new spellcheck::FeedbackSender( | |
| 89 content::BrowserContext::GetDefaultStoragePartition(context)-> | |
| 90 GetURLRequestContext(), | |
| 91 language_code, country_code)); | |
| 92 | |
| 93 pref_change_registrar_.Add( | 83 pref_change_registrar_.Add( |
| 94 prefs::kSpellCheckDictionaries, | 84 prefs::kSpellCheckDictionaries, |
| 95 base::Bind(&SpellcheckService::OnSpellCheckDictionariesChanged, | 85 base::Bind(&SpellcheckService::OnSpellCheckDictionariesChanged, |
| 96 base::Unretained(this))); | 86 base::Unretained(this))); |
| 97 pref_change_registrar_.Add( | 87 pref_change_registrar_.Add( |
| 98 prefs::kSpellCheckUseSpellingService, | 88 prefs::kSpellCheckUseSpellingService, |
| 99 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged, | 89 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged, |
| 100 base::Unretained(this))); | 90 base::Unretained(this))); |
| 101 pref_change_registrar_.Add( | 91 pref_change_registrar_.Add( |
| 102 prefs::kAcceptLanguages, | 92 prefs::kAcceptLanguages, |
| 103 base::Bind(&SpellcheckService::OnAcceptLanguagesChanged, | 93 base::Bind(&SpellcheckService::OnAcceptLanguagesChanged, |
| 104 base::Unretained(this))); | 94 base::Unretained(this))); |
| 105 pref_change_registrar_.Add( | 95 pref_change_registrar_.Add( |
| 106 prefs::kEnableContinuousSpellcheck, | 96 prefs::kEnableContinuousSpellcheck, |
| 107 base::Bind(&SpellcheckService::InitForAllRenderers, | 97 base::Bind(&SpellcheckService::InitForAllRenderers, |
| 108 base::Unretained(this))); | 98 base::Unretained(this))); |
| 109 | 99 |
| 110 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath())); | 100 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath())); |
| 111 custom_dictionary_->AddObserver(this); | 101 custom_dictionary_->AddObserver(this); |
| 112 custom_dictionary_->Load(); | 102 custom_dictionary_->Load(); |
| 113 | 103 |
| 114 registrar_.Add(this, | 104 registrar_.Add(this, |
| 115 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 105 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 116 content::NotificationService::AllSources()); | 106 content::NotificationService::AllSources()); |
| 117 | 107 |
| 118 LoadHunspellDictionaries(); | 108 LoadHunspellDictionaries(); |
| 119 UpdateFeedbackSenderState(); | |
| 120 } | 109 } |
| 121 | 110 |
| 122 SpellcheckService::~SpellcheckService() { | 111 SpellcheckService::~SpellcheckService() { |
| 123 // Remove pref observers | 112 // Remove pref observers |
| 124 pref_change_registrar_.RemoveAll(); | 113 pref_change_registrar_.RemoveAll(); |
| 125 } | 114 } |
| 126 | 115 |
| 127 base::WeakPtr<SpellcheckService> SpellcheckService::GetWeakPtr() { | 116 base::WeakPtr<SpellcheckService> SpellcheckService::GetWeakPtr() { |
| 128 return weak_ptr_factory_.GetWeakPtr(); | 117 return weak_ptr_factory_.GetWeakPtr(); |
| 129 } | 118 } |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 hunspell_dictionaries_.back()->AddObserver(this); | 222 hunspell_dictionaries_.back()->AddObserver(this); |
| 234 hunspell_dictionaries_.back()->Load(); | 223 hunspell_dictionaries_.back()->Load(); |
| 235 } | 224 } |
| 236 } | 225 } |
| 237 | 226 |
| 238 const ScopedVector<SpellcheckHunspellDictionary>& | 227 const ScopedVector<SpellcheckHunspellDictionary>& |
| 239 SpellcheckService::GetHunspellDictionaries() { | 228 SpellcheckService::GetHunspellDictionaries() { |
| 240 return hunspell_dictionaries_; | 229 return hunspell_dictionaries_; |
| 241 } | 230 } |
| 242 | 231 |
| 243 spellcheck::FeedbackSender* SpellcheckService::GetFeedbackSender() { | |
| 244 return feedback_sender_.get(); | |
| 245 } | |
| 246 | |
| 247 bool SpellcheckService::LoadExternalDictionary(std::string language, | 232 bool SpellcheckService::LoadExternalDictionary(std::string language, |
| 248 std::string locale, | 233 std::string locale, |
| 249 std::string path, | 234 std::string path, |
| 250 DictionaryFormat format) { | 235 DictionaryFormat format) { |
| 251 return false; | 236 return false; |
| 252 } | 237 } |
| 253 | 238 |
| 254 bool SpellcheckService::UnloadExternalDictionary( | 239 bool SpellcheckService::UnloadExternalDictionary( |
| 255 const std::string& /* path */) { | 240 const std::string& /* path */) { |
| 256 return false; | 241 return false; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 content::RenderProcessHost* process = i.GetCurrentValue(); | 301 content::RenderProcessHost* process = i.GetCurrentValue(); |
| 317 if (process && process->GetHandle()) | 302 if (process && process->GetHandle()) |
| 318 InitForRenderer(process); | 303 InitForRenderer(process); |
| 319 } | 304 } |
| 320 } | 305 } |
| 321 | 306 |
| 322 void SpellcheckService::OnSpellCheckDictionariesChanged() { | 307 void SpellcheckService::OnSpellCheckDictionariesChanged() { |
| 323 // If there are hunspell dictionaries, then fire off notifications to the | 308 // If there are hunspell dictionaries, then fire off notifications to the |
| 324 // renderers after the dictionaries are finished loading. | 309 // renderers after the dictionaries are finished loading. |
| 325 LoadHunspellDictionaries(); | 310 LoadHunspellDictionaries(); |
| 326 UpdateFeedbackSenderState(); | |
| 327 | 311 |
| 328 // If there are no hunspell dictionaries to load, then immediately let the | 312 // If there are no hunspell dictionaries to load, then immediately let the |
| 329 // renderers know the new state. | 313 // renderers know the new state. |
| 330 if (hunspell_dictionaries_.empty()) | 314 if (hunspell_dictionaries_.empty()) |
| 331 InitForAllRenderers(); | 315 InitForAllRenderers(); |
| 332 } | 316 } |
| 333 | 317 |
| 334 void SpellcheckService::OnUseSpellingServiceChanged() { | 318 void SpellcheckService::OnUseSpellingServiceChanged() { |
| 335 bool enabled = pref_change_registrar_.prefs()->GetBoolean( | 319 bool enabled = pref_change_registrar_.prefs()->GetBoolean( |
| 336 prefs::kSpellCheckUseSpellingService); | 320 prefs::kSpellCheckUseSpellingService); |
| 337 if (metrics_) | 321 if (metrics_) |
| 338 metrics_->RecordSpellingServiceStats(enabled); | 322 metrics_->RecordSpellingServiceStats(enabled); |
| 339 UpdateFeedbackSenderState(); | |
| 340 } | 323 } |
| 341 | 324 |
| 342 void SpellcheckService::OnAcceptLanguagesChanged() { | 325 void SpellcheckService::OnAcceptLanguagesChanged() { |
| 343 PrefService* prefs = user_prefs::UserPrefs::Get(context_); | 326 PrefService* prefs = user_prefs::UserPrefs::Get(context_); |
| 344 std::vector<std::string> accept_languages = | 327 std::vector<std::string> accept_languages = |
| 345 base::SplitString(prefs->GetString(prefs::kAcceptLanguages), ",", | 328 base::SplitString(prefs->GetString(prefs::kAcceptLanguages), ",", |
| 346 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 329 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 347 std::transform(accept_languages.begin(), accept_languages.end(), | 330 std::transform(accept_languages.begin(), accept_languages.end(), |
| 348 accept_languages.begin(), | 331 accept_languages.begin(), |
| 349 &spellcheck::GetCorrespondingSpellCheckLanguage); | 332 &spellcheck::GetCorrespondingSpellCheckLanguage); |
| 350 | 333 |
| 351 StringListPrefMember dictionaries_pref; | 334 StringListPrefMember dictionaries_pref; |
| 352 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, prefs); | 335 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, prefs); |
| 353 std::vector<std::string> dictionaries = dictionaries_pref.GetValue(); | 336 std::vector<std::string> dictionaries = dictionaries_pref.GetValue(); |
| 354 std::vector<std::string> filtered_dictionaries; | 337 std::vector<std::string> filtered_dictionaries; |
| 355 | 338 |
| 356 for (const auto& dictionary : dictionaries) { | 339 for (const auto& dictionary : dictionaries) { |
| 357 if (std::find(accept_languages.begin(), accept_languages.end(), | 340 if (std::find(accept_languages.begin(), accept_languages.end(), |
| 358 dictionary) != accept_languages.end()) { | 341 dictionary) != accept_languages.end()) { |
| 359 filtered_dictionaries.push_back(dictionary); | 342 filtered_dictionaries.push_back(dictionary); |
| 360 } | 343 } |
| 361 } | 344 } |
| 362 | 345 |
| 363 dictionaries_pref.SetValue(filtered_dictionaries); | 346 dictionaries_pref.SetValue(filtered_dictionaries); |
| 364 } | 347 } |
| 365 | |
| 366 void SpellcheckService::UpdateFeedbackSenderState() { | |
| 367 std::string feedback_language; | |
| 368 if (!hunspell_dictionaries_.empty()) | |
| 369 feedback_language = hunspell_dictionaries_.front()->GetLanguage(); | |
| 370 std::string language_code; | |
| 371 std::string country_code; | |
| 372 spellcheck::GetISOLanguageCountryCodeFromLocale( | |
| 373 feedback_language, &language_code, &country_code); | |
| 374 feedback_sender_->OnLanguageCountryChange(language_code, country_code); | |
| 375 if (SpellingServiceClient::IsAvailable( | |
| 376 context_, SpellingServiceClient::SPELLCHECK)) { | |
| 377 feedback_sender_->StartFeedbackCollection(); | |
| 378 } else { | |
| 379 feedback_sender_->StopFeedbackCollection(); | |
| 380 } | |
| 381 } | |
| OLD | NEW |