Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / modules / v8 / custom / V8SubtleCryptoCustom.cpp
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 "config.h"
6 #include "bindings/modules/v8/V8SubtleCrypto.h"
7
8 #include "bindings/core/v8/Dictionary.h"
9 #include "bindings/core/v8/V8ArrayBuffer.h"
10 #include "bindings/core/v8/V8ArrayBufferView.h"
11 #include "bindings/modules/v8/V8CryptoKey.h"
12
13 namespace blink {
14
15 ////////////////////////////////////////////////////////////////////////////////
16 // Overload resolution for verify()
17 // FIXME: needs support for union types http://crbug.com/240176
18 ////////////////////////////////////////////////////////////////////////////////
19
20 // Promise verify(Dictionary algorithm, CryptoKey key, ArrayBuffer signature, ArrayBuffer data);
21 void verify1Method(const v8::FunctionCallbackInfo<v8::Value>& info)
22 {
23     SubtleCrypto* impl = V8SubtleCrypto::toImpl(info.Holder());
24     TONATIVE_VOID(Dictionary, algorithm, Dictionary(info[0], info.GetIsolate()));
25     if (!algorithm.isUndefinedOrNull() && !algorithm.isObject()) {
26         V8ThrowException::throwTypeError(info.GetIsolate(), ExceptionMessages::failedToExecute("verify", "SubtleCrypto", "parameter 1 ('algorithm') is not an object."));
27         return;
28     }
29     TONATIVE_VOID(CryptoKey*, key, V8CryptoKey::toImplWithTypeCheck(info.GetIsolate(), info[1]));
30     TONATIVE_VOID(DOMArrayBuffer*, signature, info[2]->IsArrayBuffer() ? V8ArrayBuffer::toImpl(v8::Local<v8::ArrayBuffer>::Cast(info[2])) : 0);
31     TONATIVE_VOID(DOMArrayBuffer*, data, info[3]->IsArrayBuffer() ? V8ArrayBuffer::toImpl(v8::Local<v8::ArrayBuffer>::Cast(info[3])) : 0);
32     v8SetReturnValue(info, impl->verifySignature(ScriptState::current(info.GetIsolate()), algorithm, key, signature, data).v8Value());
33 }
34
35 // Promise verify(Dictionary algorithm, CryptoKey key, ArrayBuffer signature, ArrayBufferView data);
36 void verify2Method(const v8::FunctionCallbackInfo<v8::Value>& info)
37 {
38     SubtleCrypto* impl = V8SubtleCrypto::toImpl(info.Holder());
39     TONATIVE_VOID(Dictionary, algorithm, Dictionary(info[0], info.GetIsolate()));
40     if (!algorithm.isUndefinedOrNull() && !algorithm.isObject()) {
41         V8ThrowException::throwTypeError(info.GetIsolate(), ExceptionMessages::failedToExecute("verify", "SubtleCrypto", "parameter 1 ('algorithm') is not an object."));
42         return;
43     }
44     TONATIVE_VOID(CryptoKey*, key, V8CryptoKey::toImplWithTypeCheck(info.GetIsolate(), info[1]));
45     TONATIVE_VOID(DOMArrayBuffer*, signature, info[2]->IsArrayBuffer() ? V8ArrayBuffer::toImpl(v8::Local<v8::ArrayBuffer>::Cast(info[2])) : 0);
46     TONATIVE_VOID(DOMArrayBufferView*, data, info[3]->IsArrayBufferView() ? V8ArrayBufferView::toImpl(v8::Local<v8::ArrayBufferView>::Cast(info[3])) : 0);
47     v8SetReturnValue(info, impl->verifySignature(ScriptState::current(info.GetIsolate()), algorithm, key, signature, data).v8Value());
48 }
49
50 // Promise verify(Dictionary algorithm, CryptoKey key, ArrayBufferView signature, ArrayBuffer data);
51 void verify3Method(const v8::FunctionCallbackInfo<v8::Value>& info)
52 {
53     SubtleCrypto* impl = V8SubtleCrypto::toImpl(info.Holder());
54     TONATIVE_VOID(Dictionary, algorithm, Dictionary(info[0], info.GetIsolate()));
55     if (!algorithm.isUndefinedOrNull() && !algorithm.isObject()) {
56         V8ThrowException::throwTypeError(info.GetIsolate(), ExceptionMessages::failedToExecute("verify", "SubtleCrypto", "parameter 1 ('algorithm') is not an object."));
57         return;
58     }
59     TONATIVE_VOID(CryptoKey*, key, V8CryptoKey::toImplWithTypeCheck(info.GetIsolate(), info[1]));
60     TONATIVE_VOID(DOMArrayBufferView*, signature, info[2]->IsArrayBufferView() ? V8ArrayBufferView::toImpl(v8::Local<v8::ArrayBufferView>::Cast(info[2])) : 0);
61     TONATIVE_VOID(DOMArrayBuffer*, data, info[3]->IsArrayBuffer() ? V8ArrayBuffer::toImpl(v8::Local<v8::ArrayBuffer>::Cast(info[3])) : 0);
62     v8SetReturnValue(info, impl->verifySignature(ScriptState::current(info.GetIsolate()), algorithm, key, signature, data).v8Value());
63 }
64
65 // Promise verify(Dictionary algorithm, CryptoKey key, ArrayBufferView signature, ArrayBufferView data);
66 void verify4Method(const v8::FunctionCallbackInfo<v8::Value>& info)
67 {
68     SubtleCrypto* impl = V8SubtleCrypto::toImpl(info.Holder());
69     TONATIVE_VOID(Dictionary, algorithm, Dictionary(info[0], info.GetIsolate()));
70     if (!algorithm.isUndefinedOrNull() && !algorithm.isObject()) {
71         V8ThrowException::throwTypeError(info.GetIsolate(), ExceptionMessages::failedToExecute("verify", "SubtleCrypto", "parameter 1 ('algorithm') is not an object."));
72         return;
73     }
74     TONATIVE_VOID(CryptoKey*, key, V8CryptoKey::toImplWithTypeCheck(info.GetIsolate(), info[1]));
75     TONATIVE_VOID(DOMArrayBufferView*, signature, info[2]->IsArrayBufferView() ? V8ArrayBufferView::toImpl(v8::Local<v8::ArrayBufferView>::Cast(info[2])) : 0);
76     TONATIVE_VOID(DOMArrayBufferView*, data, info[3]->IsArrayBufferView() ? V8ArrayBufferView::toImpl(v8::Local<v8::ArrayBufferView>::Cast(info[3])) : 0);
77     v8SetReturnValue(info, impl->verifySignature(ScriptState::current(info.GetIsolate()), algorithm, key, signature, data).v8Value());
78 }
79
80 void V8SubtleCrypto::verifyMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
81 {
82     v8::Isolate* isolate = info.GetIsolate();
83     ExceptionState exceptionState(ExceptionState::ExecutionContext, "verify", "SubtleCrypto", info.Holder(), isolate);
84     // typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData;
85     //
86     // Promise verify(Dictionary algorithm, CryptoKey key,
87     //                CryptoOperationData signature,
88     //                CryptoOperationData data);
89     switch (info.Length()) {
90     case 4:
91         // Promise verify(Dictionary algorithm, CryptoKey key, ArrayBuffer signature, ArrayBuffer data);
92         if (V8ArrayBuffer::hasInstance(info[2], isolate)
93             && V8ArrayBuffer::hasInstance(info[3], isolate)) {
94             verify1Method(info);
95             return;
96         }
97         // Promise verify(Dictionary algorithm, CryptoKey key, ArrayBuffer signature, ArrayBufferView data);
98         if (V8ArrayBuffer::hasInstance(info[2], isolate)
99             && V8ArrayBufferView::hasInstance(info[3], isolate)) {
100             verify2Method(info);
101             return;
102         }
103         // Promise verify(Dictionary algorithm, CryptoKey key, ArrayBufferView signature, ArrayBuffer data);
104         if (V8ArrayBufferView::hasInstance(info[2], isolate)
105             && V8ArrayBuffer::hasInstance(info[3], isolate)) {
106             verify3Method(info);
107             return;
108         }
109         // Promise verify(Dictionary algorithm, CryptoKey key, ArrayBufferView signature, ArrayBufferView data);
110         if (V8ArrayBufferView::hasInstance(info[2], isolate)
111             && V8ArrayBufferView::hasInstance(info[3], isolate)) {
112             verify4Method(info);
113             return;
114         }
115         break;
116     default:
117         setArityTypeError(exceptionState, "[4]", info.Length());
118         exceptionState.throwIfNeeded();
119         return;
120         break;
121     }
122     exceptionState.throwTypeError("No function was found that matched the signature provided.");
123     exceptionState.throwIfNeeded();
124 }
125
126 } // namespace blink