39616c04036d0f8a492122da8b993375094f1f2b
[framework/web/wrt-plugins-tizen.git] / src / SecureElement / SEConverter.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18
19
20 #include <dpl/log/log.h>
21 #include <CommonsJavaScript/Validator.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include "SEConverter.h"
24
25 using namespace std;
26 using namespace WrtDeviceApis::CommonsJavaScript;
27 using namespace WrtDeviceApis;
28
29 #define SE_LISTENER_ONSEREADY "onSEReady"
30 #define SE_LISTENER_ONSENOTREADY "onSENotReady"
31
32
33 namespace DeviceAPI {
34 namespace SecureElement {
35
36 SEConverter::SEConverter(JSContextRef context) : Converter(context)
37 {
38     LogDebug("entered");
39 }
40
41 SEConverter::~SEConverter()
42 {
43     LogDebug("entered");
44 }
45
46
47 JSValueRef SEConverter::toJSValueRef(const std::vector<unsigned char>& arg) {
48     JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
49
50     if (NULL == jsResult) {
51         ThrowMsg(Commons::UnknownException, "Could not create js array object");
52     }
53
54     for (size_t i = 0; i < arg.size(); ++i) {
55         JSValueRef tmpVal = JSValueMakeNumber(m_context, arg[i]);
56         if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
57             ThrowMsg(Commons::UnknownException, "Could not insert value into js array");
58         }
59     }
60
61     return jsResult;
62 }
63
64 SEListener SEConverter::toSEListener(const JSValueRef& arg) {
65         LogDebug("Entered");
66         JSObjectRef object = toJSObjectRef(arg);
67
68         SEListener result;
69         Validator validator(m_context);
70
71         result.onSEReady= JSUtils::getJSPropertyOrUndefined(
72         m_context, object, SE_LISTENER_ONSEREADY
73         );
74         if (!validator.isNullOrUndefined(result.onSEReady) &&
75         !validator.isCallback(result.onSEReady)) {
76         ThrowMsg(Commons::ConversionException, "Not a valid callback.");
77         }
78
79         result.onSENotReady= JSUtils::getJSPropertyOrUndefined(
80         m_context, object, SE_LISTENER_ONSENOTREADY
81         );
82         if (!validator.isNullOrUndefined(result.onSENotReady) &&
83         !validator.isCallback(result.onSENotReady)) {
84         ThrowMsg(Commons::ConversionException, "Not a valid callback.");
85         }
86
87         if (validator.isNullOrUndefined(result.onSEReady) && validator.isNullOrUndefined(result.onSENotReady))
88         ThrowMsg(Commons::ConversionException, "Not a valid callback.");
89
90         return result;
91 }
92
93 }
94 }