Bumping manifests a=b2g-bump
[gecko.git] / dom / base / SubtleCrypto.cpp
blob05ac300ec4c357719b64d1c929d2e2ecbcdd03f6
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/SubtleCrypto.h"
9 #include "mozilla/dom/Promise.h"
10 #include "mozilla/dom/SubtleCryptoBinding.h"
11 #include "mozilla/dom/WebCryptoTask.h"
13 namespace mozilla {
14 namespace dom {
16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(SubtleCrypto, mWindow)
17 NS_IMPL_CYCLE_COLLECTING_ADDREF(SubtleCrypto)
18 NS_IMPL_CYCLE_COLLECTING_RELEASE(SubtleCrypto)
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SubtleCrypto)
20 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
21 NS_INTERFACE_MAP_ENTRY(nsISupports)
22 NS_INTERFACE_MAP_END
26 SubtleCrypto::SubtleCrypto(nsPIDOMWindow* aWindow)
27 : mWindow(aWindow)
29 SetIsDOMBinding();
32 JSObject*
33 SubtleCrypto::WrapObject(JSContext* aCx)
35 return SubtleCryptoBinding::Wrap(aCx, this);
38 #define SUBTLECRYPTO_METHOD_BODY(Operation, aRv, ...) \
39 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mWindow); \
40 MOZ_ASSERT(global); \
41 nsRefPtr<Promise> p = Promise::Create(global, aRv); \
42 if (aRv.Failed()) { \
43 return nullptr; \
44 } \
45 nsRefPtr<WebCryptoTask> task = WebCryptoTask::Create ## Operation ## Task(__VA_ARGS__); \
46 task->DispatchWithPromise(p); \
47 return p.forget();
49 already_AddRefed<Promise>
50 SubtleCrypto::Encrypt(JSContext* cx,
51 const ObjectOrString& algorithm,
52 CryptoKey& key,
53 const CryptoOperationData& data,
54 ErrorResult& aRv)
56 SUBTLECRYPTO_METHOD_BODY(Encrypt, aRv, cx, algorithm, key, data)
59 already_AddRefed<Promise>
60 SubtleCrypto::Decrypt(JSContext* cx,
61 const ObjectOrString& algorithm,
62 CryptoKey& key,
63 const CryptoOperationData& data,
64 ErrorResult& aRv)
66 SUBTLECRYPTO_METHOD_BODY(Decrypt, aRv, cx, algorithm, key, data)
69 already_AddRefed<Promise>
70 SubtleCrypto::Sign(JSContext* cx,
71 const ObjectOrString& algorithm,
72 CryptoKey& key,
73 const CryptoOperationData& data,
74 ErrorResult& aRv)
76 SUBTLECRYPTO_METHOD_BODY(Sign, aRv, cx, algorithm, key, data)
79 already_AddRefed<Promise>
80 SubtleCrypto::Verify(JSContext* cx,
81 const ObjectOrString& algorithm,
82 CryptoKey& key,
83 const CryptoOperationData& signature,
84 const CryptoOperationData& data,
85 ErrorResult& aRv)
87 SUBTLECRYPTO_METHOD_BODY(Verify, aRv, cx, algorithm, key, signature, data)
90 already_AddRefed<Promise>
91 SubtleCrypto::Digest(JSContext* cx,
92 const ObjectOrString& algorithm,
93 const CryptoOperationData& data,
94 ErrorResult& aRv)
96 SUBTLECRYPTO_METHOD_BODY(Digest, aRv, cx, algorithm, data)
100 already_AddRefed<Promise>
101 SubtleCrypto::ImportKey(JSContext* cx,
102 const nsAString& format,
103 JS::Handle<JSObject*> keyData,
104 const ObjectOrString& algorithm,
105 bool extractable,
106 const Sequence<nsString>& keyUsages,
107 ErrorResult& aRv)
109 SUBTLECRYPTO_METHOD_BODY(ImportKey, aRv, cx, format, keyData, algorithm,
110 extractable, keyUsages)
113 already_AddRefed<Promise>
114 SubtleCrypto::ExportKey(const nsAString& format,
115 CryptoKey& key,
116 ErrorResult& aRv)
118 SUBTLECRYPTO_METHOD_BODY(ExportKey, aRv, format, key)
121 already_AddRefed<Promise>
122 SubtleCrypto::GenerateKey(JSContext* cx, const ObjectOrString& algorithm,
123 bool extractable, const Sequence<nsString>& keyUsages,
124 ErrorResult& aRv)
126 SUBTLECRYPTO_METHOD_BODY(GenerateKey, aRv, cx, algorithm, extractable, keyUsages)
129 already_AddRefed<Promise>
130 SubtleCrypto::DeriveKey(JSContext* cx,
131 const ObjectOrString& algorithm,
132 CryptoKey& baseKey,
133 const ObjectOrString& derivedKeyType,
134 bool extractable, const Sequence<nsString>& keyUsages,
135 ErrorResult& aRv)
137 SUBTLECRYPTO_METHOD_BODY(DeriveKey, aRv, cx, algorithm, baseKey,
138 derivedKeyType, extractable, keyUsages)
141 already_AddRefed<Promise>
142 SubtleCrypto::DeriveBits(JSContext* cx,
143 const ObjectOrString& algorithm,
144 CryptoKey& baseKey,
145 uint32_t length,
146 ErrorResult& aRv)
148 SUBTLECRYPTO_METHOD_BODY(DeriveBits, aRv, cx, algorithm, baseKey, length)
151 already_AddRefed<Promise>
152 SubtleCrypto::WrapKey(JSContext* cx,
153 const nsAString& format,
154 CryptoKey& key,
155 CryptoKey& wrappingKey,
156 const ObjectOrString& wrapAlgorithm,
157 ErrorResult& aRv)
159 SUBTLECRYPTO_METHOD_BODY(WrapKey, aRv, cx, format, key, wrappingKey, wrapAlgorithm)
162 already_AddRefed<Promise>
163 SubtleCrypto::UnwrapKey(JSContext* cx,
164 const nsAString& format,
165 const ArrayBufferViewOrArrayBuffer& wrappedKey,
166 CryptoKey& unwrappingKey,
167 const ObjectOrString& unwrapAlgorithm,
168 const ObjectOrString& unwrappedKeyAlgorithm,
169 bool extractable,
170 const Sequence<nsString>& keyUsages,
171 ErrorResult& aRv)
173 SUBTLECRYPTO_METHOD_BODY(UnwrapKey, aRv, cx, format, wrappedKey, unwrappingKey,
174 unwrapAlgorithm, unwrappedKeyAlgorithm,
175 extractable, keyUsages)
178 } // namespace dom
179 } // namespace mozilla