1197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch// found in the LICENSE file.
4197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
5197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "config.h"
6197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/modules/v8/V8CryptoKey.h"
7197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
8197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/Dictionary.h"
9197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/V8Binding.h"
10197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/custom/V8Uint8ArrayCustom.h"
11197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "public/platform/WebCryptoKeyAlgorithm.h"
12197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "wtf/Uint8Array.h"
13197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
14c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
15197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
16197021e6b966cfb06891637935ef33fff06433d1Ben Murdochclass DictionaryBuilder : public blink::WebCryptoKeyAlgorithmDictionary {
17197021e6b966cfb06891637935ef33fff06433d1Ben Murdochpublic:
18197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    DictionaryBuilder(v8::Handle<v8::Object> holder, v8::Isolate* isolate)
19197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        : m_holder(holder)
20197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        , m_isolate(isolate)
21197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        , m_dictionary(Dictionary::createEmpty(isolate))
22197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    {
23197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
24197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
25197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    virtual void setString(const char* propertyName, const char* value)
26197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    {
27197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        m_dictionary.set(propertyName, value);
28197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
29197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
30197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    virtual void setUint(const char* propertyName, unsigned value)
31197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    {
32197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        m_dictionary.set(propertyName, value);
33197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
34197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
35197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    virtual void setAlgorithm(const char* propertyName, const blink::WebCryptoAlgorithm& algorithm)
36197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    {
37197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        ASSERT(algorithm.paramsType() == blink::WebCryptoAlgorithmParamsTypeNone);
38197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
39197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        Dictionary algorithmValue = Dictionary::createEmpty(m_isolate);
40197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        algorithmValue.set("name", blink::WebCryptoAlgorithm::lookupAlgorithmInfo(algorithm.id())->name);
41197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        m_dictionary.set(propertyName, algorithmValue);
42197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
43197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
44197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    virtual void setUint8Array(const char* propertyName, const blink::WebVector<unsigned char>& vector)
45197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    {
46197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        RefPtr<Uint8Array> uint8Array = Uint8Array::create(vector.data(), vector.size());
47197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        m_dictionary.set(propertyName, toV8(uint8Array.get(), m_holder, m_isolate));
48197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
49197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
50197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    const Dictionary& dictionary() const { return m_dictionary; }
51197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
52197021e6b966cfb06891637935ef33fff06433d1Ben Murdochprivate:
53197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    v8::Handle<v8::Object> m_holder;
54197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    v8::Isolate* m_isolate;
55197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    Dictionary m_dictionary;
56197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch};
57197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
58197021e6b966cfb06891637935ef33fff06433d1Ben Murdochvoid V8CryptoKey::algorithmAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
59197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch{
607242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    CryptoKey* impl = V8CryptoKey::toImpl(info.Holder());
61197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
62197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    DictionaryBuilder builder(info.Holder(), info.GetIsolate());
63197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    impl->key().algorithm().writeToDictionary(&builder);
64197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
65197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    v8SetReturnValue(info, builder.dictionary().v8Value());
66197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch}
67197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
68c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
69