diff options
author | rdevlin.cronin <rdevlin.cronin@chromium.org> | 2016-03-11 09:02:17 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-11 17:03:41 +0000 |
commit | 2bda7ad3889d539b27e514226d696b6b024e2ac5 (patch) | |
tree | 79e10098acf9dacbe1fc0b142d3b30838a7f58aa /extensions/renderer | |
parent | db433eabe1d4854125e00885fbba073ada75ad94 (diff) | |
download | chromium_src-2bda7ad3889d539b27e514226d696b6b024e2ac5.zip chromium_src-2bda7ad3889d539b27e514226d696b6b024e2ac5.tar.gz chromium_src-2bda7ad3889d539b27e514226d696b6b024e2ac5.tar.bz2 |
[Extensions] Use V8ValueConverter in i18n custom bindings
Instead of manually constructing a v8 object for i18n results, use the
V8 value converter to convert a base::Value. This helps us
a) Have fewer places that rely on v8_helpers::SetProperty, which should be
phased out for SetPrivateProperty.
b) Simplify the conversion so that if v8 apis change, we have to update fewer
places.
BUG=591164
Review URL: https://codereview.chromium.org/1782673006
Cr-Commit-Position: refs/heads/master@{#380655}
Diffstat (limited to 'extensions/renderer')
-rw-r--r-- | extensions/renderer/i18n_custom_bindings.cc | 45 | ||||
-rw-r--r-- | extensions/renderer/v8_helpers.h | 17 |
2 files changed, 17 insertions, 45 deletions
diff --git a/extensions/renderer/i18n_custom_bindings.cc b/extensions/renderer/i18n_custom_bindings.cc index 365f50a..08d9e75aa 100644 --- a/extensions/renderer/i18n_custom_bindings.cc +++ b/extensions/renderer/i18n_custom_bindings.cc @@ -11,6 +11,7 @@ #include "base/bind.h" #include "base/macros.h" +#include "content/public/child/v8_value_converter.h" #include "content/public/renderer/render_frame.h" #include "content/public/renderer/render_thread.h" #include "extensions/common/extension_messages.h" @@ -36,7 +37,7 @@ struct DetectedLanguage { // Returns a new v8::Local<v8::Value> representing the serialized form of // this DetectedLanguage object. - v8::Local<v8::Value> ToValue(ScriptContext* context); + scoped_ptr<base::DictionaryValue> ToDictionary() const; std::string language; int percentage; @@ -67,40 +68,28 @@ struct LanguageDetectionResult { DISALLOW_COPY_AND_ASSIGN(LanguageDetectionResult); }; -v8::Local<v8::Value> DetectedLanguage::ToValue(ScriptContext* context) { - v8::Local<v8::Context> v8_context = context->v8_context(); - v8::Isolate* isolate = v8_context->GetIsolate(); - v8::EscapableHandleScope handle_scope(isolate); - - v8::Local<v8::Object> value(v8::Object::New(isolate)); - SetProperty(v8_context, value, ToV8StringUnsafe(isolate, "language"), - ToV8StringUnsafe(isolate, language.c_str())); - - SetProperty(v8_context, value, ToV8StringUnsafe(isolate, "percentage"), - v8::Number::New(isolate, percentage)); - - v8::Local<v8::Value> result = v8::Local<v8::Value>::Cast(value); - return handle_scope.Escape(result); +scoped_ptr<base::DictionaryValue> DetectedLanguage::ToDictionary() const { + scoped_ptr<base::DictionaryValue> dict_value(new base::DictionaryValue()); + dict_value->SetString("language", language.c_str()); + dict_value->SetInteger("percentage", percentage); + return dict_value; } v8::Local<v8::Value> LanguageDetectionResult::ToValue(ScriptContext* context) { + base::DictionaryValue dict_value; + dict_value.SetBoolean("isReliable", is_reliable); + scoped_ptr<base::ListValue> languages_list(new base::ListValue()); + for (const auto& language : languages) + languages_list->Append(language->ToDictionary()); + dict_value.Set("languages", std::move(languages_list)); + v8::Local<v8::Context> v8_context = context->v8_context(); v8::Isolate* isolate = v8_context->GetIsolate(); v8::EscapableHandleScope handle_scope(isolate); - v8::Local<v8::Object> value(v8::Object::New(isolate)); - SetProperty(v8_context, value, ToV8StringUnsafe(isolate, "isReliable"), - v8::Boolean::New(isolate, is_reliable)); - - v8::Local<v8::Array> langs(v8::Array::New(isolate, languages.size())); - for (size_t i = 0; i < languages.size(); ++i) { - SetProperty(v8_context, langs, i, languages[i]->ToValue(context)); - } - - SetProperty(isolate->GetCurrentContext(), value, - ToV8StringUnsafe(isolate, "languages"), langs); - - v8::Local<v8::Value> result = v8::Local<v8::Value>::Cast(value); + scoped_ptr<content::V8ValueConverter> converter( + content::V8ValueConverter::create()); + v8::Local<v8::Value> result = converter->ToV8Value(&dict_value, v8_context); return handle_scope.Escape(result); } diff --git a/extensions/renderer/v8_helpers.h b/extensions/renderer/v8_helpers.h index 560beee..b0dd17a 100644 --- a/extensions/renderer/v8_helpers.h +++ b/extensions/renderer/v8_helpers.h @@ -71,23 +71,6 @@ inline bool SetProperty(v8::Local<v8::Context> context, return IsTrue(object->DefineOwnProperty(context, key, value)); } -inline bool SetProperty(v8::Local<v8::Context> context, - v8::Local<v8::Object> object, - const char* key, - v8::Local<v8::Value> value) { - v8::Local<v8::String> v8_key; - if (!ToV8String(context->GetIsolate(), key, &v8_key)) - return false; - return SetProperty(context, object, v8_key, value); -} - -inline bool SetProperty(v8::Local<v8::Context> context, - v8::Local<v8::Object> object, - uint32_t index, - v8::Local<v8::Value> value) { - return SetProperty(context, object, base::UintToString(index).c_str(), value); -} - // Wraps v8::Object::SetPrivate(). When possible, prefer this to SetProperty(). inline bool SetPrivateProperty(v8::Local<v8::Context> context, v8::Local<v8::Object> object, |