Chromium Code Reviews| 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 l10n_util::StringComparator<base::string16> c(collator.get()); | |
| 64 LanguageMap language_map(c); | |
|
stevenjb
2015/08/10 16:13:54
nit: elim |c|
michaelpg
2015/08/10 23:31:50
I don't write enough C++ these days to be confiden
stevenjb
2015/08/11 19:30:19
Right. It's just a nit, but the reasoning is: decl
| |
| 65 | |
| 66 // Build the list of display names and the language map. | |
| 67 for (size_t i = 0; i < language_codes.size(); ++i) { | |
|
stevenjb
2015/08/10 16:13:54
for (auto code : language_codes)
michaelpg
2015/08/10 23:31:51
Done (using const auto&)
| |
| 68 base::string16 display_name = l10n_util::GetDisplayNameForLocale( | |
| 69 language_codes[i], app_locale, false); | |
| 70 base::string16 native_display_name = l10n_util::GetDisplayNameForLocale( | |
| 71 language_codes[i], language_codes[i], false); | |
| 72 language_map[display_name] = | |
| 73 std::make_pair(language_codes[i], native_display_name); | |
| 74 } | |
| 75 | |
| 76 // Get the list of available locales (display languages) and convert to a set. | |
| 77 const std::vector<std::string>& locales = l10n_util::GetAvailableLocales(); | |
| 78 const std::unordered_set<std::string> locale_set( | |
| 79 locales.begin(), locales.end()); | |
| 80 | |
| 81 // Get the list of spell check languages and convert to a set. | |
| 82 std::vector<std::string> spellcheck_languages; | |
| 83 chrome::spellcheck_common::SpellCheckLanguages(&spellcheck_languages); | |
| 84 const std::unordered_set<std::string> spellcheck_language_set( | |
| 85 spellcheck_languages.begin(), spellcheck_languages.end()); | |
| 86 | |
| 87 // Get the list of translatable languages and convert to a set. | |
| 88 std::vector<std::string> translate_languages; | |
| 89 translate::TranslateDownloadManager::GetSupportedLanguages( | |
| 90 &translate_languages); | |
| 91 const std::unordered_set<std::string> translate_language_set( | |
| 92 translate_languages.begin(), translate_languages.end()); | |
| 93 | |
| 94 // Build the language list from the language map. | |
| 95 scoped_ptr<base::ListValue> language_list(new base::ListValue); | |
| 96 for (LanguageMap::iterator it = language_map.begin(); | |
| 97 it != language_map.end(); | |
| 98 ++it) { | |
|
stevenjb
2015/08/10 16:13:54
for (auto it : language_map)
michaelpg
2015/08/10 23:31:50
Done (using const auto&, and renaming because it's
| |
| 99 const base::string16& display_name = it->first; | |
| 100 const LanguagePair& pair = it->second; | |
| 101 | |
| 102 language_settings_private::Language language; | |
| 103 language.code = pair.first; | |
| 104 | |
| 105 base::string16 adjusted_display_name(display_name); | |
| 106 base::i18n::AdjustStringForLocaleDirection(&adjusted_display_name); | |
| 107 language.display_name = base::UTF16ToUTF8(adjusted_display_name); | |
| 108 | |
| 109 base::string16 adjusted_native_display_name(pair.second); | |
| 110 base::i18n::AdjustStringForLocaleDirection(&adjusted_native_display_name); | |
| 111 language.native_display_name = | |
| 112 base::UTF16ToUTF8(adjusted_native_display_name); | |
| 113 | |
| 114 // Set optional fields only if they differ from the default. | |
| 115 if (base::i18n::StringContainsStrongRTLChars(display_name)) | |
| 116 language.rtl.reset(new bool(true)); | |
| 117 if (locale_set.count(pair.first) > 0) | |
| 118 language.supports_ui.reset(new bool(true)); | |
| 119 if (spellcheck_language_set.count(pair.first) > 0) | |
| 120 language.supports_spellcheck.reset(new bool(true)); | |
| 121 if (translate_language_set.count(pair.first) > 0) | |
| 122 language.supports_translate.reset(new bool(true)); | |
| 123 | |
| 124 language_list->Append(language.ToValue()); | |
| 125 } | |
| 126 return RespondNow(OneArgument(language_list.release())); | |
| 25 } | 127 } |
| 26 | 128 |
| 27 LanguageSettingsPrivateSetLanguageListFunction:: | 129 LanguageSettingsPrivateSetLanguageListFunction:: |
| 28 LanguageSettingsPrivateSetLanguageListFunction() { | 130 LanguageSettingsPrivateSetLanguageListFunction() |
| 131 : chrome_details_(this) { | |
| 29 } | 132 } |
| 30 | 133 |
| 31 LanguageSettingsPrivateSetLanguageListFunction:: | 134 LanguageSettingsPrivateSetLanguageListFunction:: |
| 32 ~LanguageSettingsPrivateSetLanguageListFunction() { | 135 ~LanguageSettingsPrivateSetLanguageListFunction() { |
| 33 } | 136 } |
| 34 | 137 |
| 35 ExtensionFunction::ResponseAction | 138 ExtensionFunction::ResponseAction |
| 36 LanguageSettingsPrivateSetLanguageListFunction::Run() { | 139 LanguageSettingsPrivateSetLanguageListFunction::Run() { |
| 37 scoped_ptr<language_settings_private::SetLanguageList::Params> parameters = | 140 scoped_ptr<language_settings_private::SetLanguageList::Params> parameters = |
| 38 language_settings_private::SetLanguageList::Params::Create(*args_); | 141 language_settings_private::SetLanguageList::Params::Create(*args_); |
| 39 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | 142 EXTENSION_FUNCTION_VALIDATE(parameters.get()); |
| 40 | 143 |
| 144 Profile* profile = chrome_details_.GetProfile(); | |
| 145 scoped_ptr<translate::TranslatePrefs> translate_prefs = | |
| 146 ChromeTranslateClient::CreateTranslatePrefs(profile->GetPrefs()); | |
| 147 translate_prefs->UpdateLanguageList(parameters->language_codes); | |
| 148 | |
| 41 return RespondNow(NoArguments()); | 149 return RespondNow(NoArguments()); |
| 42 } | 150 } |
| 43 | 151 |
| 44 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction:: | 152 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction:: |
| 45 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction() { | 153 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction() { |
| 46 } | 154 } |
| 47 | 155 |
| 48 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction:: | 156 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction:: |
| 49 ~LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction() { | 157 ~LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction() { |
| 50 } | 158 } |
| 51 | 159 |
| 52 ExtensionFunction::ResponseAction | 160 ExtensionFunction::ResponseAction |
| 53 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction::Run() { | 161 LanguageSettingsPrivateGetSpellcheckDictionaryStatusesFunction::Run() { |
| 54 return RespondNow(OneArgument(new base::ListValue())); | 162 LanguageSettingsPrivateDelegate* delegate = |
| 163 LanguageSettingsPrivateDelegateFactory::GetForBrowserContext( | |
| 164 browser_context()); | |
| 165 const std::vector<scoped_ptr< | |
| 166 language_settings_private::SpellcheckDictionaryStatus>>& statuses = | |
| 167 delegate->GetHunspellDictionaryStatuses(); | |
| 168 | |
| 169 scoped_ptr<base::ListValue> return_list(new base::ListValue()); | |
| 170 for (const auto& status : statuses) | |
| 171 return_list->Append(status->ToValue()); | |
| 172 return RespondNow(OneArgument(return_list.release())); | |
| 55 } | 173 } |
| 56 | 174 |
| 57 LanguageSettingsPrivateGetSpellcheckWordsFunction:: | 175 LanguageSettingsPrivateGetSpellcheckWordsFunction:: |
| 58 LanguageSettingsPrivateGetSpellcheckWordsFunction() { | 176 LanguageSettingsPrivateGetSpellcheckWordsFunction() { |
| 59 } | 177 } |
| 60 | 178 |
| 61 LanguageSettingsPrivateGetSpellcheckWordsFunction:: | 179 LanguageSettingsPrivateGetSpellcheckWordsFunction:: |
| 62 ~LanguageSettingsPrivateGetSpellcheckWordsFunction() { | 180 ~LanguageSettingsPrivateGetSpellcheckWordsFunction() { |
| 63 } | 181 } |
| 64 | 182 |
| 65 ExtensionFunction::ResponseAction | 183 ExtensionFunction::ResponseAction |
| 66 LanguageSettingsPrivateGetSpellcheckWordsFunction::Run() { | 184 LanguageSettingsPrivateGetSpellcheckWordsFunction::Run() { |
| 67 return RespondNow(OneArgument(new base::ListValue())); | 185 SpellcheckService* service = |
| 186 SpellcheckServiceFactory::GetForContext(browser_context()); | |
| 187 SpellcheckCustomDictionary* dictionary = service->GetCustomDictionary(); | |
| 188 | |
| 189 scoped_ptr<base::ListValue> word_list(new base::ListValue()); | |
| 190 // TODO(michaelpg): observe the dictionary and respond later if not loaded. | |
| 191 if (dictionary->IsLoaded()) { | |
| 192 const std::set<std::string>& words = dictionary->GetWords(); | |
| 193 for (const std::string& word : words) | |
| 194 word_list->AppendString(word); | |
| 195 } | |
| 196 return RespondNow(OneArgument(word_list.release())); | |
| 68 } | 197 } |
| 69 | 198 |
| 70 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: | 199 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: |
| 71 LanguageSettingsPrivateGetTranslateTargetLanguageFunction() { | 200 LanguageSettingsPrivateGetTranslateTargetLanguageFunction() |
| 201 : chrome_details_(this) { | |
| 72 } | 202 } |
| 73 | 203 |
| 74 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: | 204 LanguageSettingsPrivateGetTranslateTargetLanguageFunction:: |
| 75 ~LanguageSettingsPrivateGetTranslateTargetLanguageFunction() { | 205 ~LanguageSettingsPrivateGetTranslateTargetLanguageFunction() { |
| 76 } | 206 } |
| 77 | 207 |
| 78 ExtensionFunction::ResponseAction | 208 ExtensionFunction::ResponseAction |
| 79 LanguageSettingsPrivateGetTranslateTargetLanguageFunction::Run() { | 209 LanguageSettingsPrivateGetTranslateTargetLanguageFunction::Run() { |
| 80 return RespondNow(OneArgument(new base::StringValue(""))); | 210 Profile* profile = chrome_details_.GetProfile(); |
| 211 PrefService* prefs = profile->GetPrefs(); | |
| 212 std::string target_language = | |
| 213 TranslateService::GetTargetLanguage(prefs); | |
|
stevenjb
2015/08/10 16:13:54
nit: no need for local var
michaelpg
2015/08/10 23:31:50
Done. Hope this isn't too much.
stevenjb
2015/08/11 19:30:19
Occasionally, a well named local used only once he
| |
| 214 return RespondNow(OneArgument(new base::StringValue(target_language))); | |
| 81 } | 215 } |
| 82 | 216 |
| 83 LanguageSettingsPrivateGetInputMethodListsFunction:: | 217 LanguageSettingsPrivateGetInputMethodListsFunction:: |
| 84 LanguageSettingsPrivateGetInputMethodListsFunction() { | 218 LanguageSettingsPrivateGetInputMethodListsFunction() { |
| 85 } | 219 } |
| 86 | 220 |
| 87 LanguageSettingsPrivateGetInputMethodListsFunction:: | 221 LanguageSettingsPrivateGetInputMethodListsFunction:: |
| 88 ~LanguageSettingsPrivateGetInputMethodListsFunction() { | 222 ~LanguageSettingsPrivateGetInputMethodListsFunction() { |
| 89 } | 223 } |
| 90 | 224 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 113 LanguageSettingsPrivateRemoveInputMethodFunction:: | 247 LanguageSettingsPrivateRemoveInputMethodFunction:: |
| 114 ~LanguageSettingsPrivateRemoveInputMethodFunction() { | 248 ~LanguageSettingsPrivateRemoveInputMethodFunction() { |
| 115 } | 249 } |
| 116 | 250 |
| 117 ExtensionFunction::ResponseAction | 251 ExtensionFunction::ResponseAction |
| 118 LanguageSettingsPrivateRemoveInputMethodFunction::Run() { | 252 LanguageSettingsPrivateRemoveInputMethodFunction::Run() { |
| 119 return RespondNow(OneArgument(new base::FundamentalValue(true))); | 253 return RespondNow(OneArgument(new base::FundamentalValue(true))); |
| 120 } | 254 } |
| 121 | 255 |
| 122 } // namespace extensions | 256 } // namespace extensions |
| OLD | NEW |