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/prefs/pref_member.h" | 9 #include "base/prefs/pref_member.h" |
8 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
9 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
10 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
11 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 13 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
12 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 14 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
13 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" | 15 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" |
14 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" | 16 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" |
15 #include "chrome/browser/spellchecker/spelling_service_client.h" | 17 #include "chrome/browser/spellchecker/spelling_service_client.h" |
16 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 70 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
69 content::NotificationService::AllSources()); | 71 content::NotificationService::AllSources()); |
70 } | 72 } |
71 | 73 |
72 SpellcheckService::~SpellcheckService() { | 74 SpellcheckService::~SpellcheckService() { |
73 // Remove pref observers | 75 // Remove pref observers |
74 pref_change_registrar_.RemoveAll(); | 76 pref_change_registrar_.RemoveAll(); |
75 } | 77 } |
76 | 78 |
77 // static | 79 // static |
78 int SpellcheckService::GetSpellCheckLanguages( | 80 std::set<int> SpellcheckService::GetSpellCheckLanguages( |
79 content::BrowserContext* context, | 81 content::BrowserContext* context, |
80 std::vector<std::string>* languages) { | 82 std::vector<std::string>* languages) { |
81 PrefService* prefs = user_prefs::UserPrefs::Get(context); | 83 PrefService* prefs = user_prefs::UserPrefs::Get(context); |
84 std::vector<std::string> dictionary_languages = | |
85 chrome::spellcheck_common::GetDictionaryLanguagesPref(prefs); | |
86 | |
87 std::vector<std::string> accept_languages; | |
82 StringPrefMember accept_languages_pref; | 88 StringPrefMember accept_languages_pref; |
83 StringPrefMember dictionary_language_pref; | |
84 accept_languages_pref.Init(prefs::kAcceptLanguages, prefs); | 89 accept_languages_pref.Init(prefs::kAcceptLanguages, prefs); |
85 dictionary_language_pref.Init(prefs::kSpellCheckDictionary, prefs); | |
86 std::string dictionary_language = dictionary_language_pref.GetValue(); | |
87 | |
88 // Now scan through the list of accept languages, and find possible mappings | |
89 // from this list to the existing list of spell check languages. | |
90 std::vector<std::string> accept_languages; | |
91 | 90 |
92 #if defined(OS_MACOSX) | 91 #if defined(OS_MACOSX) |
93 if (spellcheck_mac::SpellCheckerAvailable()) | 92 if (spellcheck_mac::SpellCheckerAvailable()) |
94 spellcheck_mac::GetAvailableLanguages(&accept_languages); | 93 spellcheck_mac::GetAvailableLanguages(&accept_languages); |
95 else | 94 else |
96 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); | 95 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); |
97 #else | 96 #else |
98 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); | 97 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); |
99 #endif // !OS_MACOSX | 98 #endif // !OS_MACOSX |
100 | 99 |
101 GetSpellCheckLanguagesFromAcceptLanguages( | 100 *languages = dictionary_languages; |
groby-ooo-7-16
2015/03/03 17:54:34
Can you please leave a short comment somewhere how
| |
102 accept_languages, dictionary_language, languages); | 101 GetSpellCheckLanguagesFromAcceptLanguages(accept_languages, languages); |
103 | 102 |
104 for (size_t i = 0; i < languages->size(); ++i) { | 103 // |languages| vector begins with the entries from |dictionary_languages|, |
105 if ((*languages)[i] == dictionary_language) | 104 // therefore these indices are correct. |
106 return i; | 105 std::set<int> selected_language_indices; |
groby-ooo-7-16
2015/03/03 17:54:34
If it's always the first <n> elements, why not ret
| |
107 } | 106 for (size_t i = 0; i < dictionary_languages.size(); ++i) |
108 return -1; | 107 selected_language_indices.insert(i); |
108 | |
109 return selected_language_indices; | |
109 } | 110 } |
110 | 111 |
111 // static | 112 // static |
112 void SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages( | 113 void SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages( |
113 const std::vector<std::string>& accept_languages, | 114 const std::vector<std::string>& accept_languages, |
114 const std::string& dictionary_language, | |
115 std::vector<std::string>* languages) { | 115 std::vector<std::string>* languages) { |
116 // The current dictionary language should be there. | |
117 languages->push_back(dictionary_language); | |
118 | 116 |
119 for (std::vector<std::string>::const_iterator i = accept_languages.begin(); | 117 for (std::vector<std::string>::const_iterator i = accept_languages.begin(); |
120 i != accept_languages.end(); ++i) { | 118 i != accept_languages.end(); ++i) { |
121 std::string language = | 119 std::string language = |
122 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i); | 120 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i); |
123 if (!language.empty() && | 121 if (!language.empty() && |
124 std::find(languages->begin(), languages->end(), language) == | 122 std::find(languages->begin(), languages->end(), language) == |
125 languages->end()) { | 123 languages->end()) { |
126 languages->push_back(language); | 124 languages->push_back(language); |
127 } | 125 } |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 hunspell_dictionary_->AddObserver(this); | 277 hunspell_dictionary_->AddObserver(this); |
280 hunspell_dictionary_->Load(); | 278 hunspell_dictionary_->Load(); |
281 } | 279 } |
282 | 280 |
283 void SpellcheckService::OnUseSpellingServiceChanged() { | 281 void SpellcheckService::OnUseSpellingServiceChanged() { |
284 bool enabled = pref_change_registrar_.prefs()->GetBoolean( | 282 bool enabled = pref_change_registrar_.prefs()->GetBoolean( |
285 prefs::kSpellCheckUseSpellingService); | 283 prefs::kSpellCheckUseSpellingService); |
286 if (metrics_) | 284 if (metrics_) |
287 metrics_->RecordSpellingServiceStats(enabled); | 285 metrics_->RecordSpellingServiceStats(enabled); |
288 } | 286 } |
OLD | NEW |