2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "Converter.h"
20 #include <dpl/log/log.h>
22 #include <Commons/Exception.h>
23 #include <CommonsJavaScript/Validator.h>
24 #include <CommonsJavaScript/ScopedJSStringRef.h>
25 #include <CommonsJavaScript/JSUtils.h>
26 #include <CommonsJavaScript/JSCallbackManager.h>
27 #include "CallHistoryDefine.h"
28 #include "JSCallHistoryEntry.h"
29 #include "JSRemoteParty.h"
31 using namespace WrtDeviceApis;
32 using namespace WrtDeviceApis::Commons;
33 using namespace WrtDeviceApis::CommonsJavaScript;
36 namespace CallHistory {
38 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
40 static bool init = initializeAllowedProperties();
44 std::vector<unsigned long> Converter::toVectorOfULongs(const JSValueRef& jsValue)
46 return toVectorOfT_(jsValue, &Converter::toULong);
49 CallHistoryEntryList Converter::toVectorOfCallHistoryEntryProperties(const JSValueRef& jsValue)
51 if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
52 ThrowMsg(Commons::ConversionException, "Undefined history entry");
55 if (!JSIsArrayValue(m_context, jsValue)) {
56 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
60 CallHistoryEntryList result;
61 JSObjectRef objArg = toJSObjectRef(jsValue);
62 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
63 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
64 result.push_back(toCallHistoryEntryProperties(element));
67 } catch (Commons::ConversionException& ex) {
68 ThrowMsg(Commons::ConversionException, ex.GetMessage());
69 } catch (Commons::InvalidArgumentException& ex) {
70 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
71 } catch (Commons::Exception& ex) {
72 ThrowMsg(Commons::Exception, ex.GetMessage());
77 StringArrayPtr Converter::toStringArray(const JSValueRef &jsValue)
79 StringArrayPtr result(new StringArray());
81 if (!JSValueIsNull(m_context, jsValue)) {
82 JSObjectRef jsObject = toJSObjectRef(jsValue);
84 for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
85 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
86 result->push_back(toString(element));
89 } catch (Commons::ConversionException& ex) {
90 ThrowMsg(Commons::ConversionException, ex.GetMessage());
91 } catch (Commons::InvalidArgumentException& ex) {
92 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
93 } catch (Commons::Exception& ex) {
94 ThrowMsg(Commons::Exception, ex.GetMessage());
99 RemotePartyListPtr Converter::toRemotePartyList(const JSValueRef &jsValue)
101 RemotePartyListPtr result(new RemotePartyList());
103 if (!JSValueIsNull(m_context, jsValue)) {
104 JSObjectRef jsObject = toJSObjectRef(jsValue);
106 for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
107 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
108 result->push_back(toRemoteParty(element));
111 } catch (Commons::ConversionException& ex) {
112 ThrowMsg(Commons::ConversionException, ex.GetMessage());
113 } catch (Commons::InvalidArgumentException& ex) {
114 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
115 } catch (Commons::Exception& ex) {
116 ThrowMsg(Commons::Exception, ex.GetMessage());
121 RemotePartyPtr Converter::toRemoteParty(const JSValueRef &jsValue)
124 return JSRemoteParty::getRemoteParty(m_context, jsValue);
125 } catch (Commons::ConversionException& ex) {
126 ThrowMsg(Commons::ConversionException, ex.GetMessage());
127 } catch (Commons::Exception& ex) {
128 ThrowMsg(Commons::Exception, ex.GetMessage());
132 CallHistoryEntryPropertiesPtr Converter::toCallHistoryEntryProperties(const JSValueRef &jsValue)
135 return JSCallHistoryEntry::getCallHistoryEntry(m_context, jsValue);
136 } catch (Commons::ConversionException& ex) {
137 ThrowMsg(Commons::ConversionException, ex.GetMessage());
138 } catch (Commons::Exception& ex) {
139 ThrowMsg(Commons::Exception, ex.GetMessage());
143 EventCallHistoryListenerPrivateDataPtr
144 Converter::toEventCallHistoryListenerPrivateData(const JSValueRef &jsValue, JSContextRef context)
146 if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
147 ThrowMsg(Commons::ConversionException, "Type missmatch error : Listener callback is null or undefined");
149 if (!JSValueIsObject(m_context, jsValue) || JSObjectIsFunction(m_context, toJSObjectRef(jsValue))) {
150 ThrowMsg(Commons::ConversionException, "Type missmatch error : Listener callback");
153 JSObjectRef objectCallbacks = toJSObjectRef(jsValue);
154 CallHistoryChangeCB result;
156 result.onAdded = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onadded");
157 result.onChanged = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onchanged");
159 JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(context);
160 JSCallbackManagerPtr onChangedCbm = JSCallbackManager::createObject(context);
162 if (!JSValueIsNull(m_context, result.onAdded) && !JSValueIsUndefined(m_context, result.onAdded)) {
163 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onAdded))) {
164 onAddedCbm->setOnSuccess(result.onAdded);
166 ThrowMsg(Commons::ConversionException, "Type missmatch error : onadded callback");
170 if (!JSValueIsNull(m_context, result.onChanged) && !JSValueIsUndefined(m_context, result.onChanged)) {
171 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onChanged))) {
172 onChangedCbm->setOnSuccess(result.onChanged);
174 ThrowMsg(Commons::ConversionException, "Type missmatch error : onchanged callback");
178 return EventCallHistoryListenerPrivateDataPtr(new EventCallHistoryListenerPrivateData(onAddedCbm, onChangedCbm));
181 JSValueRef Converter::toJSValueRef(const CallHistoryEntryListPtr& arg, JSContextRef context)
183 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
185 ThrowMsg(Commons::ConversionException, "Could not create js array object");
187 CallHistoryEntryProperties tmpCallEntry;
190 for (size_t i = 0; i < (*arg).size(); i++) {
191 tmpCallEntry = *((*arg)[i]);
192 JSObjectRef jsObject = JSCallHistoryEntry::createJSObject(context, tmpCallEntry);
194 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
197 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
198 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
201 } catch (Commons::ConversionException& ex) {
202 ThrowMsg(Commons::ConversionException, ex.GetMessage());
203 } catch (Commons::InvalidArgumentException& ex) {
204 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
205 } catch (Commons::Exception& ex) {
206 ThrowMsg(Commons::Exception, ex.GetMessage());
211 JSValueRef Converter::toJSValueRef(const StringArrayPtr &arg, JSContextRef context)
213 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
215 ThrowMsg(Commons::ConversionException, "Could not create js array object");
218 for (size_t i = 0; i < (*arg).size(); i++) {
219 if (!JSSetArrayElement(m_context, jsResult, i, toJSValueRef((*arg)[i]))) {
220 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
227 JSValueRef Converter::toJSValueRef(const RemotePartyListPtr& arg, JSContextRef context)
229 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
231 ThrowMsg(Commons::ConversionException, "Could not create js array object");
234 RemoteParty tmpRemoteParty;
237 for (size_t i = 0; i < (*arg).size(); i++) {
238 tmpRemoteParty = *((*arg)[i]);
239 JSObjectRef jsObject = JSRemoteParty::createJSObject(context, tmpRemoteParty);
241 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
244 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
245 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
248 } catch (Commons::ConversionException& ex) {
249 ThrowMsg(Commons::ConversionException, ex.GetMessage());
250 } catch (Commons::InvalidArgumentException& ex) {
251 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
252 } catch (Commons::Exception& ex) {
253 ThrowMsg(Commons::Exception, ex.GetMessage());
258 bool Converter::initializeAllowedProperties()