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 @@
#include "base/bind.h"
#include "base/values.h"
#include "content/public/renderer/v8_value_converter.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_params.h"
#include "third_party/blink/public/platform/web_string.h"
......@@ -43,14 +43,17 @@ bool StringToWebCryptoOperation(const std::string& str,
return false;
}
std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue(
const blink::WebCryptoAlgorithm& algorithm) {
v8::Local<v8::Object> WebCryptoAlgorithmToV8Value(
const blink::WebCryptoAlgorithm& algorithm,
v8::Local<v8::Context> context) {
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 =
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;
......@@ -59,16 +62,15 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue(
const blink::WebCryptoRsaHashedKeyGenParams* rsa_hashed_key_gen =
algorithm.RsaHashedKeyGenParams();
if (rsa_hashed_key_gen) {
dict->SetKey("modulusLength",
base::Value(static_cast<int>(
rsa_hashed_key_gen->ModulusLengthBits())));
builder.Set("modulusLength", rsa_hashed_key_gen->ModulusLengthBits());
const blink::WebVector<unsigned char>& public_exponent =
rsa_hashed_key_gen->PublicExponent();
dict->SetWithoutPathExpansion(
"publicExponent",
base::Value::CreateWithCopiedBuffer(
reinterpret_cast<const char*>(public_exponent.Data()),
public_exponent.size()));
v8::Local<v8::ArrayBuffer> buffer =
v8::ArrayBuffer::New(isolate, public_exponent.size());
memcpy(buffer->GetContents().Data(), public_exponent.Data(),
public_exponent.size());
builder.Set("publicExponent", buffer);
hash = &rsa_hashed_key_gen->GetHash();
DCHECK(!hash->IsNull());
......@@ -85,7 +87,7 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue(
const blink::WebCryptoEcKeyGenParams* ec_key_gen =
algorithm.EcKeyGenParams();
if (ec_key_gen) {
std::string named_curve;
base::StringPiece named_curve;
switch (ec_key_gen->NamedCurve()) {
case blink::kWebCryptoNamedCurveP256:
named_curve = "P-256";
......@@ -98,7 +100,7 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue(
break;
}
DCHECK(!named_curve.empty());
dict->SetKey("namedCurve", base::Value(std::move(named_curve)));
builder.Set("namedCurve", named_curve);
}
const blink::WebCryptoEcdsaParams* ecdsa = algorithm.EcdsaParams();
......@@ -117,13 +119,13 @@ std::unique_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue(
const blink::WebCryptoAlgorithmInfo* hash_info =
blink::WebCryptoAlgorithm::LookupAlgorithmInfo(hash->Id());
std::unique_ptr<base::DictionaryValue> hash_dict(new base::DictionaryValue);
hash_dict->SetKey("name", base::Value(hash_info->name));
dict->SetWithoutPathExpansion("hash", std::move(hash_dict));
builder.Set("hash", gin::DataObjectBuilder(isolate)
.Set("name", base::StringPiece(hash_info->name))
.Build());
}
// Otherwise, |algorithm| is missing support here or no parameters were
// required.
return dict;
return builder.Build();
}
} // namespace
......@@ -158,15 +160,11 @@ void PlatformKeysNatives::NormalizeAlgorithm(
v8::Local<v8::Object>::Cast(call_info[0]), operation, &exception_code,
&error_details, call_info.GetIsolate());
std::unique_ptr<base::DictionaryValue> algorithm_dict;
if (!algorithm.IsNull())
algorithm_dict = WebCryptoAlgorithmToBaseValue(algorithm);
if (!algorithm_dict)
if (algorithm.IsNull())
return;
call_info.GetReturnValue().Set(content::V8ValueConverter::Create()->ToV8Value(
algorithm_dict.get(), context()->v8_context()));
call_info.GetReturnValue().Set(
WebCryptoAlgorithmToV8Value(algorithm, context()->v8_context()));
}
} // 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