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