Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1740)

Unified Diff: chrome/browser/extensions/api/i18n/i18n_api.cc

Issue 1208993011: New thin layer of API extension chrome.i18n.detectLanguage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleaned up the code Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/i18n/i18n_api.cc
diff --git a/chrome/browser/extensions/api/i18n/i18n_api.cc b/chrome/browser/extensions/api/i18n/i18n_api.cc
index ca40229ba1a03de856fde71207b23ff3386653e6..213d0dfe74aa3202456cc8dc1289d9ca0a7ff7cd 100644
--- a/chrome/browser/extensions/api/i18n/i18n_api.cc
+++ b/chrome/browser/extensions/api/i18n/i18n_api.cc
@@ -5,23 +5,25 @@
#include "chrome/browser/extensions/api/i18n/i18n_api.h"
#include <algorithm>
-#include <string>
#include <vector>
-#include "base/lazy_instance.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "chrome/browser/profiles/profile.h"
-#include "chrome/common/extensions/api/i18n.h"
#include "chrome/common/pref_names.h"
-namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages;
-
namespace extensions {
-namespace {
+namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages;
+// Typedef only introduced to avoid unreadable code.
not at google - send to devlin 2015/07/14 18:42:57 comment not necessary.
amalika 2015/07/14 22:41:02 Done.
+typedef struct extensions::api::i18n::DetectLanguage::Results::Result::
+ LanguagesType DetectedLanguage;
+typedef struct extensions::api::i18n::DetectLanguage::Results::Result
+ LanguageDetectionResult;
not at google - send to devlin 2015/07/14 18:42:58 c++11 has "using LanguageDetectionResult = ..." no
amalika 2015/07/14 22:41:02 Done.
+namespace {
+const int kCldNumLangs = 3;
not at google - send to devlin 2015/07/14 18:42:58 comment; maintain existing blank line spacing
amalika 2015/07/14 22:41:02 Done.
// Errors.
static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty.";
@@ -59,4 +61,84 @@ bool I18nGetAcceptLanguagesFunction::RunSync() {
return true;
}
+ExtensionFunction::ResponseAction I18nDetectLanguageFunction::Run() {
+ scoped_ptr<api::i18n::DetectLanguage::Params> params(
+ api::i18n::DetectLanguage::Params::Create(*args_));
+
+ EXTENSION_FUNCTION_VALIDATE(params.get());
+
+ GetLanguage(params->text);
+
+ return RespondLater();
not at google - send to devlin 2015/07/14 18:42:57 This should all be: scoped_ptr<..> params(...); E
amalika 2015/07/14 22:41:02 Done.
+}
+
+void I18nDetectLanguageFunction::GetLanguage(const std::string& text) {
+ // Sets up the input String
+ const std::string utf8_text(text);
not at google - send to devlin 2015/07/14 18:42:57 const on local variable values (i.e. those that ar
mcindy 2015/07/14 22:41:21 Done.
+ const int num_utf8_bytes = static_cast<int>(utf8_text.size());
+ const char* raw_utf8_bytes = utf8_text.c_str();
not at google - send to devlin 2015/07/14 18:42:57 writing these to local variables before using them
mcindy 2015/07/14 21:38:27 We're currently modifying the CLD2:: call to accom
not at google - send to devlin 2015/07/14 21:48:33 I'd make it a cast for now and change it if/when i
mcindy 2015/07/14 22:41:21 Done.
+
+ // TODO(mcindy): improve this by providing better CLD hints
+ const char tld_hint[] = ""; // asummed no cld hints provided
+ int encoding_hint = CLD2::UNKNOWN_ENCODING;
+ CLD2::Language language_hint = CLD2::UNKNOWN_LANGUAGE;
+ CLD2::CLDHints cldhints = {NULL, tld_hint, encoding_hint, language_hint};
not at google - send to devlin 2015/07/14 18:42:58 NULL -> nullptr - and I think there is overuse of
mcindy 2015/07/14 22:41:21 Done.
+
+ const bool is_plain_text = true;
+ CLD2::Language languages[kCldNumLangs];
+ int percents[kCldNumLangs];
+ int text_bytes; // amount of non-tag/letters-only text (assumed 0)
+ bool is_reliable = false;
+ int flags = 0;
+ int valid_prefix_bytes = false;
+ double normalized_score[kCldNumLangs];
+
+ // populating languages and percents
+ CLD2::ExtDetectLanguageSummaryCheckUTF8(
Takashi Toyoshima 2015/07/14 07:28:16 Should we refer crbug.com/444258 here so that we c
mcindy 2015/07/14 22:41:21 Done.
+ raw_utf8_bytes, num_utf8_bytes, is_plain_text, &cldhints, flags,
+ languages, percents, normalized_score,
+ NULL, // assumed no ResultChunkVector is used
+ &text_bytes, &is_reliable, &valid_prefix_bytes);
+
+ LanguageDetectionResult result;
+ result.is_reliable = is_reliable;
+ InitDetectedLanguages(languages, percents, &result.languages);
+
+ SendLanguagesResult(result);
+}
+
+void I18nDetectLanguageFunction::SendLanguagesResult(
+ const LanguageDetectionResult& result) {
+ SetResult(result.ToValue());
+ SendResponse(true);
+}
+
+void I18nDetectLanguageFunction::InitDetectedLanguages(
+ CLD2::Language* languages,
+ int* percents,
+ std::vector<linked_ptr<DetectedLanguage>>* detected_languages) {
+ for (int i = 0; i < kCldNumLangs; i++) {
+ std::string language_code = "";
+
+ // Convert LanguageCode 'zh' to 'zh-CN' and 'zh-Hant' to 'zh-TW' for
+ // Translate server usage. see DetermineTextLanguage in
+ // components/translate/core/language_detection/language_detection_util.cc
+ if (languages[i] == CLD2::UNKNOWN_LANGUAGE) {
+ // no need to save in detected_languages
+ break;
+ } else if (languages[i] == CLD2::CHINESE) {
+ language_code = "zh-CN";
+ } else if (languages[i] == CLD2::CHINESE_T) {
+ language_code = "zh-TW";
+ } else {
+ language_code =
+ CLD2::LanguageCode(static_cast<CLD2::Language>(languages[i]));
+ }
+ linked_ptr<DetectedLanguage> detected_lang(new DetectedLanguage);
+ detected_lang->language = language_code;
+ detected_lang->percentage = percents[i];
+ detected_languages->push_back(detected_lang);
+ }
+}
+
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698