Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/extensions/api/i18n/i18n_api.h" | 5 #include "chrome/browser/extensions/api/i18n/i18n_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | |
| 9 #include <vector> | 8 #include <vector> |
| 10 | 9 |
| 11 #include "base/lazy_instance.h" | |
| 12 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 14 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/extensions/api/i18n.h" | 14 #include "chrome/common/extensions/api/i18n.h" |
| 17 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "tools/json_schema_compiler/util.h" | |
| 17 | |
| 18 #define CLD_NUM_OF_LANGS 3 | |
|
not at google - send to devlin
2015/07/10 16:21:09
use "const int kCldNumLands = 3" not #define
amalika
2015/07/13 23:57:00
Done.
| |
| 18 | 19 |
| 19 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; | 20 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; |
|
not at google - send to devlin
2015/07/10 16:21:09
It makes sense to move this namespace alias inside
amalika
2015/07/13 23:57:01
Done.
| |
| 20 | 21 |
| 21 namespace extensions { | 22 namespace extensions { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Errors. | 26 // Errors. |
| 26 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; | 27 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; |
| 27 | 28 |
| 28 } | 29 } |
| 29 | 30 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 52 | 53 |
| 53 if (languages.empty()) { | 54 if (languages.empty()) { |
| 54 error_ = kEmptyAcceptLanguagesError; | 55 error_ = kEmptyAcceptLanguagesError; |
| 55 return false; | 56 return false; |
| 56 } | 57 } |
| 57 | 58 |
| 58 results_ = GetAcceptLanguages::Results::Create(languages); | 59 results_ = GetAcceptLanguages::Results::Create(languages); |
| 59 return true; | 60 return true; |
| 60 } | 61 } |
| 61 | 62 |
| 63 | |
| 64 I18nDetectLanguageFunction::DetectedLanguage::DetectedLanguage() | |
| 65 : percentage(0) {} | |
| 66 | |
| 67 I18nDetectLanguageFunction::DetectedLanguage::DetectedLanguage( | |
| 68 std::string language, | |
| 69 int percentage) | |
| 70 : language(language), percentage(percentage) {} | |
| 71 | |
| 72 I18nDetectLanguageFunction::DetectedLanguage::~DetectedLanguage() {} | |
| 73 | |
| 74 scoped_ptr<base::DictionaryValue> | |
| 75 I18nDetectLanguageFunction::DetectedLanguage::ToValue() const { | |
| 76 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | |
| 77 | |
| 78 value->SetWithoutPathExpansion("language", | |
| 79 new base::StringValue(this->language)); | |
| 80 | |
| 81 value->SetWithoutPathExpansion("percentage", | |
| 82 new base::FundamentalValue(this->percentage)); | |
| 83 | |
| 84 | |
| 85 return value.Pass(); | |
| 86 } | |
| 87 | |
| 88 I18nDetectLanguageFunction::LanguageDetectionResult::LanguageDetectionResult() | |
| 89 : is_reliable(false) {} | |
| 90 | |
| 91 I18nDetectLanguageFunction::LanguageDetectionResult::LanguageDetectionResult( | |
| 92 bool is_reliable, | |
| 93 std::vector<linked_ptr<DetectedLanguage>> detected_languages) | |
| 94 : is_reliable(is_reliable), detected_langs(detected_languages) {} | |
| 95 | |
| 96 I18nDetectLanguageFunction::LanguageDetectionResult::~LanguageDetectionResult() | |
| 97 {} | |
| 98 | |
| 99 scoped_ptr<base::DictionaryValue> | |
| 100 I18nDetectLanguageFunction::LanguageDetectionResult::ToValue() const { | |
| 101 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | |
| 102 | |
| 103 value->SetWithoutPathExpansion("isReliable", | |
| 104 new base::FundamentalValue(this->is_reliable)); | |
| 105 | |
| 106 if (this->detected_langs.empty()) { | |
| 107 value->SetWithoutPathExpansion("languages", new base::ListValue()); | |
| 108 } else { | |
| 109 value->SetWithoutPathExpansion("languages", | |
| 110 json_schema_compiler::util::CreateValueFromArray( | |
| 111 this->detected_langs).release()); | |
| 112 } | |
| 113 | |
| 114 return value.Pass(); | |
| 115 } | |
| 116 | |
| 117 | |
| 118 ExtensionFunction::ResponseAction I18nDetectLanguageFunction::Run() { | |
| 119 scoped_ptr<api::i18n::DetectLanguage::Params> params( | |
| 120 api::i18n::DetectLanguage::Params::Create(*args_)); | |
| 121 | |
| 122 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 123 | |
| 124 GetLanguage(params->text); | |
| 125 | |
| 126 return RespondLater(); | |
| 127 } | |
| 128 | |
| 129 void I18nDetectLanguageFunction::GetLanguage(const std::string& text) { | |
| 130 // Sets up the input String | |
| 131 const std::string utf8_text(text); | |
| 132 const int num_utf8_bytes = static_cast<int>(utf8_text.size()); | |
| 133 const char* raw_utf8_bytes = utf8_text.c_str(); | |
| 134 | |
| 135 // TODO(mcindy): improve this by providing better CLD hints | |
| 136 const char tld_hint[] = ""; // asummed no cld hints provided | |
| 137 int encoding_hint = CLD2::UNKNOWN_ENCODING; | |
| 138 CLD2::Language language_hint = CLD2::UNKNOWN_LANGUAGE; | |
| 139 CLD2::CLDHints cldhints = {NULL, tld_hint, encoding_hint, language_hint}; | |
| 140 | |
| 141 const bool is_plain_text = true; | |
| 142 CLD2::Language language3[CLD_NUM_OF_LANGS]; | |
| 143 int percent3[CLD_NUM_OF_LANGS]; | |
| 144 int text_bytes; // amount of non-tag/letters-only text (assumed 0) | |
| 145 bool is_reliable = false; | |
| 146 | |
| 147 int cld_language = 0; | |
| 148 int flags = 0; | |
| 149 int valid_prefix_bytes = false; | |
| 150 double normalized_score3[CLD_NUM_OF_LANGS]; | |
|
not at google - send to devlin
2015/07/10 16:21:09
just |normalized_scores|, take out all references
amalika
2015/07/13 23:57:00
Done.
| |
| 151 | |
| 152 cld_language = CLD2::ExtDetectLanguageSummaryCheckUTF8( | |
|
not at google - send to devlin
2015/07/10 16:21:09
Declare and initialize |cld_language| here.
amalika
2015/07/13 23:57:00
Done.
| |
| 153 raw_utf8_bytes, | |
| 154 num_utf8_bytes, | |
| 155 is_plain_text, | |
| 156 &cldhints, | |
| 157 flags, | |
| 158 language3, | |
| 159 percent3, | |
| 160 normalized_score3, | |
| 161 NULL, // assumed no ResultChunkVector is used | |
| 162 &text_bytes, | |
| 163 &is_reliable, | |
| 164 &valid_prefix_bytes); | |
| 165 | |
| 166 std::vector<linked_ptr<DetectedLanguage> > detected_languages; | |
| 167 InitDetectedLanguages(language3, percent3, &detected_languages); | |
| 168 | |
| 169 SendLanguagesResult(detected_languages, is_reliable); | |
| 170 } | |
| 171 | |
| 172 void I18nDetectLanguageFunction::SendLanguagesResult( | |
| 173 std::vector<linked_ptr<DetectedLanguage> > detected_languages, | |
| 174 bool is_reliable) { | |
| 175 LanguageDetectionResult* result = new LanguageDetectionResult( | |
|
not at google - send to devlin
2015/07/10 16:21:09
I don't see where this result is destroyed; but in
amalika
2015/07/13 23:57:01
Done.
| |
| 176 is_reliable, | |
| 177 detected_languages); | |
| 178 SetResult(result->ToValue()); | |
| 179 SendResponse(true); | |
| 180 } | |
| 181 | |
| 182 void I18nDetectLanguageFunction::InitDetectedLanguages( | |
| 183 CLD2::Language* languages, int* percent3, | |
| 184 std::vector<linked_ptr<DetectedLanguage>> *detected_languages) { | |
| 185 for (int i = 0; i< CLD_NUM_OF_LANGS; i++) { | |
|
not at google - send to devlin
2015/07/10 16:21:09
"i <" not "i<"
amalika
2015/07/13 23:57:00
Done.
| |
| 186 std::string language_code = ""; | |
| 187 | |
| 188 // Convert LanguageCode 'zh' to 'zh-CN' and 'zh-Hant' to 'zh-TW' for | |
| 189 // Translate server usage. see DetermineTextLanguage in | |
| 190 // components/translate/core/language_detection/language_detection_util.cc | |
| 191 if (languages[i] == CLD2::CHINESE) { | |
| 192 language_code = "zh-CN"; | |
| 193 } else if (languages[i] == CLD2::CHINESE_T) { | |
| 194 language_code = "zh-TW"; | |
| 195 } else { | |
| 196 language_code = | |
| 197 CLD2::LanguageCode(static_cast<CLD2::Language>(languages[i])); | |
| 198 // "un" stands for undefined language | |
| 199 // no need to save in detected_languages | |
| 200 if (language_code == "un") { | |
| 201 break; | |
|
not at google - send to devlin
2015/07/10 16:21:09
Didn't we establish not to use "un"?
amalika
2015/07/13 23:57:01
Done.
| |
| 202 } | |
| 203 } | |
| 204 | |
| 205 detected_languages->push_back(linked_ptr<DetectedLanguage>( | |
| 206 new DetectedLanguage(language_code, percent3[i]))); | |
| 207 } | |
| 208 } | |
| 209 | |
| 62 } // namespace extensions | 210 } // namespace extensions |
| OLD | NEW |