OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/extensions/api/language_settings_private/language_setti
ngs_private_api.h" | 5 #include "chrome/browser/extensions/api/language_settings_private/language_setti
ngs_private_api.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
13 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/extensions/api/language_settings_private/language_setti
ngs_private_delegate.h" | 19 #include "chrome/browser/extensions/api/language_settings_private/language_setti
ngs_private_delegate.h" |
20 #include "chrome/browser/extensions/api/language_settings_private/language_setti
ngs_private_delegate_factory.h" | 20 #include "chrome/browser/extensions/api/language_settings_private/language_setti
ngs_private_delegate_factory.h" |
21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 22 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
23 #include "chrome/browser/spellchecker/spellcheck_service.h" | 23 #include "chrome/browser/spellchecker/spellcheck_service.h" |
24 #include "chrome/browser/translate/chrome_translate_client.h" | 24 #include "chrome/browser/translate/chrome_translate_client.h" |
25 #include "chrome/browser/translate/translate_service.h" | 25 #include "chrome/browser/translate/translate_service.h" |
26 #include "chrome/common/extensions/api/language_settings_private.h" | 26 #include "chrome/common/extensions/api/language_settings_private.h" |
27 #include "chrome/common/spellcheck_common.h" | 27 #include "chrome/common/spellcheck_common.h" |
28 #include "components/translate/core/browser/translate_download_manager.h" | 28 #include "components/translate/core/browser/translate_download_manager.h" |
29 #include "components/translate/core/common/translate_util.h" | |
30 #include "third_party/icu/source/i18n/unicode/coll.h" | 29 #include "third_party/icu/source/i18n/unicode/coll.h" |
31 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
32 #include "ui/base/l10n/l10n_util_collator.h" | 31 #include "ui/base/l10n/l10n_util_collator.h" |
33 | 32 |
34 namespace extensions { | 33 namespace extensions { |
35 | 34 |
36 namespace language_settings_private = api::language_settings_private; | 35 namespace language_settings_private = api::language_settings_private; |
37 | 36 |
38 LanguageSettingsPrivateGetLanguageListFunction:: | 37 LanguageSettingsPrivateGetLanguageListFunction:: |
39 LanguageSettingsPrivateGetLanguageListFunction() { | 38 LanguageSettingsPrivateGetLanguageListFunction() { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 base::string16 adjusted_native_display_name(pair.second); | 106 base::string16 adjusted_native_display_name(pair.second); |
108 base::i18n::AdjustStringForLocaleDirection(&adjusted_native_display_name); | 107 base::i18n::AdjustStringForLocaleDirection(&adjusted_native_display_name); |
109 language.native_display_name = | 108 language.native_display_name = |
110 base::UTF16ToUTF8(adjusted_native_display_name); | 109 base::UTF16ToUTF8(adjusted_native_display_name); |
111 | 110 |
112 // Set optional fields only if they differ from the default. | 111 // Set optional fields only if they differ from the default. |
113 if (base::i18n::StringContainsStrongRTLChars(display_name)) | 112 if (base::i18n::StringContainsStrongRTLChars(display_name)) |
114 language.display_name_rtl.reset(new bool(true)); | 113 language.display_name_rtl.reset(new bool(true)); |
115 if (locale_set.count(pair.first) > 0) | 114 if (locale_set.count(pair.first) > 0) |
116 language.supports_ui.reset(new bool(true)); | 115 language.supports_ui.reset(new bool(true)); |
117 if (spellcheck_language_set.count(language.code) > 0) | 116 if (spellcheck_language_set.count(pair.first) > 0) |
118 language.supports_spellcheck.reset(new bool(true)); | 117 language.supports_spellcheck.reset(new bool(true)); |
119 std::string translate_code = language.code; | 118 if (translate_language_set.count(pair.first) > 0) |
120 translate::ToTranslateLanguageSynonym(&translate_code); | |
121 if (translate_language_set.count(translate_code) > 0) | |
122 language.supports_translate.reset(new bool(true)); | 119 language.supports_translate.reset(new bool(true)); |
123 | 120 |
124 language_list->Append(language.ToValue()); | 121 language_list->Append(language.ToValue()); |
125 } | 122 } |
126 return RespondNow(OneArgument(language_list.release())); | 123 return RespondNow(OneArgument(language_list.release())); |
127 } | 124 } |
128 | 125 |
129 LanguageSettingsPrivateSetLanguageListFunction:: | 126 LanguageSettingsPrivateSetLanguageListFunction:: |
130 LanguageSettingsPrivateSetLanguageListFunction() | 127 LanguageSettingsPrivateSetLanguageListFunction() |
131 : chrome_details_(this) { | 128 : chrome_details_(this) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 LanguageSettingsPrivateGetSpellcheckWordsFunction:: | 173 LanguageSettingsPrivateGetSpellcheckWordsFunction:: |
177 ~LanguageSettingsPrivateGetSpellcheckWordsFunction() { | 174 ~LanguageSettingsPrivateGetSpellcheckWordsFunction() { |
178 } | 175 } |
179 | 176 |
180 ExtensionFunction::ResponseAction | 177 ExtensionFunction::ResponseAction |
181 LanguageSettingsPrivateGetSpellcheckWordsFunction::Run() { | 178 LanguageSettingsPrivateGetSpellcheckWordsFunction::Run() { |
182 SpellcheckService* service = | 179 SpellcheckService* service = |
183 SpellcheckServiceFactory::GetForContext(browser_context()); | 180 SpellcheckServiceFactory::GetForContext(browser_context()); |
184 SpellcheckCustomDictionary* dictionary = service->GetCustomDictionary(); | 181 SpellcheckCustomDictionary* dictionary = service->GetCustomDictionary(); |
185 | 182 |
| 183 if (dictionary->IsLoaded()) |
| 184 return RespondNow(OneArgument(GetSpellcheckWords().release())); |
| 185 |
| 186 dictionary->AddObserver(this); |
| 187 AddRef(); // Balanced in OnCustomDictionaryLoaded(). |
| 188 return RespondLater(); |
| 189 } |
| 190 |
| 191 void |
| 192 LanguageSettingsPrivateGetSpellcheckWordsFunction::OnCustomDictionaryLoaded() { |
| 193 SpellcheckService* service = |
| 194 SpellcheckServiceFactory::GetForContext(browser_context()); |
| 195 service->GetCustomDictionary()->RemoveObserver(this); |
| 196 Respond(OneArgument(GetSpellcheckWords().release())); |
| 197 Release(); |
| 198 } |
| 199 |
| 200 void |
| 201 LanguageSettingsPrivateGetSpellcheckWordsFunction::OnCustomDictionaryChanged( |
| 202 const SpellcheckCustomDictionary::Change& dictionary_change) { |
| 203 NOTREACHED() << "SpellcheckCustomDictionary::Observer: " |
| 204 "OnCustomDictionaryChanged() called before " |
| 205 "OnCustomDictionaryLoaded()"; |
| 206 } |
| 207 |
| 208 scoped_ptr<base::ListValue> |
| 209 LanguageSettingsPrivateGetSpellcheckWordsFunction::GetSpellcheckWords() const { |
| 210 SpellcheckService* service = |
| 211 SpellcheckServiceFactory::GetForContext(browser_context()); |
| 212 SpellcheckCustomDictionary* dictionary = service->GetCustomDictionary(); |
| 213 DCHECK(dictionary->IsLoaded()); |
| 214 |
| 215 // TODO(michaelpg): Sort using app locale. |
186 scoped_ptr<base::ListValue> word_list(new base::ListValue()); | 216 scoped_ptr<base::ListValue> word_list(new base::ListValue()); |
187 // TODO(michaelpg): observe the dictionary and respond later if not loaded. | 217 const std::set<std::string>& words = dictionary->GetWords(); |
188 if (dictionary->IsLoaded()) { | 218 for (const std::string& word : words) |
189 const std::set<std::string>& words = dictionary->GetWords(); | 219 word_list->AppendString(word); |
190 for (const std::string& word : words) | 220 return word_list.Pass(); |
191 word_list->AppendString(word); | 221 } |
192 } | 222 |
193 return RespondNow(OneArgument(word_list.release())); | 223 LanguageSettingsPrivateAddSpellcheckWordFunction:: |
| 224 LanguageSettingsPrivateAddSpellcheckWordFunction() { |
| 225 } |
| 226 |
| 227 LanguageSettingsPrivateAddSpellcheckWordFunction:: |
| 228 ~LanguageSettingsPrivateAddSpellcheckWordFunction() { |
| 229 } |
| 230 |
| 231 ExtensionFunction::ResponseAction |
| 232 LanguageSettingsPrivateAddSpellcheckWordFunction::Run() { |
| 233 scoped_ptr<language_settings_private::AddSpellcheckWord::Params> params = |
| 234 language_settings_private::AddSpellcheckWord::Params::Create(*args_); |
| 235 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 236 |
| 237 SpellcheckService* service = |
| 238 SpellcheckServiceFactory::GetForContext(browser_context()); |
| 239 bool success = service->GetCustomDictionary()->AddWord(params->word); |
| 240 |
| 241 return RespondNow(OneArgument(new base::FundamentalValue(success))); |
| 242 } |
| 243 |
| 244 LanguageSettingsPrivateRemoveSpellcheckWordFunction:: |
| 245 LanguageSettingsPrivateRemoveSpellcheckWordFunction() { |
| 246 } |
| 247 |
| 248 LanguageSettingsPrivateRemoveSpellcheckWordFunction:: |
| 249 ~LanguageSettingsPrivateRemoveSpellcheckWordFunction() { |
| 250 } |
| 251 |
| 252 ExtensionFunction::ResponseAction |
| 253 LanguageSettingsPrivateRemoveSpellcheckWordFunction::Run() { |
| 254 scoped_ptr<language_settings_private::RemoveSpellcheckWord::Params> params = |
| 255 language_settings_private::RemoveSpellcheckWord::Params::Create(*args_); |
| 256 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 257 |
| 258 SpellcheckService* service = |
| 259 SpellcheckServiceFactory::GetForContext(browser_context()); |
| 260 bool success = service->GetCustomDictionary()->RemoveWord(params->word); |
| 261 |
| 262 return RespondNow(OneArgument(new base::FundamentalValue(success))); |
194 } | 263 } |
195 | 264 |
196 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: | 265 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: |
197 LanguageSettingsPrivateGetTranslateTargetLanguageFunction() | 266 LanguageSettingsPrivateGetTranslateTargetLanguageFunction() |
198 : chrome_details_(this) { | 267 : chrome_details_(this) { |
199 } | 268 } |
200 | 269 |
201 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: | 270 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: |
202 ~LanguageSettingsPrivateGetTranslateTargetLanguageFunction() { | 271 ~LanguageSettingsPrivateGetTranslateTargetLanguageFunction() { |
203 } | 272 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 LanguageSettingsPrivateRemoveInputMethodFunction:: | 311 LanguageSettingsPrivateRemoveInputMethodFunction:: |
243 ~LanguageSettingsPrivateRemoveInputMethodFunction() { | 312 ~LanguageSettingsPrivateRemoveInputMethodFunction() { |
244 } | 313 } |
245 | 314 |
246 ExtensionFunction::ResponseAction | 315 ExtensionFunction::ResponseAction |
247 LanguageSettingsPrivateRemoveInputMethodFunction::Run() { | 316 LanguageSettingsPrivateRemoveInputMethodFunction::Run() { |
248 return RespondNow(OneArgument(new base::FundamentalValue(true))); | 317 return RespondNow(OneArgument(new base::FundamentalValue(true))); |
249 } | 318 } |
250 | 319 |
251 } // namespace extensions | 320 } // namespace extensions |
OLD | NEW |