OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/renderer/extensions/enterprise_platform_keys_natives.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/values.h" | |
10 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
11 #include "content/public/renderer/v8_value_converter.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/web/WebCryptoNormalize.h" | |
16 | |
17 namespace extensions { | |
18 | |
19 namespace { | |
20 | |
21 bool StringToWebCryptoOperation(const std::string& str, | |
22 blink::WebCryptoOperation* op) { | |
23 if (str == "GenerateKey") { | |
24 *op = blink::WebCryptoOperationGenerateKey; | |
25 return true; | |
26 } | |
27 if (str == "Sign") { | |
28 *op = blink::WebCryptoOperationSign; | |
29 return true; | |
30 } | |
31 if (str == "Verify") { | |
32 *op = blink::WebCryptoOperationVerify; | |
33 return true; | |
34 } | |
35 return false; | |
36 } | |
37 | |
38 scoped_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue( | |
39 const blink::WebCryptoAlgorithm& algorithm) { | |
40 DCHECK(!algorithm.isNull()); | |
41 | |
42 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | |
43 dict->SetStringWithoutPathExpansion( | |
44 "name", blink::WebCryptoAlgorithm::idToName(algorithm.id())); | |
45 const blink::WebCryptoRsaHashedKeyGenParams* rsaHashedKeyGen = | |
46 algorithm.rsaHashedKeyGenParams(); | |
47 if (rsaHashedKeyGen) { | |
48 dict->SetIntegerWithoutPathExpansion("modulusLength", | |
49 rsaHashedKeyGen->modulusLengthBits()); | |
50 return dict.Pass(); | |
not at google - send to devlin
2014/06/10 18:00:44
nit: looks like you return dict.Pass() either way,
pneubeck (no reviews)
2014/06/11 12:49:09
Done.
| |
51 } | |
52 // Otherwise, |algorithm| is missing support here or no parameters were | |
53 // required. | |
54 return dict.Pass(); | |
55 } | |
56 | |
57 } // namespace | |
58 | |
59 EnterprisePlatformKeysNatives::EnterprisePlatformKeysNatives( | |
60 ScriptContext* context) | |
61 : ObjectBackedNativeHandler(context) { | |
62 RouteFunction("NormalizeAlgorithm", | |
63 base::Bind(&EnterprisePlatformKeysNatives::NormalizeAlgorithm, | |
64 base::Unretained(this))); | |
65 } | |
66 | |
67 void EnterprisePlatformKeysNatives::NormalizeAlgorithm( | |
68 const v8::FunctionCallbackInfo<v8::Value>& call_info) { | |
69 DCHECK_EQ(call_info.Length(), 2); | |
70 DCHECK(call_info[0]->IsObject()); | |
71 DCHECK(call_info[1]->IsString()); | |
72 | |
73 blink::WebCryptoOperation operation; | |
74 if (!StringToWebCryptoOperation(*v8::String::Utf8Value(call_info[1]), | |
75 &operation)) { | |
76 return; | |
77 } | |
78 | |
79 blink::WebString error_details; | |
80 int exception_code = 0; | |
81 | |
82 blink::WebCryptoAlgorithm algorithm = | |
83 blink::normalizeCryptoAlgorithm(call_info[0]->ToObject(), | |
84 operation, | |
85 &exception_code, | |
86 &error_details, | |
87 call_info.GetIsolate()); | |
88 | |
89 scoped_ptr<base::DictionaryValue> algorithm_dict; | |
90 if (!algorithm.isNull()) | |
91 algorithm_dict = WebCryptoAlgorithmToBaseValue(algorithm); | |
92 | |
93 if (!algorithm_dict) | |
94 return; | |
95 | |
96 scoped_ptr<content::V8ValueConverter> converter( | |
97 content::V8ValueConverter::create()); | |
98 call_info.GetReturnValue().Set( | |
99 converter->ToV8Value(algorithm_dict.get(), context()->v8_context())); | |
100 } | |
101 | |
102 } // namespace extensions | |
OLD | NEW |