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

Side by Side 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 unified diff | Download patch
OLDNEW
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"
17 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
18 15
19 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages;
20
21 namespace extensions { 16 namespace extensions {
22 17
18 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages;
19 // 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.
20 typedef struct extensions::api::i18n::DetectLanguage::Results::Result::
21 LanguagesType DetectedLanguage;
22 typedef struct extensions::api::i18n::DetectLanguage::Results::Result
23 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.
24
23 namespace { 25 namespace {
24 26 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.
25 // Errors. 27 // Errors.
26 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; 28 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty.";
27 29
28 } 30 }
29 31
30 bool I18nGetAcceptLanguagesFunction::RunSync() { 32 bool I18nGetAcceptLanguagesFunction::RunSync() {
31 std::string accept_languages = 33 std::string accept_languages =
32 GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); 34 GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
33 // Currently, there are 2 ways to set browser's accept-languages: through UI 35 // Currently, there are 2 ways to set browser's accept-languages: through UI
34 // or directly modify the preference file. The accept-languages set through 36 // or directly modify the preference file. The accept-languages set through
(...skipping 17 matching lines...) Expand all
52 54
53 if (languages.empty()) { 55 if (languages.empty()) {
54 error_ = kEmptyAcceptLanguagesError; 56 error_ = kEmptyAcceptLanguagesError;
55 return false; 57 return false;
56 } 58 }
57 59
58 results_ = GetAcceptLanguages::Results::Create(languages); 60 results_ = GetAcceptLanguages::Results::Create(languages);
59 return true; 61 return true;
60 } 62 }
61 63
64 ExtensionFunction::ResponseAction I18nDetectLanguageFunction::Run() {
65 scoped_ptr<api::i18n::DetectLanguage::Params> params(
66 api::i18n::DetectLanguage::Params::Create(*args_));
67
68 EXTENSION_FUNCTION_VALIDATE(params.get());
69
70 GetLanguage(params->text);
71
72 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.
73 }
74
75 void I18nDetectLanguageFunction::GetLanguage(const std::string& text) {
76 // Sets up the input String
77 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.
78 const int num_utf8_bytes = static_cast<int>(utf8_text.size());
79 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.
80
81 // TODO(mcindy): improve this by providing better CLD hints
82 const char tld_hint[] = ""; // asummed no cld hints provided
83 int encoding_hint = CLD2::UNKNOWN_ENCODING;
84 CLD2::Language language_hint = CLD2::UNKNOWN_LANGUAGE;
85 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.
86
87 const bool is_plain_text = true;
88 CLD2::Language languages[kCldNumLangs];
89 int percents[kCldNumLangs];
90 int text_bytes; // amount of non-tag/letters-only text (assumed 0)
91 bool is_reliable = false;
92 int flags = 0;
93 int valid_prefix_bytes = false;
94 double normalized_score[kCldNumLangs];
95
96 // populating languages and percents
97 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.
98 raw_utf8_bytes, num_utf8_bytes, is_plain_text, &cldhints, flags,
99 languages, percents, normalized_score,
100 NULL, // assumed no ResultChunkVector is used
101 &text_bytes, &is_reliable, &valid_prefix_bytes);
102
103 LanguageDetectionResult result;
104 result.is_reliable = is_reliable;
105 InitDetectedLanguages(languages, percents, &result.languages);
106
107 SendLanguagesResult(result);
108 }
109
110 void I18nDetectLanguageFunction::SendLanguagesResult(
111 const LanguageDetectionResult& result) {
112 SetResult(result.ToValue());
113 SendResponse(true);
114 }
115
116 void I18nDetectLanguageFunction::InitDetectedLanguages(
117 CLD2::Language* languages,
118 int* percents,
119 std::vector<linked_ptr<DetectedLanguage>>* detected_languages) {
120 for (int i = 0; i < kCldNumLangs; i++) {
121 std::string language_code = "";
122
123 // Convert LanguageCode 'zh' to 'zh-CN' and 'zh-Hant' to 'zh-TW' for
124 // Translate server usage. see DetermineTextLanguage in
125 // components/translate/core/language_detection/language_detection_util.cc
126 if (languages[i] == CLD2::UNKNOWN_LANGUAGE) {
127 // no need to save in detected_languages
128 break;
129 } else if (languages[i] == CLD2::CHINESE) {
130 language_code = "zh-CN";
131 } else if (languages[i] == CLD2::CHINESE_T) {
132 language_code = "zh-TW";
133 } else {
134 language_code =
135 CLD2::LanguageCode(static_cast<CLD2::Language>(languages[i]));
136 }
137 linked_ptr<DetectedLanguage> detected_lang(new DetectedLanguage);
138 detected_lang->language = language_code;
139 detected_lang->percentage = percents[i];
140 detected_languages->push_back(detected_lang);
141 }
142 }
143
62 } // namespace extensions 144 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698