Commit e9b08d07 authored by Jeremy Roman's avatar Jeremy Roman Committed by Commit Bot

extensions: Use gin::DataObjectBuilder in PlatformKeysNatives.

This is more direct and avoids use of the legacy base::Value API, since
nothing else is done with this base::Value anyway.

Bug: 646113
Change-Id: I335ad2c355734cd8fff45be4996383f39f5bf678
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2233923
Auto-Submit: Jeremy Roman <jbroman@chromium.org>
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#776339}
parent 91551cde
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/values.h" #include "base/values.h"
#include "content/public/renderer/v8_value_converter.h"
#include "extensions/renderer/script_context.h" #include "extensions/renderer/script_context.h"
#include "gin/data_object_builder.h"
#include "third_party/blink/public/platform/web_crypto_algorithm.h" #include "third_party/blink/public/platform/web_crypto_algorithm.h"
#include "third_party/blink/public/platform/web_crypto_algorithm_params.h" #include "third_party/blink/public/platform/web_crypto_algorithm_params.h"
#include "third_party/blink/public/platform/web_string.h" #include "third_party/blink/public/platform/web_string.h"
...@@ -43,14 +43,17 @@ bool StringToWebCryptoOperation(const std::string& str, ...@@ -43,14 +43,17 @@ bool StringToWebCryptoOperation(const std::string& str,
return false; return false;
} }
std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue( v8::Local<v8::Object> WebCryptoAlgorithmToV8Value(
const blink::WebCryptoAlgorithm& algorithm) { const blink::WebCryptoAlgorithm& algorithm,
v8::Local<v8::Context> context) {
DCHECK(!algorithm.IsNull()); DCHECK(!algorithm.IsNull());
v8::Context::Scope scope(context);
v8::Isolate* isolate = context->GetIsolate();
std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
const blink::WebCryptoAlgorithmInfo* info = const blink::WebCryptoAlgorithmInfo* info =
blink::WebCryptoAlgorithm::LookupAlgorithmInfo(algorithm.Id()); blink::WebCryptoAlgorithm::LookupAlgorithmInfo(algorithm.Id());
dict->SetKey("name", base::Value(info->name)); gin::DataObjectBuilder builder(isolate);
builder.Set("name", base::StringPiece(info->name));
const blink::WebCryptoAlgorithm* hash = nullptr; const blink::WebCryptoAlgorithm* hash = nullptr;
...@@ -59,16 +62,15 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue( ...@@ -59,16 +62,15 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue(
const blink::WebCryptoRsaHashedKeyGenParams* rsa_hashed_key_gen = const blink::WebCryptoRsaHashedKeyGenParams* rsa_hashed_key_gen =
algorithm.RsaHashedKeyGenParams(); algorithm.RsaHashedKeyGenParams();
if (rsa_hashed_key_gen) { if (rsa_hashed_key_gen) {
dict->SetKey("modulusLength", builder.Set("modulusLength", rsa_hashed_key_gen->ModulusLengthBits());
base::Value(static_cast<int>(
rsa_hashed_key_gen->ModulusLengthBits())));
const blink::WebVector<unsigned char>& public_exponent = const blink::WebVector<unsigned char>& public_exponent =
rsa_hashed_key_gen->PublicExponent(); rsa_hashed_key_gen->PublicExponent();
dict->SetWithoutPathExpansion( v8::Local<v8::ArrayBuffer> buffer =
"publicExponent", v8::ArrayBuffer::New(isolate, public_exponent.size());
base::Value::CreateWithCopiedBuffer( memcpy(buffer->GetContents().Data(), public_exponent.Data(),
reinterpret_cast<const char*>(public_exponent.Data()), public_exponent.size());
public_exponent.size())); builder.Set("publicExponent", buffer);
hash = &rsa_hashed_key_gen->GetHash(); hash = &rsa_hashed_key_gen->GetHash();
DCHECK(!hash->IsNull()); DCHECK(!hash->IsNull());
...@@ -85,7 +87,7 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue( ...@@ -85,7 +87,7 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue(
const blink::WebCryptoEcKeyGenParams* ec_key_gen = const blink::WebCryptoEcKeyGenParams* ec_key_gen =
algorithm.EcKeyGenParams(); algorithm.EcKeyGenParams();
if (ec_key_gen) { if (ec_key_gen) {
std::string named_curve; base::StringPiece named_curve;
switch (ec_key_gen->NamedCurve()) { switch (ec_key_gen->NamedCurve()) {
case blink::kWebCryptoNamedCurveP256: case blink::kWebCryptoNamedCurveP256:
named_curve = "P-256"; named_curve = "P-256";
...@@ -98,7 +100,7 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue( ...@@ -98,7 +100,7 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue(
break; break;
} }
DCHECK(!named_curve.empty()); DCHECK(!named_curve.empty());
dict->SetKey("namedCurve", base::Value(std::move(named_curve))); builder.Set("namedCurve", named_curve);
} }
const blink::WebCryptoEcdsaParams* ecdsa = algorithm.EcdsaParams(); const blink::WebCryptoEcdsaParams* ecdsa = algorithm.EcdsaParams();
...@@ -117,13 +119,13 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue( ...@@ -117,13 +119,13 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue(
const blink::WebCryptoAlgorithmInfo* hash_info = const blink::WebCryptoAlgorithmInfo* hash_info =
blink::WebCryptoAlgorithm::LookupAlgorithmInfo(hash->Id()); blink::WebCryptoAlgorithm::LookupAlgorithmInfo(hash->Id());
std::unique_ptr<base::DictionaryValue> hash_dict(new base::DictionaryValue); builder.Set("hash", gin::DataObjectBuilder(isolate)
hash_dict->SetKey("name", base::Value(hash_info->name)); .Set("name", base::StringPiece(hash_info->name))
dict->SetWithoutPathExpansion("hash", std::move(hash_dict)); .Build());
} }
// Otherwise, |algorithm| is missing support here or no parameters were // Otherwise, |algorithm| is missing support here or no parameters were
// required. // required.
return dict; return builder.Build();
} }
} // namespace } // namespace
...@@ -158,15 +160,11 @@ void PlatformKeysNatives::NormalizeAlgorithm( ...@@ -158,15 +160,11 @@ void PlatformKeysNatives::NormalizeAlgorithm(
v8::Local<v8::Object>::Cast(call_info[0]), operation, &exception_code, v8::Local<v8::Object>::Cast(call_info[0]), operation, &exception_code,
&error_details, call_info.GetIsolate()); &error_details, call_info.GetIsolate());
std::unique_ptr<base::DictionaryValue> algorithm_dict; if (algorithm.IsNull())
if (!algorithm.IsNull())
algorithm_dict = WebCryptoAlgorithmToBaseValue(algorithm);
if (!algorithm_dict)
return; return;
call_info.GetReturnValue().Set(content::V8ValueConverter::Create()->ToV8Value( call_info.GetReturnValue().Set(
algorithm_dict.get(), context()->v8_context())); WebCryptoAlgorithmToV8Value(algorithm, 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