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 | |
9 #include "base/command_line.h" | |
7 #include "base/prefs/pref_member.h" | 10 #include "base/prefs/pref_member.h" |
8 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
9 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
10 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
11 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 14 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
12 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 15 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
13 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" | 16 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" |
14 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" | 17 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h" |
15 #include "chrome/browser/spellchecker/spelling_service_client.h" | 18 #include "chrome/browser/spellchecker/spelling_service_client.h" |
19 #include "chrome/common/chrome_switches.h" | |
16 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
17 #include "chrome/common/spellcheck_messages.h" | 21 #include "chrome/common/spellcheck_messages.h" |
18 #include "components/user_prefs/user_prefs.h" | 22 #include "components/user_prefs/user_prefs.h" |
19 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
20 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/notification_types.h" | 26 #include "content/public/browser/notification_types.h" |
23 #include "content/public/browser/render_process_host.h" | 27 #include "content/public/browser/render_process_host.h" |
24 #include "ipc/ipc_platform_file.h" | 28 #include "ipc/ipc_platform_file.h" |
25 | 29 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 72 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
69 content::NotificationService::AllSources()); | 73 content::NotificationService::AllSources()); |
70 } | 74 } |
71 | 75 |
72 SpellcheckService::~SpellcheckService() { | 76 SpellcheckService::~SpellcheckService() { |
73 // Remove pref observers | 77 // Remove pref observers |
74 pref_change_registrar_.RemoveAll(); | 78 pref_change_registrar_.RemoveAll(); |
75 } | 79 } |
76 | 80 |
77 // static | 81 // static |
78 int SpellcheckService::GetSpellCheckLanguages( | 82 std::set<int> SpellcheckService::GetSpellCheckLanguages( |
79 content::BrowserContext* context, | 83 content::BrowserContext* context, |
80 std::vector<std::string>* languages) { | 84 std::vector<std::string>* languages) { |
81 PrefService* prefs = user_prefs::UserPrefs::Get(context); | 85 PrefService* prefs = user_prefs::UserPrefs::Get(context); |
86 std::vector<std::string> dictionary_languages; | |
87 | |
88 const base::CommandLine* command_line = | |
89 base::CommandLine::ForCurrentProcess(); | |
90 | |
91 StringPrefMember dictionary_languages_pref; | |
92 if (command_line->HasSwitch(switches::kEnableMultilingualSpellChecker)) { | |
93 dictionary_languages_pref.Init(prefs::kSpellCheckDictionaries, prefs); | |
94 base::SplitString(dictionary_languages_pref.GetValue(), | |
95 chrome::spellcheck_common::kDictionaryLanguagesSeparator, | |
96 &dictionary_languages); | |
97 } else { | |
98 dictionary_languages_pref.Init(prefs::kSpellCheckDictionary, prefs); | |
99 dictionary_languages.push_back(dictionary_languages_pref.GetValue()); | |
100 } | |
101 | |
102 auto partition_point = std::partition( | |
103 dictionary_languages.begin(), dictionary_languages.end(), | |
104 [](const std::string& language) { return language.length() > 0; }); | |
105 dictionary_languages.erase(partition_point, dictionary_languages.end()); | |
please use gerrit instead
2015/02/26 21:30:19
Instead of repeating this code, perhaps we can put
Dan Beam
2015/02/26 22:04:32
size_t < 0?! mind == blown. i think rouslan@ meant
Dan Beam
2015/02/26 22:30:23
oh, I guess the operand order is right in rouslan@
Klemen Forstnerič
2015/02/27 15:43:44
I think so too, DCHECK(!language.empty()) is more
Klemen Forstnerič
2015/02/27 15:43:44
Good idea. Done.
| |
106 | |
107 std::vector<std::string> accept_languages; | |
82 StringPrefMember accept_languages_pref; | 108 StringPrefMember accept_languages_pref; |
83 StringPrefMember dictionary_language_pref; | |
84 accept_languages_pref.Init(prefs::kAcceptLanguages, prefs); | 109 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 | 110 |
92 #if defined(OS_MACOSX) | 111 #if defined(OS_MACOSX) |
93 if (spellcheck_mac::SpellCheckerAvailable()) | 112 if (spellcheck_mac::SpellCheckerAvailable()) |
94 spellcheck_mac::GetAvailableLanguages(&accept_languages); | 113 spellcheck_mac::GetAvailableLanguages(&accept_languages); |
95 else | 114 else |
96 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); | 115 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); |
97 #else | 116 #else |
98 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); | 117 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages); |
99 #endif // !OS_MACOSX | 118 #endif // !OS_MACOSX |
100 | 119 |
101 GetSpellCheckLanguagesFromAcceptLanguages( | 120 *languages = dictionary_languages; |
102 accept_languages, dictionary_language, languages); | 121 GetSpellCheckLanguagesFromAcceptLanguages(accept_languages, languages); |
103 | 122 |
104 for (size_t i = 0; i < languages->size(); ++i) { | 123 // |languages| vector begins with the entries from |dictionary_languages|, |
105 if ((*languages)[i] == dictionary_language) | 124 // therefore these indices are correct. |
106 return i; | 125 std::set<int> selected_language_indices; |
107 } | 126 for (size_t i = 0; i < dictionary_languages.size(); ++i) |
108 return -1; | 127 selected_language_indices.insert(i); |
128 | |
129 return selected_language_indices; | |
109 } | 130 } |
110 | 131 |
111 // static | 132 // static |
112 void SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages( | 133 void SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages( |
113 const std::vector<std::string>& accept_languages, | 134 const std::vector<std::string>& accept_languages, |
114 const std::string& dictionary_language, | |
115 std::vector<std::string>* languages) { | 135 std::vector<std::string>* languages) { |
116 // The current dictionary language should be there. | |
117 languages->push_back(dictionary_language); | |
118 | 136 |
119 for (std::vector<std::string>::const_iterator i = accept_languages.begin(); | 137 for (std::vector<std::string>::const_iterator i = accept_languages.begin(); |
120 i != accept_languages.end(); ++i) { | 138 i != accept_languages.end(); ++i) { |
121 std::string language = | 139 std::string language = |
122 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i); | 140 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i); |
123 if (!language.empty() && | 141 if (!language.empty() && |
124 std::find(languages->begin(), languages->end(), language) == | 142 std::find(languages->begin(), languages->end(), language) == |
125 languages->end()) { | 143 languages->end()) { |
126 languages->push_back(language); | 144 languages->push_back(language); |
127 } | 145 } |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 hunspell_dictionary_->AddObserver(this); | 297 hunspell_dictionary_->AddObserver(this); |
280 hunspell_dictionary_->Load(); | 298 hunspell_dictionary_->Load(); |
281 } | 299 } |
282 | 300 |
283 void SpellcheckService::OnUseSpellingServiceChanged() { | 301 void SpellcheckService::OnUseSpellingServiceChanged() { |
284 bool enabled = pref_change_registrar_.prefs()->GetBoolean( | 302 bool enabled = pref_change_registrar_.prefs()->GetBoolean( |
285 prefs::kSpellCheckUseSpellingService); | 303 prefs::kSpellCheckUseSpellingService); |
286 if (metrics_) | 304 if (metrics_) |
287 metrics_->RecordSpellingServiceStats(enabled); | 305 metrics_->RecordSpellingServiceStats(enabled); |
288 } | 306 } |
OLD | NEW |