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 <algorithm> | |
8 | |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
9 #include "base/supports_user_data.h" | 11 #include "base/supports_user_data.h" |
10 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
11 #include "build/build_config.h" | 13 #include "build/build_config.h" |
12 #include "chrome/browser/spellchecker/feedback_sender.h" | 14 #include "chrome/browser/spellchecker/feedback_sender.h" |
13 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 15 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
14 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 16 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
15 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" | 17 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" |
16 #include "chrome/browser/spellchecker/spellcheck_platform.h" | 18 #include "chrome/browser/spellchecker/spellcheck_platform.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 if (!dictionaries_pref.GetValue().empty()) | 71 if (!dictionaries_pref.GetValue().empty()) |
70 first_of_dictionaries = dictionaries_pref.GetValue().front(); | 72 first_of_dictionaries = dictionaries_pref.GetValue().front(); |
71 | 73 |
72 if (first_of_dictionaries.empty() && !single_dictionary.empty()) { | 74 if (first_of_dictionaries.empty() && !single_dictionary.empty()) { |
73 first_of_dictionaries = single_dictionary; | 75 first_of_dictionaries = single_dictionary; |
74 dictionaries_pref.SetValue( | 76 dictionaries_pref.SetValue( |
75 std::vector<std::string>(1, first_of_dictionaries)); | 77 std::vector<std::string>(1, first_of_dictionaries)); |
76 } | 78 } |
77 | 79 |
78 single_dictionary_pref.SetValue(""); | 80 single_dictionary_pref.SetValue(""); |
79 | |
80 #endif // defined(USE_BROWSER_SPELLCHECKER) | 81 #endif // defined(USE_BROWSER_SPELLCHECKER) |
81 | 82 |
82 std::string language_code; | 83 std::string language_code; |
83 std::string country_code; | 84 std::string country_code; |
84 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( | 85 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( |
85 first_of_dictionaries, | 86 first_of_dictionaries, |
86 &language_code, | 87 &language_code, |
87 &country_code); | 88 &country_code); |
88 feedback_sender_.reset(new spellcheck::FeedbackSender( | 89 feedback_sender_.reset(new spellcheck::FeedbackSender( |
89 context->GetRequestContext(), language_code, country_code)); | 90 context->GetRequestContext(), language_code, country_code)); |
90 | 91 |
91 pref_change_registrar_.Add( | 92 pref_change_registrar_.Add( |
92 prefs::kSpellCheckDictionaries, | 93 prefs::kSpellCheckDictionaries, |
93 base::Bind(&SpellcheckService::OnSpellCheckDictionariesChanged, | 94 base::Bind(&SpellcheckService::OnSpellCheckDictionariesChanged, |
94 base::Unretained(this))); | 95 base::Unretained(this))); |
95 pref_change_registrar_.Add( | 96 pref_change_registrar_.Add( |
96 prefs::kSpellCheckUseSpellingService, | 97 prefs::kSpellCheckUseSpellingService, |
97 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged, | 98 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged, |
98 base::Unretained(this))); | 99 base::Unretained(this))); |
99 | 100 pref_change_registrar_.Add( |
101 prefs::kAcceptLanguages, | |
102 base::Bind(&SpellcheckService::OnAcceptLanguagesChanged, | |
103 base::Unretained(this))); | |
100 pref_change_registrar_.Add( | 104 pref_change_registrar_.Add( |
101 prefs::kEnableContinuousSpellcheck, | 105 prefs::kEnableContinuousSpellcheck, |
102 base::Bind(&SpellcheckService::InitForAllRenderers, | 106 base::Bind(&SpellcheckService::InitForAllRenderers, |
103 base::Unretained(this))); | 107 base::Unretained(this))); |
104 | 108 |
105 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath())); | 109 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath())); |
106 custom_dictionary_->AddObserver(this); | 110 custom_dictionary_->AddObserver(this); |
107 custom_dictionary_->Load(); | 111 custom_dictionary_->Load(); |
108 | 112 |
109 registrar_.Add(this, | 113 registrar_.Add(this, |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 } | 333 } |
330 | 334 |
331 void SpellcheckService::OnUseSpellingServiceChanged() { | 335 void SpellcheckService::OnUseSpellingServiceChanged() { |
332 bool enabled = pref_change_registrar_.prefs()->GetBoolean( | 336 bool enabled = pref_change_registrar_.prefs()->GetBoolean( |
333 prefs::kSpellCheckUseSpellingService); | 337 prefs::kSpellCheckUseSpellingService); |
334 if (metrics_) | 338 if (metrics_) |
335 metrics_->RecordSpellingServiceStats(enabled); | 339 metrics_->RecordSpellingServiceStats(enabled); |
336 UpdateFeedbackSenderState(); | 340 UpdateFeedbackSenderState(); |
337 } | 341 } |
338 | 342 |
343 void SpellcheckService::OnAcceptLanguagesChanged() { | |
344 PrefService* prefs = user_prefs::UserPrefs::Get(context_); | |
345 std::vector<std::string> accept_languages = | |
346 base::SplitString(prefs->GetString(prefs::kAcceptLanguages), ",", | |
groby-ooo-7-16
2016/02/09 02:51:33
Sigh. Why is this not a list? (Undirected grumblin
please use gerrit instead
2016/02/09 20:45:49
Acknowledged.
| |
347 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
348 std::transform( | |
349 accept_languages.begin(), accept_languages.end(), | |
350 accept_languages.begin(), | |
351 &chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage); | |
352 | |
353 StringListPrefMember dictionaries_pref; | |
354 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, prefs); | |
groby-ooo-7-16
2016/02/09 02:51:33
If dictionaries and accept_languages were ordered,
please use gerrit instead
2016/02/09 20:45:49
Right, we can't order them, because we want to pre
| |
355 std::vector<std::string> dictionaries = dictionaries_pref.GetValue(); | |
356 std::vector<std::string> filtered_dictionaries; | |
357 | |
358 for (const auto& dictionary : dictionaries) { | |
359 if (std::find(accept_languages.begin(), accept_languages.end(), | |
360 dictionary) != accept_languages.end()) { | |
361 filtered_dictionaries.push_back(dictionary); | |
362 } | |
363 } | |
364 | |
365 dictionaries_pref.SetValue(filtered_dictionaries); | |
366 } | |
367 | |
339 void SpellcheckService::UpdateFeedbackSenderState() { | 368 void SpellcheckService::UpdateFeedbackSenderState() { |
340 std::string feedback_language; | 369 std::string feedback_language; |
341 if (!hunspell_dictionaries_.empty()) | 370 if (!hunspell_dictionaries_.empty()) |
342 feedback_language = hunspell_dictionaries_.front()->GetLanguage(); | 371 feedback_language = hunspell_dictionaries_.front()->GetLanguage(); |
343 std::string language_code; | 372 std::string language_code; |
344 std::string country_code; | 373 std::string country_code; |
345 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( | 374 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( |
346 feedback_language, &language_code, &country_code); | 375 feedback_language, &language_code, &country_code); |
347 feedback_sender_->OnLanguageCountryChange(language_code, country_code); | 376 feedback_sender_->OnLanguageCountryChange(language_code, country_code); |
348 if (SpellingServiceClient::IsAvailable( | 377 if (SpellingServiceClient::IsAvailable( |
349 context_, SpellingServiceClient::SPELLCHECK)) { | 378 context_, SpellingServiceClient::SPELLCHECK)) { |
350 feedback_sender_->StartFeedbackCollection(); | 379 feedback_sender_->StartFeedbackCollection(); |
351 } else { | 380 } else { |
352 feedback_sender_->StopFeedbackCollection(); | 381 feedback_sender_->StopFeedbackCollection(); |
353 } | 382 } |
354 } | 383 } |
OLD | NEW |