Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / crypto / CryptoResultImpl.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "modules/crypto/CryptoResultImpl.h"
33
34 #include "bindings/v8/ScriptPromiseResolverWithContext.h"
35 #include "bindings/v8/ScriptState.h"
36 #include "core/dom/ContextLifecycleObserver.h"
37 #include "core/dom/DOMError.h"
38 #include "core/dom/DOMException.h"
39 #include "core/dom/ExecutionContext.h"
40 #include "modules/crypto/Key.h"
41 #include "modules/crypto/KeyPair.h"
42 #include "modules/crypto/NormalizeAlgorithm.h"
43 #include "public/platform/Platform.h"
44 #include "public/platform/WebArrayBuffer.h"
45 #include "public/platform/WebCryptoAlgorithm.h"
46 #include "wtf/ArrayBufferView.h"
47
48 namespace WebCore {
49
50 namespace {
51
52 ExceptionCode toExceptionCode(blink::WebCryptoErrorType errorType)
53 {
54     switch (errorType) {
55     case blink::WebCryptoErrorTypeNotSupported:
56         return NotSupportedError;
57     case blink::WebCryptoErrorTypeSyntax:
58         return SyntaxError;
59     case blink::WebCryptoErrorTypeInvalidState:
60         return InvalidStateError;
61     case blink::WebCryptoErrorTypeInvalidAccess:
62         return InvalidAccessError;
63     case blink::WebCryptoErrorTypeUnknown:
64         return UnknownError;
65     case blink::WebCryptoErrorTypeData:
66         return DataError;
67     case blink::WebCryptoErrorTypeOperation:
68         return OperationError;
69     case blink::WebCryptoErrorTypeType:
70         // FIXME: This should construct a TypeError instead. For now do
71         //        something to facilitate refactor, but this will need to be
72         //        revisited.
73         return DataError;
74     }
75
76     ASSERT_NOT_REACHED();
77     return 0;
78 }
79
80 } // namespace
81
82 // The PromiseState class contains all the state which is tied to an
83 // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread,
84 // PromiseState is not thread safe and must only be accessed and deleted from
85 // the blink thread.
86 //
87 // This is achieved by making CryptoResultImpl hold a WeakPtr to the PromiseState.
88 // The PromiseState deletes itself after being notified of completion.
89 // Additionally the PromiseState deletes itself when the ExecutionContext is
90 // destroyed (necessary to avoid leaks when dealing with WebWorker threads,
91 // which may die before the operation is completed).
92 class CryptoResultImpl::PromiseState FINAL : public ContextLifecycleObserver {
93 public:
94     static WeakPtr<PromiseState> create(ExecutionContext* context)
95     {
96         PromiseState* promiseState = new PromiseState(context);
97         return promiseState->m_weakFactory.createWeakPtr();
98     }
99
100     // Override from ContextLifecycleObserver
101     virtual void contextDestroyed() OVERRIDE
102     {
103         ContextLifecycleObserver::contextDestroyed();
104         delete this;
105     }
106
107     ScriptPromise promise()
108     {
109         return m_promiseResolver->promise();
110     }
111
112     void completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails)
113     {
114         m_promiseResolver->reject(DOMException::create(toExceptionCode(errorType), errorDetails));
115         delete this;
116     }
117
118     void completeWithBuffer(const blink::WebArrayBuffer& buffer)
119     {
120         m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer));
121         delete this;
122     }
123
124     void completeWithBoolean(bool b)
125     {
126         m_promiseResolver->resolve(b);
127         delete this;
128     }
129
130     void completeWithKey(const blink::WebCryptoKey& key)
131     {
132         m_promiseResolver->resolve(Key::create(key));
133         delete this;
134     }
135
136     void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey)
137     {
138         m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey));
139         delete this;
140     }
141
142 private:
143     explicit PromiseState(ExecutionContext* context)
144         : ContextLifecycleObserver(context)
145         , m_weakFactory(this)
146         , m_promiseResolver(ScriptPromiseResolverWithContext::create(ScriptState::current(toIsolate(context))))
147     {
148     }
149
150     WeakPtrFactory<PromiseState> m_weakFactory;
151     RefPtr<ScriptPromiseResolverWithContext> m_promiseResolver;
152 };
153
154 CryptoResultImpl::~CryptoResultImpl()
155 {
156 }
157
158 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create()
159 {
160     return adoptRef(new CryptoResultImpl(callingExecutionContext(v8::Isolate::GetCurrent())));
161 }
162
163 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails)
164 {
165     if (m_promiseState)
166         m_promiseState->completeWithError(errorType, errorDetails);
167 }
168
169 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer)
170 {
171     if (m_promiseState)
172         m_promiseState->completeWithBuffer(buffer);
173 }
174
175 void CryptoResultImpl::completeWithBoolean(bool b)
176 {
177     if (m_promiseState)
178         m_promiseState->completeWithBoolean(b);
179 }
180
181 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key)
182 {
183     if (m_promiseState)
184         m_promiseState->completeWithKey(key);
185 }
186
187 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey)
188 {
189     if (m_promiseState)
190         m_promiseState->completeWithKeyPair(publicKey, privateKey);
191 }
192
193 CryptoResultImpl::CryptoResultImpl(ExecutionContext* context)
194     : m_promiseState(PromiseState::create(context))
195 {
196 }
197
198 ScriptPromise CryptoResultImpl::promise()
199 {
200     return m_promiseState->promise();
201 }
202
203 } // namespace WebCore