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> | |
8 #include <string> | |
9 #include <unordered_set> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/i18n/rtl.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/strings/string16.h" | |
16 #include "base/strings/utf_string_conversions.h" | |
7 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chrome/browser/browser_process.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" | |
21 #include "chrome/browser/profiles/profile.h" | |
22 #include "chrome/browser/spellchecker/spellcheck_factory.h" | |
23 #include "chrome/browser/spellchecker/spellcheck_service.h" | |
24 #include "chrome/browser/translate/chrome_translate_client.h" | |
25 #include "chrome/browser/translate/translate_service.h" | |
8 #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" | |
28 #include "components/translate/core/browser/translate_download_manager.h" | |
29 #include "third_party/icu/source/i18n/unicode/coll.h" | |
30 #include "ui/base/l10n/l10n_util.h" | |
31 #include "ui/base/l10n/l10n_util_collator.h" | |
9 | 32 |
10 namespace extensions { | 33 namespace extensions { |
11 | 34 |
12 namespace language_settings_private = api::language_settings_private; | 35 namespace language_settings_private = api::language_settings_private; |
13 | 36 |
14 LanguageSettingsPrivateGetLanguageListFunction:: | 37 LanguageSettingsPrivateGetLanguageListFunction:: |
15 LanguageSettingsPrivateGetLanguageListFunction() { | 38 LanguageSettingsPrivateGetLanguageListFunction() { |
16 } | 39 } |
17 | 40 |
18 LanguageSettingsPrivateGetLanguageListFunction:: | 41 LanguageSettingsPrivateGetLanguageListFunction:: |
19 ~LanguageSettingsPrivateGetLanguageListFunction() { | 42 ~LanguageSettingsPrivateGetLanguageListFunction() { |
20 } | 43 } |
21 | 44 |
22 ExtensionFunction::ResponseAction | 45 ExtensionFunction::ResponseAction |
23 LanguageSettingsPrivateGetLanguageListFunction::Run() { | 46 LanguageSettingsPrivateGetLanguageListFunction::Run() { |
24 return RespondNow(OneArgument(new base::ListValue())); | 47 // Collect the language codes from the supported accept-languages. |
48 const std::string app_locale = g_browser_process->GetApplicationLocale(); | |
49 std::vector<std::string> language_codes; | |
50 l10n_util::GetAcceptLanguagesForLocale(app_locale, &language_codes); | |
51 | |
52 // Map of display name -> {language code, native display name}. | |
53 typedef std::pair<std::string, base::string16> LanguagePair; | |
54 typedef std::map<base::string16, LanguagePair, | |
55 l10n_util::StringComparator<base::string16>> LanguageMap; | |
56 | |
57 // Collator used to sort display names in the current locale. | |
58 UErrorCode error = U_ZERO_ERROR; | |
59 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance( | |
60 icu::Locale(app_locale.c_str()), error)); | |
61 if (U_FAILURE(error)) | |
62 collator.reset(); | |
63 LanguageMap language_map( | |
64 l10n_util::StringComparator<base::string16>(collator.get())); | |
65 | |
66 // Build the list of display names and the language map. | |
67 for (const auto& code : language_codes) { | |
68 base::string16 display_name = l10n_util::GetDisplayNameForLocale( | |
69 code, app_locale, false); | |
70 base::string16 native_display_name = l10n_util::GetDisplayNameForLocale( | |
71 code, code, false); | |
72 language_map[display_name] = std::make_pair(code, native_display_name); | |
73 } | |
74 | |
75 // Get the list of available locales (display languages) and convert to a set. | |
76 const std::vector<std::string>& locales = l10n_util::GetAvailableLocales(); | |
77 const std::unordered_set<std::string> locale_set( | |
78 locales.begin(), locales.end()); | |
79 | |
80 // Get the list of spell check languages and convert to a set. | |
81 std::vector<std::string> spellcheck_languages; | |
82 chrome::spellcheck_common::SpellCheckLanguages(&spellcheck_languages); | |
83 const std::unordered_set<std::string> spellcheck_language_set( | |
84 spellcheck_languages.begin(), spellcheck_languages.end()); | |
85 | |
86 // Get the list of translatable languages and convert to a set. | |
87 std::vector<std::string> translate_languages; | |
88 translate::TranslateDownloadManager::GetSupportedLanguages( | |
Seigo Nonaka
2015/08/11 18:48:35
Is this function called different from UI thread?
hajimehoshi
2015/08/12 05:34:48
I don't think this function is thread-safe.
michaelpg
2015/08/14 00:10:48
Could you elaborate on why not?
language_options_
michaelpg
2015/08/14 00:10:49
This function is called on the UI thread (it's par
Seigo Nonaka
2015/08/14 00:18:47
Okay, I'm sorry I was not aware of UIThreadExtensi
| |
89 &translate_languages); | |
90 const std::unordered_set<std::string> translate_language_set( | |
91 translate_languages.begin(), translate_languages.end()); | |
92 | |
93 // Build the language list from the language map. | |
94 scoped_ptr<base::ListValue> language_list(new base::ListValue); | |
95 for (const auto& entry : language_map) { | |
96 const base::string16& display_name = entry.first; | |
97 const LanguagePair& pair = entry.second; | |
98 | |
99 language_settings_private::Language language; | |
100 language.code = pair.first; | |
101 | |
102 base::string16 adjusted_display_name(display_name); | |
103 base::i18n::AdjustStringForLocaleDirection(&adjusted_display_name); | |
104 language.display_name = base::UTF16ToUTF8(adjusted_display_name); | |
105 | |
106 base::string16 adjusted_native_display_name(pair.second); | |
107 base::i18n::AdjustStringForLocaleDirection(&adjusted_native_display_name); | |
108 language.native_display_name = | |
109 base::UTF16ToUTF8(adjusted_native_display_name); | |
110 | |
111 // Set optional fields only if they differ from the default. | |
112 if (base::i18n::StringContainsStrongRTLChars(display_name)) | |
113 language.display_name_rtl.reset(new bool(true)); | |
114 if (locale_set.count(pair.first) > 0) | |
115 language.supports_ui.reset(new bool(true)); | |
116 if (spellcheck_language_set.count(pair.first) > 0) | |
117 language.supports_spellcheck.reset(new bool(true)); | |
118 if (translate_language_set.count(pair.first) > 0) | |
119 language.supports_translate.reset(new bool(true)); | |
120 | |
121 language_list->Append(language.ToValue()); | |
122 } | |
123 return RespondNow(OneArgument(language_list.release())); | |
25 } | 124 } |
26 | 125 |
27 LanguageSettingsPrivateSetLanguageListFunction:: | 126 LanguageSettingsPrivateSetLanguageListFunction:: |
28 LanguageSettingsPrivateSetLanguageListFunction() { | 127 LanguageSettingsPrivateSetLanguageListFunction() |
128 : chrome_details_(this) { | |
29 } | 129 } |
30 | 130 |
31 LanguageSettingsPrivateSetLanguageListFunction:: | 131 LanguageSettingsPrivateSetLanguageListFunction:: |
32 ~LanguageSettingsPrivateSetLanguageListFunction() { | 132 ~LanguageSettingsPrivateSetLanguageListFunction() { |
33 } | 133 } |
34 | 134 |
35 ExtensionFunction::ResponseAction | 135 ExtensionFunction::ResponseAction |
36 LanguageSettingsPrivateSetLanguageListFunction::Run() { | 136 LanguageSettingsPrivateSetLanguageListFunction::Run() { |
37 scoped_ptr<language_settings_private::SetLanguageList::Params> parameters = | 137 scoped_ptr<language_settings_private::SetLanguageList::Params> parameters = |
38 language_settings_private::SetLanguageList::Params::Create(*args_); | 138 language_settings_private::SetLanguageList::Params::Create(*args_); |
39 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | 139 EXTENSION_FUNCTION_VALIDATE(parameters.get()); |
40 | 140 |
141 scoped_ptr<translate::TranslatePrefs> translate_prefs = | |
142 ChromeTranslateClient::CreateTranslatePrefs( | |
143 chrome_details_.GetProfile()->GetPrefs()); | |
144 translate_prefs->UpdateLanguageList(parameters->language_codes); | |
145 | |
41 return RespondNow(NoArguments()); | 146 return RespondNow(NoArguments()); |
42 } | 147 } |
43 | 148 |
44 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction:: | 149 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction:: |
45 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction() { | 150 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction() { |
46 } | 151 } |
47 | 152 |
48 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction:: | 153 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction:: |
49 ~LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction() { | 154 ~LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction() { |
50 } | 155 } |
51 | 156 |
52 ExtensionFunction::ResponseAction | 157 ExtensionFunction::ResponseAction |
53 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction::Run() { | 158 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction::Run() { |
54 return RespondNow(OneArgument(new base::ListValue())); | 159 LanguageSettingsPrivateDelegate* delegate = |
160 LanguageSettingsPrivateDelegateFactory::GetForBrowserContext( | |
161 browser_context()); | |
162 | |
163 scoped_ptr<base::ListValue> return_list(new base::ListValue()); | |
164 for (const auto& status : delegate->GetHunspellDictionaryStatuses()) | |
165 return_list->Append(status->ToValue()); | |
166 return RespondNow(OneArgument(return_list.release())); | |
55 } | 167 } |
56 | 168 |
57 LanguageSettingsPrivateGetSpellcheckWordsFunction:: | 169 LanguageSettingsPrivateGetSpellcheckWordsFunction:: |
58 LanguageSettingsPrivateGetSpellcheckWordsFunction() { | 170 LanguageSettingsPrivateGetSpellcheckWordsFunction() { |
59 } | 171 } |
60 | 172 |
61 LanguageSettingsPrivateGetSpellcheckWordsFunction:: | 173 LanguageSettingsPrivateGetSpellcheckWordsFunction:: |
62 ~LanguageSettingsPrivateGetSpellcheckWordsFunction() { | 174 ~LanguageSettingsPrivateGetSpellcheckWordsFunction() { |
63 } | 175 } |
64 | 176 |
65 ExtensionFunction::ResponseAction | 177 ExtensionFunction::ResponseAction |
66 LanguageSettingsPrivateGetSpellcheckWordsFunction::Run() { | 178 LanguageSettingsPrivateGetSpellcheckWordsFunction::Run() { |
67 return RespondNow(OneArgument(new base::ListValue())); | 179 SpellcheckService* service = |
180 SpellcheckServiceFactory::GetForContext(browser_context()); | |
181 SpellcheckCustomDictionary* dictionary = service->GetCustomDictionary(); | |
182 | |
183 scoped_ptr<base::ListValue> word_list(new base::ListValue()); | |
184 // TODO(michaelpg): observe the dictionary and respond later if not loaded. | |
185 if (dictionary->IsLoaded()) { | |
186 const std::set<std::string>& words = dictionary->GetWords(); | |
187 for (const std::string& word : words) | |
188 word_list->AppendString(word); | |
189 } | |
190 return RespondNow(OneArgument(word_list.release())); | |
68 } | 191 } |
69 | 192 |
70 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: | 193 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: |
71 LanguageSettingsPrivateGetTranslateTargetLanguageFunction() { | 194 LanguageSettingsPrivateGetTranslateTargetLanguageFunction() |
195 : chrome_details_(this) { | |
72 } | 196 } |
73 | 197 |
74 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: | 198 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: |
75 ~LanguageSettingsPrivateGetTranslateTargetLanguageFunction() { | 199 ~LanguageSettingsPrivateGetTranslateTargetLanguageFunction() { |
76 } | 200 } |
77 | 201 |
78 ExtensionFunction::ResponseAction | 202 ExtensionFunction::ResponseAction |
79 LanguageSettingsPrivateGetTranslateTargetLanguageFunction::Run() { | 203 LanguageSettingsPrivateGetTranslateTargetLanguageFunction::Run() { |
80 return RespondNow(OneArgument(new base::StringValue(""))); | 204 return RespondNow(OneArgument(new base::StringValue( |
205 TranslateService::GetTargetLanguage( | |
206 chrome_details_.GetProfile()->GetPrefs())))); | |
81 } | 207 } |
82 | 208 |
83 LanguageSettingsPrivateGetInputMethodListsFunction:: | 209 LanguageSettingsPrivateGetInputMethodListsFunction:: |
84 LanguageSettingsPrivateGetInputMethodListsFunction() { | 210 LanguageSettingsPrivateGetInputMethodListsFunction() { |
85 } | 211 } |
86 | 212 |
87 LanguageSettingsPrivateGetInputMethodListsFunction:: | 213 LanguageSettingsPrivateGetInputMethodListsFunction:: |
88 ~LanguageSettingsPrivateGetInputMethodListsFunction() { | 214 ~LanguageSettingsPrivateGetInputMethodListsFunction() { |
89 } | 215 } |
90 | 216 |
(...skipping 22 matching lines...) Expand all Loading... | |
113 LanguageSettingsPrivateRemoveInputMethodFunction:: | 239 LanguageSettingsPrivateRemoveInputMethodFunction:: |
114 ~LanguageSettingsPrivateRemoveInputMethodFunction() { | 240 ~LanguageSettingsPrivateRemoveInputMethodFunction() { |
115 } | 241 } |
116 | 242 |
117 ExtensionFunction::ResponseAction | 243 ExtensionFunction::ResponseAction |
118 LanguageSettingsPrivateRemoveInputMethodFunction::Run() { | 244 LanguageSettingsPrivateRemoveInputMethodFunction::Run() { |
119 return RespondNow(OneArgument(new base::FundamentalValue(true))); | 245 return RespondNow(OneArgument(new base::FundamentalValue(true))); |
120 } | 246 } |
121 | 247 |
122 } // namespace extensions | 248 } // namespace extensions |
OLD | NEW |