Componentize ShortcutsBackend
[chromium-blink-merge.git] / chrome / renderer / extensions / platform_keys_natives.cc
blob8ec0092976cc70f6a2b506bd7e1e90b77b6a20e6
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/renderer/extensions/platform_keys_natives.h"
7 #include <string>
9 #include "base/values.h"
10 #include "content/public/child/v8_value_converter.h"
11 #include "extensions/renderer/script_context.h"
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
14 #include "third_party/WebKit/public/platform/WebString.h"
15 #include "third_party/WebKit/public/platform/WebVector.h"
16 #include "third_party/WebKit/public/web/WebCryptoNormalize.h"
18 namespace extensions {
20 namespace {
22 bool StringToWebCryptoOperation(const std::string& str,
23 blink::WebCryptoOperation* op) {
24 if (str == "GenerateKey") {
25 *op = blink::WebCryptoOperationGenerateKey;
26 return true;
28 if (str == "ImportKey") {
29 *op = blink::WebCryptoOperationImportKey;
30 return true;
32 if (str == "Sign") {
33 *op = blink::WebCryptoOperationSign;
34 return true;
36 if (str == "Verify") {
37 *op = blink::WebCryptoOperationVerify;
38 return true;
40 return false;
43 scoped_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue(
44 const blink::WebCryptoAlgorithm& algorithm) {
45 DCHECK(!algorithm.isNull());
47 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
48 const blink::WebCryptoAlgorithmInfo* info =
49 blink::WebCryptoAlgorithm::lookupAlgorithmInfo(algorithm.id());
50 dict->SetStringWithoutPathExpansion("name", info->name);
52 const blink::WebCryptoAlgorithm* hash = nullptr;
54 const blink::WebCryptoRsaHashedKeyGenParams* rsaHashedKeyGen =
55 algorithm.rsaHashedKeyGenParams();
56 if (rsaHashedKeyGen) {
57 dict->SetIntegerWithoutPathExpansion("modulusLength",
58 rsaHashedKeyGen->modulusLengthBits());
59 const blink::WebVector<unsigned char>& public_exponent =
60 rsaHashedKeyGen->publicExponent();
61 dict->SetWithoutPathExpansion(
62 "publicExponent",
63 base::BinaryValue::CreateWithCopiedBuffer(
64 reinterpret_cast<const char*>(public_exponent.data()),
65 public_exponent.size()));
67 hash = &rsaHashedKeyGen->hash();
68 DCHECK(!hash->isNull());
71 const blink::WebCryptoRsaHashedImportParams* rsaHashedImport =
72 algorithm.rsaHashedImportParams();
73 if (rsaHashedImport) {
74 hash = &rsaHashedImport->hash();
75 DCHECK(!hash->isNull());
78 if (hash) {
79 const blink::WebCryptoAlgorithmInfo* hash_info =
80 blink::WebCryptoAlgorithm::lookupAlgorithmInfo(hash->id());
82 scoped_ptr<base::DictionaryValue> hash_dict(new base::DictionaryValue);
83 hash_dict->SetStringWithoutPathExpansion("name", hash_info->name);
84 dict->SetWithoutPathExpansion("hash", hash_dict.release());
86 // Otherwise, |algorithm| is missing support here or no parameters were
87 // required.
88 return dict.Pass();
91 } // namespace
93 PlatformKeysNatives::PlatformKeysNatives(ScriptContext* context)
94 : ObjectBackedNativeHandler(context) {
95 RouteFunction("NormalizeAlgorithm",
96 base::Bind(&PlatformKeysNatives::NormalizeAlgorithm,
97 base::Unretained(this)));
100 void PlatformKeysNatives::NormalizeAlgorithm(
101 const v8::FunctionCallbackInfo<v8::Value>& call_info) {
102 DCHECK_EQ(call_info.Length(), 2);
103 DCHECK(call_info[0]->IsObject());
104 DCHECK(call_info[1]->IsString());
106 blink::WebCryptoOperation operation;
107 if (!StringToWebCryptoOperation(*v8::String::Utf8Value(call_info[1]),
108 &operation)) {
109 return;
112 blink::WebString error_details;
113 int exception_code = 0;
115 blink::WebCryptoAlgorithm algorithm = blink::normalizeCryptoAlgorithm(
116 v8::Local<v8::Object>::Cast(call_info[0]), operation, &exception_code,
117 &error_details, call_info.GetIsolate());
119 scoped_ptr<base::DictionaryValue> algorithm_dict;
120 if (!algorithm.isNull())
121 algorithm_dict = WebCryptoAlgorithmToBaseValue(algorithm);
123 if (!algorithm_dict)
124 return;
126 scoped_ptr<content::V8ValueConverter> converter(
127 content::V8ValueConverter::create());
128 call_info.GetReturnValue().Set(
129 converter->ToV8Value(algorithm_dict.get(), context()->v8_context()));
132 } // namespace extensions