Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(605)

Side by Side Diff: chrome/browser/spellchecker/spellcheck_service.cc

Issue 1156473007: Enables the user to select multiple languages for spellchecking (UI) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Answered comments. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
48 std::string first_of_dictionaries; 49 std::string first_of_dictionaries;
49 if (!dictionaries_pref.GetValue().empty()) 50 if (!dictionaries_pref.GetValue().empty()) {
50 first_of_dictionaries = dictionaries_pref.GetValue().front(); 51 first_of_dictionaries = dictionaries_pref.GetValue().front();
52 }
please use gerrit instead 2015/07/07 01:30:40 Unnecessary whitespace and curly changes. Please u
Julius 2015/07/07 22:16:25 Done.
51 53
52 // For preference migration, set the new preference kSpellCheckDictionaries 54 // For preference migration, set the new preference kSpellCheckDictionaries
53 // to be the same as the old kSpellCheckDictionary. 55 // to be the same as the old kSpellCheckDictionary.
54 StringPrefMember single_dictionary_pref; 56 StringPrefMember single_dictionary_pref;
55 single_dictionary_pref.Init(prefs::kSpellCheckDictionary, prefs); 57 single_dictionary_pref.Init(prefs::kSpellCheckDictionary, prefs);
56 std::string single_dictionary = single_dictionary_pref.GetValue(); 58 std::string single_dictionary = single_dictionary_pref.GetValue();
57 59
58 if (first_of_dictionaries.empty() && !single_dictionary.empty()) { 60 if (first_of_dictionaries.empty() && !single_dictionary.empty()) {
59 first_of_dictionaries = single_dictionary; 61 first_of_dictionaries = single_dictionary;
60 dictionaries_pref.SetValue( 62 dictionaries_pref.SetValue(
(...skipping 10 matching lines...) Expand all
71 &country_code); 73 &country_code);
72 feedback_sender_.reset(new spellcheck::FeedbackSender( 74 feedback_sender_.reset(new spellcheck::FeedbackSender(
73 context->GetRequestContext(), language_code, country_code)); 75 context->GetRequestContext(), language_code, country_code));
74 76
75 pref_change_registrar_.Add( 77 pref_change_registrar_.Add(
76 prefs::kEnableAutoSpellCorrect, 78 prefs::kEnableAutoSpellCorrect,
77 base::Bind(&SpellcheckService::OnEnableAutoSpellCorrectChanged, 79 base::Bind(&SpellcheckService::OnEnableAutoSpellCorrectChanged,
78 base::Unretained(this))); 80 base::Unretained(this)));
79 pref_change_registrar_.Add( 81 pref_change_registrar_.Add(
80 prefs::kSpellCheckDictionaries, 82 prefs::kSpellCheckDictionaries,
81 base::Bind(&SpellcheckService::OnSpellCheckDictionaryChanged, 83 base::Bind(&SpellcheckService::OnSpellCheckDictionariesChanged,
82 base::Unretained(this))); 84 base::Unretained(this)));
83 if (!chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) { 85 if (!chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) {
84 pref_change_registrar_.Add( 86 pref_change_registrar_.Add(
85 prefs::kSpellCheckUseSpellingService, 87 prefs::kSpellCheckUseSpellingService,
86 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged, 88 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged,
87 base::Unretained(this))); 89 base::Unretained(this)));
88 } 90 }
91
89 pref_change_registrar_.Add( 92 pref_change_registrar_.Add(
90 prefs::kEnableContinuousSpellcheck, 93 prefs::kEnableContinuousSpellcheck,
91 base::Bind(&SpellcheckService::InitForAllRenderers, 94 base::Bind(&SpellcheckService::InitForAllRenderers,
92 base::Unretained(this))); 95 base::Unretained(this)));
93 96
94 OnSpellCheckDictionaryChanged(); 97 OnSpellCheckDictionariesChanged();
95 98
96 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath())); 99 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath()));
97 custom_dictionary_->AddObserver(this); 100 custom_dictionary_->AddObserver(this);
98 custom_dictionary_->Load(); 101 custom_dictionary_->Load();
99 102
100 registrar_.Add(this, 103 registrar_.Add(this,
101 content::NOTIFICATION_RENDERER_PROCESS_CREATED, 104 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
102 content::NotificationService::AllSources()); 105 content::NotificationService::AllSources());
103 } 106 }
104 107
105 SpellcheckService::~SpellcheckService() { 108 SpellcheckService::~SpellcheckService() {
106 // Remove pref observers 109 // Remove pref observers
107 pref_change_registrar_.RemoveAll(); 110 pref_change_registrar_.RemoveAll();
108 } 111 }
109 112
110 base::WeakPtr<SpellcheckService> SpellcheckService::GetWeakPtr() { 113 base::WeakPtr<SpellcheckService> SpellcheckService::GetWeakPtr() {
111 return weak_ptr_factory_.GetWeakPtr(); 114 return weak_ptr_factory_.GetWeakPtr();
112 } 115 }
113 116
117 #if !defined(OS_MACOSX)
114 // static 118 // static
115 int SpellcheckService::GetSpellCheckLanguages( 119 size_t SpellcheckService::GetSpellCheckLanguages(
116 base::SupportsUserData* context, 120 base::SupportsUserData* context,
117 std::vector<std::string>* languages) { 121 std::vector<std::string>* languages) {
118 PrefService* prefs = user_prefs::UserPrefs::Get(context); 122 PrefService* prefs = user_prefs::UserPrefs::Get(context);
119 StringPrefMember accept_languages_pref; 123 StringPrefMember accept_languages_pref;
120 accept_languages_pref.Init(prefs::kAcceptLanguages, prefs); 124 accept_languages_pref.Init(prefs::kAcceptLanguages, prefs);
121 125
122 std::string dictionary_language; 126 std::vector<std::string> accept_languages;
123 prefs->GetList(prefs::kSpellCheckDictionaries) 127 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages);
124 ->GetString(0, &dictionary_language);
125 128
126 // Now scan through the list of accept languages, and find possible mappings 129 StringListPrefMember dictionaries_pref;
127 // from this list to the existing list of spell check languages. 130 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, prefs);
128 std::vector<std::string> accept_languages; 131 *languages = dictionaries_pref.GetValue();
129 132 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 133
141 for (std::vector<std::string>::const_iterator i = accept_languages.begin(); 134 for (std::vector<std::string>::const_iterator i = accept_languages.begin();
142 i != accept_languages.end(); ++i) { 135 i != accept_languages.end(); ++i) {
143 std::string language = 136 std::string language =
144 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i); 137 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i);
145 if (!language.empty() && 138 if (!language.empty() &&
146 std::find(languages->begin(), languages->end(), language) == 139 std::find(languages->begin(), languages->end(), language) ==
147 languages->end()) { 140 languages->end()) {
148 languages->push_back(language); 141 languages->push_back(language);
149 } 142 }
150 } 143 }
151 144
152 for (size_t i = 0; i < languages->size(); ++i) { 145 return enabled_spellcheck_languages;
153 if ((*languages)[i] == dictionary_language)
154 return i;
155 }
156
157 return -1;
158 } 146 }
147 #endif // !OS_MACOSX
159 148
160 // static 149 // static
161 bool SpellcheckService::SignalStatusEvent( 150 bool SpellcheckService::SignalStatusEvent(
162 SpellcheckService::EventType status_type) { 151 SpellcheckService::EventType status_type) {
163 DCHECK_CURRENTLY_ON(BrowserThread::UI); 152 DCHECK_CURRENTLY_ON(BrowserThread::UI);
164 153
165 if (!g_status_event) 154 if (!g_status_event)
166 return false; 155 return false;
167 g_status_type = status_type; 156 g_status_type = status_type;
168 g_status_event->Signal(); 157 g_status_event->Signal();
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 bool enabled = pref_change_registrar_.prefs()->GetBoolean( 281 bool enabled = pref_change_registrar_.prefs()->GetBoolean(
293 prefs::kEnableAutoSpellCorrect); 282 prefs::kEnableAutoSpellCorrect);
294 for (content::RenderProcessHost::iterator i( 283 for (content::RenderProcessHost::iterator i(
295 content::RenderProcessHost::AllHostsIterator()); 284 content::RenderProcessHost::AllHostsIterator());
296 !i.IsAtEnd(); i.Advance()) { 285 !i.IsAtEnd(); i.Advance()) {
297 content::RenderProcessHost* process = i.GetCurrentValue(); 286 content::RenderProcessHost* process = i.GetCurrentValue();
298 process->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled)); 287 process->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled));
299 } 288 }
300 } 289 }
301 290
302 void SpellcheckService::OnSpellCheckDictionaryChanged() { 291 void SpellcheckService::OnSpellCheckDictionariesChanged() {
303 if (hunspell_dictionary_.get()) 292 if (hunspell_dictionary_.get())
304 hunspell_dictionary_->RemoveObserver(this); 293 hunspell_dictionary_->RemoveObserver(this);
305 PrefService* prefs = user_prefs::UserPrefs::Get(context_); 294 PrefService* prefs = user_prefs::UserPrefs::Get(context_);
306 DCHECK(prefs); 295 DCHECK(prefs);
307 296
308 std::string dictionary; 297 std::string dictionary;
309 prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &dictionary); 298 prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &dictionary);
310 299
311 hunspell_dictionary_.reset(new SpellcheckHunspellDictionary( 300 hunspell_dictionary_.reset(new SpellcheckHunspellDictionary(
312 dictionary, context_->GetRequestContext(), this)); 301 dictionary, context_->GetRequestContext(), this));
(...skipping 16 matching lines...) Expand all
329 } 318 }
330 319
331 void SpellcheckService::UpdateFeedbackSenderState() { 320 void SpellcheckService::UpdateFeedbackSenderState() {
332 if (SpellingServiceClient::IsAvailable( 321 if (SpellingServiceClient::IsAvailable(
333 context_, SpellingServiceClient::SPELLCHECK)) { 322 context_, SpellingServiceClient::SPELLCHECK)) {
334 feedback_sender_->StartFeedbackCollection(); 323 feedback_sender_->StartFeedbackCollection();
335 } else { 324 } else {
336 feedback_sender_->StopFeedbackCollection(); 325 feedback_sender_->StopFeedbackCollection();
337 } 326 }
338 } 327 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698