Commit 39745234 authored by Devlin Cronin's avatar Devlin Cronin Committed by Commit Bot

[Extensions Bindings] Clean up i18n custom bindings value construction

The i18n custom bindings create v8 values from results for language
detection, but do so in an inefficient way (constructing a base::Value
and then converting to v8::Value). Update this to use
gin::DataObjectBuilder to construct the v8 value directly, and, while
we're at it, use some C++11 awesomeness in movable types.

Bug: None
Change-Id: I80597627b0b3803e50ecbe34009682161d178c7d
Reviewed-on: https://chromium-review.googlesource.com/892202Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532968}
parent fd506b0a
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "extensions/common/message_bundle.h" #include "extensions/common/message_bundle.h"
#include "extensions/renderer/script_context.h" #include "extensions/renderer/script_context.h"
#include "extensions/renderer/v8_helpers.h" #include "extensions/renderer/v8_helpers.h"
#include "gin/data_object_builder.h"
#include "third_party/cld_3/src/src/nnet_language_identifier.h" #include "third_party/cld_3/src/src/nnet_language_identifier.h"
namespace extensions { namespace extensions {
...@@ -39,17 +40,13 @@ const int kCld3MinimumByteThreshold = 50; ...@@ -39,17 +40,13 @@ const int kCld3MinimumByteThreshold = 50;
struct DetectedLanguage { struct DetectedLanguage {
DetectedLanguage(const std::string& language, int percentage) DetectedLanguage(const std::string& language, int percentage)
: language(language), percentage(percentage) {} : language(language), percentage(percentage) {}
~DetectedLanguage() {}
// Returns a new v8::Local<v8::Value> representing the serialized form of // Returns a new v8::Local<v8::Value> representing the serialized form of
// this DetectedLanguage object. // this DetectedLanguage object.
std::unique_ptr<base::DictionaryValue> ToDictionary() const; v8::Local<v8::Value> ToV8(v8::Isolate* isolate) const;
std::string language; std::string language;
int percentage; int percentage;
private:
DISALLOW_COPY_AND_ASSIGN(DetectedLanguage);
}; };
// LanguageDetectionResult object that holds detected langugae reliability and // LanguageDetectionResult object that holds detected langugae reliability and
...@@ -62,50 +59,49 @@ struct LanguageDetectionResult { ...@@ -62,50 +59,49 @@ struct LanguageDetectionResult {
// Returns a new v8::Local<v8::Value> representing the serialized form of // Returns a new v8::Local<v8::Value> representing the serialized form of
// this Result object. // this Result object.
v8::Local<v8::Value> ToValue(ScriptContext* context); v8::Local<v8::Value> ToV8(v8::Local<v8::Context> context) const;
// CLD detected language reliability // CLD detected language reliability
bool is_reliable; bool is_reliable;
// Array of detectedLanguage of size 1-3. The null is returned if // Array of detectedLanguage of size 1-3. The null is returned if
// there were no languages detected // there were no languages detected
std::vector<std::unique_ptr<DetectedLanguage>> languages; std::vector<DetectedLanguage> languages;
private: private:
DISALLOW_COPY_AND_ASSIGN(LanguageDetectionResult); DISALLOW_COPY_AND_ASSIGN(LanguageDetectionResult);
}; };
std::unique_ptr<base::DictionaryValue> DetectedLanguage::ToDictionary() const { v8::Local<v8::Value> DetectedLanguage::ToV8(v8::Isolate* isolate) const {
std::unique_ptr<base::DictionaryValue> dict_value( return gin::DataObjectBuilder(isolate)
new base::DictionaryValue()); .Set("language", language)
dict_value->SetString("language", language.c_str()); .Set("percentage", percentage)
dict_value->SetInteger("percentage", percentage); .Build();
return dict_value;
} }
v8::Local<v8::Value> LanguageDetectionResult::ToValue(ScriptContext* context) { v8::Local<v8::Value> LanguageDetectionResult::ToV8(
base::DictionaryValue dict_value; v8::Local<v8::Context> context) const {
dict_value.SetBoolean("isReliable", is_reliable); v8::Isolate* isolate = context->GetIsolate();
std::unique_ptr<base::ListValue> languages_list(new base::ListValue()); DCHECK(isolate->GetCurrentContext() == context);
for (const auto& language : languages)
languages_list->Append(language->ToDictionary()); v8::Local<v8::Array> v8_languages = v8::Array::New(isolate, languages.size());
dict_value.Set("languages", std::move(languages_list)); for (uint32_t i = 0; i < languages.size(); ++i) {
bool success =
v8::Local<v8::Context> v8_context = context->v8_context(); v8_languages->CreateDataProperty(context, i, languages[i].ToV8(isolate))
v8::Isolate* isolate = v8_context->GetIsolate(); .ToChecked();
v8::EscapableHandleScope handle_scope(isolate); DCHECK(success) << "CreateDataProperty() should never fail.";
}
v8::Local<v8::Value> result = return gin::DataObjectBuilder(isolate)
content::V8ValueConverter::Create()->ToV8Value(&dict_value, v8_context); .Set("isReliable", is_reliable)
return handle_scope.Escape(result); .Set("languages", v8_languages.As<v8::Value>())
.Build();
} }
void InitDetectedLanguages( void InitDetectedLanguages(
const std::vector<chrome_lang_id::NNetLanguageIdentifier::Result>& const std::vector<chrome_lang_id::NNetLanguageIdentifier::Result>&
lang_results, lang_results,
LanguageDetectionResult* result) { LanguageDetectionResult* result) {
std::vector<std::unique_ptr<DetectedLanguage>>* detected_languages = std::vector<DetectedLanguage>* detected_languages = &result->languages;
&result->languages;
DCHECK(detected_languages->empty()); DCHECK(detected_languages->empty());
bool* is_reliable = &result->is_reliable; bool* is_reliable = &result->is_reliable;
...@@ -136,13 +132,11 @@ void InitDetectedLanguages( ...@@ -136,13 +132,11 @@ void InitDetectedLanguages(
*is_reliable = *is_reliable && lang_result.is_reliable; *is_reliable = *is_reliable && lang_result.is_reliable;
const int percent = static_cast<int>(100 * lang_result.proportion); const int percent = static_cast<int>(100 * lang_result.proportion);
detected_languages->push_back( detected_languages->emplace_back(language_code, percent);
std::make_unique<DetectedLanguage>(language_code, percent));
} }
if (detected_languages->empty()) { if (detected_languages->empty())
*is_reliable = false; *is_reliable = false;
}
} }
} // namespace } // namespace
...@@ -256,7 +250,7 @@ void I18NCustomBindings::DetectTextLanguage( ...@@ -256,7 +250,7 @@ void I18NCustomBindings::DetectTextLanguage(
// Populate LanguageDetectionResult with prediction reliability, languages, // Populate LanguageDetectionResult with prediction reliability, languages,
// and the corresponding percentages. // and the corresponding percentages.
InitDetectedLanguages(lang_results, &result); InitDetectedLanguages(lang_results, &result);
args.GetReturnValue().Set(result.ToValue(context())); args.GetReturnValue().Set(result.ToV8(context()->v8_context()));
} }
} // namespace extensions } // namespace extensions
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment