wrt-plugins-tizen_0.4.23
[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 #include <CommonsJavaScript/Validator.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include "SEConverter.h"
21 #include <JSWebAPIError.h>
22 #include <TizenExceptionData.h>
23 #include <Logger.h>
24
25 using namespace std;
26 using namespace WrtDeviceApis::CommonsJavaScript;
27 using namespace WrtDeviceApis;
28 using namespace DeviceAPI::Common;
29
30 #define SE_LISTENER_ONSEREADY "onSEReady"
31 #define SE_LISTENER_ONSENOTREADY "onSENotReady"
32
33
34 namespace DeviceAPI {
35 namespace SecureElement {
36
37 SEConverter::SEConverter(JSContextRef context) : Converter(context)
38 {
39     LoggerD("entered");
40 }
41
42 SEConverter::~SEConverter()
43 {
44     LoggerD("entered");
45 }
46
47
48 JSValueRef SEConverter::toJSValueRef(const std::vector<unsigned char>& arg) {
49     JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
50
51     if (NULL == jsResult) {
52         ThrowMsg(Commons::UnknownException, "Could not create js array object");
53     }
54
55     for (size_t i = 0; i < arg.size(); ++i) {
56         JSValueRef tmpVal = JSValueMakeNumber(m_context, arg[i]);
57         if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
58             ThrowMsg(Commons::UnknownException, "Could not insert value into js array");
59         }
60     }
61
62     return jsResult;
63 }
64
65 SEListener SEConverter::toSEListener(const JSValueRef& arg) {
66         LoggerD("Entered");
67         JSObjectRef object = toJSObjectRef(arg);
68
69         SEListener result;
70         Validator validator(m_context);
71
72         result.onSEReady= JSUtils::getJSPropertyOrUndefined(
73         m_context, object, SE_LISTENER_ONSEREADY
74         );
75         if (!validator.isNullOrUndefined(result.onSEReady) &&
76         !validator.isCallback(result.onSEReady)) {
77         ThrowMsg(Commons::ConversionException, "Not a valid callback.");
78         }
79
80         result.onSENotReady= JSUtils::getJSPropertyOrUndefined(
81         m_context, object, SE_LISTENER_ONSENOTREADY
82         );
83         if (!validator.isNullOrUndefined(result.onSENotReady) &&
84         !validator.isCallback(result.onSENotReady)) {
85         ThrowMsg(Commons::ConversionException, "Not a valid callback.");
86         }
87
88         if (validator.isNullOrUndefined(result.onSEReady) && validator.isNullOrUndefined(result.onSENotReady))
89         ThrowMsg(Commons::ConversionException, "Not a valid callback.");
90
91         return result;
92 }
93
94 JSValueRef SEConverter::makeSeErrorObject(std::string error, std::string message) {
95         int code = JSWebAPIError::UNKNOWN_ERR;
96         if (error.compare("InvalidStateError") == 0)
97                 code = JSWebAPIError::INVALID_STATE_ERR;
98         else
99                 code = JSWebAPIError::convertToWebAPIErrorCode(error);
100
101         JSWebAPIError::PrivateObject::ObjectType data(new TizenExceptionData(code, error, message));
102         return JSUtils::makeObject(m_context, JSWebAPIError::getClassRef(), data);
103 }
104
105 }
106 }