| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/i18n_custom_bindings.h" | 5 #include "extensions/renderer/i18n_custom_bindings.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/logging.h" |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 16 #include "content/public/child/v8_value_converter.h" | 17 #include "content/public/child/v8_value_converter.h" |
| 17 #include "content/public/renderer/render_frame.h" | 18 #include "content/public/renderer/render_frame.h" |
| 18 #include "content/public/renderer/render_thread.h" | 19 #include "content/public/renderer/render_thread.h" |
| 19 #include "extensions/common/extension_messages.h" | 20 #include "extensions/common/extension_messages.h" |
| 20 #include "extensions/common/message_bundle.h" | 21 #include "extensions/common/message_bundle.h" |
| 21 #include "extensions/renderer/script_context.h" | 22 #include "extensions/renderer/script_context.h" |
| 22 #include "extensions/renderer/v8_helpers.h" | 23 #include "extensions/renderer/v8_helpers.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 } | 129 } |
| 129 } | 130 } |
| 130 | 131 |
| 131 #elif BUILDFLAG(CLD_VERSION) == 3 | 132 #elif BUILDFLAG(CLD_VERSION) == 3 |
| 132 void InitDetectedLanguages( | 133 void InitDetectedLanguages( |
| 133 const std::vector<chrome_lang_id::NNetLanguageIdentifier::Result>& | 134 const std::vector<chrome_lang_id::NNetLanguageIdentifier::Result>& |
| 134 lang_results, | 135 lang_results, |
| 135 LanguageDetectionResult* result) { | 136 LanguageDetectionResult* result) { |
| 136 std::vector<std::unique_ptr<DetectedLanguage>>* detected_languages = | 137 std::vector<std::unique_ptr<DetectedLanguage>>* detected_languages = |
| 137 &result->languages; | 138 &result->languages; |
| 139 DCHECK(detected_languages->empty()); |
| 138 bool* is_reliable = &result->is_reliable; | 140 bool* is_reliable = &result->is_reliable; |
| 139 | 141 |
| 140 // is_reliable is set to "true", so that the reliability can be calculated by | 142 // is_reliable is set to "true", so that the reliability can be calculated by |
| 141 // &&'ing the reliability of each predicted language. | 143 // &&'ing the reliability of each predicted language. |
| 142 *is_reliable = true; | 144 *is_reliable = true; |
| 143 for (size_t i = 0; i < lang_results.size(); ++i) { | 145 for (size_t i = 0; i < lang_results.size(); ++i) { |
| 144 const chrome_lang_id::NNetLanguageIdentifier::Result& lang_result = | 146 const chrome_lang_id::NNetLanguageIdentifier::Result& lang_result = |
| 145 lang_results.at(i); | 147 lang_results.at(i); |
| 146 const std::string& language_code = lang_result.language; | 148 const std::string& language_code = lang_result.language; |
| 149 |
| 150 // If a language is kUnknown, then the remaining ones are also kUnknown. |
| 147 if (language_code == chrome_lang_id::NNetLanguageIdentifier::kUnknown) { | 151 if (language_code == chrome_lang_id::NNetLanguageIdentifier::kUnknown) { |
| 148 // If the first language is kUnknown, then all languages are kUnknown. | |
| 149 // Thus, is_reliable is set to "false". | |
| 150 if (i == 0) { | |
| 151 *is_reliable = false; | |
| 152 } | |
| 153 break; | 152 break; |
| 154 } | 153 } |
| 155 | 154 |
| 155 // The list of languages supported by CLD3 is saved in kLanguageNames |
| 156 // in the following file: |
| 157 // //src/third_party/cld_3/src/src/task_context_params.cc |
| 158 // Among the entries in this list are transliterated languages |
| 159 // (called xx-Latn) which don't belong to the spec ISO639-1 used by |
| 160 // the previous model, CLD2. Thus, to maintain backwards compatibility, |
| 161 // xx-Latn predictions are ignored for now. |
| 162 if (base::EndsWith(language_code, "-Latn", |
| 163 base::CompareCase::INSENSITIVE_ASCII)) { |
| 164 continue; |
| 165 } |
| 166 |
| 156 *is_reliable = *is_reliable && lang_result.is_reliable; | 167 *is_reliable = *is_reliable && lang_result.is_reliable; |
| 157 const int percent = static_cast<int>(100 * lang_result.proportion); | 168 const int percent = static_cast<int>(100 * lang_result.proportion); |
| 158 detected_languages->push_back( | 169 detected_languages->push_back( |
| 159 base::MakeUnique<DetectedLanguage>(language_code, percent)); | 170 base::MakeUnique<DetectedLanguage>(language_code, percent)); |
| 160 } | 171 } |
| 172 |
| 173 if (detected_languages->empty()) { |
| 174 *is_reliable = false; |
| 175 } |
| 161 } | 176 } |
| 162 #else | 177 #else |
| 163 # error "CLD_VERSION must be 2 or 3" | 178 # error "CLD_VERSION must be 2 or 3" |
| 164 #endif | 179 #endif |
| 165 | 180 |
| 166 } // namespace | 181 } // namespace |
| 167 | 182 |
| 168 I18NCustomBindings::I18NCustomBindings(ScriptContext* context) | 183 I18NCustomBindings::I18NCustomBindings(ScriptContext* context) |
| 169 : ObjectBackedNativeHandler(context) { | 184 : ObjectBackedNativeHandler(context) { |
| 170 RouteFunction( | 185 RouteFunction( |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 // Populate LanguageDetectionResult with prediction reliability, languages, | 317 // Populate LanguageDetectionResult with prediction reliability, languages, |
| 303 // and the corresponding percentages. | 318 // and the corresponding percentages. |
| 304 InitDetectedLanguages(lang_results, &result); | 319 InitDetectedLanguages(lang_results, &result); |
| 305 args.GetReturnValue().Set(result.ToValue(context())); | 320 args.GetReturnValue().Set(result.ToValue(context())); |
| 306 #else | 321 #else |
| 307 # error "CLD_VERSION must be 2 or 3" | 322 # error "CLD_VERSION must be 2 or 3" |
| 308 #endif | 323 #endif |
| 309 } | 324 } |
| 310 | 325 |
| 311 } // namespace extensions | 326 } // namespace extensions |
| OLD | NEW |